Add map shorthand

This commit is contained in:
Adam Scarr 2017-08-13 12:56:46 +10:00
parent f5b81e8e2e
commit 77930f8f06
6 changed files with 23 additions and 12 deletions

View File

@ -12,11 +12,11 @@ var (
sumOp = Chars("+-", 1, 1) sumOp = Chars("+-", 1, 1)
prodOp = 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} 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) { switch i := n.Result.(type) {
case int64: case int64:
return Result{Result: float64(i)} 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) i := n.Child[0].Result.(float64)
for _, op := range n.Child[1].Child { for _, op := range n.Child[1].Child {
@ -42,7 +42,7 @@ var (
return Result{Result: i} 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) i := n.Child[0].Result.(float64)
for _, op := range n.Child[1].Child { for _, op := range n.Child[1].Child {

View File

@ -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) { func assertSequence(t *testing.T, node Result, expected ...string) {
require.NotNil(t, node) require.NotNil(t, node)
actual := []string{} actual := []string{}

View File

@ -18,12 +18,12 @@ var (
tag Parser tag Parser
identifier = Regex("[a-zA-Z][a-zA-Z0-9]*") 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} return Result{Result: n.Token}
}) })
element = Any(text, &tag) element = Any(text, &tag)
elements = Map(Some(element), func(n Result) Result { elements = Some(element).Map(func(n Result) Result {
ret := []interface{}{} ret := []interface{}{}
for _, child := range n.Child { for _, child := range n.Child {
ret = append(ret, child.Result) ret = append(ret, child.Result)
@ -32,7 +32,7 @@ var (
}) })
attr = Seq(identifier, "=", StringLit(`"'`)) attr = Seq(identifier, "=", StringLit(`"'`))
attrs = Map(Some(attr), func(node Result) Result { attrs = Some(attr).Map(func(node Result) Result {
attr := map[string]string{} attr := map[string]string{}
for _, attrNode := range node.Child { for _, attrNode := range node.Child {
@ -47,7 +47,7 @@ var (
) )
func init() { 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] openTag := node.Child[0]
return Result{Result: htmlTag{ return Result{Result: htmlTag{
Name: openTag.Child[1].Token, Name: openTag.Child[1].Token,

View File

@ -13,7 +13,7 @@ var (
_number = NumberLit() _number = NumberLit()
_properties = Some(Seq(StringLit(`"`), ":", &_value), ",") _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{}{} ret := []interface{}{}
for _, child := range n.Child[2].Child { for _, child := range n.Child[2].Child {
ret = append(ret, child.Result) ret = append(ret, child.Result)
@ -21,7 +21,7 @@ var (
return Result{Result: ret} 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{}{} ret := map[string]interface{}{}
for _, prop := range n.Child[2].Child { for _, prop := range n.Child[2].Child {

View File

@ -25,6 +25,11 @@ type Result struct {
// - A parser that consumed some input should advance state.Pos // - A parser that consumed some input should advance state.Pos
type Parser func(*State) Result 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 // VoidParser is a special type of parser that never returns anything but can still consume input
type VoidParser func(*State) type VoidParser func(*State)

View File

@ -123,7 +123,7 @@ func TestNumbers(t *testing.T) {
Then define a parser for numbers Then define a parser for numbers
```go ```go
var number = Map(NumberLit(), func(n Result) Result { var number = NumberLit().Map(func(n Result) Result {
switch i := n.Result.(type) { switch i := n.Result.(type) {
case int64: case int64:
return Result{Result: float64(i)} return Result{Result: float64(i)}
@ -161,7 +161,7 @@ func TestAddition(t *testing.T) {
var sumOp = Chars("+-", 1, 1) 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) i := n.Child[0].Result.(float64)
for _, op := range n.Child[1].Child { for _, op := range n.Child[1].Child {