diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/packrat-lexers.adb | 2 | ||||
-rw-r--r-- | src/packrat-lexers.ads | 6 | ||||
-rw-r--r-- | src/packrat-parse_graphs.adb | 192 | ||||
-rw-r--r-- | src/packrat-parse_graphs.ads | 121 | ||||
-rw-r--r-- | src/packrat-parsers.adb | 12 | ||||
-rw-r--r-- | src/packrat-parsers.ads | 6 | ||||
-rw-r--r-- | src/packrat-standard.ads | 2 | ||||
-rw-r--r-- | src/packrat-tokens.adb | 72 | ||||
-rw-r--r-- | src/packrat-tokens.ads | 61 |
9 files changed, 252 insertions, 222 deletions
diff --git a/src/packrat-lexers.adb b/src/packrat-lexers.adb index 830fdeb..34bd5c0 100644 --- a/src/packrat-lexers.adb +++ b/src/packrat-lexers.adb @@ -71,7 +71,7 @@ package body Packrat.Lexers is procedure Token_Vector_To_Array (Input_Vector : in Token_Vectors.Vector; - Padding : in Traits.Tokens.Token; + Padding : in Traits.Tokens.Token_Type; Output_Array : out Traits.Tokens.Token_Array) is begin for N in Integer range 1 .. Output_Array'Length loop diff --git a/src/packrat-lexers.ads b/src/packrat-lexers.ads index 68a01d0..034590c 100644 --- a/src/packrat-lexers.ads +++ b/src/packrat-lexers.ads @@ -94,7 +94,7 @@ package Packrat.Lexers is generic Components : in Component_Array; Pad_In : in Traits.Element_Type; - Pad_Out : in Traits.Tokens.Token; + Pad_Out : in Traits.Tokens.Token_Type; package Scan_Set is procedure Scan @@ -109,7 +109,7 @@ package Packrat.Lexers is generic Components : in Component_Array; Pad_In : in Traits.Element_Type; - Pad_Out : in Traits.Tokens.Token; + Pad_Out : in Traits.Tokens.Token_Type; package Scan_Set_With is procedure Scan @@ -294,7 +294,7 @@ private package Token_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, - Element_Type => Traits.Tokens.Token, + Element_Type => Traits.Tokens.Token_Type, "=" => Traits.Tokens."="); package Label_Vectors is new Ada.Containers.Vectors diff --git a/src/packrat-parse_graphs.adb b/src/packrat-parse_graphs.adb index b919cff..2d5c2a5 100644 --- a/src/packrat-parse_graphs.adb +++ b/src/packrat-parse_graphs.adb @@ -30,69 +30,11 @@ package body Packrat.Parse_Graphs is function "<" - (Left, Right : Finished_Token) - return Boolean - is - Left_Index, Right_Index : Positive; - begin - if Traits.Tokens.Start (Left.Token) = Traits.Tokens.Start (Right.Token) then - if Left.Finish = Right.Finish then - 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 Traits.Tokens.Value (Left.Token) (Left_Index) < - Traits.Tokens.Value (Right.Token) (Right_Index) - then - return True; - 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 Traits.Tokens.Value (Left.Token)'Length < - Traits.Tokens.Value (Right.Token)'Length; - else - return Traits.Tokens.Label (Left.Token) < Traits.Tokens.Label (Right.Token); - end if; - else - return Left.Finish < Right.Finish; - end if; - else - return Traits.Tokens.Start (Left.Token) < Traits.Tokens.Start (Right.Token); - end if; - end "<"; - - - function "<" - (Left, Right : in Finished_Token_Array) - return Boolean - is - Left_Index : Positive := Left'First; - Right_Index : Positive := Right'First; - begin - while Left_Index <= Left'Last and Right_Index <= Right'Last loop - if Left (Left_Index) < Right (Right_Index) then - return True; - elsif Left (Left_Index) /= Right (Right_Index) then - return False; - end if; - Left_Index := Left_Index + 1; - Right_Index := Right_Index + 1; - end loop; - return Left'Length < Right'Length; - end "<"; - - - function "<" (Left, Right : in Finished_Token_Vectors.Vector) return Boolean is + use type Ada.Containers.Count_Type; + use type Traits.Tokens.Finished_Token_Type; Left_Index : Positive := Left.First_Index; Right_Index : Positive := Right.First_Index; begin @@ -190,8 +132,8 @@ package body Packrat.Parse_Graphs is return Finish_Vectors.Vector is Result : Finish_Vectors.Vector; - Current : Finish_Type; - Node_Label : Traits.Tokens.Token; + Current : Traits.Tokens.Finish_Type; + Node_Label : Traits.Tokens.Token_Type; begin Node_Label := Container.Internal_Graph.Label (Node); for Fin_Token of Container.Root_Elems loop @@ -217,7 +159,7 @@ package body Packrat.Parse_Graphs is return Finish_Vectors.Vector is Result : Finish_Vectors.Vector; - Current : Finish_Type; + Current : Traits.Tokens.Finish_Type; begin for Edge of Container.Internal_Graph.Outbound (Node) loop Current := Container.Internal_Graph.Label (Edge).Group_Finish; @@ -261,7 +203,7 @@ package body Packrat.Parse_Graphs is end Image; Mapping : Enum_Node_Maps.Map := Generate_Map (Container); - Current : Traits.Tokens.Token; + Current : Traits.Tokens.Token_Type; Result : SU.Unbounded_String; begin for Iter in Mapping.Iterate loop @@ -272,7 +214,8 @@ package body Packrat.Parse_Graphs is for Fin of In_Finishes (Container, Node) loop SU.Append (Result, Image (Fin) & " ->" & Latin.HT); declare - Groupings : Token_Group_Array := Container.Subgroups ((Current, Fin)); + A_Fin_Token : Traits.Tokens.Finished_Token_Type := (Current, Fin); + Groupings : Token_Group_Array := Container.Subgroups (A_Fin_Token); begin if Groupings'Length = 0 then SU.Append (Result, "Leaf" & Latin.LF); @@ -309,7 +252,7 @@ package body Packrat.Parse_Graphs is function Contains (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) return Boolean is begin return Container.Label_Map.Contains (Token); @@ -318,9 +261,10 @@ package body Packrat.Parse_Graphs is function Contains (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean is + use type Traits.Tokens.Finished_Token_Type; Node : Node_ID_Type; begin if not Container.Contains (Position.Token) then @@ -374,13 +318,15 @@ package body Packrat.Parse_Graphs is function Reachable (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean is -- This is basically a depth first search function. function Finder - (Current : in Finished_Token) - return Boolean is + (Current : in Traits.Tokens.Finished_Token_Type) + return Boolean + is + use type Traits.Tokens.Finished_Token_Type; begin return Current = Position or else (for some Grouping of Container.Subgroups (Current) => @@ -431,7 +377,7 @@ package body Packrat.Parse_Graphs is function Valid_Token - (Fin_Token : in Finished_Token) + (Fin_Token : in Traits.Tokens.Finished_Token_Type) return Boolean is begin return Fin_Token.Finish + 1 >= Traits.Tokens.Start (Fin_Token.Token); @@ -439,8 +385,8 @@ package body Packrat.Parse_Graphs is function Valid_Starts_Finishes - (Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + (Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) return Boolean is Subvec : Finished_Token_Vectors.Vector; @@ -468,13 +414,15 @@ package body Packrat.Parse_Graphs is function Loops_Introduced (Container : in Parse_Graph; - Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) return Boolean is function Looper - (Current : in Finished_Token) - return Boolean is + (Current : in Traits.Tokens.Finished_Token_Type) + return Boolean + is + use type Traits.Tokens.Finished_Token_Type; begin if not Container.Contains (Current.Token) then return False; @@ -494,20 +442,24 @@ package body Packrat.Parse_Graphs is function Is_Sorted - (Finishes : in Finish_Array) + (Finishes : in Traits.Tokens.Finish_Array) return Boolean is - function Actual is new Sorted (Finish_Type, Finish_Array); + function Actual is new Sorted + (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array); begin return Actual (Finishes); end Is_Sorted; function Is_Sorted - (Positions : in Finished_Token_Array) + (Positions : in Traits.Tokens.Finished_Token_Array) return Boolean is - function Actual is new Sorted (Finished_Token, Finished_Token_Array); + function Actual is new Sorted + (Traits.Tokens.Finished_Token_Type, + Traits.Tokens.Finished_Token_Array, + Traits.Tokens."<"); begin return Actual (Positions); end Is_Sorted; @@ -524,20 +476,24 @@ package body Packrat.Parse_Graphs is function No_Duplicates - (Finishes : in Finish_Array) + (Finishes : in Traits.Tokens.Finish_Array) return Boolean is - function Actual is new No_Dups (Finish_Type, Finish_Array); + function Actual is new No_Dups + (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array); begin return Actual (Finishes); end No_Duplicates; function No_Duplicates - (Positions : in Finished_Token_Array) + (Positions : in Traits.Tokens.Finished_Token_Array) return Boolean is - function Actual is new No_Dups (Finished_Token, Finished_Token_Array); + function Actual is new No_Dups + (Traits.Tokens.Finished_Token_Type, + Traits.Tokens.Finished_Token_Array, + Traits.Tokens."="); begin return Actual (Positions); end No_Duplicates; @@ -558,7 +514,7 @@ package body Packrat.Parse_Graphs is procedure Include (Container : in out Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) is Node_ID : Node_ID_Type; begin @@ -572,8 +528,8 @@ package body Packrat.Parse_Graphs is procedure Connect (Container : in out Parse_Graph; - Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) is Parent_ID : Node_ID_Type; Use_GID : Group_ID_Type; @@ -608,7 +564,7 @@ package body Packrat.Parse_Graphs is procedure Prune (Container : in out Parse_Graph; - Token : in Traits.Tokens.Token) is + Token : in Traits.Tokens.Token_Type) is begin if not Container.Contains (Token) then return; @@ -625,8 +581,9 @@ package body Packrat.Parse_Graphs is procedure Prune (Container : in out Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) is + use type Traits.Tokens.Finished_Token_Type; Node : Node_ID_Type; begin if not Container.Contains (Position.Token) then @@ -735,7 +692,7 @@ package body Packrat.Parse_Graphs is procedure Set_Root (Container : in out Parse_Graph; - Tokens : in Finished_Token_Array) is + Tokens : in Traits.Tokens.Finished_Token_Array) is begin Container.Clear_Root; for Fin_Token of Tokens loop @@ -756,10 +713,12 @@ package body Packrat.Parse_Graphs is function Root_Elements (Container : in Parse_Graph) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array is function V2A is new Vector_To_Array - (Finished_Token, Finished_Token_Array, Finished_Token_Vectors); + (Traits.Tokens.Finished_Token_Type, + Traits.Tokens.Finished_Token_Array, + Finished_Token_Vectors); begin return V2A (Container.Root_Elems); end Root_Elements; @@ -770,10 +729,11 @@ package body Packrat.Parse_Graphs is function Finish_List (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) - return Finish_Array + Token : in Traits.Tokens.Token_Type) + return Traits.Tokens.Finish_Array is - function V2A is new Vector_To_Array (Finish_Type, Finish_Array, Finish_Vectors); + function V2A is new Vector_To_Array + (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array, Finish_Vectors); Result : Finish_Vectors.Vector; begin for Fin_Token of Container.Root_Elems loop @@ -793,7 +753,7 @@ package body Packrat.Parse_Graphs is function Is_Leaf (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean is begin for Edge of Container.Internal_Graph.Outbound @@ -809,7 +769,7 @@ package body Packrat.Parse_Graphs is function Is_Branch (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean is begin return not Container.Is_Leaf (Position); @@ -821,14 +781,14 @@ package body Packrat.Parse_Graphs is function Subgroups (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Token_Group_Array is function V2A is new Vector_To_Array (Token_Group, Token_Group_Array, Token_Group_Vectors); Groupings : Group_Finished_Token_Maps.Map; Edge_Label : Edge_Label_Type; - Next_Token : Finished_Token; + Next_Token : Traits.Tokens.Finished_Token_Type; Result : Token_Group_Vectors.Vector; begin for Edge of Container.Internal_Graph.Outbound @@ -883,7 +843,7 @@ package body Packrat.Parse_Graphs is function Element (Grouping : in Token_Group; Index : in Positive) - return Finished_Token is + return Traits.Tokens.Finished_Token_Type is begin return Grouping.Elems.Element (Index); end Element; @@ -891,10 +851,12 @@ package body Packrat.Parse_Graphs is function Elements (Grouping : in Token_Group) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array is function V2A is new Vector_To_Array - (Finished_Token, Finished_Token_Array, Finished_Token_Vectors); + (Traits.Tokens.Finished_Token_Type, + Traits.Tokens.Finished_Token_Array, + Finished_Token_Vectors); begin return V2A (Grouping.Elems); end Elements; @@ -902,7 +864,7 @@ package body Packrat.Parse_Graphs is function Parent (Grouping : in Token_Group) - return Finished_Token is + return Traits.Tokens.Finished_Token_Type is begin return Grouping.Parent; end Parent; @@ -910,7 +872,7 @@ package body Packrat.Parse_Graphs is function Finish (Grouping : in Token_Group) - return Finish_Type is + return Traits.Tokens.Finish_Type is begin return Grouping.Parent.Finish; end Finish; @@ -959,13 +921,15 @@ package body Packrat.Parse_Graphs is function Ambiguities (Container : in Parse_Graph; Ambiguous_Root : out Boolean) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array is function V2A is new Vector_To_Array - (Finished_Token, Finished_Token_Array, Finished_Token_Vectors); + (Traits.Tokens.Finished_Token_Type, + Traits.Tokens.Finished_Token_Array, + Finished_Token_Vectors); Seen_Finishes : Finish_Group_Maps.Map; Edge_Label : Edge_Label_Type; - Next_Token : Finished_Token; + Next_Token : Traits.Tokens.Finished_Token_Type; Result : Finished_Token_Vectors.Vector; begin Ambiguous_Root := Container.Has_Root and then Container.Is_Root_Ambiguous; @@ -1037,9 +1001,9 @@ package body Packrat.Parse_Graphs is function Token_Isomorph (Left_Graph : in Parse_Graph; - Left_Position : in Finished_Token; + Left_Position : in Traits.Tokens.Finished_Token_Type; Right_Graph : in Parse_Graph; - Right_Position : in Finished_Token; + Right_Position : in Traits.Tokens.Finished_Token_Type; Offset : in Integer; Mapping : in out Isomorph_Maps.Map) return Boolean @@ -1115,9 +1079,9 @@ package body Packrat.Parse_Graphs is function Isomorphic_Subgraph (Left_Graph : in Parse_Graph; - Left_Position : in Finished_Token; + Left_Position : in Traits.Tokens.Finished_Token_Type; Right_Graph : in Parse_Graph; - Right_Position : in Finished_Token) + Right_Position : in Traits.Tokens.Finished_Token_Type) return Boolean is Offset : Integer := @@ -1136,7 +1100,7 @@ package body Packrat.Parse_Graphs is function To_Node (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) return Node_ID_Type is begin return Container.Label_Map.Element (Token); @@ -1145,7 +1109,7 @@ package body Packrat.Parse_Graphs is function To_Node (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Node_ID_Type is begin return Container.Label_Map.Element (Position.Token); diff --git a/src/packrat-parse_graphs.ads b/src/packrat-parse_graphs.ads index 208292c..7d14652 100644 --- a/src/packrat-parse_graphs.ads +++ b/src/packrat-parse_graphs.ads @@ -27,26 +27,7 @@ package Packrat.Parse_Graphs is Empty_Graph : constant Parse_Graph; - subtype Finish_Type is Natural; - type Finish_Array is array (Positive range <>) of Finish_Type; - - type Finished_Token is record - Token : Traits.Tokens.Token; - Finish : Finish_Type; - end record; - - type Finished_Token_Array is array (Positive range <>) of Finished_Token; - - function "<" - (Left, Right : in Finished_Token) - return Boolean; - - function "<" - (Left, Right : in Finished_Token_Array) - return Boolean; - - use type Ada.Containers.Count_Type; - type Token_Group is private with Type_Invariant => Length (Token_Group) > 0; + type Token_Group is private with Type_Invariant => Integer (Length (Token_Group)) > 0; type Token_Group_Array is array (Positive range <>) of Token_Group; function "<" @@ -89,12 +70,12 @@ package Packrat.Parse_Graphs is function Contains (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) return Boolean; function Contains (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean; function Contains @@ -104,7 +85,7 @@ package Packrat.Parse_Graphs is function Reachable (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean with Pre => Container.Has_Root; @@ -114,29 +95,29 @@ package Packrat.Parse_Graphs is with Pre => Container.Has_Root; function Valid_Token - (Fin_Token : in Finished_Token) + (Fin_Token : in Traits.Tokens.Finished_Token_Type) return Boolean; function Valid_Starts_Finishes - (Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + (Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) return Boolean with Pre => Subtokens'Length > 0; function Loops_Introduced (Container : in Parse_Graph; - Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) return Boolean with Pre => Subtokens'Length > 0 and Valid_Starts_Finishes (Parent, Subtokens); function Is_Sorted - (Finishes : in Finish_Array) + (Finishes : in Traits.Tokens.Finish_Array) return Boolean; function Is_Sorted - (Positions : in Finished_Token_Array) + (Positions : in Traits.Tokens.Finished_Token_Array) return Boolean; function Is_Sorted @@ -144,11 +125,11 @@ package Packrat.Parse_Graphs is return Boolean; function No_Duplicates - (Finishes : in Finish_Array) + (Finishes : in Traits.Tokens.Finish_Array) return Boolean; function No_Duplicates - (Positions : in Finished_Token_Array) + (Positions : in Traits.Tokens.Finished_Token_Array) return Boolean; function No_Duplicates @@ -160,25 +141,25 @@ package Packrat.Parse_Graphs is procedure Include (Container : in out Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) with Post => Container.Contains (Token); procedure Connect (Container : in out Parse_Graph; - Parent : in Finished_Token; - Subtokens : in Finished_Token_Array) + Parent : in Traits.Tokens.Finished_Token_Type; + Subtokens : in Traits.Tokens.Finished_Token_Array) with Pre => Subtokens'Length > 0 and Valid_Starts_Finishes (Parent, Subtokens) and not Container.Loops_Introduced (Parent, Subtokens); procedure Prune (Container : in out Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) with Post => not Container.Contains (Token); procedure Prune (Container : in out Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) with Post => not Container.Contains (Position); procedure Prune @@ -200,7 +181,7 @@ package Packrat.Parse_Graphs is procedure Set_Root (Container : in out Parse_Graph; - Tokens : in Finished_Token_Array) + Tokens : in Traits.Tokens.Finished_Token_Array) with Pre => (for all F of Tokens => Container.Contains (F.Token)), Post => Container.Has_Root; @@ -210,7 +191,7 @@ package Packrat.Parse_Graphs is function Root_Elements (Container : in Parse_Graph) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array with Pre => Container.Has_Root; @@ -218,21 +199,21 @@ package Packrat.Parse_Graphs is function Finish_List (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) - return Finish_Array + Token : in Traits.Tokens.Token_Type) + return Traits.Tokens.Finish_Array with Pre => Container.Contains (Token), Post => Is_Sorted (Finish_List'Result) and No_Duplicates (Finish_List'Result); function Is_Leaf (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean with Pre => Container.Contains (Position); function Is_Branch (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Boolean with Pre => Container.Contains (Position); @@ -241,7 +222,7 @@ package Packrat.Parse_Graphs is function Subgroups (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Token_Group_Array with Pre => Container.Contains (Position), Post => Is_Sorted (Subgroups'Result) and @@ -263,22 +244,22 @@ package Packrat.Parse_Graphs is function Element (Grouping : in Token_Group; Index : in Positive) - return Finished_Token + return Traits.Tokens.Finished_Token_Type with Pre => Index in First_Index (Grouping) .. Last_Index (Grouping); function Elements (Grouping : in Token_Group) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array with Post => Is_Sorted (Elements'Result) and Valid_Starts_Finishes (Parent (Grouping), Elements'Result); function Parent (Grouping : in Token_Group) - return Finished_Token; + return Traits.Tokens.Finished_Token_Type; function Finish (Grouping : in Token_Group) - return Finish_Type; + return Traits.Tokens.Finish_Type; @@ -300,7 +281,7 @@ package Packrat.Parse_Graphs is function Ambiguities (Container : in Parse_Graph; Ambiguous_Root : out Boolean) - return Finished_Token_Array + return Traits.Tokens.Finished_Token_Array with Post => Is_Sorted (Ambiguities'Result) and No_Duplicates (Ambiguities'Result); @@ -314,9 +295,9 @@ package Packrat.Parse_Graphs is function Isomorphic_Subgraph (Left_Graph : in Parse_Graph; - Left_Position : in Finished_Token; + Left_Position : in Traits.Tokens.Finished_Token_Type; Right_Graph : in Parse_Graph; - Right_Position : in Finished_Token) + Right_Position : in Traits.Tokens.Finished_Token_Type) return Boolean with Pre => Left_Graph.Contains (Left_Position) and Right_Graph.Contains (Right_Position); @@ -335,14 +316,14 @@ private type Node_ID_Type is new Positive; type Edge_ID_Type is new Positive; - subtype Node_Label_Type is Traits.Tokens.Token; + subtype Node_Label_Type is Traits.Tokens.Token_Type; subtype Group_ID_Type is Positive; type Edge_Label_Type is record Group_ID : Group_ID_Type; - Group_Finish : Finish_Type; - Subnode_Finish : Finish_Type; + Group_Finish : Traits.Tokens.Finish_Type; + Subnode_Finish : Traits.Tokens.Finish_Type; end record; @@ -350,12 +331,12 @@ private function To_Node (Container : in Parse_Graph; - Token : in Traits.Tokens.Token) + Token : in Traits.Tokens.Token_Type) return Node_ID_Type; function To_Node (Container : in Parse_Graph; - Position : in Finished_Token) + Position : in Traits.Tokens.Finished_Token_Type) return Node_ID_Type; function Locally_Reachable @@ -368,7 +349,7 @@ private -- This 'use type' is to avoid some ambiguities with "=" functions when -- instantiating the Base package. - use type Traits.Tokens.Token; + use type Traits.Tokens.Token_Type; package Base is new Directed_Graphs (Node_ID_Type => Node_ID_Type, @@ -382,14 +363,15 @@ private package Finished_Token_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, - Element_Type => Finished_Token); + Element_Type => Traits.Tokens.Finished_Token_Type, + "=" => Traits.Tokens."="); function "<" (Left, Right : in Finished_Token_Vectors.Vector) return Boolean; type Token_Group is record - Parent : Finished_Token; + Parent : Traits.Tokens.Finished_Token_Type; Elems : Finished_Token_Vectors.Vector; end record; @@ -399,13 +381,14 @@ private package Finish_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, - Element_Type => Finish_Type); + Element_Type => Traits.Tokens.Finish_Type); package Finish_Sort is new Finish_Vectors.Generic_Sorting; package Node_Label_Maps is new Ada.Containers.Ordered_Maps - (Key_Type => Traits.Tokens.Token, - Element_Type => Node_ID_Type); + (Key_Type => Traits.Tokens.Token_Type, + Element_Type => Node_ID_Type, + "<" => Traits.Tokens."<"); type Parse_Graph is tagged record Internal_Graph : Base.Graph := Base.Empty_Graph; @@ -429,7 +412,8 @@ private (Index_Type => Positive, Element_Type => Token_Group); - package Finished_Token_Sort is new Finished_Token_Vectors.Generic_Sorting; + package Finished_Token_Sort is new + Finished_Token_Vectors.Generic_Sorting ("<" => Traits.Tokens."<"); package Token_Group_Sort is new Token_Group_Vectors.Generic_Sorting; package Group_Finished_Token_Maps is new Ada.Containers.Ordered_Maps @@ -438,7 +422,7 @@ private "=" => Finished_Token_Vectors."="); package Finish_Group_Maps is new Ada.Containers.Ordered_Maps - (Key_Type => Finish_Type, + (Key_Type => Traits.Tokens.Finish_Type, Element_Type => Group_ID_Type); package Enum_Node_Maps is new Ada.Containers.Ordered_Maps @@ -451,9 +435,10 @@ private package Isomorph_Maps is new Ada.Containers.Ordered_Maps - (Key_Type => Finished_Token, + (Key_Type => Traits.Tokens.Finished_Token_Type, Element_Type => Finished_Token_Vectors.Vector, - "=" => Finished_Token_Vectors."="); + "=" => Finished_Token_Vectors."=", + "<" => Traits.Tokens."<"); function Group_Isomorph (Left_Graph : in Parse_Graph; @@ -466,9 +451,9 @@ private function Token_Isomorph (Left_Graph : in Parse_Graph; - Left_Position : in Finished_Token; + Left_Position : in Traits.Tokens.Finished_Token_Type; Right_Graph : in Parse_Graph; - Right_Position : in Finished_Token; + Right_Position : in Traits.Tokens.Finished_Token_Type; Offset : in Integer; Mapping : in out Isomorph_Maps.Map) return Boolean; diff --git a/src/packrat-parsers.adb b/src/packrat-parsers.adb index d854f73..1a3935c 100644 --- a/src/packrat-parsers.adb +++ b/src/packrat-parsers.adb @@ -23,7 +23,7 @@ package body Packrat.Parsers is (Left, Right : in Tok_Holds.Holder) return Boolean is - use type Graphs.Finished_Token_Array; + use type Traits.Tokens.Finished_Token_Array; begin return Left.Element < Right.Element; end "<"; @@ -86,10 +86,10 @@ package body Packrat.Parsers is function Element (Hold : in Tok_Holds.Holder) - return Graphs.Finished_Token_Array is + return Traits.Tokens.Finished_Token_Array is begin if Hold.Is_Empty then - return Value : Graphs.Finished_Token_Array (1 .. 0); + return Value : Traits.Tokens.Finished_Token_Array (1 .. 0); else return Hold.Element; end if; @@ -300,7 +300,7 @@ package body Packrat.Parsers is return Combinator_Result is use type Traits.Element_Array; - use type Graphs.Finished_Token_Array; + use type Traits.Tokens.Finished_Token_Array; Salt, Temp : Combinator_Result; Adjust : Result_Sets.Set; begin @@ -401,7 +401,7 @@ package body Packrat.Parsers is return Graphs.Empty_Graph; end if; declare - Root_Elems : Graphs.Finished_Token_Array (1 .. Length); + Root_Elems : Traits.Tokens.Finished_Token_Array (1 .. Length); begin for R of Root_Result.Results loop for T of Element (R.Tokens) loop @@ -538,7 +538,7 @@ package body Packrat.Parsers is return Combinator_Result is Salt : Combinator_Result := Combo (Input, Context, Start); - Current : Graphs.Finished_Token; + Current : Traits.Tokens.Finished_Token_Type; Processed : Result_Sets.Set; begin if Salt.Status = Failure then diff --git a/src/packrat-parsers.ads b/src/packrat-parsers.ads index 93d06dd..03b262c 100644 --- a/src/packrat-parsers.ads +++ b/src/packrat-parsers.ads @@ -331,8 +331,8 @@ private "=" => Traits."="); package Tok_Holds is new Ada.Containers.Indefinite_Holders - (Element_Type => Graphs.Finished_Token_Array, - "=" => Graphs."="); + (Element_Type => Traits.Tokens.Finished_Token_Array, + "=" => Traits.Tokens."="); function "<" (Left, Right : in Elem_Holds.Holder) @@ -348,7 +348,7 @@ private function Element (Hold : in Tok_Holds.Holder) - return Graphs.Finished_Token_Array; + return Traits.Tokens.Finished_Token_Array; diff --git a/src/packrat-standard.ads b/src/packrat-standard.ads index bf0b815..b51804d 100644 --- a/src/packrat-standard.ads +++ b/src/packrat-standard.ads @@ -36,7 +36,7 @@ package Packrat.Standard is package Parser_Traits is new Packrat.Traits (Label_Enum => Parser_Labels, - Element_Type => Lexer_Tokens.Token, + Element_Type => Lexer_Tokens.Token_Type, Element_Array => Lexer_Tokens.Token_Array, "<" => Lexer_Tokens."<"); diff --git a/src/packrat-tokens.adb b/src/packrat-tokens.adb index 81d68c3..2e47db9 100644 --- a/src/packrat-tokens.adb +++ b/src/packrat-tokens.adb @@ -24,7 +24,7 @@ package body Packrat.Tokens is function "<" - (Left, Right : in Token) + (Left, Right : in Token_Type) return Boolean is begin if Left.Start_At = Right.Start_At then @@ -44,13 +44,48 @@ package body Packrat.Tokens is return Boolean is function LT is new Array_Less_Than - (Base_Type => Token, + (Base_Type => Token_Type, Array_Type => Token_Array); begin return LT (Left, Right); end "<"; + function "<" + (Left, Right : Finished_Token_Type) + return Boolean + is + function LT is new Array_Less_Than + (Base_Type => Element_Type, + Array_Type => Element_Array); + begin + if Start (Left.Token) = Start (Right.Token) then + if Left.Finish = Right.Finish then + if Label (Left.Token) = Label (Right.Token) then + return Value (Left.Token) < Value (Right.Token); + else + return Label (Left.Token) < Label (Right.Token); + end if; + else + return Left.Finish < Right.Finish; + end if; + else + return Start (Left.Token) < Start (Right.Token); + end if; + end "<"; + + + function "<" + (Left, Right : in Finished_Token_Array) + return Boolean + is + function LT is new Array_Less_Than + (Base_Type => Finished_Token_Type, + Array_Type => Finished_Token_Array); + begin + return LT (Left, Right); + end "<"; + @@ -58,9 +93,9 @@ package body Packrat.Tokens is (Ident : in Label_Enum; Start : in Positive; Value : in Element_Array) - return Token is + return Token_Type is begin - return This : Token do + return This : Token_Type do This.Identifier := Ident; This.Start_At := Start; This.Token_Value := Value_Holders.To_Holder (Value); @@ -72,7 +107,7 @@ package body Packrat.Tokens is function Debug_String - (This : in Token) + (This : in Token_Type) return String is Result : SU.Unbounded_String := +""; @@ -84,11 +119,26 @@ package body Packrat.Tokens is end Debug_String; + function Debug_String + (This : in Finished_Token_Type) + return String + is + Result : SU.Unbounded_String := +""; + begin + SU.Append (Result, "Token " & Label_Enum'Image (This.Token.Identifier) & + " at input position" & Integer'Image (This.Token.Start_At) & " to" & + Integer'Image (This.Finish) & " with value length" & + Integer'Image (This.Token.Token_Value.Constant_Reference.Element'Length) & + Latin.LF); + return -Result; + end Debug_String; + + function Label - (This : in Token) + (This : in Token_Type) return Label_Enum is begin return This.Identifier; @@ -96,7 +146,7 @@ package body Packrat.Tokens is function Start - (This : in Token) + (This : in Token_Type) return Positive is begin return This.Start_At; @@ -104,7 +154,7 @@ package body Packrat.Tokens is function Value - (This : in Token) + (This : in Token_Type) return Element_Array is begin return This.Token_Value.Element; @@ -115,7 +165,7 @@ package body Packrat.Tokens is function Is_Label - (This : in Token) + (This : in Token_Type) return Boolean is begin return This.Identifier = Ident; @@ -123,7 +173,7 @@ package body Packrat.Tokens is function Is_Start - (This : in Token) + (This : in Token_Type) return Boolean is begin return This.Start_At = Start; @@ -131,7 +181,7 @@ package body Packrat.Tokens is function Is_Value - (This : in Token) + (This : in Token_Type) return Boolean is begin return This.Token_Value.Element = Value; diff --git a/src/packrat-tokens.ads b/src/packrat-tokens.ads index b731b13..5018610 100644 --- a/src/packrat-tokens.ads +++ b/src/packrat-tokens.ads @@ -17,65 +17,96 @@ generic package Packrat.Tokens is - type Token is private; - type Token_Array is array (Positive range <>) of Token; + type Token_Type is private; + type Token_Array is array (Positive range <>) of Token_Type; + + subtype Finish_Type is Natural; + type Finish_Array is array (Positive range <>) of Finish_Type; + + type Finished_Token_Type is record + Token : Token_Type; + Finish : Finish_Type; + end record; + + type Finished_Token_Array is array (Positive range <>) of Finished_Token_Type; + + function "<" - (Left, Right : in Token) + (Left, Right : in Token_Type) return Boolean; function "<" (Left, Right : in Token_Array) return Boolean; + function "<" + (Left, Right : in Finished_Token_Type) + return Boolean; + + function "<" + (Left, Right : in Finished_Token_Array) + return Boolean; + + + + + -- 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 Create (Ident : in Label_Enum; Start : in Positive; Value : in Element_Array) - return Token; + return Token_Type; - -- 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) + (This : in Token_Type) return String; + function Debug_String + (This : in Finished_Token_Type) + return String; + + + function Label - (This : in Token) + (This : in Token_Type) return Label_Enum; function Start - (This : in Token) + (This : in Token_Type) return Positive; function Value - (This : in Token) + (This : in Token_Type) return Element_Array; + + generic Ident : in Label_Enum; function Is_Label - (This : in Token) + (This : in Token_Type) return Boolean; generic Start : in Positive; function Is_Start - (This : in Token) + (This : in Token_Type) return Boolean; generic Value : in Element_Array; function Is_Value - (This : in Token) + (This : in Token_Type) return Boolean; @@ -90,7 +121,7 @@ private return Boolean; - type Token is record + type Token_Type is record Identifier : Label_Enum; Start_At : Positive; Token_Value : Value_Holders.Holder; |