diff --git a/calc/calc.go b/calc/calc.go index 3c4ad4f..6dcd304 100644 --- a/calc/calc.go +++ b/calc/calc.go @@ -12,11 +12,11 @@ var ( sumOp = Chars("+-", 1, 1) prodOp = Chars("/*", 1, 1) - groupExpr = Map(Seq("(", sum, ")"), func(n Result) Result { + groupExpr = Seq("(", sum, ")").Map(func(n Result) Result { return Result{Result: n.Child[1].Result} }) - number = Map(NumberLit(), func(n Result) Result { + number = NumberLit().Map(func(n Result) Result { switch i := n.Result.(type) { case int64: return Result{Result: float64(i)} @@ -27,7 +27,7 @@ var ( } }) - sum = Map(Seq(prod, Some(Seq(sumOp, prod))), func(n Result) Result { + sum = Seq(prod, Some(Seq(sumOp, prod))).Map(func(n Result) Result { i := n.Child[0].Result.(float64) for _, op := range n.Child[1].Child { @@ -42,7 +42,7 @@ var ( return Result{Result: i} }) - prod = Map(Seq(&value, Some(Seq(prodOp, &value))), func(n Result) Result { + prod = Seq(&value, Some(Seq(prodOp, &value))).Map(func(n Result) Result { i := n.Child[0].Result.(float64) for _, op := range n.Child[1].Child { diff --git a/combinator_test.go b/combinator_test.go index b197105..50b7010 100644 --- a/combinator_test.go +++ b/combinator_test.go @@ -199,6 +199,12 @@ func TestMerge(t *testing.T) { }) } +func TestMapShorthand(t *testing.T) { + Chars("a-z").Map(func(n Result) Result { + return Result{Result: n.Token} + }) +} + func assertSequence(t *testing.T, node Result, expected ...string) { require.NotNil(t, node) actual := []string{} diff --git a/html/html.go b/html/html.go index 5fe9480..33e5cc4 100644 --- a/html/html.go +++ b/html/html.go @@ -18,12 +18,12 @@ var ( tag Parser identifier = Regex("[a-zA-Z][a-zA-Z0-9]*") - text = Map(NotChars("<>"), func(n Result) Result { + text = NotChars("<>").Map(func(n Result) Result { return Result{Result: n.Token} }) element = Any(text, &tag) - elements = Map(Some(element), func(n Result) Result { + elements = Some(element).Map(func(n Result) Result { ret := []interface{}{} for _, child := range n.Child { ret = append(ret, child.Result) @@ -32,7 +32,7 @@ var ( }) attr = Seq(identifier, "=", StringLit(`"'`)) - attrs = Map(Some(attr), func(node Result) Result { + attrs = Some(attr).Map(func(node Result) Result { attr := map[string]string{} for _, attrNode := range node.Child { @@ -47,7 +47,7 @@ var ( ) func init() { - tag = Map(Seq(tstart, Cut(), elements, tend), func(node Result) Result { + tag = Seq(tstart, Cut(), elements, tend).Map(func(node Result) Result { openTag := node.Child[0] return Result{Result: htmlTag{ Name: openTag.Child[1].Token, diff --git a/json/json.go b/json/json.go index 172099c..7cc53a1 100644 --- a/json/json.go +++ b/json/json.go @@ -13,7 +13,7 @@ var ( _number = NumberLit() _properties = Some(Seq(StringLit(`"`), ":", &_value), ",") - _array = Map(Seq("[", Cut(), Some(&_value, ","), "]"), func(n Result) Result { + _array = Seq("[", Cut(), Some(&_value, ","), "]").Map(func(n Result) Result { ret := []interface{}{} for _, child := range n.Child[2].Child { ret = append(ret, child.Result) @@ -21,7 +21,7 @@ var ( return Result{Result: ret} }) - _object = Map(Seq("{", Cut(), _properties, "}"), func(n Result) Result { + _object = Seq("{", Cut(), _properties, "}").Map(func(n Result) Result { ret := map[string]interface{}{} for _, prop := range n.Child[2].Child { diff --git a/parser.go b/parser.go index 9bbe56a..87705ad 100644 --- a/parser.go +++ b/parser.go @@ -25,6 +25,11 @@ type Result struct { // - A parser that consumed some input should advance state.Pos type Parser func(*State) Result +// Map shorthand for Map(p, func()) +func (p Parser) Map(f func(n Result) Result) Parser { + return Map(p, f) +} + // VoidParser is a special type of parser that never returns anything but can still consume input type VoidParser func(*State) diff --git a/readme.md b/readme.md index 99745bf..e285701 100644 --- a/readme.md +++ b/readme.md @@ -123,7 +123,7 @@ func TestNumbers(t *testing.T) { Then define a parser for numbers ```go -var number = Map(NumberLit(), func(n Result) Result { +var number = NumberLit().Map(func(n Result) Result { switch i := n.Result.(type) { case int64: return Result{Result: float64(i)} @@ -161,7 +161,7 @@ func TestAddition(t *testing.T) { var sumOp = Chars("+-", 1, 1) -sum = Map(Seq(number, Some(And(sumOp, number))), func(n Result) Result { +sum = Seq(number, Some(And(sumOp, number))).Map(func(n Result) Result { i := n.Child[0].Result.(float64) for _, op := range n.Child[1].Child {