Add Bind for constants

This commit is contained in:
Adam Scarr 2017-08-07 22:57:06 +10:00
parent 4594587e3b
commit 6e1c004fea
2 changed files with 17 additions and 16 deletions

View File

@ -123,6 +123,19 @@ func Maybe(parser Parserish) Parser {
}) })
} }
func Bind(parser Parserish, val interface{}) Parser {
p := Parsify(parser)
return func(ps *State) Node {
node := p(ps)
if ps.Errored() {
return node
}
node.Result = val
return node
}
}
func Map(parser Parserish, f func(n Node) Node) Parser { func Map(parser Parserish, f func(n Node) Node) Parser {
p := Parsify(parser) p := Parsify(parser)

View File

@ -27,25 +27,13 @@ var (
return Node{Result: ret} return Node{Result: ret}
}) })
_null = Map("null", func(n Node) Node { _null = Bind("null", nil)
return Node{Result: nil} _true = Bind("true", true)
}) _false = Bind("false", false)
_true = Map("true", func(n Node) Node {
return Node{Result: true}
})
_false = Map("false", func(n Node) Node {
return Node{Result: false}
})
_string = Map(String('"'), func(n Node) Node { _string = Map(String('"'), func(n Node) Node {
return Node{Result: n.Token} return Node{Result: n.Token}
}) })
Y = Map(&value, func(n Node) Node {
return Node{Result: n.Result}
})
) )
func init() { func init() {
@ -53,7 +41,7 @@ func init() {
} }
func Unmarshal(input string) (interface{}, error) { func Unmarshal(input string) (interface{}, error) {
result, remaining, err := ParseString(Y, input) result, remaining, err := ParseString(value, input)
if err != nil { if err != nil {
return result, err return result, err