aboutsummaryrefslogtreecommitdiff
path: root/src/parcom.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-09 15:39:01 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-09 15:39:01 +1300
commit15e3a96876ca9e8bd67c7408ebcc123c35a1e447 (patch)
tree77e299346e3198eaf4494742d142f8b3e47eb9d7 /src/parcom.cr
parentf5e87b41ffcb036c705c32b55161ae91a04c72e1 (diff)
Implement Many and Some
Diffstat (limited to 'src/parcom.cr')
-rw-r--r--src/parcom.cr31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index 760a450..8d7c754 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -246,10 +246,37 @@ module Parcom
end
end
- class Many
+ class Many(T, V) < Parser(T, Array(V))
+ def initialize(@p : Parser(T, V))
+ end
+
+ def parse(tokens : TokenStream(T)) : Result(T, Array(V))
+ parsed = [] of V
+
+ loop do
+ result = @p.parse?(tokens)
+ if result.nil?
+ break
+ else
+ parsed << result.value
+ tokens = result.tokens
+ end
+ end
+
+ Result.new(tokens, parsed)
+ end
end
- class Some
+ class Some(T, V) < Parser(T, Array(V))
+ @p : Assert(T, Array(V))
+
+ def initialize(p : Parser(T, V))
+ @p = Many.new(p).assert { |arr| !arr.empty? }
+ end
+
+ def parse(tokens : TokenStream(T)) : Result(T, Array(V))
+ @p.parse(tokens)
+ end
end
class Exactly