diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom.cr | 15 | ||||
| -rw-r--r-- | src/parcom/exactly.cr | 28 |
2 files changed, 28 insertions, 15 deletions
diff --git a/src/parcom.cr b/src/parcom.cr index 4bfed1c..2e2d417 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -81,21 +81,6 @@ module Parcom end end - 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 : Tokens(T)) : Result(T, Array(V)) - @p.parse(tokens) - rescue ex : ParserFail - raise ParserFail.new("Exactly: #{ex.message}") - end - end - class AtLeast(T, V) < Parser(T, Array(V)) @p : Map(T, {Array(V), Array(V)}, Array(V)) diff --git a/src/parcom/exactly.cr b/src/parcom/exactly.cr new file mode 100644 index 0000000..a34fd0d --- /dev/null +++ b/src/parcom/exactly.cr @@ -0,0 +1,28 @@ +require "./parser.cr" + +module Parcom + # `Exactly` is a `Parser` that tries to parse with another parser + # a specific number of times. The results of each successful parse + # are returned in an array. If the number of successes is less than + # OR greater than the specified number, the parser fails. + class Exactly(T, V) < Parser(T, Array(V)) + @p : Sequence(T, V) + + # Accepts the number of parsing attempts, and the parser to use. + # If a negative int is given, it is treated as if it were `0`. + def initialize(i : Int, p : Parser(T, V)) + i = i.negative? ? 0 : i + # put `i` copies of `p` into an array + ps = ([p] of Parser(T, V)) * i + @p = Sequence.new(ps) + end + + # Tries to parse the given number of times. + def parse(tokens : Tokens(T)) : Result(T, Array(V)) + @p.parse(tokens) + rescue ex : ParserFail + raise ParserFail.new("Exactly: #{ex.message}") + end + end +end + |
