From 5b94e65c29f141b40b4d0333a1de6968d1670b1e Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 16:20:10 +1300 Subject: Implement Sequence --- src/parcom.cr | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/parcom.cr') diff --git a/src/parcom.cr b/src/parcom.cr index 8d7c754..6fc6339 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -228,6 +228,23 @@ module Parcom end end + class Sequence(T, V) < Parser(T, Array(V)) + def initialize(@ps : Iterable(Parser(T, V))) + end + + def parse(tokens : TokenStream(T)) : Result(T, Array(V)) + parsed = [] of V + + @ps.each do |p| + r = p.parse(tokens) + parsed << r.value + tokens = r.tokens + end + + Result.new(tokens, parsed) + end + end + class Tokens(T) < Parser(T, Array(T)) def initialize(@expected : Iterable(T)) end -- cgit v1.2.1