diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-26 15:42:56 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-26 15:42:56 +1300 |
| commit | fc1afdcb72d786836479367ef42d7d82069aaf97 (patch) | |
| tree | aa442410a45e31bb23c1b88d88bdb0c969f3dab1 | |
| parent | f156d80cc3c80385456bc3ec80adb5ad892f5039 (diff) | |
More refactoring
| -rw-r--r-- | spec/practical/bf_spec.cr | 8 | ||||
| -rw-r--r-- | spec/practical/json_spec.cr | 8 | ||||
| -rw-r--r-- | src/parcom/parser.cr | 4 |
3 files changed, 8 insertions, 12 deletions
diff --git a/spec/practical/bf_spec.cr b/spec/practical/bf_spec.cr index d1ad488..1eb1d04 100644 --- a/spec/practical/bf_spec.cr +++ b/spec/practical/bf_spec.cr @@ -105,13 +105,9 @@ describe "brainfuck parser" do char_body = bf_char.some.sep_by(other.some).map(&.flatten) just_bf_chars = other.many >> char_body << other.many - loop_start = Parser(Char, Char).token('[').map do |_| - {type: BFOpType::LoopStart, amount: 0} - end + loop_start = Parser(Char, Char).token('[').map_const({type: BFOpType::LoopStart, amount: 0}) - loop_end = Parser(Char, Char).token(']').map do |_| - {type: BFOpType::LoopEnd, amount: 0} - end + loop_end = Parser(Char, Char).token(']').map_const({type: BFOpType::LoopEnd, amount: 0}) read_block = Parser(Char, Char).token(',').some.map do |cs| {type: BFOpType::ByteIn, amount: cs.size} diff --git a/spec/practical/json_spec.cr b/spec/practical/json_spec.cr index ed0fcfb..95795e2 100644 --- a/spec/practical/json_spec.cr +++ b/spec/practical/json_spec.cr @@ -20,12 +20,12 @@ struct JSONValue end def json_null - Parser.token_sequence("null".chars).map { |_| JSONValue.new(nil) } + Parser.token_sequence("null".chars).map_const(JSONValue.new(nil)) end def json_bool - t = Parser.token_sequence("true".chars).map { |_| true } - f = Parser.token_sequence("false".chars).map { |_| false } + t = Parser.token_sequence("true".chars).map_const(true) + f = Parser.token_sequence("false".chars).map_const(false) (t | f).map { |b| JSONValue.new(b) } end @@ -34,7 +34,7 @@ def json_number base_num = Parser(Char, Char).satisfy(&.ascii_number?).some.map do |cs| cs.join.to_i64 end - sign = Parser.token('-').map { |_| -1 }.recover(1) + sign = Parser.token('-').map_const(-1).recover(1) (sign + base_num).map { |s, n| JSONValue.new(s.to_i64 * n) } end diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr index 57c7b44..af486c4 100644 --- a/src/parcom/parser.cr +++ b/src/parcom/parser.cr @@ -456,8 +456,8 @@ module Parcom # instance of `self`. It will succeed if it can parse an instance of `self` # that is not followed by any `sep` instances. def sep_by(sep : Parser(T, _)) : Parser(T, Array(U)) - (self + (sep >> self).many).map do |tup| - tup.last.unshift(tup.first) + (self + (sep >> self).many).map do |head, tail| + tail.unshift(head) end.named("<#{@name}> sep by <#{sep.name}>") end end |
