From 1a9792afb92e227adcb564a051b678faa9fe2036 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 16:52:30 +1300 Subject: Implement Exactly + extra test for Sequence --- src/parcom.cr | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/parcom.cr') diff --git a/src/parcom.cr b/src/parcom.cr index d7e6682..c7f4012 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -295,7 +295,17 @@ module Parcom end end - class Exactly + class Exactly(T, V) < Parser(T, Array(V)) + @p : Sequence(T, V) + + def initialize(i : Int, p : Parser(T, V)) + i = i.negative? ? 0 : i + @p = Sequence.new(([p] of Parser(T, V)) * i) + end + + def parse(tokens : TokenStream(T)) : Result(T, Array(V)) + @p.parse(tokens) + end end class AtLeast -- cgit v1.2.1