From ca4ef7c14c8398b24db54c58bb4062607eb44067 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 00:33:49 +1300 Subject: Rewrite Phrase in terms of other parsers --- src/parcom.cr | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/parcom.cr b/src/parcom.cr index 9e90562..dbdd44d 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -176,17 +176,15 @@ module Parcom end class Phrase(T, V) - def initialize(@p : Parser(T, V)) + @p : Map(T, {V, Nil}, V) + + def initialize(p : Parser(T, V)) + f = ->(tup : {V, Nil}) { tup[0] } + @p = (p + Eof(T).new).map(f) end def parse(tokens : TokenStream(T)) : Result(T, V) - r = @p.parse(tokens) - - unless r.tokens.empty? - raise ParserException.new("Phrase: some of the input was not parsed") - end - - r + @p.parse(tokens) end end -- cgit v1.2.1