From fdfd7f9b0bd01d671cd1fc14aea7e33092ed72f8 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 3 Dec 2020 19:41:56 +1100 Subject: Removed component/combinator distinction in parsers --- src/packrat-parsers.adb | 82 +++++++++++++++++++++++-------------------------- src/packrat-parsers.ads | 42 ++++++++----------------- 2 files changed, 52 insertions(+), 72 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 diff --git a/src/packrat-parsers.ads b/src/packrat-parsers.ads index 6568238..471d667 100644 --- a/src/packrat-parsers.ads +++ b/src/packrat-parsers.ads @@ -63,17 +63,6 @@ package Packrat.Parsers is - type Component_Result is private; - - type Component is access function - (Input : in Traits.Element_Array; - Context : in out Parser_Context; - Start : in Positive) - return Component_Result; - - - - type With_Input is access function return Traits.Element_Array; @@ -82,36 +71,35 @@ package Packrat.Parsers is generic Label : in Traits.Label_Enum; - with function Combo + with function Root (Input : in Traits.Element_Array; Context : in out Parser_Context; Start : in Positive) return Combinator_Result; - function Root - (Input : in Traits.Element_Array; - Context : in out Parser_Context; - Start : in Positive) - return Component_Result; - - - - - generic - Root_Component : in Component; procedure Parse (Input : in Traits.Element_Array; Context : in out Parser_Context; Result : out Graphs.Parse_Graph); generic - Root_Component : in Component; + Label : in Traits.Label_Enum; + with function Root + (Input : in Traits.Element_Array; + Context : in out Parser_Context; + Start : in Positive) + return Combinator_Result; function Parse_Only (Input : in Traits.Element_Array; Context : in out Parser_Context) return Graphs.Parse_Graph; generic - Root_Component : in Component; + Label : in Traits.Label_Enum; + with function Root + (Input : in Traits.Element_Array; + Context : in out Parser_Context; + Start : in Positive) + return Combinator_Result; function Parse_With (Input : in With_Input; Context : in out Parser_Context) @@ -429,10 +417,6 @@ private Status : Result_Status := Failure; end record; - type Component_Result is record - Status : Result_Status; - end record; - Empty_Fail : constant Combinator_Result := (Results => Result_Sets.Empty_Set, Curtails => Curtail_Maps.Empty_Map, -- cgit