aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-04-02 11:38:47 +1200
committerMatthew Hall <hallmatthew314@gmail.com>2023-04-02 11:38:47 +1200
commit4fd257bb084d173fa07aa057e6d294ee525721e8 (patch)
treedac30ab25ec823ec727e6db6d1ecbc411185bac4
parent42e04f13d0968ff8d40d7e7ad3fd51c4e70eeed0 (diff)
Refactor parser_chain argument order
-rw-r--r--spec/parser_chain_spec.cr8
-rw-r--r--spec/practical/json_spec.cr2
-rw-r--r--src/parcom.cr10
-rw-r--r--src/parcom/parser.cr4
4 files changed, 12 insertions, 12 deletions
diff --git a/spec/parser_chain_spec.cr b/spec/parser_chain_spec.cr
index 3f1e3a0..05156ac 100644
--- a/spec/parser_chain_spec.cr
+++ b/spec/parser_chain_spec.cr
@@ -4,7 +4,7 @@ include Parcom
describe "parser_chain" do
it "works with zero intermediate steps" do
- p = parser_chain "test", Char, Char, finally: Parser(Char, Char).pure('x')
+ p = parser_chain Char, Char, "test", finally: Parser(Char, Char).pure('x')
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
@@ -13,7 +13,7 @@ describe "parser_chain" do
end
it "works with one intermediate step" do
- p = parser_chain "test", Char, Char,
+ p = parser_chain Char, Char, "test",
{c, Parser(Char, Char).any_token},
finally: Parser(Char, Char).pure(c)
tokens = Tokens.from_string("abcd")
@@ -25,7 +25,7 @@ describe "parser_chain" do
it "works with many intermediate steps" do
digit = Parser(Char, Char).satisfy(&.number?)
- p = parser_chain "float", Char, Float64,
+ p = parser_chain Char, Float64, "float",
{sign, Parser.token('-').map_const(-1).recover(1)},
{front, digit.many},
{point, Parser.token('.').optional},
@@ -61,7 +61,7 @@ describe "parser_chain" do
it "allows ignoring results with underscores" do
any_word = Parser(Char, String).satisfy(&.letter?).some.map(&.join)
ws = Parser(Char, Array(Char)).satisfy(&.whitespace?).many
- two_words = parser_chain "two words", Char, {String, String},
+ two_words = parser_chain Char, {String, String}, "two_words",
{word, any_word},
{_, ws},
finally: Parser.token_sequence(word.chars).map_const({word, word})
diff --git a/spec/practical/json_spec.cr b/spec/practical/json_spec.cr
index 9d47b93..a0ce2db 100644
--- a/spec/practical/json_spec.cr
+++ b/spec/practical/json_spec.cr
@@ -42,7 +42,7 @@ describe "example: JSON parsing", tags: "example" do
sign = Parser.token('-').map_const(-1).recover(1)
point = Parser.token('.').optional
- json_number = parser_chain "json_number", Char, JSONValue,
+ json_number = parser_chain Char, JSONValue, "json_number",
{s, sign},
{front, digits},
{p, point},
diff --git a/src/parcom.cr b/src/parcom.cr
index b75cb0f..0963184 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -9,9 +9,9 @@ module Parcom
end
# Provides a more convenient syntax for combining parsers via `Parser#and_then`.
- # The first argument is a string literal used for the name of the parser.
- # The second and third arguments are types used for the parser's type.
- # These are followed by any number of 2-tuples containing a variable name and
+ # The first and second arguments are types used for the parser's type.
+ # The thirs argument is a string literal used for the name of the parser.
+ # This is followed by any number of 2-tuples containing a variable name and
# an expression resolving to a `Parser(t.Class, _)`, whose success value will
# be stored in the aformentioned variable. The `finally` named argument is an
# expression that resolves to a `Parser(t.class, u.class)`.
@@ -20,7 +20,7 @@ module Parcom
# ```
# any_word = Parser(Char, Char).satisfy(&.letter?).some.map(&.join)
# ws = Parser(Char, Array(Char)).satisfy(&.whitespace?).many
- # two_of_same_word = parser_chain "two words", Char, String,
+ # two_of_same_word = parser_chain Char, String, "two words",
# {word, any_word},
# {_, ws},
# finally: Parser.token_sequence(word.chars).map(&.join)
@@ -39,7 +39,7 @@ module Parcom
# ```
#
# This macro is based on Haskell's do-notation.
- macro parser_chain(name, t, u, *steps, finally)
+ macro parser_chain(t, u, name, *steps, finally)
Parser({{t}}, {{u}}).new({{name}}) do |tokens|
{% for tup, index in steps %}
{{tup.last}}.and_then do |{{tup.first}}|
diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr
index 3fe4746..efa9780 100644
--- a/src/parcom/parser.cr
+++ b/src/parcom/parser.cr
@@ -252,7 +252,7 @@ module Parcom
# try to parse with both parsers and return both results. If either
# sub-parser fails, the whole parser fails.
def +(other : Parser(T, V)) : Parser(T, {U, V}) forall V
- parser_chain "#{@name} + #{other.name}", T, {U, V},
+ parser_chain T, {U, V}, "#{@name} + #{other.name}",
{x, self},
{y, other},
finally: Parser(T, {U, V}).pure({x, y})
@@ -437,7 +437,7 @@ 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))
- parser_chain "<#{@name}> sep by <#{sep.name}>", T, Array(U),
+ parser_chain T, Array(U), "<#{@name}> sep by <#{sep.name}>",
{head, self},
{tail, (sep >> self).many},
finally: Parser(T, Array(U)).pure(tail.unshift(head))