-- This source is licensed under the Sunset License v1.0 with Ada.Text_IO, Packrat.Utilities; package body Rat_Tests.Parsers is use type Packrat.Result_Status; function Alphanum_Switch (Char : in Character) return Character is begin case Char is when 'a' .. 'z' => return Character'Val (48 + (Character'Pos (Char) - 97) mod 10); when 'A' .. 'Z' => return Character'Val (48 + (Character'Pos (Char) - 65) mod 10); when '0' .. '9' => return Character'Val (49 + (Character'Pos (Char))); when others => return Char; end case; end Alphanum_Switch; function Sequence_Check return Test_Result is Input : String := "abcdefghi"; Context1, Context2 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; Result1 : Pone.Parsers.Combinator_Result := Seq_ABCDEFG (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Seq_ABCDEFG (Input, Context2, 4); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 7 or One_Debug.Value (Result1_Parts (1)) /= "abcdefg" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Sequence_Check; function Sequence_2_Check return Test_Result is Input : String := "aaaaa"; Input2 : String := "aaaab"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_A is new Pone.Parsers.Match ('a'); function Many_A is new Pone.Parsers.Many (Match_A, 0); function Two_A is new Pone.Parsers.Multimatch ("aa"); function Seq_A is new Pone.Parsers.Sequence_2 (Many_A, Two_A); Result1 : Pone.Parsers.Combinator_Result := Seq_A (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Seq_A (Input, Context2, 5); Result3 : Pone.Parsers.Combinator_Result := Seq_A (Input2, Context3, 1); Result4 : Pone.Parsers.Combinator_Result := Seq_A (Input2, Context4, 4); begin if One_Debug.Status (Result1) /= Packrat.Optional_More or One_Debug.Status (Result2) /= Packrat.Needs_More or One_Debug.Status (Result3) /= Packrat.Success or One_Debug.Status (Result4) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 4 or One_Debug.Parts (Result2)'Length /= 0 or Result3_Parts'Length /= 3 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 2 or One_Debug.Finish (Result1_Parts (2)) /= 3 or One_Debug.Finish (Result1_Parts (3)) /= 4 or One_Debug.Finish (Result1_Parts (4)) /= 5 then return Fail; end if; if One_Debug.Value (Result1_Parts (1)) /= "aa" or One_Debug.Value (Result1_Parts (2)) /= "aaa" or One_Debug.Value (Result1_Parts (3)) /= "aaaa" or One_Debug.Value (Result1_Parts (4)) /= "aaaaa" then return Fail; end if; if (for some P of Result1_Parts => One_Debug.Tokens (P)'Length /= 0) then return Fail; end if; if One_Debug.Finish (Result3_Parts (1)) /= 2 or One_Debug.Finish (Result3_Parts (2)) /= 3 or One_Debug.Finish (Result3_Parts (3)) /= 4 then return Fail; end if; if One_Debug.Value (Result3_Parts (1)) /= "aa" or One_Debug.Value (Result3_Parts (2)) /= "aaa" or One_Debug.Value (Result3_Parts (3)) /= "aaaa" then return Fail; end if; if (for some P of Result3_Parts => One_Debug.Tokens (P)'Length /= 0) then return Fail; end if; end; return Pass; end Sequence_2_Check; function Choice_Check return Test_Result is Input : String := "cccdefg"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; Result1 : Pone.Parsers.Combinator_Result := Choose_CCCDE (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Choose_CCCDE (Input, Context2, 3); Result3 : Pone.Parsers.Combinator_Result := Choose_CCCDE (Input, Context3, 5); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if Result1_Parts'Length /= 4 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 1 or One_Debug.Finish (Result1_Parts (2)) /= 2 or One_Debug.Finish (Result1_Parts (3)) /= 3 or One_Debug.Finish (Result1_Parts (4)) /= 5 then return Fail; end if; if One_Debug.Value (Result1_Parts (1)) /= "c" or One_Debug.Value (Result1_Parts (2)) /= "cc" or One_Debug.Value (Result1_Parts (3)) /= "ccc" or One_Debug.Value (Result1_Parts (4)) /= "cccde" then return Fail; end if; if (for some P of Result1_Parts => One_Debug.Tokens (P)'Length /= 0) then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 3 or One_Debug.Value (Result2_Parts (1)) /= "c" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Choice_Check; function Choice_2_Check return Test_Result is Input : String := "matmat"; Input2 : String := "ma"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_Mat is new Pone.Parsers.Multimatch ("mat"); function Match_Match is new Pone.Parsers.Multimatch ("match"); function Choo_Choo is new Pone.Parsers.Choice_2 (Match_Mat, Match_Match); Result1 : Pone.Parsers.Combinator_Result := Choo_Choo (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Choo_Choo (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Choo_Choo (Input2, Context3, 1); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Optional_More or One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "mat" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 6 or One_Debug.Value (Result2_Parts (1)) /= "mat" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Choice_2_Check; function Count_Check return Test_Result is Input : String := "aaaa12aa"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_A is new Pone.Parsers.Match ('a'); function Three_A is new Pone.Parsers.Count (Match_A, 3); Result1 : Pone.Parsers.Combinator_Result := Three_A (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Three_A (Input, Context2, 3); Result3 : Pone.Parsers.Combinator_Result := Three_A (Input, Context3, 5); Result4 : Pone.Parsers.Combinator_Result := Three_A (Input, Context4, 7); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Failure or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 or One_Debug.Parts (Result3)'Length /= 0 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "aaa" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Count_Check; function Many_Nomin_Check return Test_Result is Input : String := "abcd123efghi"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Letter is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Letter); function Many_Letter is new Pone.Parsers.Many (Sat_Letter, 0); Result1 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context2, 5); Result3 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context3, 8); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Optional_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 5 or Result2_Parts'Length /= 1 or Result3_Parts'Length /= 6 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 0 or One_Debug.Finish (Result1_Parts (2)) /= 1 or One_Debug.Finish (Result1_Parts (3)) /= 2 or One_Debug.Finish (Result1_Parts (4)) /= 3 or One_Debug.Finish (Result1_Parts (5)) /= 4 then return Fail; end if; if One_Debug.Value (Result1_Parts (1))'Length /= 0 or One_Debug.Value (Result1_Parts (2)) /= "a" or One_Debug.Value (Result1_Parts (3)) /= "ab" or One_Debug.Value (Result1_Parts (4)) /= "abc" or One_Debug.Value (Result1_Parts (5)) /= "abcd" then return Fail; end if; if (for some P of Result1_Parts => One_Debug.Tokens (P)'Length /= 0) then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 4 or One_Debug.Value (Result2_Parts (1))'Length /= 0 or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result3_Parts (1)) /= 7 or One_Debug.Finish (Result3_Parts (2)) /= 8 or One_Debug.Finish (Result3_Parts (3)) /= 9 or One_Debug.Finish (Result3_Parts (4)) /= 10 or One_Debug.Finish (Result3_Parts (5)) /= 11 or One_Debug.Finish (Result3_Parts (6)) /= 12 then return Fail; end if; if One_Debug.Value (Result3_Parts (1))'Length /= 0 or One_Debug.Value (Result3_Parts (2)) /= "e" or One_Debug.Value (Result3_Parts (3)) /= "ef" or One_Debug.Value (Result3_Parts (4)) /= "efg" or One_Debug.Value (Result3_Parts (5)) /= "efgh" or One_Debug.Value (Result3_Parts (6)) /= "efghi" then return Fail; end if; if (for some P of Result3_Parts => One_Debug.Tokens (P)'Length /= 0) then return Fail; end if; end; return Pass; end Many_Nomin_Check; function Many_Min_Check return Test_Result is Input : String := "abcd123efghi"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Letter is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Letter); function Many_Letter is new Pone.Parsers.Many (Sat_Letter, 3); Result1 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context2, 5); Result3 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context3, 8); Result4 : Pone.Parsers.Combinator_Result := Many_Letter (Input, Context4, 11); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Optional_More or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 2 or One_Debug.Parts (Result2)'Length /= 0 or Result3_Parts'Length /= 3 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Finish (Result1_Parts (2)) /= 4 then return Fail; end if; if One_Debug.Value (Result1_Parts (1)) /= "abc" or One_Debug.Value (Result1_Parts (2)) /= "abcd" then return Fail; end if; if One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Tokens (Result1_Parts (2))'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result3_Parts (1)) /= 10 or One_Debug.Finish (Result3_Parts (2)) /= 11 or One_Debug.Finish (Result3_Parts (3)) /= 12 then return Fail; end if; if One_Debug.Value (Result3_Parts (1)) /= "efg" or One_Debug.Value (Result3_Parts (2)) /= "efgh" or One_Debug.Value (Result3_Parts (3)) /= "efghi" then return Fail; end if; if One_Debug.Tokens (Result3_Parts (1))'Length /= 0 or One_Debug.Tokens (Result3_Parts (2))'Length /= 0 or One_Debug.Tokens (Result3_Parts (3))'Length /= 0 then return Fail; end if; end; return Pass; end Many_Min_Check; function Followed_By_Check return Test_Result is Input : String := "abc12de3"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Digit is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Digit); function Digit_Follows is new Pone.Parsers.Followed_By (Sat_Digit); Result1 : Pone.Parsers.Combinator_Result := Digit_Follows (Input, Context1, 3); Result2 : Pone.Parsers.Combinator_Result := Digit_Follows (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Digit_Follows (Input, Context3, 9); begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; declare Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if One_Debug.Parts (Result1)'Length /= 0 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 3 or One_Debug.Value (Result2_Parts (1))'Length /= 0 or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Followed_By_Check; function Not_Followed_By_Check return Test_Result is Input : String := "abc12de3"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Digit is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Digit); function Digit_Not_Follows is new Pone.Parsers.Not_Followed_By (Sat_Digit); Result1 : Pone.Parsers.Combinator_Result := Digit_Not_Follows (Input, Context1, 3); Result2 : Pone.Parsers.Combinator_Result := Digit_Not_Follows (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Digit_Not_Follows (Input, Context3, 9); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 or One_Debug.Parts (Result3)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 2 or One_Debug.Value (Result1_Parts (1))'Length /= 0 or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Not_Followed_By_Check; function Many_Until_Nomin_Check return Test_Result is Input : String := "abc12de;fghi"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Letter is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Letter); function Sat_Digit is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Digit); function Body_Is_Dry is new Pone.Parsers.Many_Until (Sat_Letter, Sat_Digit, 0); Result1 : Pone.Parsers.Combinator_Result := Body_Is_Dry (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Body_Is_Dry (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Body_Is_Dry (Input, Context3, 6); Result4 : Pone.Parsers.Combinator_Result := Body_Is_Dry (Input, Context4, 9); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Failure or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "abc" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 3 or One_Debug.Value (Result2_Parts (1))'Length /= 0 or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Many_Until_Nomin_Check; function Many_Until_Min_Check return Test_Result is Input : String := "abcde12fgh"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Sat_Letter is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Letter); function Sat_Digit is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Digit); function Your_Way is new Pone.Parsers.Many_Until (Sat_Letter, Sat_Digit, 3); Result1 : Pone.Parsers.Combinator_Result := Your_Way (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Your_Way (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Your_Way (Input, Context3, 8); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 or One_Debug.Parts (Result3)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 5 or One_Debug.Value (Result1_Parts (1)) /= "abcde" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Many_Until_Min_Check; function Satisfy_Check return Test_Result is Input : String := "abc123def"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Satisfy_Letter is new Pone.Parsers.Satisfy (Packrat.Utilities.Is_Letter); Result1 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context1, 2); Result2 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context2, 6); Result3 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context3, 10); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 0 or Result3_Parts'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 2 or One_Debug.Value (Result1_Parts (1)) /= "b" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Satisfy_Check; function Satisfy_With_Check return Test_Result is Input : String := "abc123def"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Satisfy_Letter is new Pone.Parsers.Satisfy_With (Packrat.Utilities.Is_Letter, Alphanum_Switch); Result1 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context1, 2); Result2 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context2, 6); Result3 : Pone.Parsers.Combinator_Result := Satisfy_Letter (Input, Context3, 10); begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 0 or Result2_Parts'Length /= 1 or Result3_Parts'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 6 or One_Debug.Value (Result2_Parts (1)) /= "3" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Satisfy_With_Check; function Match_Check return Test_Result is Input : String := "aaabbbccc"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_B is new Pone.Parsers.Match ('b'); Result1 : Pone.Parsers.Combinator_Result := Match_B (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Match_B (Input, Context2, 5); Result3 : Pone.Parsers.Combinator_Result := Match_B (Input, Context3, 200); begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 0 or Result2_Parts'Length /= 1 or Result3_Parts'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 5 or One_Debug.Value (Result2_Parts (1)) /= "b" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Match_Check; function Match_With_Check return Test_Result is Input : String := "aaa111b2"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_0 is new Pone.Parsers.Match_With ('0', Alphanum_Switch); Result1 : Pone.Parsers.Combinator_Result := Match_0 (Input, Context1, 3); Result2 : Pone.Parsers.Combinator_Result := Match_0 (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Match_0 (Input, Context3, 7); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 0 or Result3_Parts'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "a" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Match_With_Check; function Multimatch_Check return Test_Result is Input : String := "abcdefghi"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Multi is new Pone.Parsers.Multimatch ("def"); function Multi2 is new Pone.Parsers.Multimatch ("hijk"); Result1 : Pone.Parsers.Combinator_Result := Multi (Input, Context1, 2); Result2 : Pone.Parsers.Combinator_Result := Multi (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Multi (Input, Context3, 300); Result4 : Pone.Parsers.Combinator_Result := Multi2 (Input, Context4, 8); begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Failure or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; declare Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if One_Debug.Parts (Result1)'Length /= 0 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result2_Parts (1)) /= 6 or One_Debug.Value (Result2_Parts (1)) /= "def" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Multimatch_Check; function Take_Check return Test_Result is Input : String := "abcdefghi"; Context1, Context2, Context3, Context4 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Take_2 is new Pone.Parsers.Take (2); function Take_5 is new Pone.Parsers.Take (5); Result1 : Pone.Parsers.Combinator_Result := Take_2 (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Take_5 (Input, Context2, 3); Result3 : Pone.Parsers.Combinator_Result := Take_5 (Input, Context3, 7); Result4 : Pone.Parsers.Combinator_Result := Take_2 (Input, Context4, 100); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Needs_More or One_Debug.Status (Result4) /= Packrat.Failure then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 1 or One_Debug.Parts (Result3)'Length /= 0 or One_Debug.Parts (Result4)'Length /= 0 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 2 or One_Debug.Value (Result1_Parts (1)) /= "ab" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Finish (Result2_Parts (1)) /= 7 or One_Debug.Value (Result2_Parts (1)) /= "cdefg" or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Take_Check; function Take_While_Check return Test_Result is Input : String := "abc123def"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Take_On_Me is new Pone.Parsers.Take_While (Packrat.Utilities.Is_Letter); Result1 : Pone.Parsers.Combinator_Result := Take_On_Me (Input, Context1, 2); Result2 : Pone.Parsers.Combinator_Result := Take_On_Me (Input, Context2, 4); Result3 : Pone.Parsers.Combinator_Result := Take_On_Me (Input, Context3, 7); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Optional_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 or Result3_Parts'Length /= 1 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "bc" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Finish (Result3_Parts (1)) /= 9 or One_Debug.Value (Result3_Parts (1)) /= "def" or One_Debug.Tokens (Result3_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Take_While_Check; function Take_Until_Check return Test_Result is Input : String := "abc123def"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Take_Me_On is new Pone.Parsers.Take_Until (Packrat.Utilities.Is_Digit); Result1 : Pone.Parsers.Combinator_Result := Take_Me_On (Input, Context1, 2); Result2 : Pone.Parsers.Combinator_Result := Take_Me_On (Input, Context2, 6); Result3 : Pone.Parsers.Combinator_Result := Take_Me_On (Input, Context3, 8); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or One_Debug.Status (Result3) /= Packrat.Optional_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 1 or One_Debug.Parts (Result2)'Length /= 0 or Result3_Parts'Length /= 1 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 3 or One_Debug.Value (Result1_Parts (1)) /= "bc" or One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Finish (Result3_Parts (1)) /= 9 or One_Debug.Value (Result3_Parts (1)) /= "ef" or One_Debug.Tokens (Result3_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Take_Until_Check; function Empty_Check return Test_Result is Input : String := "abcdef"; Context1, Context2, Context3 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; Result1 : Pone.Parsers.Combinator_Result := Pone.Parsers.Empty (Input, Context1, 1); Result2 : Pone.Parsers.Combinator_Result := Pone.Parsers.Empty (Input, Context2, 3); Result3 : Pone.Parsers.Combinator_Result := Pone.Parsers.Empty (Input, Context3, 10); begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Success then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); Result2_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result2); Result3_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result3); begin if Result1_Parts'Length /= 1 or Result2_Parts'Length /= 1 or Result3_Parts'Length /= 1 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 0 or One_Debug.Finish (Result2_Parts (1)) /= 2 or One_Debug.Finish (Result3_Parts (1)) /= 9 then return Fail; end if; if One_Debug.Value (Result1_Parts (1))'Length /= 0 or One_Debug.Value (Result2_Parts (1))'Length /= 0 or One_Debug.Value (Result3_Parts (1))'Length /= 0 then return Fail; end if; if One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Tokens (Result2_Parts (1))'Length /= 0 or One_Debug.Tokens (Result3_Parts (1))'Length /= 0 then return Fail; end if; end; return Pass; end Empty_Check; function Not_Empty_Check return Test_Result is Input : String := "aa"; Context1 : Pone.Parsers.Parser_Context := One_Debug.Empty_Context; function Match_A is new Pone.Parsers.Match ('a'); function Many_A is new Pone.Parsers.Many (Match_A, 0); function NE_Many_A is new Pone.Parsers.Not_Empty (Many_A); Result1 : Pone.Parsers.Combinator_Result := NE_Many_A (Input, Context1, 1); begin if One_Debug.Status (Result1) /= Packrat.Optional_More then return Fail; end if; declare Result1_Parts : One_Debug.Result_Part_Array := One_Debug.Parts (Result1); begin if Result1_Parts'Length /= 2 then return Fail; end if; if One_Debug.Finish (Result1_Parts (1)) /= 1 or One_Debug.Finish (Result1_Parts (2)) /= 2 then return Fail; end if; if One_Debug.Value (Result1_Parts (1)) /= "a" or One_Debug.Value (Result1_Parts (2)) /= "aa" then return Fail; end if; if One_Debug.Tokens (Result1_Parts (1))'Length /= 0 or One_Debug.Tokens (Result1_Parts (2))'Length /= 0 then return Fail; end if; end; return Pass; end Not_Empty_Check; function Parse_Once_Check return Test_Result is pragma Polling (On); Input : String := "1+2+3"; use Left_Sums; Sum_P : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, "+"); Sum_E : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, ""); Number_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 1, "1"); Number_2 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 3, "2"); Number_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 5, "3"); Expected_Graph, Actual_Graph : Parser_Result; begin Expected_Graph.Connect ((Sum_E, 1), (1 => (Number_1, 1))); Expected_Graph.Connect ((Sum_P, 3), ((Sum_E, 1), (Number_2, 3))); Expected_Graph.Connect ((Sum_P, 5), ((Sum_P, 3), (Number_3, 5))); Expected_Graph.Set_Root ((1 => (Sum_P, 5))); Add_Parse_Once.Reset; Actual_Graph := Add_Parse_Once.Parse (Input); if Actual_Graph.Isomorphic (Expected_Graph) then return Pass; else return Fail; end if; end Parse_Once_Check; function Parse_Once_Exception_Check return Test_Result is pragma Polling (On); use type Packrat.Errors.Error_Info_Array; Input : String := "1 + 2"; Expected_Errors : Packrat.Errors.Error_Info_Array := ((+"PLUS_ERR", 2), (+"EOF_ERR", 2)); Result_Graph : Add_Errors.Parser_Result; begin Adder_Parse_Once.Reset; Result_Graph := Adder_Parse_Once.Parse (Input); return Fail; exception when Msg : Packrat.Parser_Error => if Packrat.Errors.Decode (Except.Exception_Message (Msg)) /= Expected_Errors then return Fail; end if; return Pass; end Parse_Once_Exception_Check; function Default_Result_Check return Test_Result is Default : Pone.Parsers.Combinator_Result; begin if One_Debug.Status (Default) /= Packrat.Failure or One_Debug.Parts (Default)'Length /= 0 or not One_Debug.Is_Empty (One_Debug.Curtails (Default)) then return Fail; end if; return Pass; end Default_Result_Check; function Left_Recursion_Check return Test_Result is pragma Polling (On); Input : String := "1-2-3"; use Left_Sums; Sum_M : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, "-"); Sum_E : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, ""); Number_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 1, "1"); Number_2 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 3, "2"); Number_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 5, "3"); Expected_Graph, Actual_Graph : Parser_Result; begin Expected_Graph.Connect ((Sum_E, 1), (1 => (Number_1, 1))); Expected_Graph.Connect ((Sum_M, 3), ((Sum_E, 1), (Number_2, 3))); Expected_Graph.Connect ((Sum_M, 5), ((Sum_M, 3), (Number_3, 5))); Expected_Graph.Set_Root ((1 => (Sum_M, 5))); Sum_Parse_Once.Reset; Actual_Graph := Sum_Parse_Once.Parse (Input); if Actual_Graph.Isomorphic (Expected_Graph) then return Pass; else return Fail; end if; end Left_Recursion_Check; begin Sum_Redir.Set (Sum'Access); Add_Redir.Set (Add'Access); Adder_Redir.Set (Sumer'Access); end Rat_Tests.Parsers;