From 43eea37daf162473c8c8e8279c9159d8b052ffdf Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Fri, 13 Nov 2020 11:36:41 +1100 Subject: Refactored Tokens, Errors, Traits --- src/packrat-errors.adb | 5 +- src/packrat-errors.ads | 81 +++++++++++++++++++++ src/packrat-lexer.adb | 77 ++++++++++---------- src/packrat-lexer.ads | 133 +++++++++++++++++----------------- src/packrat-parse_graphs.adb | 85 +++++++++++----------- src/packrat-parse_graphs.ads | 44 ++++++------ src/packrat-tokens.adb | 5 +- src/packrat-tokens.ads | 73 +++++++++++++++++++ src/packrat-traits.ads | 24 +++++++ src/packrat.adb | 12 ---- src/packrat.ads | 156 ---------------------------------------- test/packrat-lexer-debug.adb | 4 +- test/packrat-lexer-debug.ads | 4 +- test/rat_tests-errors.adb | 2 +- test/rat_tests-lexer.adb | 140 ++++++++++++++++++------------------ test/rat_tests-parse_graphs.adb | 45 ++++++------ test/rat_tests-tokens.adb | 2 +- test/test_main.adb | 3 +- 18 files changed, 462 insertions(+), 433 deletions(-) create mode 100644 src/packrat-errors.ads create mode 100644 src/packrat-tokens.ads create mode 100644 src/packrat-traits.ads delete mode 100644 src/packrat.adb diff --git a/src/packrat-errors.adb b/src/packrat-errors.adb index 82f02f9..e362343 100644 --- a/src/packrat-errors.adb +++ b/src/packrat-errors.adb @@ -7,8 +7,7 @@ with Ada.Characters.Latin_1; -separate (Packrat) -package body Errors is +package body Packrat.Errors is package SU renames Ada.Strings.Unbounded; @@ -311,6 +310,6 @@ package body Errors is end Decode; -end Errors; +end Packrat.Errors; diff --git a/src/packrat-errors.ads b/src/packrat-errors.ads new file mode 100644 index 0000000..57d7b9b --- /dev/null +++ b/src/packrat-errors.ads @@ -0,0 +1,81 @@ + + +package Packrat.Errors is + + + subtype Error_Message is String + with Dynamic_Predicate => Valid_Message (Error_Message); + + type Error_Info is record + Symbol : Ada.Strings.Unbounded.Unbounded_String; + Position : Natural; + end record; + + type Error_Info_Array is array (Positive range <>) of Error_Info; + + + -- Note: No consideration is given to ordering of Error_Info items + -- encoded into an Error_Message string. + + -- Note: Using "&" to join two Valid Error_Messages together + -- will result in an Error_Message that is also Valid, + -- but for best results Join should be used instead to + -- prevent duplication of Error_Info in the message. + + + function Valid_Identifier + (Check : in String) + return Boolean; + + function Valid_Identifier + (Check : in Ada.Strings.Unbounded.Unbounded_String) + return Boolean; + + function Valid_Identifier_Array + (Check : in Error_Info_Array) + return Boolean; + + function Valid_Message + (Check : in String) + return Boolean; + + function Debug_String + (This : in Error_Message) + return String; + + + function Join + (Left, Right : in Error_Message) + return Error_Message; + + + function Encode + (Name : in String; + Pos : in Natural) + return Error_Message + with Pre => Valid_Identifier (Name); + + function Encode + (Name : in Ada.Strings.Unbounded.Unbounded_String; + Pos : in Natural) + return Error_Message + with Pre => Valid_Identifier (Name); + + function Encode + (Info : in Error_Info) + return Error_Message + with Pre => Valid_Identifier (Info.Symbol); + + function Encode_Array + (Info : in Error_Info_Array) + return Error_Message + with Pre => Valid_Identifier_Array (Info); + + function Decode + (Msg : in Error_Message) + return Error_Info_Array; + + +end Packrat.Errors; + + diff --git a/src/packrat-lexer.adb b/src/packrat-lexer.adb index bd8d7ea..486aae5 100644 --- a/src/packrat-lexer.adb +++ b/src/packrat-lexer.adb @@ -1,5 +1,10 @@ +with + + Packrat.Errors; + + package body Packrat.Lexer is @@ -21,7 +26,7 @@ package body Packrat.Lexer is function Stamp - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result is @@ -44,7 +49,7 @@ package body Packrat.Lexer is if (Current_Result.Status = Optional_More and not Context.Allow_Incomplete) or Current_Result.Status = Success then - Context.Result_So_Far.Append (Gen_Tokens.Create + Context.Result_So_Far.Append (Traits.Tokens.Create (Label, Context.Position + Context.Offset, Input (Context.Position .. Current_Result.Finish))); @@ -67,7 +72,7 @@ package body Packrat.Lexer is function Ignore - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result is @@ -131,22 +136,22 @@ package body Packrat.Lexer is (Label_List : in Label_Vectors.Vector; Position : in Positive) is - Error_Info_List : Errors.Error_Info_Array + Error_Info_List : Packrat.Errors.Error_Info_Array (Label_List.First_Index .. Label_List.Last_Index); begin for I in Integer range Error_Info_List'First .. Error_Info_List'Last loop - Error_Info_List (I).Symbol := +Label_Enum'Image (Label_List.Element (I)); + Error_Info_List (I).Symbol := +Traits.Label_Enum'Image (Label_List.Element (I)); Error_Info_List (I).Position := Position; end loop; - raise Lexer_Error with Errors.Encode_Array (Error_Info_List); + raise Lexer_Error with Packrat.Errors.Encode_Array (Error_Info_List); end Raise_Lexer_Error; function Token_Vector_To_Array (Input_Vector : in Token_Vectors.Vector) - return Gen_Tokens.Token_Array + return Traits.Tokens.Token_Array is - Result_Array : Gen_Tokens.Token_Array (1 .. Integer (Input_Vector.Length)); + Result_Array : Traits.Tokens.Token_Array (1 .. Integer (Input_Vector.Length)); begin for I in Integer range 1 .. Integer (Input_Vector.Length) loop Result_Array (I) := Input_Vector.Element (I); @@ -157,8 +162,8 @@ package body Packrat.Lexer is procedure Token_Vector_To_Array (Input_Vector : in Token_Vectors.Vector; - Padding : in Gen_Tokens.Token; - Output_Array : out Gen_Tokens.Token_Array) is + Padding : in Traits.Tokens.Token; + Output_Array : out Traits.Tokens.Token_Array) is begin for N in Integer range 1 .. Output_Array'Length loop if N <= Integer (Input_Vector.Length) then @@ -171,17 +176,17 @@ package body Packrat.Lexer is function Slide - (Input : in Element_Array) - return Element_Array + (Input : in Traits.Element_Array) + return Traits.Element_Array is - subtype Slider is Element_Array (1 .. Input'Length); + subtype Slider is Traits.Element_Array (1 .. Input'Length); begin return Slider (Input); end Slide; procedure Internal_Scan_Core - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context; Components : in Component_Array) is @@ -204,9 +209,9 @@ package body Packrat.Lexer is function Scan - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array + return Traits.Tokens.Token_Array is Real_Input : Input_Holders.Holder; begin @@ -229,9 +234,9 @@ package body Packrat.Lexer is function Scan_Only - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array + return Traits.Tokens.Token_Array is Real_Input : Input_Holders.Holder; begin @@ -256,7 +261,7 @@ package body Packrat.Lexer is function Scan_With (Input : in With_Input; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array + return Traits.Tokens.Token_Array is Real_Input : Input_Holders.Holder; Empty_Input : Boolean; @@ -286,9 +291,9 @@ package body Packrat.Lexer is procedure Scan_Set - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context; - Output : out Gen_Tokens.Token_Array) + Output : out Traits.Tokens.Token_Array) is Real_Input : Input_Holders.Holder; begin @@ -321,7 +326,7 @@ package body Packrat.Lexer is procedure Scan_Set_With (Input : in With_Input; Context : in out Lexer_Context; - Output : out Gen_Tokens.Token_Array) + Output : out Traits.Tokens.Token_Array) is Real_Input : Input_Holders.Holder; Empty_Input : Boolean; @@ -365,7 +370,7 @@ package body Packrat.Lexer is function Sequence - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -389,7 +394,7 @@ package body Packrat.Lexer is function Count - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -413,7 +418,7 @@ package body Packrat.Lexer is function Many - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -449,7 +454,7 @@ package body Packrat.Lexer is function Many_Until - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -492,7 +497,7 @@ package body Packrat.Lexer is function Satisfy - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -507,7 +512,7 @@ package body Packrat.Lexer is function Satisfy_With - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -522,7 +527,7 @@ package body Packrat.Lexer is function Match - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -537,7 +542,7 @@ package body Packrat.Lexer is function Match_With - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -552,7 +557,7 @@ package body Packrat.Lexer is function Multimatch - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -583,7 +588,7 @@ package body Packrat.Lexer is function Take - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -598,7 +603,7 @@ package body Packrat.Lexer is function Take_While - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -621,7 +626,7 @@ package body Packrat.Lexer is function Take_Until - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is @@ -647,7 +652,7 @@ package body Packrat.Lexer is function Line_End - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin @@ -662,7 +667,7 @@ package body Packrat.Lexer is function Input_End - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result is begin diff --git a/src/packrat-lexer.ads b/src/packrat-lexer.ads index 9499d50..b8090c9 100644 --- a/src/packrat-lexer.ads +++ b/src/packrat-lexer.ads @@ -1,5 +1,9 @@ +with + + Packrat.Traits; + private with Ada.Containers.Vectors, @@ -8,13 +12,7 @@ private with generic - type Label_Enum is (<>); - type Element_Type is private; - type Element_Array is array (Positive range <>) of Element_Type; - - with function "<" (Left, Right : in Element_Type) return Boolean is <>; - - with package Gen_Tokens is new Tokens (Label_Enum, Element_Type, Element_Array); + with package Traits is new Packrat.Traits (<>); package Packrat.Lexer is @@ -22,7 +20,7 @@ package Packrat.Lexer is type Combinator_Result is private; type Combinator is access function - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; @@ -41,7 +39,7 @@ package Packrat.Lexer is type Component_Result is private; type Component is access function - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result; @@ -51,30 +49,30 @@ package Packrat.Lexer is type With_Input is access function - return Element_Array; + return Traits.Element_Array; generic - Label : in Label_Enum; + Label : in Traits.Label_Enum; with function Combo - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; function Stamp - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result; generic - Label : in Label_Enum; + Label : in Traits.Label_Enum; with function Combo - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; function Ignore - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result; @@ -84,41 +82,41 @@ package Packrat.Lexer is generic Components : in Component_Array; function Scan - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array; + return Traits.Tokens.Token_Array; generic Components : in Component_Array; function Scan_Only - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array; + return Traits.Tokens.Token_Array; generic Components : in Component_Array; function Scan_With (Input : in With_Input; Context : in out Lexer_Context) - return Gen_Tokens.Token_Array; + return Traits.Tokens.Token_Array; generic Components : in Component_Array; - Pad_In : in Element_Type; - Pad_Out : in Gen_Tokens.Token; + Pad_In : in Traits.Element_Type; + Pad_Out : in Traits.Tokens.Token; procedure Scan_Set - (Input : in Element_Array; + (Input : in Traits.Element_Array; Context : in out Lexer_Context; - Output : out Gen_Tokens.Token_Array); + Output : out Traits.Tokens.Token_Array); generic Components : in Component_Array; - Pad_In : in Element_Type; - Pad_Out : in Gen_Tokens.Token; + Pad_In : in Traits.Element_Type; + Pad_Out : in Traits.Tokens.Token; procedure Scan_Set_With (Input : in With_Input; Context : in out Lexer_Context; - Output : out Gen_Tokens.Token_Array); + Output : out Traits.Tokens.Token_Array); @@ -126,43 +124,43 @@ package Packrat.Lexer is generic Params : in Combinator_Array; function Sequence - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Param - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; Number : in Positive; function Count - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Param - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; Minimum : in Natural := 0; function Many - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Param - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; with function Test - (Item : in Element_Type) + (Item : in Traits.Element_Type) return Boolean; Minimum : in Natural := 0; function Many_Until - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; @@ -171,71 +169,71 @@ package Packrat.Lexer is generic with function Test - (Item : in Element_Type) + (Item : in Traits.Element_Type) return Boolean; function Satisfy - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Test - (Item : in Element_Type) + (Item : in Traits.Element_Type) return Boolean; with function Change - (From : in Element_Type) - return Element_Type; + (From : in Traits.Element_Type) + return Traits.Element_Type; function Satisfy_With - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic - Item : in Element_Type; + Item : in Traits.Element_Type; function Match - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic - Item : in Element_Type; + Item : in Traits.Element_Type; with function Change - (From : in Element_Type) - return Element_Type; + (From : in Traits.Element_Type) + return Traits.Element_Type; function Match_With - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic - Items : in Element_Array; + Items : in Traits.Element_Array; function Multimatch - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic Number : in Positive := 1; function Take - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Test - (Item : in Element_Type) + (Item : in Traits.Element_Type) return Boolean; function Take_While - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic with function Test - (Item : in Element_Type) + (Item : in Traits.Element_Type) return Boolean; function Take_Until - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; @@ -243,16 +241,16 @@ package Packrat.Lexer is generic - EOL_Item : in Element_Type; + EOL_Item : in Traits.Element_Type; function Line_End - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; generic - EOF_Item : in Element_Type; + EOF_Item : in Traits.Element_Type; function Input_End - (Input : in Element_Array; + (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; @@ -260,20 +258,27 @@ package Packrat.Lexer is private + use type Traits.Label_Enum; + use type Traits.Element_Type; + use type Traits.Element_Array; + + + + package Token_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, - Element_Type => Gen_Tokens.Token, - "=" => Gen_Tokens."="); + Element_Type => Traits.Tokens.Token, + "=" => Traits.Tokens."="); package Label_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, - Element_Type => Label_Enum); + Element_Type => Traits.Label_Enum); package Label_Sets is new Ada.Containers.Ordered_Sets - (Element_Type => Label_Enum); + (Element_Type => Traits.Label_Enum); package Input_Holders is new Ada.Containers.Indefinite_Holders - (Element_Type => Element_Array); + (Element_Type => Traits.Element_Array); diff --git a/src/packrat-parse_graphs.adb b/src/packrat-parse_graphs.adb index b0e5554..c743163 100644 --- a/src/packrat-parse_graphs.adb +++ b/src/packrat-parse_graphs.adb @@ -36,36 +36,36 @@ package body Packrat.Parse_Graphs is is Left_Index, Right_Index : Positive; begin - if Gen_Tokens.Start (Left.Token) = Gen_Tokens.Start (Right.Token) then + if Traits.Tokens.Start (Left.Token) = Traits.Tokens.Start (Right.Token) then if Left.Finish = Right.Finish then - if Gen_Tokens.Label (Left.Token) = Gen_Tokens.Label (Right.Token) then - Left_Index := Gen_Tokens.Value (Left.Token)'First; - Right_Index := Gen_Tokens.Value (Right.Token)'First; - while Left_Index <= Gen_Tokens.Value (Left.Token)'Last and - Right_Index <= Gen_Tokens.Value (Right.Token)'Last + if Traits.Tokens.Label (Left.Token) = Traits.Tokens.Label (Right.Token) then + Left_Index := Traits.Tokens.Value (Left.Token)'First; + Right_Index := Traits.Tokens.Value (Right.Token)'First; + while Left_Index <= Traits.Tokens.Value (Left.Token)'Last and + Right_Index <= Traits.Tokens.Value (Right.Token)'Last loop - if Gen_Tokens.Value (Left.Token) (Left_Index) < - Gen_Tokens.Value (Right.Token) (Right_Index) + if Traits.Tokens.Value (Left.Token) (Left_Index) < + Traits.Tokens.Value (Right.Token) (Right_Index) then return True; - elsif Gen_Tokens.Value (Left.Token) (Left_Index) /= - Gen_Tokens.Value (Right.Token) (Right_Index) + elsif Traits.Tokens.Value (Left.Token) (Left_Index) /= + Traits.Tokens.Value (Right.Token) (Right_Index) then return False; end if; Left_Index := Left_Index + 1; Right_Index := Right_Index + 1; end loop; - return Gen_Tokens.Value (Left.Token)'Length < - Gen_Tokens.Value (Right.Token)'Length; + return Traits.Tokens.Value (Left.Token)'Length < + Traits.Tokens.Value (Right.Token)'Length; else - return Gen_Tokens.Label (Left.Token) < Gen_Tokens.Label (Right.Token); + return Traits.Tokens.Label (Left.Token) < Traits.Tokens.Label (Right.Token); end if; else return Left.Finish < Right.Finish; end if; else - return Gen_Tokens.Start (Left.Token) < Gen_Tokens.Start (Right.Token); + return Traits.Tokens.Start (Left.Token) < Traits.Tokens.Start (Right.Token); end if; end "<"; @@ -94,14 +94,15 @@ package body Packrat.Parse_Graphs is (Left, Right : in Token_Group) return Boolean is begin - if Gen_Tokens.Start (Left.Parent.Token) = Gen_Tokens.Start (Right.Parent.Token) then + if Traits.Tokens.Start (Left.Parent.Token) = Traits.Tokens.Start (Right.Parent.Token) then if Finish (Left) = Finish (Right) then return Left.Elems.Element < Right.Elems.Element; else return Finish (Left) < Finish (Right); end if; else - return Gen_Tokens.Start (Left.Parent.Token) < Gen_Tokens.Start (Right.Parent.Token); + return Traits.Tokens.Start (Left.Parent.Token) < + Traits.Tokens.Start (Right.Parent.Token); end if; end "<"; @@ -219,10 +220,10 @@ package body Packrat.Parse_Graphs is return Enum_Node_Maps.Map is Result : Enum_Node_Maps.Map; - Current : Label_Enum; + Current : Traits.Label_Enum; begin for Node of Container.Internal_Graph.Nodes loop - Current := Gen_Tokens.Label (Container.Internal_Graph.Label (Node)); + Current := Traits.Tokens.Label (Container.Internal_Graph.Label (Node)); if not Result.Contains (Current) then Result.Insert (Current, Node_Vectors.Empty_Vector); end if; @@ -232,10 +233,10 @@ package body Packrat.Parse_Graphs is end Generate_Map; function Image - (Input : in Label_Enum) + (Input : in Traits.Label_Enum) return String is - Raw : String := Label_Enum'Image (Input); + Raw : String := Traits.Label_Enum'Image (Input); begin if Raw'Length <= Cutoff then return Raw; @@ -252,14 +253,14 @@ package body Packrat.Parse_Graphs is end Image; Mapping : Enum_Node_Maps.Map := Generate_Map (Container); - Current : Gen_Tokens.Token; + Current : Traits.Tokens.Token; Result : SU.Unbounded_String; begin for Iter in Mapping.Iterate loop SU.Append (Result, Image (Enum_Node_Maps.Key (Iter)) & Latin.HT); for Node of Enum_Node_Maps.Element (Iter) loop Current := Container.Internal_Graph.Label (Node); - SU.Append (Result, Image (Gen_Tokens.Start (Current)) & " ->" & Latin.HT); + SU.Append (Result, Image (Traits.Tokens.Start (Current)) & " ->" & Latin.HT); for Fin of In_Finishes (Container, Node) loop SU.Append (Result, Image (Fin) & " ->" & Latin.HT); declare @@ -272,8 +273,8 @@ package body Packrat.Parse_Graphs is for Grouping of Groupings loop for Fin_Token of Elements (Grouping) loop SU.Append (Result, "Subnode " & - Image (Gen_Tokens.Label (Fin_Token.Token)) & - " (" & Image (Gen_Tokens.Start (Fin_Token.Token)) & + Image (Traits.Tokens.Label (Fin_Token.Token)) & + " (" & Image (Traits.Tokens.Start (Fin_Token.Token)) & "," & Image (Fin_Token.Finish) & "), "); end loop; SU.Delete (Result, SU.Length (Result) - 1, SU.Length (Result)); @@ -299,7 +300,7 @@ package body Packrat.Parse_Graphs is function Contains (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Boolean is begin return Container.Label_Map.Contains (Token); @@ -431,19 +432,19 @@ package body Packrat.Parse_Graphs is Subvec : Finished_Token_Vectors.Vector; begin for Sub of Subtokens loop - if Gen_Tokens.Start (Sub.Token) > Sub.Finish + 1 then + if Traits.Tokens.Start (Sub.Token) > Sub.Finish + 1 then return False; end if; Subvec.Append (Sub); end loop; Finished_Token_Sort.Sort (Subvec); for Index in Subvec.First_Index .. Subvec.Last_Index - 1 loop - if Subvec (Index).Finish >= Gen_Tokens.Start (Subvec (Index + 1).Token) then + if Subvec (Index).Finish >= Traits.Tokens.Start (Subvec (Index + 1).Token) then return False; end if; end loop; if Parent.Finish < Subvec.Last_Element.Finish or else - Gen_Tokens.Start (Parent.Token) > Gen_Tokens.Start (Subvec.First_Element.Token) + Traits.Tokens.Start (Parent.Token) > Traits.Tokens.Start (Subvec.First_Element.Token) then return False; end if; @@ -465,7 +466,7 @@ package body Packrat.Parse_Graphs is return False; elsif Current = Parent then return True; - elsif Gen_Tokens.Start (Current.Token) > Gen_Tokens.Start (Parent.Token) then + elsif Traits.Tokens.Start (Current.Token) > Traits.Tokens.Start (Parent.Token) then return False; else return (for some Grouping of Container.Subgroups (Current) => @@ -543,7 +544,7 @@ package body Packrat.Parse_Graphs is procedure Include (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) is Node_ID : Node_ID_Type; begin @@ -593,7 +594,7 @@ package body Packrat.Parse_Graphs is procedure Prune (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token) is + Token : in Traits.Tokens.Token) is begin if not Container.Contains (Token) then return; @@ -720,7 +721,7 @@ package body Packrat.Parse_Graphs is procedure Set_Root (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token; + Token : in Traits.Tokens.Token; Finishes : in Finish_Array) is begin Container.Root_Node := Container.Label_Map.Element (Token); @@ -744,7 +745,7 @@ package body Packrat.Parse_Graphs is function Root_Token (Container : in Parse_Graph) - return Gen_Tokens.Token is + return Traits.Tokens.Token is begin return Container.Internal_Graph.Label (Container.Root_Node); end Root_Token; @@ -776,7 +777,7 @@ package body Packrat.Parse_Graphs is function Finish_List (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Finish_Array is function V2A is new Vector_To_Array (Finish_Type, Finish_Array, Finish_Vectors); @@ -1072,14 +1073,16 @@ package body Packrat.Parse_Graphs is then return True; end if; - if Gen_Tokens.Start (Left_Position.Token) + Offset /= - Gen_Tokens.Start (Right_Position.Token) or else + if Traits.Tokens.Start (Left_Position.Token) + Offset /= + Traits.Tokens.Start (Right_Position.Token) or else Left_Position.Finish + Offset /= Right_Position.Finish then return False; end if; - if Gen_Tokens.Label (Left_Position.Token) /= Gen_Tokens.Label (Right_Position.Token) or else - Gen_Tokens.Value (Left_Position.Token) /= Gen_Tokens.Value (Right_Position.Token) + if Traits.Tokens.Label (Left_Position.Token) /= + Traits.Tokens.Label (Right_Position.Token) or else + Traits.Tokens.Value (Left_Position.Token) /= + Traits.Tokens.Value (Right_Position.Token) then return False; end if; @@ -1115,7 +1118,7 @@ package body Packrat.Parse_Graphs is return Boolean is Offset : Integer := - Gen_Tokens.Start (Right.Root_Token) - Gen_Tokens.Start (Left.Root_Token); + Traits.Tokens.Start (Right.Root_Token) - Traits.Tokens.Start (Left.Root_Token); Left_Finishes : Finish_Array := Left.Root_Finish_List; Right_Finishes : Finish_Array := Right.Root_Finish_List; Mapping : Isomorph_Maps.Map; @@ -1139,7 +1142,7 @@ package body Packrat.Parse_Graphs is return Boolean is Offset : Integer := - Gen_Tokens.Start (Right_Position.Token) - Gen_Tokens.Start (Left_Position.Token); + Traits.Tokens.Start (Right_Position.Token) - Traits.Tokens.Start (Left_Position.Token); Mapping : Isomorph_Maps.Map; begin return Token_Isomorph @@ -1154,7 +1157,7 @@ package body Packrat.Parse_Graphs is function To_Node (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Node_ID_Type is begin return Container.Label_Map.Element (Token); diff --git a/src/packrat-parse_graphs.ads b/src/packrat-parse_graphs.ads index 291a467..6d79802 100644 --- a/src/packrat-parse_graphs.ads +++ b/src/packrat-parse_graphs.ads @@ -2,7 +2,8 @@ with - Ada.Containers; + Ada.Containers, + Packrat.Traits; private with @@ -14,13 +15,7 @@ private with generic - type Label_Enum is (<>); - type Element_Type is private; - type Element_Array is array (Positive range <>) of Element_Type; - - with function "<" (Left, Right : in Element_Type) return Boolean is <>; - - with package Gen_Tokens is new Tokens (Label_Enum, Element_Type, Element_Array); + with package Traits is new Packrat.Traits (<>); package Packrat.Parse_Graphs is @@ -54,7 +49,7 @@ package Packrat.Parse_Graphs is type Finish_Array is array (Positive range <>) of Finish_Type; type Finished_Token is record - Token : Gen_Tokens.Token; + Token : Traits.Tokens.Token; Finish : Finish_Type; end record; @@ -113,7 +108,7 @@ package Packrat.Parse_Graphs is function Contains (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Boolean; function Contains @@ -180,7 +175,7 @@ package Packrat.Parse_Graphs is procedure Include (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) with Post => Container.Contains (Token); procedure Connect @@ -193,7 +188,7 @@ package Packrat.Parse_Graphs is procedure Prune (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) with Post => not Container.Contains (Token); procedure Prune @@ -220,10 +215,10 @@ package Packrat.Parse_Graphs is procedure Set_Root (Container : in out Parse_Graph; - Token : in Gen_Tokens.Token; + Token : in Traits.Tokens.Token; Finishes : in Finish_Array) with Pre => Container.Contains (Token) and - (for all F of Finishes => F >= Gen_Tokens.Start (Token) - 1), + (for all F of Finishes => F >= Traits.Tokens.Start (Token) - 1), Post => Container.Has_Root; procedure Clear_Root @@ -232,7 +227,7 @@ package Packrat.Parse_Graphs is function Root_Token (Container : in Parse_Graph) - return Gen_Tokens.Token + return Traits.Tokens.Token with Pre => Container.Has_Root; function Root_Finish_List @@ -254,7 +249,7 @@ package Packrat.Parse_Graphs is function Finish_List (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Finish_Array with Pre => Container.Contains (Token), Post => Is_Sorted (Finish_List'Result) and @@ -360,10 +355,17 @@ package Packrat.Parse_Graphs is private + use type Traits.Label_Enum; + use type Traits.Element_Type; + use type Traits.Element_Array; + + + + type Node_ID_Type is new Positive; type Edge_ID_Type is new Positive; - subtype Node_Label_Type is Gen_Tokens.Token; + subtype Node_Label_Type is Traits.Tokens.Token; subtype Group_ID_Type is Positive; @@ -378,7 +380,7 @@ private function To_Node (Container : in Parse_Graph; - Token : in Gen_Tokens.Token) + Token : in Traits.Tokens.Token) return Node_ID_Type; function To_Node @@ -396,7 +398,7 @@ private -- This 'use type' is to avoid some ambiguities with "=" functions when -- instantiating the Base package. - use type Gen_Tokens.Token; + use type Traits.Tokens.Token; package Base is new Directed_Graphs (Node_ID_Type => Node_ID_Type, @@ -414,7 +416,7 @@ private package Finish_Sort is new Finish_Vectors.Generic_Sorting; package Node_Label_Maps is new Ada.Containers.Ordered_Maps - (Key_Type => Gen_Tokens.Token, + (Key_Type => Traits.Tokens.Token, Element_Type => Node_ID_Type); type Parse_Graph is tagged record @@ -468,7 +470,7 @@ private Element_Type => Group_ID_Type); package Enum_Node_Maps is new Ada.Containers.Ordered_Maps - (Key_Type => Label_Enum, + (Key_Type => Traits.Label_Enum, Element_Type => Node_Vectors.Vector, "=" => Node_Vectors."="); diff --git a/src/packrat-tokens.adb b/src/packrat-tokens.adb index 08e0181..60d03e3 100644 --- a/src/packrat-tokens.adb +++ b/src/packrat-tokens.adb @@ -5,8 +5,7 @@ with Ada.Characters.Latin_1; -separate (Packrat) -package body Tokens is +package body Packrat.Tokens is package SU renames Ada.Strings.Unbounded; @@ -112,6 +111,6 @@ package body Tokens is end Value; -end Tokens; +end Packrat.Tokens; diff --git a/src/packrat-tokens.ads b/src/packrat-tokens.ads new file mode 100644 index 0000000..bfb70ac --- /dev/null +++ b/src/packrat-tokens.ads @@ -0,0 +1,73 @@ + + +private with + + Ada.Containers.Indefinite_Holders; + + +generic + + type Label_Enum is (<>); + type Element_Type is private; + type Element_Array is array (Positive range <>) of Element_Type; + + with function "<" (Left, Right : in Element_Type) return Boolean is <>; + +package Packrat.Tokens is + + + type Token is private; + type Token_Array is array (Positive range <>) of Token; + + + function "<" + (Left, Right : in Token) + return Boolean; + + + function Create + (Ident : in Label_Enum; + Start : in Positive; + Value : in Element_Array) + return Token; + + + -- Note: The Start index indicates where the token was found + -- in whatever array it was lexed from. The Value does *not* + -- have to correspond with whatever is found there. + + + function Debug_String + (This : in Token) + return String; + + + function Label + (This : in Token) + return Label_Enum; + + function Start + (This : in Token) + return Positive; + + function Value + (This : in Token) + return Element_Array; + + +private + + + package Value_Holders is new Ada.Containers.Indefinite_Holders (Element_Array); + + + type Token is record + Identifier : Label_Enum; + Start_At : Positive; + Token_Value : Value_Holders.Holder; + end record; + + +end Packrat.Tokens; + + diff --git a/src/packrat-traits.ads b/src/packrat-traits.ads new file mode 100644 index 0000000..d14b2fd --- /dev/null +++ b/src/packrat-traits.ads @@ -0,0 +1,24 @@ + + +with + + Packrat.Tokens; + + +generic + + type Label_Enum is (<>); + type Element_Type is private; + type Element_Array is array (Positive range <>) of Element_Type; + + with function "<" (Left, Right : in Element_Type) return Boolean is <>; + +package Packrat.Traits is + + + package Tokens is new Packrat.Tokens (Label_Enum, Element_Type, Element_Array); + + +end Packrat.Traits; + + diff --git a/src/packrat.adb b/src/packrat.adb deleted file mode 100644 index de623e2..0000000 --- a/src/packrat.adb +++ /dev/null @@ -1,12 +0,0 @@ - - -package body Packrat is - - - package body Errors is separate; - package body Tokens is separate; - - -end Packrat; - - diff --git a/src/packrat.ads b/src/packrat.ads index 58afbaa..7f124e1 100644 --- a/src/packrat.ads +++ b/src/packrat.ads @@ -4,10 +4,6 @@ with Ada.Strings.Unbounded; -private with - - Ada.Containers.Indefinite_Holders; - package Packrat is @@ -19,158 +15,6 @@ package Packrat is Lexer_Error : exception; - - -- need to restructure all this to separate these nested packages out into their own files - -- and also to add in a Traits package to contain the Enum/Element/Array generic stuff - - - - package Errors is - - - subtype Error_Message is String - with Dynamic_Predicate => Valid_Message (Error_Message); - - type Error_Info is record - Symbol : Ada.Strings.Unbounded.Unbounded_String; - Position : Natural; - end record; - - type Error_Info_Array is array (Positive range <>) of Error_Info; - - - -- Note: No consideration is given to ordering of Error_Info items - -- encoded into an Error_Message string. - - -- Note: Using "&" to join two Valid Error_Messages together - -- will result in an Error_Message that is also Valid, - -- but for best results Join should be used instead to - -- prevent duplication of Error_Info in the message. - - - function Valid_Identifier - (Check : in String) - return Boolean; - - function Valid_Identifier - (Check : in Ada.Strings.Unbounded.Unbounded_String) - return Boolean; - - function Valid_Identifier_Array - (Check : in Error_Info_Array) - return Boolean; - - function Valid_Message - (Check : in String) - return Boolean; - - function Debug_String - (This : in Error_Message) - return String; - - - function Join - (Left, Right : in Error_Message) - return Error_Message; - - - function Encode - (Name : in String; - Pos : in Natural) - return Error_Message - with Pre => Valid_Identifier (Name); - - function Encode - (Name : in Ada.Strings.Unbounded.Unbounded_String; - Pos : in Natural) - return Error_Message - with Pre => Valid_Identifier (Name); - - function Encode - (Info : in Error_Info) - return Error_Message - with Pre => Valid_Identifier (Info.Symbol); - - function Encode_Array - (Info : in Error_Info_Array) - return Error_Message - with Pre => Valid_Identifier_Array (Info); - - function Decode - (Msg : in Error_Message) - return Error_Info_Array; - - - end Errors; - - - - - generic - type Label_Enum is (<>); - type Element_Type is private; - type Element_Array is array (Positive range <>) of Element_Type; - with function "<" (Left, Right : in Element_Type) return Boolean is <>; - package Tokens is - - - type Token is private; - type Token_Array is array (Positive range <>) of Token; - - - function "<" - (Left, Right : in Token) - return Boolean; - - - function Create - (Ident : in Label_Enum; - Start : in Positive; - Value : in Element_Array) - return Token; - - - -- Note: The Start index indicates where the token was found - -- in whatever array it was lexed from. The Value does *not* - -- have to correspond with whatever is found there. - - - function Debug_String - (This : in Token) - return String; - - - function Label - (This : in Token) - return Label_Enum; - - function Start - (This : in Token) - return Positive; - - function Value - (This : in Token) - return Element_Array; - - - private - - - package Value_Holders is new Ada.Containers.Indefinite_Holders (Element_Array); - - - type Token is record - Identifier : Label_Enum; - Start_At : Positive; - Token_Value : Value_Holders.Holder; - end record; - - - end Tokens; - - - - private diff --git a/test/packrat-lexer-debug.adb b/test/packrat-lexer-debug.adb index f6c57ef..40dbd87 100644 --- a/test/packrat-lexer-debug.adb +++ b/test/packrat-lexer-debug.adb @@ -76,7 +76,7 @@ package body Packrat.Lexer.Debug is function Pass (This : in Lexer_Context) - return Element_Array is + return Traits.Element_Array is begin return This.Pass_Forward.Element; end Pass; @@ -91,7 +91,7 @@ package body Packrat.Lexer.Debug is function Element (Vec : in Token_Vector; Dex : in Positive) - return Gen_Tokens.Token is + return Traits.Tokens.Token is begin return Token_Vectors.Vector (Vec).Element (Dex); end Element; diff --git a/test/packrat-lexer-debug.ads b/test/packrat-lexer-debug.ads index 05d05b2..bd6c2de 100644 --- a/test/packrat-lexer-debug.ads +++ b/test/packrat-lexer-debug.ads @@ -52,7 +52,7 @@ package Packrat.Lexer.Debug is function Pass (This : in Lexer_Context) - return Element_Array; + return Traits.Element_Array; function Length (Vec : in Token_Vector) @@ -61,7 +61,7 @@ package Packrat.Lexer.Debug is function Element (Vec : in Token_Vector; Dex : in Positive) - return Gen_Tokens.Token; + return Traits.Tokens.Token; diff --git a/test/rat_tests-errors.adb b/test/rat_tests-errors.adb index 75d2c34..ee95004 100644 --- a/test/rat_tests-errors.adb +++ b/test/rat_tests-errors.adb @@ -1,6 +1,6 @@ -with Packrat; +with Packrat.Errors; package body Rat_Tests.Errors is diff --git a/test/rat_tests-lexer.adb b/test/rat_tests-lexer.adb index 484c86b..531d175 100644 --- a/test/rat_tests-lexer.adb +++ b/test/rat_tests-lexer.adb @@ -2,6 +2,8 @@ with + Packrat.Errors, + Packrat.Traits, Packrat.Lexer.Debug, Packrat.Util; @@ -15,8 +17,8 @@ package body Rat_Tests.Lexer is type My_Labels is (One, Two, Three); - package String_Tokens is new Packrat.Tokens (My_Labels, Character, String); - package Slexy is new Packrat.Lexer (My_Labels, Character, String, "<", String_Tokens); + package Slexy_Traits is new Packrat.Traits (My_Labels, Character, String); + package Slexy is new Packrat.Lexer (Slexy_Traits); package Slebug is new Slexy.Debug; @@ -553,7 +555,7 @@ package body Rat_Tests.Lexer is function Stamp_Check return Test_Result is - use type String_Tokens.Token; + use type Slexy_Traits.Tokens.Token; use type Packrat.Result_Status; use type Slexy.Component_Result; @@ -576,7 +578,7 @@ package body Rat_Tests.Lexer is begin Comp_Code := My_Stamp (Test_Str1, Context1); if (Slebug.So_Far (Context1).Length /= 1 or else - Slebug.So_Far (Context1).Element (1) /= String_Tokens.Create (One, 1, "abc")) or + Slebug.So_Far (Context1).Element (1) /= Slexy_Traits.Tokens.Create (One, 1, "abc")) or Slebug.Position (Context1) /= 4 or Slebug.Status (Context1) /= Packrat.Success or Slebug.Has_Pass (Context1) then @@ -584,7 +586,7 @@ package body Rat_Tests.Lexer is end if; Comp_Code := My_Stamp (Test_Str1, Context1); if (Slebug.So_Far (Context1).Length /= 1 or else - Slebug.So_Far (Context1).Element (1) /= String_Tokens.Create (One, 1, "abc")) or + Slebug.So_Far (Context1).Element (1) /= Slexy_Traits.Tokens.Create (One, 1, "abc")) or Slebug.Position (Context1) /= 4 or not Slebug.Is_Failure (Comp_Code) or Slebug.Has_Pass (Context1) then @@ -648,12 +650,12 @@ package body Rat_Tests.Lexer is type Word_Enum is (Blank, Word, Whitespace); - package Word_Tokens is new Packrat.Tokens (Word_Enum, Character, String); - package Swordy is new Packrat.Lexer (Word_Enum, Character, String, "<", Word_Tokens); + package Swordy_Traits is new Packrat.Traits (Word_Enum, Character, String); + package Swordy is new Packrat.Lexer (Swordy_Traits); package Swolbug is new Swordy.Debug; - use type Word_Tokens.Token; - use type Word_Tokens.Token_Array; + use type Swordy_Traits.Tokens.Token; + use type Swordy_Traits.Tokens.Token_Array; function Satisfy_Letter is new Swordy.Satisfy (PU.Is_Letter); function Many_Letter is new Swordy.Many (Satisfy_Letter, 1); @@ -673,15 +675,15 @@ package body Rat_Tests.Lexer is Test_Str : String := "one fine day"; Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Intended_Result1 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 1, "one"), - 2 => Word_Tokens.Create (Word, 5, "fine")); - Intended_Result2 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 10, "day")); + Intended_Result1 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 1, "one"), + 2 => Swordy_Traits.Tokens.Create (Word, 5, "fine")); + Intended_Result2 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 10, "day")); - Actual_Result1 : Word_Tokens.Token_Array := + Actual_Result1 : Swordy_Traits.Tokens.Token_Array := My_Scan (Test_Str, Test_Context); - Actual_Result2 : Word_Tokens.Token_Array := + Actual_Result2 : Swordy_Traits.Tokens.Token_Array := My_Scan ("", Test_Context); begin if Actual_Result1 /= Intended_Result1 or Actual_Result2 /= Intended_Result2 then @@ -700,12 +702,12 @@ package body Rat_Tests.Lexer is Test_Str : String := "one fine day"; Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Intended_Result : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 1, "one"), - 2 => Word_Tokens.Create (Word, 5, "fine"), - 3 => Word_Tokens.Create (Word, 10, "day")); + Intended_Result : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 1, "one"), + 2 => Swordy_Traits.Tokens.Create (Word, 5, "fine"), + 3 => Swordy_Traits.Tokens.Create (Word, 10, "day")); - Actual_Result : Word_Tokens.Token_Array := + Actual_Result : Swordy_Traits.Tokens.Token_Array := My_Scan (Test_Str, Test_Context); begin if Actual_Result /= Intended_Result then @@ -738,17 +740,17 @@ package body Rat_Tests.Lexer is Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Intended_Result : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 1, "it"), - 2 => Word_Tokens.Create (Word, 4, "will"), - 3 => Word_Tokens.Create (Word, 9, "happen"), - 4 => Word_Tokens.Create (Word, 17, "again"), - 5 => Word_Tokens.Create (Word, 23, "and"), - 6 => Word_Tokens.Create (Word, 27, "again"), - 7 => Word_Tokens.Create (Word, 33, "and"), - 8 => Word_Tokens.Create (Word, 37, "again")); - - Actual_Result : Word_Tokens.Token_Array := + Intended_Result : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 1, "it"), + 2 => Swordy_Traits.Tokens.Create (Word, 4, "will"), + 3 => Swordy_Traits.Tokens.Create (Word, 9, "happen"), + 4 => Swordy_Traits.Tokens.Create (Word, 17, "again"), + 5 => Swordy_Traits.Tokens.Create (Word, 23, "and"), + 6 => Swordy_Traits.Tokens.Create (Word, 27, "again"), + 7 => Swordy_Traits.Tokens.Create (Word, 33, "and"), + 8 => Swordy_Traits.Tokens.Create (Word, 37, "again")); + + Actual_Result : Swordy_Traits.Tokens.Token_Array := My_Scan (More_Input'Unrestricted_Access, Test_Context); begin if Actual_Result /= Intended_Result then @@ -763,27 +765,27 @@ package body Rat_Tests.Lexer is is procedure My_Scan is new Swordy.Scan_Set ((Stamp_Word'Access, Ignore_Whitespace'Access), - Latin.EOT, Word_Tokens.Create (Blank, 1, "")); + Latin.EOT, Swordy_Traits.Tokens.Create (Blank, 1, "")); Test_Str1 : String (1 .. 10) := "one tw"; Test_Str2 : String (1 .. 10) := "o three"; Test_Str3 : String (1 .. 10) := Latin.EOT & " "; Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Intended_Result1 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 1, "one"), - 2 => Word_Tokens.Create (Blank, 1, ""), - 3 => Word_Tokens.Create (Blank, 1, "")); - Intended_Result2 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 9, "two"), - 2 => Word_Tokens.Create (Blank, 1, ""), - 3 => Word_Tokens.Create (Blank, 1, "")); - Intended_Result3 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 16, "three"), - 2 => Word_Tokens.Create (Blank, 1, ""), - 3 => Word_Tokens.Create (Blank, 1, "")); - - Actual_Result : Word_Tokens.Token_Array (1 .. 3); + Intended_Result1 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 1, "one"), + 2 => Swordy_Traits.Tokens.Create (Blank, 1, ""), + 3 => Swordy_Traits.Tokens.Create (Blank, 1, "")); + Intended_Result2 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 9, "two"), + 2 => Swordy_Traits.Tokens.Create (Blank, 1, ""), + 3 => Swordy_Traits.Tokens.Create (Blank, 1, "")); + Intended_Result3 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 16, "three"), + 2 => Swordy_Traits.Tokens.Create (Blank, 1, ""), + 3 => Swordy_Traits.Tokens.Create (Blank, 1, "")); + + Actual_Result : Swordy_Traits.Tokens.Token_Array (1 .. 3); begin My_Scan (Test_Str1, Test_Context, Actual_Result); if Actual_Result /= Intended_Result1 then @@ -821,24 +823,24 @@ package body Rat_Tests.Lexer is procedure My_Scan is new Swordy.Scan_Set_With ((Stamp_Word'Access, Ignore_Whitespace'Access), - Latin.EOT, Word_Tokens.Create (Blank, 1, "")); + Latin.EOT, Swordy_Traits.Tokens.Create (Blank, 1, "")); Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Intended_Result1 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 1, "it"), - 2 => Word_Tokens.Create (Word, 4, "will"), - 3 => Word_Tokens.Create (Word, 9, "happen"), - 4 => Word_Tokens.Create (Word, 16, "again"), - 5 => Word_Tokens.Create (Word, 22, "and")); - Intended_Result2 : Word_Tokens.Token_Array := - (1 => Word_Tokens.Create (Word, 26, "again"), - 2 => Word_Tokens.Create (Word, 32, "and"), - 3 => Word_Tokens.Create (Word, 36, "again"), - 4 => Word_Tokens.Create (Blank, 1, ""), - 5 => Word_Tokens.Create (Blank, 1, "")); - - Actual_Result : Word_Tokens.Token_Array (1 .. 5); + Intended_Result1 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 1, "it"), + 2 => Swordy_Traits.Tokens.Create (Word, 4, "will"), + 3 => Swordy_Traits.Tokens.Create (Word, 9, "happen"), + 4 => Swordy_Traits.Tokens.Create (Word, 16, "again"), + 5 => Swordy_Traits.Tokens.Create (Word, 22, "and")); + Intended_Result2 : Swordy_Traits.Tokens.Token_Array := + (1 => Swordy_Traits.Tokens.Create (Word, 26, "again"), + 2 => Swordy_Traits.Tokens.Create (Word, 32, "and"), + 3 => Swordy_Traits.Tokens.Create (Word, 36, "again"), + 4 => Swordy_Traits.Tokens.Create (Blank, 1, ""), + 5 => Swordy_Traits.Tokens.Create (Blank, 1, "")); + + Actual_Result : Swordy_Traits.Tokens.Token_Array (1 .. 5); begin My_Scan (More_Input'Unrestricted_Access, Test_Context, Actual_Result); if Actual_Result /= Intended_Result1 then @@ -867,7 +869,7 @@ package body Rat_Tests.Lexer is ((+"WORD", 1), (+"WHITESPACE", 1)); begin declare - Result : Word_Tokens.Token_Array := My_Scan (Test_Str, Test_Context); + Result : Swordy_Traits.Tokens.Token_Array := My_Scan (Test_Str, Test_Context); begin return Fail; end; @@ -895,7 +897,7 @@ package body Rat_Tests.Lexer is ((+"WORD", 1), (+"WHITESPACE", 1)); begin declare - Result : Word_Tokens.Token_Array := My_Scan (Test_Str, Test_Context); + Result : Swordy_Traits.Tokens.Token_Array := My_Scan (Test_Str, Test_Context); begin return Fail; end; @@ -934,7 +936,7 @@ package body Rat_Tests.Lexer is ((+"WORD", 1), (+"WHITESPACE", 1)); begin declare - Result : Word_Tokens.Token_Array := + Result : Swordy_Traits.Tokens.Token_Array := My_Scan (Get_Input'Unrestricted_Access, Test_Context); begin return Fail; @@ -955,12 +957,12 @@ package body Rat_Tests.Lexer is procedure My_Scan is new Swordy.Scan_Set ((Stamp_Word'Access, Ignore_Whitespace'Access), - Latin.EOT, Word_Tokens.Create (Blank, 1, "")); + Latin.EOT, Swordy_Traits.Tokens.Create (Blank, 1, "")); Test_Str : String := "()()"; Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Result : Word_Tokens.Token_Array (1 .. 5); + Result : Swordy_Traits.Tokens.Token_Array (1 .. 5); Expected_Errors : Packrat.Errors.Error_Info_Array := ((+"WORD", 1), (+"WHITESPACE", 1)); @@ -995,11 +997,11 @@ package body Rat_Tests.Lexer is procedure My_Scan is new Swordy.Scan_Set_With ((Stamp_Word'Access, Ignore_Whitespace'Access), - Latin.EOT, Word_Tokens.Create (Blank, 1, "")); + Latin.EOT, Swordy_Traits.Tokens.Create (Blank, 1, "")); Test_Context : Swordy.Lexer_Context := Swordy.Empty_Context; - Result : Word_Tokens.Token_Array (1 .. 5); + Result : Swordy_Traits.Tokens.Token_Array (1 .. 5); Expected_Errors : Packrat.Errors.Error_Info_Array := ((+"WORD", 1), (+"WHITESPACE", 1)); diff --git a/test/rat_tests-parse_graphs.adb b/test/rat_tests-parse_graphs.adb index e445f1b..981bb2c 100644 --- a/test/rat_tests-parse_graphs.adb +++ b/test/rat_tests-parse_graphs.adb @@ -1,6 +1,9 @@ -with Packrat.Parse_Graphs; +with + + Packrat.Traits, + Packrat.Parse_Graphs; package body Rat_Tests.Parse_Graphs is @@ -10,38 +13,38 @@ package body Rat_Tests.Parse_Graphs is (Noun, Determiner, Noun_Phrase, Preposition, Prepositional_Phrase, Verb, Verb_Phrase, Sentence); - package String_Tokens is new Packrat.Tokens (My_Labels, Character, String); - package Graphs is new Packrat.Parse_Graphs (My_Labels, Character, String, "<", String_Tokens); + package My_Traits is new Packrat.Traits (My_Labels, Character, String); + package Graphs is new Packrat.Parse_Graphs (My_Traits); -- These tokens are defined here purely to reduce verbosity when -- manually constructing the various test graphs in the package initialisation. - Noun_1 : String_Tokens.Token := String_Tokens.Create (Noun, 1, "i"); - Noun_4 : String_Tokens.Token := String_Tokens.Create (Noun, 4, "man"); - Noun_7 : String_Tokens.Token := String_Tokens.Create (Noun, 7, "park"); - Noun_10 : String_Tokens.Token := String_Tokens.Create (Noun, 10, "bat"); + Noun_1 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun, 1, "i"); + Noun_4 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun, 4, "man"); + Noun_7 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun, 7, "park"); + Noun_10 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun, 10, "bat"); - Det_3 : String_Tokens.Token := String_Tokens.Create (Determiner, 3, "a"); - Det_6 : String_Tokens.Token := String_Tokens.Create (Determiner, 6, "the"); - Det_9 : String_Tokens.Token := String_Tokens.Create (Determiner, 9, "a"); + Det_3 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Determiner, 3, "a"); + Det_6 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Determiner, 6, "the"); + Det_9 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Determiner, 9, "a"); - NP_1 : String_Tokens.Token := String_Tokens.Create (Noun_Phrase, 1, ""); - NP_3 : String_Tokens.Token := String_Tokens.Create (Noun_Phrase, 3, ""); - NP_6 : String_Tokens.Token := String_Tokens.Create (Noun_Phrase, 6, ""); - NP_9 : String_Tokens.Token := String_Tokens.Create (Noun_Phrase, 9, ""); + NP_1 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun_Phrase, 1, ""); + NP_3 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun_Phrase, 3, ""); + NP_6 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun_Phrase, 6, ""); + NP_9 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Noun_Phrase, 9, ""); - Prep_5 : String_Tokens.Token := String_Tokens.Create (Preposition, 5, "in"); - Prep_8 : String_Tokens.Token := String_Tokens.Create (Preposition, 8, "with"); + Prep_5 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Preposition, 5, "in"); + Prep_8 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Preposition, 8, "with"); - PP_5 : String_Tokens.Token := String_Tokens.Create (Prepositional_Phrase, 5, ""); - PP_8 : String_Tokens.Token := String_Tokens.Create (Prepositional_Phrase, 8, ""); + PP_5 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Prepositional_Phrase, 5, ""); + PP_8 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Prepositional_Phrase, 8, ""); - Verb_2 : String_Tokens.Token := String_Tokens.Create (Verb, 2, "saw"); + Verb_2 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Verb, 2, "saw"); - VP_2 : String_Tokens.Token := String_Tokens.Create (Verb_Phrase, 2, ""); + VP_2 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Verb_Phrase, 2, ""); - Sen_1 : String_Tokens.Token := String_Tokens.Create (Sentence, 1, ""); + Sen_1 : My_Traits.Tokens.Token := My_Traits.Tokens.Create (Sentence, 1, ""); -- This should be set up to be identical to the example parse in the paper diff --git a/test/rat_tests-tokens.adb b/test/rat_tests-tokens.adb index 8bfb516..74a3c0a 100644 --- a/test/rat_tests-tokens.adb +++ b/test/rat_tests-tokens.adb @@ -1,6 +1,6 @@ -with Packrat; +with Packrat.Tokens; package body Rat_Tests.Tokens is diff --git a/test/test_main.adb b/test/test_main.adb index 7a96cbd..ebca4fd 100644 --- a/test/test_main.adb +++ b/test/test_main.adb @@ -6,7 +6,8 @@ with Ada.Command_Line, Ada.Characters.Latin_1, Unit_Tests, - Packrat, + Packrat.Errors, + Packrat.Tokens, Rat_Tests.Errors, Rat_Tests.Tokens, Rat_Tests.Lexer, -- cgit