From 81c4526fa275a256bfefe0f8a7cd638369ea1252 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sat, 28 Nov 2020 16:24:04 +1100 Subject: Cleaned up Lexer, Util package names --- src/packrat-lexers.ads | 327 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 src/packrat-lexers.ads (limited to 'src/packrat-lexers.ads') diff --git a/src/packrat-lexers.ads b/src/packrat-lexers.ads new file mode 100644 index 0000000..57fc462 --- /dev/null +++ b/src/packrat-lexers.ads @@ -0,0 +1,327 @@ + + +with + + Packrat.Traits; + +private with + + Ada.Containers.Vectors, + Ada.Containers.Ordered_Sets; + + +generic + + with package Traits is new Packrat.Traits (<>); + +package Packrat.Lexers is + + + type Combinator_Result is private; + + type Combinator is access function + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + type Combinator_Array is array (Positive range <>) of Combinator; + + + + + type Lexer_Context is private; + + Empty_Context : constant Lexer_Context; + + + + + type Component_Result is private; + + type Component is access function + (Input : in Traits.Element_Array; + Context : in out Lexer_Context) + return Component_Result; + + type Component_Array is array (Positive range <>) of Component; + + + + + type With_Input is access function + return Traits.Element_Array; + + + + + generic + Label : in Traits.Label_Enum; + with function Combo + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + function Stamp + (Input : in Traits.Element_Array; + Context : in out Lexer_Context) + return Component_Result; + + generic + Label : in Traits.Label_Enum; + with function Combo + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + function Ignore + (Input : in Traits.Element_Array; + Context : in out Lexer_Context) + return Component_Result; + + + + + generic + Components : in Component_Array; + function Scan + (Input : in Traits.Element_Array; + Context : in out Lexer_Context) + return Traits.Tokens.Token_Array; + + generic + Components : in Component_Array; + function Scan_Only + (Input : in Traits.Element_Array; + Context : in out Lexer_Context) + return Traits.Tokens.Token_Array; + + generic + Components : in Component_Array; + function Scan_With + (Input : in With_Input; + Context : in out Lexer_Context) + return Traits.Tokens.Token_Array; + + generic + Components : in Component_Array; + Pad_In : in Traits.Element_Type; + Pad_Out : in Traits.Tokens.Token; + procedure Scan_Set + (Input : in Traits.Element_Array; + Context : in out Lexer_Context; + Output : out Traits.Tokens.Token_Array); + + generic + Components : in Component_Array; + 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 Traits.Tokens.Token_Array); + + + + + generic + Params : in Combinator_Array; + function Sequence + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Param + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + Number : in Positive; + function Count + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Param + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + Minimum : in Natural := 0; + function Many + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Param + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + with function Test + (Item : in Traits.Element_Type) + return Boolean; + Minimum : in Natural := 0; + function Many_Until + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + + + + generic + with function Test + (Item : in Traits.Element_Type) + return Boolean; + function Satisfy + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Test + (Item : in Traits.Element_Type) + return Boolean; + with function Change + (From : in Traits.Element_Type) + return Traits.Element_Type; + function Satisfy_With + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + Item : in Traits.Element_Type; + function Match + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + Item : in Traits.Element_Type; + with function Change + (From : in Traits.Element_Type) + return Traits.Element_Type; + function Match_With + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + Items : in Traits.Element_Array; + function Multimatch + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + Number : in Positive := 1; + function Take + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Test + (Item : in Traits.Element_Type) + return Boolean; + function Take_While + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + with function Test + (Item : in Traits.Element_Type) + return Boolean; + function Take_Until + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + + + + generic + EOL_Item : in Traits.Element_Type; + function Line_End + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + generic + EOF_Item : in Traits.Element_Type; + function Input_End + (Input : in Traits.Element_Array; + Start : in Positive) + return Combinator_Result; + + +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 => Traits.Tokens.Token, + "=" => Traits.Tokens."="); + + package Label_Vectors is new Ada.Containers.Vectors + (Index_Type => Positive, + Element_Type => Traits.Label_Enum); + + package Label_Sets is new Ada.Containers.Ordered_Sets + (Element_Type => Traits.Label_Enum); + + package Input_Holders is new Ada.Containers.Indefinite_Holders + (Element_Type => Traits.Element_Array); + + + + + type Combinator_Result is record + Finish : Natural; + Status : Result_Status; + end record; + + Empty_Fail : constant Combinator_Result := + (Finish => 0, + Status => Failure); + + + + + type Component_Result is (Component_Failure, Component_Success); + + + + + type Lexer_Context is record + Result_So_Far : Token_Vectors.Vector; + Position : Positive := 1; + Offset : Natural := 0; + Status : Result_Status := Success; + Pass_Forward : Input_Holders.Holder; + Empty_Labels : Label_Sets.Set; + Error_Labels : Label_Vectors.Vector; + Allow_Incomplete : Boolean := True; + end record; + + Empty_Context : constant Lexer_Context := + (Result_So_Far => Token_Vectors.Empty_Vector, + Position => 1, + Offset => 0, + Status => Success, + Pass_Forward => Input_Holders.Empty_Holder, + Empty_Labels => Label_Sets.Empty_Set, + Error_Labels => Label_Vectors.Empty_Vector, + Allow_Incomplete => True); + + +end Packrat.Lexers; + + -- cgit