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 {
p := Parsify(parser)

View File

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