summaryrefslogtreecommitdiff
path: root/src/packrat-lexer.adb
diff options
context:
space:
mode:
Diffstat (limited to 'src/packrat-lexer.adb')
-rw-r--r--src/packrat-lexer.adb265
1 files changed, 97 insertions, 168 deletions
diff --git a/src/packrat-lexer.adb b/src/packrat-lexer.adb
index eb126eb..faf8f71 100644
--- a/src/packrat-lexer.adb
+++ b/src/packrat-lexer.adb
@@ -1,81 +1,8 @@
-with
-
- Ada.Unchecked_Deallocation;
-
-
package body Packrat.Lexer is
- procedure Free_Array is new Ada.Unchecked_Deallocation
- (Object => Element_Array, Name => Element_Array_Access);
-
-
- procedure Initialize
- (This : in out Lexer_Context) is
- begin
- null;
- end Initialize;
-
-
- procedure Adjust
- (This : in out Lexer_Context)
- is
- New_Array : Element_Array_Access;
- begin
- if This.Pass_Forward /= null then
- New_Array := new Element_Array (1 .. This.Pass_Forward.all'Length);
- New_Array.all := This.Pass_Forward.all;
- This.Pass_Forward := New_Array;
- end if;
- end Adjust;
-
-
- procedure Finalize
- (This : in out Lexer_Context) is
- begin
- if This.Pass_Forward /= null then
- Free_Array (This.Pass_Forward);
- end if;
- end Finalize;
-
-
-
-
-
- procedure Finalize
- (This : in out Input_Container) is
- begin
- if This.Dealloc then
- Free_Array (This.Data);
- end if;
- end Finalize;
-
-
- function Pass_Input
- (Passed, Continuing : in Element_Array_Access)
- return Input_Container is
- begin
- if Passed = null then
- return This : Input_Container do
- This.Data := Continuing;
- This.Dealloc := False;
- end return;
- else
- return This : Input_Container do
- This.Data := new Element_Array (1 .. Passed'Length + Continuing'Length);
- This.Data (1 .. Passed'Length) := Passed.all;
- This.Data (Passed'Length + 1 .. This.Data'Last) := Continuing.all;
- This.Dealloc := True;
- end return;
- end if;
- end Pass_Input;
-
-
-
-
-
function Join
(Left, Right : in Combinator_Result)
return Combinator_Result is
@@ -130,9 +57,7 @@ package body Packrat.Lexer is
end if;
else
Context.Status := Current_Result.Status;
- Context.Pass_Forward := new Element_Array
- (1 .. Current_Result.Finish - Context.Position + 1);
- Context.Pass_Forward.all := Input (Context.Position .. Current_Result.Finish);
+ Context.Pass_Forward.Replace_Element (Input (Context.Position .. Current_Result.Finish));
Context.Empty_Labels.Clear;
end if;
@@ -173,9 +98,7 @@ package body Packrat.Lexer is
end if;
else
Context.Status := Current_Result.Status;
- Context.Pass_Forward := new Element_Array
- (1 .. Current_Result.Finish - Context.Position + 1);
- Context.Pass_Forward.all := Input (Context.Position .. Current_Result.Finish);
+ Context.Pass_Forward.Replace_Element (Input (Context.Position .. Current_Result.Finish));
Context.Empty_Labels.Clear;
end if;
@@ -191,10 +114,7 @@ package body Packrat.Lexer is
(Details : in out Lexer_Context;
Number_Comp : in Ada.Containers.Count_Type) is
begin
- if Details.Pass_Forward /= null then
- Free_Array (Details.Pass_Forward);
- Details.Pass_Forward := null;
- end if;
+ Details.Pass_Forward := Input_Holders.Empty_Holder;
Details.Empty_Labels.Clear;
Details.Error_Labels.Clear;
@@ -249,18 +169,6 @@ package body Packrat.Lexer is
end Token_Vector_To_Array;
- procedure Assign_New
- (Location : in out Element_Array_Access;
- Items : in Element_Array) is
- begin
- if Location /= null then
- Free_Array (Location);
- end if;
- Location := new Element_Array (1 .. Items'Last - Items'First + 1);
- Location.all := Items;
- end Assign_New;
-
-
@@ -269,18 +177,23 @@ package body Packrat.Lexer is
Context : in out Lexer_Context)
return Gen_Tokens.Token_Array
is
- Real_Input : Input_Container :=
- Pass_Input (Context.Pass_Forward, Input'Unrestricted_Access);
+ Real_Input : Input_Holders.Holder;
Raise_Error : Boolean;
begin
+ if not Context.Pass_Forward.Is_Empty then
+ Real_Input := Input_Holders.To_Holder (Context.Pass_Forward.Element & Input);
+ else
+ Real_Input := Input_Holders.To_Holder (Input);
+ end if;
+
Tidy_Context (Context, Components'Length);
Context.Result_So_Far.Clear;
- Context.Allow_Incomplete := not (Input = Empty_Array);
+ Context.Allow_Incomplete := Input'Length > 0;
- while Context.Status = Success and Context.Position <= Real_Input.Data'Length loop
+ while Context.Status = Success and Context.Position <= Real_Input.Constant_Reference.Element'Length loop
Raise_Error := True;
for C of Components loop
- if C (Real_Input.Data.all, Context) = Component_Success then
+ if C (Real_Input.Element, Context) = Component_Success then
Raise_Error := False;
exit;
end if;
@@ -298,18 +211,23 @@ package body Packrat.Lexer is
Context : in out Lexer_Context)
return Gen_Tokens.Token_Array
is
- Real_Input : Input_Container :=
- Pass_Input (Context.Pass_Forward, Input'Unrestricted_Access);
+ Real_Input : Input_Holders.Holder;
Raise_Error : Boolean;
begin
+ if not Context.Pass_Forward.Is_Empty then
+ Real_Input := Input_Holders.To_Holder (Context.Pass_Forward.Element & Input);
+ else
+ Real_Input := Input_Holders.To_Holder (Input);
+ end if;
+
Tidy_Context (Context, Components'Length);
Context.Result_So_Far.Clear;
Context.Allow_Incomplete := False;
- while Context.Status = Success and Context.Position <= Real_Input.Data'Length loop
+ while Context.Status = Success and Context.Position <= Real_Input.Constant_Reference.Element'Length loop
Raise_Error := True;
for C of Components loop
- if C (Real_Input.Data.all, Context) = Component_Success then
+ if C (Real_Input.Element, Context) = Component_Success then
Raise_Error := False;
exit;
end if;
@@ -327,35 +245,36 @@ package body Packrat.Lexer is
Context : in out Lexer_Context)
return Gen_Tokens.Token_Array
is
- Raise_Error : Boolean;
+ Real_Input : Input_Holders.Holder;
+ Empty_Input, Raise_Error : Boolean;
begin
Context.Result_So_Far.Clear;
loop
- declare
- New_Input : Element_Array := Input.all;
- Real_Input : Input_Container :=
- Pass_Input (Context.Pass_Forward, New_Input'Unrestricted_Access);
- begin
- Tidy_Context (Context, Components'Length);
- Context.Allow_Incomplete := not (New_Input = Empty_Array);
-
- while Context.Status = Success and Context.Position <= Real_Input.Data'Length loop
- Raise_Error := True;
- for C of Components loop
- if C (Real_Input.Data.all, Context) = Component_Success then
- Raise_Error := False;
- exit;
- end if;
- end loop;
- if Raise_Error then
- Raise_Lexer_Error (Context.Error_Labels, Context.Position);
+ Real_Input := Input_Holders.To_Holder (Input.all);
+ Empty_Input := Real_Input.Constant_Reference.Element'Length = 0;
+ if not Context.Pass_Forward.Is_Empty then
+ Real_Input := Input_Holders.To_Holder (Context.Pass_Forward.Element & Real_Input.Element);
+ end if;
+
+ Tidy_Context (Context, Components'Length);
+ Context.Allow_Incomplete := not Empty_Input;
+
+ while Context.Status = Success and Context.Position <= Real_Input.Constant_Reference.Element'Length loop
+ Raise_Error := True;
+ for C of Components loop
+ if C (Real_Input.Element, Context) = Component_Success then
+ Raise_Error := False;
+ exit;
end if;
end loop;
-
- if New_Input = Empty_Array then
- exit;
+ if Raise_Error then
+ Raise_Lexer_Error (Context.Error_Labels, Context.Position);
end if;
- end;
+ end loop;
+
+ if Empty_Input then
+ exit;
+ end if;
end loop;
return Token_Vector_To_Array (Context.Result_So_Far);
end Scan_With;
@@ -366,22 +285,27 @@ package body Packrat.Lexer is
Context : in out Lexer_Context;
Output : out Gen_Tokens.Token_Array)
is
- Real_Input : Input_Container :=
- Pass_Input (Context.Pass_Forward, Input'Unrestricted_Access);
+ Real_Input : Input_Holders.Holder;
Raise_Error : Boolean;
begin
+ if not Context.Pass_Forward.Is_Empty then
+ Real_Input := Input_Holders.To_Holder (Context.Pass_Forward.Element & Input);
+ else
+ Real_Input := Input_Holders.To_Holder (Input);
+ end if;
+
Tidy_Context (Context, Components'Length);
Context.Result_So_Far.Clear;
- Context.Allow_Incomplete := not (Input = Empty_Array or else Input (Input'First) = Pad_In);
+ Context.Allow_Incomplete := not (Input'Length = 0 or else Input (Input'First) = Pad_In);
while Context.Status = Success and then
Integer (Context.Result_So_Far.Length) < Output'Length and then
- Context.Position <= Real_Input.Data'Length and then
- Real_Input.Data (Context.Position) /= Pad_In
+ Context.Position <= Real_Input.Constant_Reference.Element'Length and then
+ Real_Input.Constant_Reference.Element (Context.Position) /= Pad_In
loop
Raise_Error := True;
for C of Components loop
- if C (Real_Input.Data.all, Context) = Component_Success then
+ if C (Real_Input.Element, Context) = Component_Success then
Raise_Error := False;
exit;
end if;
@@ -391,10 +315,11 @@ package body Packrat.Lexer is
end if;
end loop;
+ -- suspect this is wrong, test more
if Integer (Context.Result_So_Far.Length) >= Output'Length then
- Assign_New (Context.Pass_Forward,
- Real_Input.Data (Context.Position .. Real_Input.Data'Last));
+ Context.Pass_Forward.Replace_Element (Real_Input.Element (Context.Position .. Real_Input.Element'Last));
end if;
+
Token_Vector_To_Array (Context.Result_So_Far, Pad_Out, Output);
end Scan_Set;
@@ -404,51 +329,55 @@ package body Packrat.Lexer is
Context : in out Lexer_Context;
Output : out Gen_Tokens.Token_Array)
is
- Raise_Error : Boolean;
+ Real_Input : Input_Holders.Holder;
+ Empty_Input, Raise_Error : Boolean;
begin
Context.Result_So_Far.Clear;
loop
- declare
- New_Input : Element_Array := Input.all;
- Real_Input : Input_Container :=
- Pass_Input (Context.Pass_Forward, New_Input'Unrestricted_Access);
- begin
- Tidy_Context (Context, Components'Length);
- Context.Allow_Incomplete := not
- (New_Input = Empty_Array or else New_Input (New_Input'First) = Pad_In);
-
- while Context.Status = Success and then
- Integer (Context.Result_So_Far.Length) < Output'Length and then
- Context.Position <= Real_Input.Data'Length and then
- Real_Input.Data (Context.Position) /= Pad_In
- loop
- Raise_Error := True;
- for C of Components loop
- if C (Real_Input.Data.all, Context) = Component_Success then
- Raise_Error := False;
- exit;
- end if;
- end loop;
- if Raise_Error then
- Raise_Lexer_Error (Context.Error_Labels, Context.Position);
+ Real_Input := Input_Holders.To_Holder (Input.all);
+ Empty_Input := Real_Input.Constant_Reference.Element'Length = 0 or
+ Real_Input.Constant_Reference.Element (Real_Input.Constant_Reference.Element'First) = Pad_In;
+ if not Context.Pass_Forward.Is_Empty then
+ Real_Input.Replace_Element (Context.Pass_Forward.Element & Real_Input.Element);
+ end if;
+
+ Tidy_Context (Context, Components'Length);
+ Context.Allow_Incomplete := not Empty_Input;
+
+ while Context.Status = Success and then
+ Integer (Context.Result_So_Far.Length) < Output'Length and then
+ Context.Position <= Real_Input.Constant_Reference.Element'Length and then
+ Real_Input.Constant_Reference.Element (Context.Position) /= Pad_In
+ loop
+ Raise_Error := True;
+ for C of Components loop
+ if C (Real_Input.Element, Context) = Component_Success then
+ Raise_Error := False;
+ exit;
end if;
end loop;
-
- if New_Input = Empty_Array or else New_Input (New_Input'First) = Pad_In then
- exit;
+ if Raise_Error then
+ Raise_Lexer_Error (Context.Error_Labels, Context.Position);
end if;
+ end loop;
- if Integer (Context.Result_So_Far.Length) >= Output'Length then
- Assign_New (Context.Pass_Forward,
- Real_Input.Data (Context.Position .. Real_Input.Data'Last));
- exit;
- end if;
- end;
+ if Empty_Input then
+ exit;
+ end if;
+
+ -- suspect this is wrong, test more
+ if Integer (Context.Result_So_Far.Length) >= Output'Length then
+ Context.Pass_Forward.Replace_Element (Real_Input.Element (Context.Position .. Real_Input.Element'Last));
+ exit;
+ end if;
end loop;
Token_Vector_To_Array (Context.Result_So_Far, Pad_Out, Output);
end Scan_Set_With;
+ -- factor out the internal scan loop to an internal function/procedure here
+
+