diff --git a/combinator.go b/combinator.go index f072913..e83a0c1 100644 --- a/combinator.go +++ b/combinator.go @@ -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) diff --git a/json/json.go b/json/json.go index 28889bd..13db8a5 100644 --- a/json/json.go +++ b/json/json.go @@ -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