diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-09 00:33:49 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-09 00:33:49 +1300 |
| commit | ca4ef7c14c8398b24db54c58bb4062607eb44067 (patch) | |
| tree | 58e25606cf990be7fcef8c3473ab9d1948366d2c | |
| parent | 3e970f7aa51c69d7e659392120722ebc1611b4d6 (diff) | |
Rewrite Phrase in terms of other parsers
| -rw-r--r-- | src/parcom.cr | 14 |
1 files changed, 6 insertions, 8 deletions
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 |
