aboutsummaryrefslogtreecommitdiff
path: root/src/parcom.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-09 20:48:34 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-09 20:48:34 +1300
commit9dc06838e3c78e4a77731cab6bc773846eafce99 (patch)
tree2fa1b675b5a384d6ae88e0327aa9240f5cf3cea7 /src/parcom.cr
parent1a9792afb92e227adcb564a051b678faa9fe2036 (diff)
Implement AtLeast and AtMost
Diffstat (limited to 'src/parcom.cr')
-rw-r--r--src/parcom.cr24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index c7f4012..0a97e51 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -308,10 +308,30 @@ module Parcom
end
end
- class AtLeast
+ class AtLeast(T, V) < Parser(T, Array(V))
+ @p : Map(T, {Array(V), Array(V)}, Array(V))
+
+ def initialize(i : Int, p : Parser(T, V))
+ @p = (Exactly.new(i, p) + Many.new(p)).map do |tup|
+ tup[0] + tup[1]
+ end
+ end
+
+ def parse(tokens : TokenStream(T)) : Result(T, Array(V))
+ @p.parse(tokens)
+ end
end
- class AtMost
+ class AtMost(T, V) < Parser(T, Array(V))
+ @p : Map(T, Array(V?), Array(V))
+
+ def initialize(i : Int, p : Parser(T, V))
+ @p = Exactly.new(i, Optional.new(p)).map(&.compact)
+ end
+
+ def parse(tokens : TokenStream(T)) : Result(T, Array(V))
+ @p.parse(tokens)
+ end
end
class Between