From 6f93f16468d83b84ea386fcf74666a89e80bc704 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sat, 11 Mar 2023 00:54:33 +1300 Subject: Implement SepBy --- src/parcom.cr | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/parcom.cr') 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 -- cgit v1.2.1