diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-16 20:36:03 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-16 20:36:03 +1300 |
| commit | b274828831fec26cd8b3089ffef14cb96ce2de2f (patch) | |
| tree | ff5927b85a59b4d85c9e4aa269a475a7a37a54a0 /src/__OLD_parcom/many.cr | |
| parent | 77c370d27be174e0b036b33d1469e84e67a7153a (diff) | |
Second rewrite attempt, this one should work, monkaS
Diffstat (limited to 'src/__OLD_parcom/many.cr')
| -rw-r--r-- | src/__OLD_parcom/many.cr | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/__OLD_parcom/many.cr b/src/__OLD_parcom/many.cr new file mode 100644 index 0000000..a734c63 --- /dev/null +++ b/src/__OLD_parcom/many.cr @@ -0,0 +1,36 @@ +require "./parser.cr" + +module Parcom + # `Many` is a `Parser` that repeatedly tries to parse with another parser. + # The `Many` object will collect all success values in an array, and return + # them with the final state of the input stream. If the wrapped parser ever + # fails or succeeds without parsing any input, the `Many` object will stop + # attempting to parse will will return the array of prrevious successes. + # + # `Many` will return an empty array if the parser never succeeds or consumes + # input. For cases where at least one parserr success is needed, use `Some`. + class Many(T, V) < Parser(T, Array(V)) + # Accepts the parser to use. + def initialize(@p : Parser(T, V)) + end + + # Continuously parses with the wrapped parser, returns all successes. + # Parsing stops when the parser fails, or succeeds without consuming input. + def parse(tokens : Tokens(T)) : Result(T, Array(V)) + parsed = [] of V + + loop do + result = @p.parse?(tokens) + break unless !result.nil? && result.tokens != tokens + + parsed << result.value + tokens = result.tokens + end + + Result.new(tokens, parsed) + rescue ex : ParserFail + raise ParserFail.new("Many: #{ex.message}") + end + end +end + |
