aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/parcom.cr23
-rw-r--r--src/parcom/many.cr28
2 files changed, 28 insertions, 23 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index 6715e07..c7dde7b 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -119,29 +119,6 @@ module Parcom
end
end
- class Many(T, V) < Parser(T, Array(V))
- def initialize(@p : Parser(T, V))
- end
-
- def parse(tokens : Tokens(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)
- rescue ex : ParserFail
- raise ParserFail.new("Many: #{ex.message}")
- end
- end
-
class Some(T, V) < Parser(T, Array(V))
@p : Assert(T, Array(V))
diff --git a/src/parcom/many.cr b/src/parcom/many.cr
new file mode 100644
index 0000000..dbe3b13
--- /dev/null
+++ b/src/parcom/many.cr
@@ -0,0 +1,28 @@
+require "./parser.cr"
+
+module Parcom
+ class Many(T, V) < Parser(T, Array(V))
+ def initialize(@p : Parser(T, V))
+ end
+
+ def parse(tokens : Tokens(T)) : Result(T, Array(V))
+ parsed = [] of V
+
+ loop do
+ result = @p.parse?(tokens)
+
+ if !result.nil? && result.tokens != tokens
+ parsed << result.value
+ tokens = result.tokens
+ else
+ break
+ end
+ end
+
+ Result.new(tokens, parsed)
+ rescue ex : ParserFail
+ raise ParserFail.new("Many: #{ex.message}")
+ end
+ end
+end
+