aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-09 16:30:59 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-09 16:30:59 +1300
commit61f7421c2feb36b529a51589967efaa11e6a1814 (patch)
tree59bac2e822fa3972dd83a13752f1e9a45d6b362d
parent5b94e65c29f141b40b4d0333a1de6968d1670b1e (diff)
Reimplement Tokens in terms of Sequence
-rw-r--r--src/parcom.cr21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index 6fc6339..d7e6682 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -232,6 +232,8 @@ module Parcom
def initialize(@ps : Iterable(Parser(T, V)))
end
+ # TODO: this can probably be optimised more for Arrays
+ # TODO: might be better to use #zip
def parse(tokens : TokenStream(T)) : Result(T, Array(V))
parsed = [] of V
@@ -246,20 +248,17 @@ module Parcom
end
class Tokens(T) < Parser(T, Array(T))
- def initialize(@expected : Iterable(T))
- end
+ @p : Sequence(T, T)
- # TODO: this can probably be optimised more for Arrays
- # TODO: might be better to use #zip?
- def parse(tokens : TokenStream(T)) : Result(T, Array(T))
- parsed_tokens = [] of T
+ def initialize(expected : Iterable(T))
+ ps = [] of Parser(T, T)
+ expected.each { |t| ps << Token.new(t) }
- @expected.each_with_index do |t, i|
- r = Token.new(t).parse(tokens[i..])
- parsed_tokens << r.value
- end
+ @p = Sequence.new(ps)
+ end
- Result.new(tokens[parsed_tokens.size..], parsed_tokens)
+ def parse(tokens : TokenStream(T)) : Result(T, Array(T))
+ @p.parse(tokens)
end
end