From 4fd257bb084d173fa07aa057e6d294ee525721e8 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sun, 2 Apr 2023 11:38:47 +1200 Subject: Refactor parser_chain argument order --- src/parcom.cr | 10 +++++----- src/parcom/parser.cr | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src') 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)) -- cgit v1.2.1