2017-08-07 12:07:29 +02:00
|
|
|
package goparsify
|
2017-08-06 06:31:35 +02:00
|
|
|
|
2017-08-06 09:02:39 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
2017-08-07 12:07:29 +02:00
|
|
|
var Nil = NewParser("Nil", func(ps *State) *Node {
|
2017-08-06 15:32:10 +02:00
|
|
|
return nil
|
2017-08-07 12:07:29 +02:00
|
|
|
})
|
2017-08-06 06:31:35 +02:00
|
|
|
|
|
|
|
func And(parsers ...Parserish) Parser {
|
|
|
|
if len(parsers) == 0 {
|
|
|
|
return Nil
|
|
|
|
}
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
parserfied := ParsifyAll(parsers...)
|
2017-08-06 06:31:35 +02:00
|
|
|
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("And()", func(ps *State) *Node {
|
2017-08-07 10:25:23 +02:00
|
|
|
var nodes = make([]*Node, 0, len(parserfied))
|
2017-08-06 15:32:10 +02:00
|
|
|
startpos := ps.Pos
|
|
|
|
for _, parser := range parserfied {
|
|
|
|
node := parser(ps)
|
|
|
|
if ps.Errored() {
|
|
|
|
ps.Pos = startpos
|
|
|
|
return nil
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
if node != nil {
|
|
|
|
nodes = append(nodes, node)
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 10:25:23 +02:00
|
|
|
return &Node{Children: nodes}
|
2017-08-07 12:07:29 +02:00
|
|
|
})
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 13:20:30 +02:00
|
|
|
func NoAutoWS(parser Parserish) Parser {
|
|
|
|
parserfied := Parsify(parser)
|
|
|
|
return func(ps *State) *Node {
|
|
|
|
ps.NoAutoWS = true
|
|
|
|
|
|
|
|
ret := parserfied(ps)
|
|
|
|
|
|
|
|
ps.NoAutoWS = false
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 06:31:35 +02:00
|
|
|
func Any(parsers ...Parserish) Parser {
|
|
|
|
if len(parsers) == 0 {
|
|
|
|
return Nil
|
|
|
|
}
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
parserfied := ParsifyAll(parsers...)
|
2017-08-06 06:31:35 +02:00
|
|
|
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Any()", func(ps *State) *Node {
|
2017-08-06 15:32:10 +02:00
|
|
|
longestError := Error{}
|
|
|
|
startpos := ps.Pos
|
|
|
|
for _, parser := range parserfied {
|
|
|
|
node := parser(ps)
|
|
|
|
if ps.Errored() {
|
|
|
|
if ps.Error.pos > longestError.pos {
|
|
|
|
longestError = ps.Error
|
|
|
|
}
|
|
|
|
ps.ClearError()
|
2017-08-06 06:31:35 +02:00
|
|
|
continue
|
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
return node
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
ps.Error = longestError
|
|
|
|
ps.Pos = startpos
|
|
|
|
return nil
|
2017-08-07 12:07:29 +02:00
|
|
|
})
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Kleene(opScan Parserish, sepScan ...Parserish) Parser {
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Kleene()", manyImpl(0, opScan, nil, sepScan...))
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func KleeneUntil(opScan Parserish, untilScan Parserish, sepScan ...Parserish) Parser {
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("KleeneUntil()", manyImpl(0, opScan, untilScan, sepScan...))
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Many(opScan Parserish, sepScan ...Parserish) Parser {
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Many()", manyImpl(1, opScan, nil, sepScan...))
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ManyUntil(opScan Parserish, untilScan Parserish, sepScan ...Parserish) Parser {
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("ManyUntil()", manyImpl(1, opScan, untilScan, sepScan...))
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func manyImpl(min int, op Parserish, until Parserish, sep ...Parserish) Parser {
|
|
|
|
opParser := Parsify(op)
|
|
|
|
untilParser := Parsify(until)
|
|
|
|
sepParser := Nil
|
|
|
|
if len(sep) > 0 {
|
|
|
|
sepParser = Parsify(sep[0])
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:25:23 +02:00
|
|
|
return func(ps *State) *Node {
|
|
|
|
var node *Node
|
|
|
|
nodes := make([]*Node, 0, 20)
|
2017-08-06 15:32:10 +02:00
|
|
|
startpos := ps.Pos
|
2017-08-06 06:31:35 +02:00
|
|
|
for {
|
2017-08-06 15:32:10 +02:00
|
|
|
tempPos := ps.Pos
|
2017-08-07 13:38:46 +02:00
|
|
|
if untilParser != nil {
|
|
|
|
node = untilParser(ps)
|
|
|
|
if !ps.Errored() {
|
|
|
|
ps.Pos = tempPos
|
|
|
|
if len(nodes) < min {
|
|
|
|
ps.Pos = startpos
|
|
|
|
ps.ErrorHere("something else")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
break
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
2017-08-07 13:38:46 +02:00
|
|
|
ps.ClearError()
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
node = opParser(ps)
|
|
|
|
if ps.Errored() {
|
2017-08-06 06:31:35 +02:00
|
|
|
if len(nodes) < min {
|
2017-08-06 15:32:10 +02:00
|
|
|
ps.Pos = startpos
|
|
|
|
return nil
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
ps.ClearError()
|
2017-08-06 06:31:35 +02:00
|
|
|
break
|
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
|
2017-08-06 06:31:35 +02:00
|
|
|
nodes = append(nodes, node)
|
2017-08-06 15:32:10 +02:00
|
|
|
|
|
|
|
node = sepParser(ps)
|
|
|
|
if ps.Errored() {
|
|
|
|
ps.ClearError()
|
2017-08-06 06:31:35 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 10:25:23 +02:00
|
|
|
return &Node{Children: nodes}
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Maybe(parser Parserish) Parser {
|
2017-08-06 15:32:10 +02:00
|
|
|
parserfied := Parsify(parser)
|
2017-08-06 09:02:39 +02:00
|
|
|
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Maybe()", func(ps *State) *Node {
|
2017-08-06 15:32:10 +02:00
|
|
|
node := parserfied(ps)
|
|
|
|
if ps.Errored() {
|
|
|
|
ps.ClearError()
|
|
|
|
return nil
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
|
|
|
|
return node
|
2017-08-07 12:07:29 +02:00
|
|
|
})
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 10:25:23 +02:00
|
|
|
func Map(parser Parserish, f func(n *Node) *Node) Parser {
|
2017-08-06 09:02:39 +02:00
|
|
|
p := Parsify(parser)
|
|
|
|
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Map()", func(ps *State) *Node {
|
2017-08-06 15:32:10 +02:00
|
|
|
node := p(ps)
|
|
|
|
if ps.Errored() {
|
|
|
|
return nil
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
2017-08-06 15:32:10 +02:00
|
|
|
return f(node)
|
2017-08-07 12:07:29 +02:00
|
|
|
})
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 10:25:23 +02:00
|
|
|
func flatten(n *Node) string {
|
|
|
|
if n.Token != "" {
|
|
|
|
return n.Token
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|
2017-08-06 09:02:39 +02:00
|
|
|
|
2017-08-07 10:25:23 +02:00
|
|
|
if len(n.Children) > 0 {
|
2017-08-06 09:02:39 +02:00
|
|
|
sbuf := &bytes.Buffer{}
|
2017-08-07 10:25:23 +02:00
|
|
|
for _, node := range n.Children {
|
|
|
|
sbuf.WriteString(flatten(node))
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
return sbuf.String()
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:25:23 +02:00
|
|
|
return ""
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Merge(parser Parserish) Parser {
|
2017-08-07 13:38:46 +02:00
|
|
|
return NewParser("Merge()", Map(parser, func(n *Node) *Node {
|
2017-08-07 10:25:23 +02:00
|
|
|
return &Node{Token: flatten(n)}
|
2017-08-07 12:07:29 +02:00
|
|
|
}))
|
2017-08-06 06:31:35 +02:00
|
|
|
}
|