diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-09 16:20:10 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-09 16:20:10 +1300 |
| commit | 5b94e65c29f141b40b4d0333a1de6968d1670b1e (patch) | |
| tree | 9997696b6a9e346bce63848a30062c14efab6b81 /src | |
| parent | 15e3a96876ca9e8bd67c7408ebcc123c35a1e447 (diff) | |
Implement Sequence
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom.cr | 17 |
1 files changed, 17 insertions, 0 deletions
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 |
