From 3fa431a2268a11dec6fd82c8f9b59cc2d0af9a93 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 13 Jan 2021 18:31:27 +1100 Subject: Stamp/Ignore now Stamp/Discard/Ignore --- src/packrat-lexers.adb | 4 ++-- src/packrat-lexers.ads | 2 +- src/packrat-parsers.adb | 40 ++++++++++++++++++++++++++++++++++++++++ src/packrat-parsers.ads | 13 +++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/packrat-lexers.adb b/src/packrat-lexers.adb index 3a51e1a..bf5d109 100644 --- a/src/packrat-lexers.adb +++ b/src/packrat-lexers.adb @@ -363,7 +363,7 @@ package body Packrat.Lexers is end Stamp; - function Ignore + function Discard (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result @@ -402,7 +402,7 @@ package body Packrat.Lexers is Context.Error_Labels.Clear; return Component_Success; - end Ignore; + end Discard; diff --git a/src/packrat-lexers.ads b/src/packrat-lexers.ads index 9fd61e2..ac0ba2b 100644 --- a/src/packrat-lexers.ads +++ b/src/packrat-lexers.ads @@ -143,7 +143,7 @@ package Packrat.Lexers is (Input : in Traits.Element_Array; Start : in Positive) return Combinator_Result; - function Ignore + function Discard (Input : in Traits.Element_Array; Context : in out Lexer_Context) return Component_Result; diff --git a/src/packrat-parsers.adb b/src/packrat-parsers.adb index e4fa97d..0ddd864 100644 --- a/src/packrat-parsers.adb +++ b/src/packrat-parsers.adb @@ -622,6 +622,46 @@ package body Packrat.Parsers is end Stamp; + function Discard + (Input : in Traits.Element_Array; + Context : in out Parser_Context; + Start : in Positive) + return Combinator_Result + is + function Actual + (Context : in out Parser_Context) + return Combinator_Result + is + Salt : Combinator_Result := Combo (Input, Context, Start); + Processed : Result_Sets.Set; + begin + if Salt.Status = Failure then + declare + Error : String := Packrat.Errors.Encode + (Traits.Label_Enum'Image (Label), Start); + begin + if Ada.Strings.Unbounded.Index (Context.Error_String, Error) = 0 then + Ada.Strings.Unbounded.Append (Context.Error_String, Error); + end if; + end; + return Salt; + end if; + for R of Salt.Results loop + Processed.Include + ((Finish => R.Finish, + Value => Elem_Holds.Empty_Holder, + Tokens => Tok_Holds.Empty_Holder)); + end loop; + Salt.Results := Processed; + return Salt; + end Actual; + function Memo is new Memoize (To_Key (Start, Discard'Access), Actual); + function Curt is new Curtailment (To_Key (Start, Discard'Access), Input, Memo); + begin + return Curt (Context); + end Discard; + + function Ignore (Input : in Traits.Element_Array; Context : in out Parser_Context; diff --git a/src/packrat-parsers.ads b/src/packrat-parsers.ads index e11d623..1e058dc 100644 --- a/src/packrat-parsers.ads +++ b/src/packrat-parsers.ads @@ -115,6 +115,19 @@ package Packrat.Parsers is Start : in Positive) return Combinator_Result; + generic + Label : in Traits.Label_Enum; + with function Combo + (Input : in Traits.Element_Array; + Context : in out Parser_Context; + Start : in Positive) + return Combinator_Result; + function Discard + (Input : in Traits.Element_Array; + Context : in out Parser_Context; + Start : in Positive) + return Combinator_Result; + generic with function Combo (Input : in Traits.Element_Array; -- cgit