diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-11 00:54:33 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-11 00:54:33 +1300 |
| commit | 6f93f16468d83b84ea386fcf74666a89e80bc704 (patch) | |
| tree | c532afb7b396a789720fd0b797a88e78d987f3a2 /src | |
| parent | 27fb4fa0babc2c3db41c93865a35430515ff3630 (diff) | |
Implement SepBy
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom.cr | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/parcom.cr b/src/parcom.cr index f0295a3..034c8e1 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -432,15 +432,6 @@ module Parcom end end - class StopAt - end - - class StopAfter - end - - class StopIf - end - class FirstOf(T, V) < Parser(T, V) @p : Parser(T, V) @@ -468,7 +459,20 @@ module Parcom end end - class SepBy + class SepBy(T, V, U) < Parser(T, Array(V)) + @p : Map(T, {V, Array(V)}, Array(V)) + + def initialize(elem : Parser(T, V), sep : Parser(T, U)) + @p = (elem + Many(T, U).new(sep >> elem)).map do |tup| + [tup[0]] + tup[1] + end + end + + def parse(tokens : TokenStream(T)) : Result(T, Array(V)) + @p.parse(tokens) + rescue ex : ParserException + raise ParserException.new("SepBy: #{ex.message}") + end end end |
