summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/packrat-errors.adb5
-rw-r--r--src/packrat-errors.ads81
-rw-r--r--src/packrat-lexer.adb77
-rw-r--r--src/packrat-lexer.ads133
-rw-r--r--src/packrat-parse_graphs.adb85
-rw-r--r--src/packrat-parse_graphs.ads44
-rw-r--r--src/packrat-tokens.adb5
-rw-r--r--src/packrat-tokens.ads73
-rw-r--r--src/packrat-traits.ads24
-rw-r--r--src/packrat.adb12
-rw-r--r--src/packrat.ads156
11 files changed, 359 insertions, 336 deletions
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