diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/rat_tests-parsers.adb | 145 | ||||
-rw-r--r-- | test/rat_tests-parsers.ads | 21 |
2 files changed, 152 insertions, 14 deletions
diff --git a/test/rat_tests-parsers.adb b/test/rat_tests-parsers.adb index 8d54168..be3c1cd 100644 --- a/test/rat_tests-parsers.adb +++ b/test/rat_tests-parsers.adb @@ -602,7 +602,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -641,7 +641,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -679,7 +679,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -758,7 +758,7 @@ package body Rat_Tests.Parsers is 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 (Result3) /= Packrat.Needs_More or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; @@ -800,7 +800,7 @@ package body Rat_Tests.Parsers is 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 + One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; @@ -1000,6 +1000,134 @@ package body Rat_Tests.Parsers is + function Parse_Parts_Check + return Test_Result + is + pragma Polling (On); + + -- 1+2+3+4 + Input1 : String := "1+"; + Input2 : String := "2+3"; + Input3 : String := "+4"; + Input4 : String := ""; + + use Add_Errors; + Add_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 1, ""); + Add_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 3, ""); + Add_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 5, ""); + Add_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 7, ""); + Number_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 1, "1"); + Number_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 3, "2"); + Number_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 5, "3"); + Number_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 7, "4"); + + Expected_Graph, Actual_Graph : Parser_Result; + begin + Expected_Graph.Connect ((Add_7, 7), + (1 => (Number_7, 7))); + Expected_Graph.Connect ((Add_5, 7), + ((Number_5, 5), (Add_7, 7))); + Expected_Graph.Connect ((Add_3, 7), + ((Number_3, 3), (Add_5, 7))); + Expected_Graph.Connect ((Add_1, 7), + ((Number_1, 1), (Add_3, 7))); + + Expected_Graph.Set_Root ((1 => (Add_1, 7))); + Adder_Parse_Parts.Reset; + + Adder_Parse_Parts.Parse (Input1, Actual_Graph); + Adder_Parse_Parts.Parse (Input2, Actual_Graph); + Adder_Parse_Parts.Parse (Input3, Actual_Graph); + Adder_Parse_Parts.Parse (Input4, Actual_Graph); + + if Actual_Graph.Isomorphic (Expected_Graph) then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Check; + + + function Parse_Parts_Left_Check + return Test_Result + is + pragma Polling (On); + + -- 1+2+3+4 + Input1 : String := "1+"; + Input2 : String := "2+3"; + Input3 : String := "+4"; + Input4 : String := ""; + + 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_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 3, "2"); + Number_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 5, "3"); + Number_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 7, "4"); + + 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_3, 3))); + Expected_Graph.Connect ((Sum_P, 5), + ((Sum_P, 3), (Number_5, 5))); + Expected_Graph.Connect ((Sum_P, 7), + ((Sum_P, 5), (Number_7, 7))); + + Expected_Graph.Set_Root ((1 => (Sum_P, 7))); + Add_Parse_Parts.Reset; + + Add_Parse_Parts.Parse (Input1, Actual_Graph); + Add_Parse_Parts.Parse (Input2, Actual_Graph); + Add_Parse_Parts.Parse (Input3, Actual_Graph); + Add_Parse_Parts.Parse (Input4, Actual_Graph); + + if Actual_Graph.Isomorphic (Expected_Graph) then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Left_Check; + + + function Parse_Parts_Exception_Check + return Test_Result + is + pragma Polling (On); + use type Packrat.Errors.Error_Info_Array; + + -- 1+2+ 3+4 + Input1 : String := "1+"; + Input2 : String := "2+ 3"; + Input3 : String := "+4"; + Input4 : String := ""; + + Expected_Errors : Packrat.Errors.Error_Info_Array := + ((+"EOF_ERR", 2), (+"EOF_ERR", 4), (+"NUMBER_ERR", 5), (+"SUM_ERR", 5)); + Result_Graph : Add_Errors.Parser_Result; + begin + Adder_Parse_Parts.Reset; + Adder_Parse_Parts.Parse (Input1, Result_Graph); + Adder_Parse_Parts.Parse (Input2, Result_Graph); + Adder_Parse_Parts.Parse (Input3, Result_Graph); + Adder_Parse_Parts.Parse (Input4, Result_Graph); + return Fail; + exception + when Msg : Packrat.Parser_Error => + if Packrat.Errors.Equivalent + (Packrat.Errors.Decode (Except.Exception_Message (Msg)), Expected_Errors) + then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Exception_Check; + + function Parse_Once_Check return Test_Result is @@ -1050,10 +1178,13 @@ package body Rat_Tests.Parsers is return Fail; exception when Msg : Packrat.Parser_Error => - if Packrat.Errors.Decode (Except.Exception_Message (Msg)) /= Expected_Errors then + if Packrat.Errors.Equivalent + (Packrat.Errors.Decode (Except.Exception_Message (Msg)), Expected_Errors) + then + return Pass; + else return Fail; end if; - return Pass; end Parse_Once_Exception_Check; diff --git a/test/rat_tests-parsers.ads b/test/rat_tests-parsers.ads index 198b622..bca6e30 100644 --- a/test/rat_tests-parsers.ads +++ b/test/rat_tests-parsers.ads @@ -72,11 +72,17 @@ package Rat_Tests.Parsers is (+"Not_Empty", Not_Empty_Check'Access)); + function Parse_Parts_Check return Test_Result; + function Parse_Parts_Left_Check return Test_Result; + function Parse_Parts_Exception_Check return Test_Result; function Parse_Once_Check return Test_Result; function Parse_Once_Exception_Check return Test_Result; Parser_Tests : Test_Array := - ((+"Parse_Once", Parse_Once_Check'Access), + ((+"Parse_Parts", Parse_Parts_Check'Access), + (+"Parse_Parts Left Recursive", Parse_Parts_Left_Check'Access), + (+"Parse_Parts Exception", Parse_Parts_Exception_Check'Access), + (+"Parse_Once", Parse_Once_Check'Access), (+"Parse_Once Exception", Parse_Once_Exception_Check'Access)); @@ -151,6 +157,7 @@ private function Add_Expr is new Left_Sums.Parsers.Sequence_2 (Add, Left_Sums.Parsers.End_Of_Input); package Add_Parse_Once is new Left_Sums.Parsers.Parse_Once (Add_Expr); + package Add_Parse_Parts is new Left_Sums.Parsers.Parse_Parts (Add_Expr); @@ -167,20 +174,20 @@ private function Stamper_Number is new Add_Errors.Parsers.Stamp (Number_Err, Sater_Digit); function Matcher_Plus is new Add_Errors.Parsers.Match ('+'); - function Stamper_Plus is new Add_Errors.Parsers.Stamp (Plus_Err, Matcher_Plus); - function Ignorer_Plus is new Add_Errors.Parsers.Ignore (Stamper_Plus); + function Discarder_Plus is new Add_Errors.Parsers.Discard (Plus_Err, Matcher_Plus); function Sumer_Plus is new Add_Errors.Parsers.Sequence - ((Adder_Redir.Call'Access, Ignorer_Plus'Access, Stamper_Number'Access)); + ((Stamper_Number'Access, Discarder_Plus'Access, Adder_Redir.Call'Access)); function Sumer_Choice is new Add_Errors.Parsers.Choice ((Sumer_Plus'Access, Stamper_Number'Access)); function Sumer is new Add_Errors.Parsers.Stamp (Sum_Err, Sumer_Choice); - function Stamper_End is new Add_Errors.Parsers.Stamp (Eof_Err, Add_Errors.Parsers.End_Of_Input); - function Ignorer_End is new Add_Errors.Parsers.Ignore (Stamper_End); - function Sumer_Expr is new Add_Errors.Parsers.Sequence_2 (Sumer, Ignorer_End); + function Discarder_End is new Add_Errors.Parsers.Discard + (Eof_Err, Add_Errors.Parsers.End_Of_Input); + function Sumer_Expr is new Add_Errors.Parsers.Sequence_2 (Sumer, Discarder_End); package Adder_Parse_Once is new Add_Errors.Parsers.Parse_Once (Sumer_Expr); + package Adder_Parse_Parts is new Add_Errors.Parsers.Parse_Parts (Sumer_Expr); |