summaryrefslogtreecommitdiff
path: root/src/packrat-parsers.adb
diff options
context:
space:
mode:
Diffstat (limited to 'src/packrat-parsers.adb')
-rw-r--r--src/packrat-parsers.adb82
1 files changed, 39 insertions, 43 deletions
diff --git a/src/packrat-parsers.adb b/src/packrat-parsers.adb
index 946bfdd..85bfbc9 100644
--- a/src/packrat-parsers.adb
+++ b/src/packrat-parsers.adb
@@ -328,40 +328,6 @@ package body Packrat.Parsers is
- function Root
- (Input : in Traits.Element_Array;
- Context : in out Parser_Context;
- Start : in Positive)
- return Component_Result
- is
- Salt : Combinator_Result := Combo (Input, Context, Start);
- Index : Positive := 1;
- Root_Elems : Graphs.Finished_Token_Array (1 .. Integer (Salt.Results.Length));
- begin
- if Salt.Status = Failure then
- raise Parser_Error with Packrat.Errors.Encode
- (Traits.Label_Enum'Image (Label), Start);
- elsif Salt.Status /= Success then
- return (Status => Salt.Status);
- end if;
- for R of Salt.Results loop
- Root_Elems (Index) :=
- (Token => Traits.Tokens.Create (Label, Start, R.Value.Element),
- Finish => R.Finish);
- if R.Tokens.Element'Length > 0 then
- Context.Result_So_Far.Connect (Root_Elems (Index), R.Tokens.Element);
- else
- Context.Result_So_Far.Include (Root_Elems (Index).Token);
- end if;
- end loop;
- Context.Result_So_Far.Set_Root (Root_Elems);
- return (Status => Success);
- end Root;
-
-
-
-
-
procedure Tidy_Context
(Input : in Traits.Element_Array;
Context : in out Parser_Context)
@@ -391,6 +357,32 @@ package body Packrat.Parsers is
end Tidy_Context;
+ function Finish_Root
+ (Root_Result : in Combinator_Result;
+ Context : in out Parser_Context;
+ Label : in Traits.Label_Enum)
+ return Graphs.Parse_Graph
+ is
+ Root_Elems : Graphs.Finished_Token_Array (1 .. Integer (Root_Result.Results.Length));
+ Index : Positive := 1;
+ begin
+ for R of Root_Result.Results loop
+ Root_Elems (Index) :=
+ (Token => Traits.Tokens.Create (Label, Context.Global_Start, R.Value.Element),
+ Finish => R.Finish);
+ if R.Tokens.Element'Length > 0 then
+ Context.Result_So_Far.Connect (Root_Elems (Index), R.Tokens.Element);
+ else
+ Context.Result_So_Far.Include (Root_Elems (Index).Token);
+ end if;
+ Index := Index + 1;
+ end loop;
+ Context.Result_So_Far.Set_Root (Root_Elems);
+ Context.Result_So_Far.Delete_Unreachable;
+ return Context.Result_So_Far;
+ end Finish_Root;
+
+
procedure Parse
(Input : in Traits.Element_Array;
Context : in out Parser_Context;
@@ -404,13 +396,15 @@ package body Packrat.Parsers is
(if Context.Pass_Forward.Is_Empty
then Slide (Input, Context.Current_Position)
else Context.Pass_Forward.Element & Input);
+ Root_Result : Combinator_Result :=
+ Root (Real_Input, Context, Context.Global_Start);
begin
- if Root_Component (Real_Input, Context, Context.Global_Start).Status = Failure then
- raise Parser_Error;
+ if Root_Result.Status = Failure then
+ raise Parser_Error with Packrat.Errors.Encode
+ (Traits.Label_Enum'Image (Label), Context.Global_Start);
end if;
if Input'Length = 0 then
- Result := Context.Result_So_Far;
- Result.Delete_Unreachable;
+ Result := Finish_Root (Root_Result, Context, Label);
return;
end if;
if not Context.Needs_More.Is_Empty then
@@ -438,12 +432,14 @@ package body Packrat.Parsers is
(if Context.Pass_Forward.Is_Empty
then Slide (Input, Context.Current_Position)
else Context.Pass_Forward.Element & Input);
+ Root_Result : Combinator_Result :=
+ Root (Real_Input, Context, Context.Global_Start);
begin
- if Root_Component (Real_Input, Context, Context.Global_Start).Status /= Success then
- raise Parser_Error;
+ if Root_Result.Status /= Success then
+ raise Parser_Error with Packrat.Errors.Encode
+ (Traits.Label_Enum'Image (Label), Context.Global_Start);
end if;
- Context.Result_So_Far.Delete_Unreachable;
- return Context.Result_So_Far;
+ return Finish_Root (Root_Result, Context, Label);
end;
end Parse_Only;
@@ -453,7 +449,7 @@ package body Packrat.Parsers is
Context : in out Parser_Context)
return Graphs.Parse_Graph
is
- procedure My_Parse is new Parse (Root_Component);
+ procedure My_Parse is new Parse (Label, Root);
Result : Graphs.Parse_Graph;
begin
loop