From 6e4c12589c58b41cf748b1913823df71cec7215f Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Mon, 13 Mar 2023 20:22:07 +1300 Subject: Documentation for Between --- src/parcom.cr | 18 ------------------ src/parcom/between.cr | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 src/parcom/between.cr diff --git a/src/parcom.cr b/src/parcom.cr index 24fb76c..0170fdc 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -81,24 +81,6 @@ module Parcom end end - class Between(T, V) < Parser(T, Array(V)) - @p : Map(T, {Array(V), Array(V)}, Array(V)) - - def initialize(i : Int, j : Int, p : Parser(T, V)) - lower = i < j ? i : j - upper = (i - j).abs - @p = (Exactly.new(lower, p) + AtMost.new(upper, p)).map do |tup| - tup[0] + tup[1] - end - end - - def parse(tokens : Tokens(T)) : Result(T, Array(V)) - @p.parse(tokens) - rescue ex : ParserFail - raise ParserFail.new("Between: #{ex.message}") - end - end - class FirstOf(T, V) < Parser(T, V) @p : Parser(T, V) diff --git a/src/parcom/between.cr b/src/parcom/between.cr new file mode 100644 index 0000000..05519e4 --- /dev/null +++ b/src/parcom/between.cr @@ -0,0 +1,37 @@ +require "./parser.cr" +require "./map.cr" +require "./exactly.cr" +require "./at_most.cr" + +module Parcom + # `Between` is a `Parser` that tries to parse with another parser a number + # of times within a specified range. The results of each successful parse + # are returned in an array. If the number of successful parses is out of + # the lower bound of the range, the parser fails. If the number of successful + # parses reaches thhe upper bound of the range, the parsing stops, even if it + # is possible to keep parsing. + class Between(T, V) < Parser(T, Array(V)) + @p : Map(T, {Array(V), Array(V)}, Array(V)) + + # Accepts the lower and upper bounds for the number of parsing attempts, + # as well as the parser to use. This method works the same way whether or + # not the larger value is passed first or second. If a negative int value + # is given for either parameter, it is treated as `0`. + # TODO: Overload to allow for Range objects. + def initialize(i : Int, j : Int, p : Parser(T, V)) + lower = i < j ? i : j + upper = (i - j).abs + @p = (Exactly.new(lower, p) + AtMost.new(upper, p)).map do |tup| + tup[0] + tup[1] + end + end + + # Tries to parse a numebr of times within the given range. + def parse(tokens : Tokens(T)) : Result(T, Array(V)) + @p.parse(tokens) + rescue ex : ParserFail + raise ParserFail.new("Between: #{ex.message}") + end + end +end + -- cgit v1.2.1