1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package parser
import (
"fmt"
"github.com/ChrisTrenkamp/goxpath/lexer"
)
type stateType int
const (
defState stateType = iota
xpathState
funcState
paramState
predState
parenState
)
type parseStack struct {
stack []*Node
stateTypes []stateType
cur *Node
}
func (p *parseStack) push(t stateType) {
p.stack = append(p.stack, p.cur)
p.stateTypes = append(p.stateTypes, t)
}
func (p *parseStack) pop() {
stackPos := len(p.stack) - 1
p.cur = p.stack[stackPos]
p.stack = p.stack[:stackPos]
p.stateTypes = p.stateTypes[:stackPos]
}
func (p *parseStack) curState() stateType {
if len(p.stateTypes) == 0 {
return defState
}
return p.stateTypes[len(p.stateTypes)-1]
}
type lexFn func(*parseStack, lexer.XItem)
var parseMap = map[lexer.XItemType]lexFn{
lexer.XItemAbsLocPath: xiXPath,
lexer.XItemAbbrAbsLocPath: xiXPath,
lexer.XItemAbbrRelLocPath: xiXPath,
lexer.XItemRelLocPath: xiXPath,
lexer.XItemEndPath: xiEndPath,
lexer.XItemAxis: xiXPath,
lexer.XItemAbbrAxis: xiXPath,
lexer.XItemNCName: xiXPath,
lexer.XItemQName: xiXPath,
lexer.XItemNodeType: xiXPath,
lexer.XItemProcLit: xiXPath,
lexer.XItemFunction: xiFunc,
lexer.XItemArgument: xiFuncArg,
lexer.XItemEndFunction: xiEndFunc,
lexer.XItemPredicate: xiPred,
lexer.XItemEndPredicate: xiEndPred,
lexer.XItemStrLit: xiValue,
lexer.XItemNumLit: xiValue,
lexer.XItemOperator: xiOp,
lexer.XItemVariable: xiValue,
}
var opPrecedence = map[string]int{
"|": 1,
"*": 2,
"div": 2,
"mod": 2,
"+": 3,
"-": 3,
"=": 4,
"!=": 4,
"<": 4,
"<=": 4,
">": 4,
">=": 4,
"and": 5,
"or": 6,
}
//Parse creates an AST tree for XPath expressions.
func Parse(xp string) (*Node, error) {
var err error
c := lexer.Lex(xp)
n := &Node{}
p := &parseStack{cur: n}
for next := range c {
if next.Typ != lexer.XItemError {
parseMap[next.Typ](p, next)
} else if err == nil {
err = fmt.Errorf(next.Val)
}
}
return n, err
}
func xiXPath(p *parseStack, i lexer.XItem) {
if p.curState() == xpathState {
p.cur.push(i)
p.cur = p.cur.next
return
}
if p.cur.Val.Typ == lexer.XItemFunction {
p.cur.Right = &Node{Val: i, Parent: p.cur}
p.cur.next = p.cur.Right
p.push(xpathState)
p.cur = p.cur.next
return
}
p.cur.pushNotEmpty(i)
p.push(xpathState)
p.cur = p.cur.next
}
func xiEndPath(p *parseStack, i lexer.XItem) {
p.pop()
}
func xiFunc(p *parseStack, i lexer.XItem) {
p.cur.push(i)
p.cur = p.cur.next
p.push(funcState)
}
func xiFuncArg(p *parseStack, i lexer.XItem) {
if p.curState() != funcState {
p.pop()
}
p.cur.push(i)
p.cur = p.cur.next
p.push(paramState)
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.cur = p.cur.next
}
func xiEndFunc(p *parseStack, i lexer.XItem) {
if p.curState() == paramState {
p.pop()
}
p.pop()
}
func xiPred(p *parseStack, i lexer.XItem) {
p.cur.push(i)
p.cur = p.cur.next
p.push(predState)
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.cur = p.cur.next
}
func xiEndPred(p *parseStack, i lexer.XItem) {
p.pop()
}
func xiValue(p *parseStack, i lexer.XItem) {
p.cur.add(i)
}
func xiOp(p *parseStack, i lexer.XItem) {
if i.Val == "(" {
p.cur.push(lexer.XItem{Typ: Empty, Val: ""})
p.push(parenState)
p.cur = p.cur.next
return
}
if i.Val == ")" {
p.pop()
return
}
if p.cur.Val.Typ == lexer.XItemOperator {
if opPrecedence[p.cur.Val.Val] <= opPrecedence[i.Val] {
p.cur.add(i)
} else {
p.cur.push(i)
}
} else {
p.cur.add(i)
}
p.cur = p.cur.next
}
|