-
Notifications
You must be signed in to change notification settings - Fork 3
/
xpath.go
288 lines (242 loc) · 5.64 KB
/
xpath.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2017 Santhosh Kumar Tekuri. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xpathparser
import (
"fmt"
"runtime"
"strconv"
"strings"
)
// Error is the error type returned by Parse function.
//
// It represents a syntax error in the XPath expression.
type Error struct {
Msg string
XPath string
Offset int
}
func (e *Error) Error() string {
return fmt.Sprintf("%s in xpath %s at offset %d", e.Msg, e.XPath, e.Offset)
}
// Axis specifies the tree relationship between the nodes selected by the location step and the context node.
type Axis int
// Possible values for Axis.
const (
Child Axis = iota
Descendant
Parent
Ancestor
FollowingSibling
PrecedingSibling
Following
Preceding
Attribute
Namespace
Self
DescendantOrSelf
AncestorOrSelf
)
var axisNames = []string{
"child",
"descendant",
"parent",
"ancestor",
"following-sibling",
"preceding-sibling",
"following",
"preceding",
"attribute",
"namespace",
"self",
"descendant-or-self",
"ancestor-or-self",
}
func (a Axis) String() string {
return axisNames[a]
}
var name2Axis = make(map[string]Axis)
func init() {
for i, name := range axisNames {
name2Axis[name] = Axis(i)
}
}
// NodeType represents test on node type.
type NodeType int
// Possible values for NodeType.
const (
Comment NodeType = iota
Text
Node
)
var nodeTypeNames = []string{"comment()", "text()", "node()"}
func (nt NodeType) String() string {
return nodeTypeNames[nt]
}
// Op represents XPath binrary operator.
type Op int
// Possible values for Op.
const (
EQ Op = iota
NEQ
LT
LTE
GT
GTE
Add
Subtract
Multiply
Mod
Div
And
Or
Union
)
func (op Op) String() string {
str := kind(op).String()
return str[1 : len(str)-1]
}
// An Expr is an interface holding one of the types:
// *LocationPath, *FilterExpr, *PathExpr, *BinaryExpr, *NegateExpr, *VarRef, *FuncCall, Number or String
type Expr interface{}
// BinaryExpr represents a binary operation.
type BinaryExpr struct {
LHS Expr
Op Op
RHS Expr
}
func (b *BinaryExpr) String() string {
return fmt.Sprintf("(%s %s %s)", b.LHS, b.Op, b.RHS)
}
// NegateExpr represents unary operator `-`.
type NegateExpr struct {
Expr Expr
}
func (n *NegateExpr) String() string {
return fmt.Sprintf("-%s", n.Expr)
}
// LocationPath represents XPath location path.
type LocationPath struct {
Abs bool
Steps []*Step
}
func (lp *LocationPath) String() string {
s := make([]string, len(lp.Steps))
for i, step := range lp.Steps {
s[i] = step.String()
}
if lp.Abs {
return fmt.Sprintf("/%s", strings.Join(s, "/"))
}
return fmt.Sprintf("%s", strings.Join(s, "/"))
}
// FilterExpr represents https://www.w3.org/TR/xpath/#NT-FilterExpr.
type FilterExpr struct {
Expr Expr
Predicates []Expr
}
func (f *FilterExpr) String() string {
return fmt.Sprintf("(%s)%s", f.Expr, predicatesString(f.Predicates))
}
// PathExpr represents https://www.w3.org/TR/xpath/#NT-PathExpr.
type PathExpr struct {
Filter Expr
LocationPath *LocationPath
}
func (p *PathExpr) String() string {
return fmt.Sprintf("(%s)/%s", p.Filter, p.LocationPath)
}
// Step represents XPath location step.
type Step struct {
Axis Axis
NodeTest NodeTest
Predicates []Expr
}
func (s *Step) String() string {
return fmt.Sprintf("%v::%s%s", s.Axis, s.NodeTest, predicatesString(s.Predicates))
}
// A NodeTest is an interface holding one of the types:
// NodeType, *NameTest, or PITest.
type NodeTest interface{}
// NameTest represents https://www.w3.org/TR/xpath/#NT-NameTest.
type NameTest struct {
Prefix string
Local string
}
func (nt *NameTest) String() string {
if nt.Prefix == "" {
return nt.Local
}
return fmt.Sprintf("%s:%s", nt.Prefix, nt.Local)
}
// PITest represents processing-instruction test.
type PITest string
func (pt PITest) String() string {
return fmt.Sprintf("processing-instruction(%q)", string(pt))
}
// VarRef represents https://www.w3.org/TR/xpath/#NT-VariableReference.
type VarRef struct {
Prefix string
Local string
}
func (vr *VarRef) String() string {
if vr.Prefix == "" {
return fmt.Sprintf("$%s", vr.Local)
}
return fmt.Sprintf("$%s:%s", vr.Prefix, vr.Local)
}
// FuncCall represents https://www.w3.org/TR/xpath/#section-Function-Calls.
type FuncCall struct {
Prefix string
Local string
Args []Expr
}
func (fc *FuncCall) String() string {
p := make([]string, len(fc.Args))
for i, param := range fc.Args {
p[i] = fmt.Sprint(param)
}
if fc.Prefix == "" {
return fmt.Sprintf("%s(%s)", fc.Local, strings.Join(p, ", "))
}
return fmt.Sprintf("%s:%s(%s)", fc.Prefix, fc.Local, strings.Join(p, ", "))
}
// Number represents number literal.
type Number float64
func (n Number) String() string {
return strconv.FormatFloat(float64(n), 'f', -1, 64)
}
// String represents string literal.
type String string
func (s String) String() string {
return strconv.Quote(string(s))
}
// MustParse is like Parse but panics if the xpath expression has error.
// It simplifies safe initialization of global variables holding parsed expressions.
func MustParse(xpath string) Expr {
p := &parser{lexer: lexer{xpath: xpath}}
return p.parse()
}
// Parse parses given xpath 1.0 expression.
func Parse(xpath string) (expr Expr, err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
if _, ok := r.(error); ok {
err = r.(error)
} else {
err = fmt.Errorf("%v", r)
}
}
}()
return MustParse(xpath), nil
}
func predicatesString(predicates []Expr) string {
p := make([]string, len(predicates))
for i, predicate := range predicates {
p[i] = fmt.Sprintf("[%s]", predicate)
}
return strings.Join(p, "")
}