-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpath_parser.g
159 lines (130 loc) · 3.93 KB
/
xpath_parser.g
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
# Relevant documentation:
# https://www.w3.org/TR/1999/REC-xpath-19991116
# https://github.com/we-like-parsers/pegen
# https://we-like-parsers.github.io/pegen
#
# TODO can in fact use the entry point we want in the generated parser, so
# that for supporting match patterns
@subheader'''
def assign(obj, **kwargs):
for k, v in kwargs.items():
assert hasattr(obj, k), "%r has no attr %r" % (obj, k)
setattr(obj, k, v)
return obj
class Path:
def __init__(self):
self.steps = []
self.absolute = False
def append(self, step):
self.steps.append(step)
return self
def prepend(self, step):
self.steps.insert(0, step)
return self
def __repr__(self):
return "Path(absolute=%r, steps=%r)" % (self.absolute, self.steps)
class Step:
def __init__(self):
self.axis = "child"
self.name_test = None
self.predicates = []
self.abbreviated = False
def __repr__(self):
return "Step(axis=%r, name_test=%r, predicates=%r)" % (self.axis, self.name_test, self.predicates)
class Op:
# l is None if this is a unary operator.
def __init__(self, val, l, r):
self.val = val
self.l = l
self.r = r
def __repr__(self):
return "Op(%r, %r, %r)" % (self.val, self.l, self.r)
class Func:
def __init__(self, name, args):
self.name = name
self.args = args
def __repr__(self):
return "Func(%r, %r)" % (self.name, self.args)
'''
start: r=LocationPath $ { r }
LocationPath:
| RelativeLocationPath
| r=AbsoluteLocationPath { assign(r, absolute=True) }
AbsoluteLocationPath:
| AbbreviatedAbsoluteLocationPath
| '/' r=RelativeLocationPath? { r or Path() }
RelativeLocationPath:
| AbbreviatedRelativeLocationPath
| r=RelativeLocationPath '/' s=Step { r.append(s) }
| r=Step { Path().append(r) }
AbbreviatedAbsoluteLocationPath: '//' r=RelativeLocationPath {
r.prepend(assign(Step(), axis="descendant-or-self", abbreviated=True)) }
AbbreviatedRelativeLocationPath: r=RelativeLocationPath '//' s=Step {
r.append(assign(Step(), axis="descendant-or-self", abbreviated=True)).append(s) }
AbbreviatedStep:
| '.' '.' { assign(Step(), axis="parent", abbreviated=True) }
| '.' { assign(Step(), axis="self", abbreviated=True) }
Step:
| q=AxisSpecifier? r=NodeTest s=Predicate* {
assign(r, axis=q or "child", predicates=s) }
| r=AbbreviatedStep { r }
AxisSpecifier: r=AxisName ':' ':' { r.string }
AxisName:
| "ancestor-or-self"
| "ancestor"
| "child"
| "descendant-or-self"
| "descendant"
| "following-sibling"
| "parent"
| "preceding-sibling"
| "self"
| "stuck-child"
| "stuck-following-sibling"
| "stuck-preceding-sibling"
NodeTest: NameTest
NameTest:
| '*' { Step() }
| r=NAME { assign(Step(), name_test=r.string) }
Predicate: '[' r=PredicateExpr ']' { r }
PredicateExpr: Expr
Expr: OrExpr
PrimaryExpr:
| '(' r=Expr ')' { r }
| Literal
| FunctionCall
| '@' r=NAME { f"(node[{r.string!r}])" }
OrExpr:
| r=OrExpr "or" s=AndExpr { Op("or", r, s) }
| AndExpr
AndExpr:
| r=AndExpr "and" s=NotExpr { Op("and", r, s) }
| NotExpr
# The xpath spec defines "not" as a function, but we make it an operator.
# Thus both foo[not(kid)] and foo[not kid] can be used and are equivalent.
# We use "not" as a soft keyword, so foo[not] is valid and refers to "foo"
# elements that have "not" child elements.
NotExpr:
| "not" r=NotExpr { Op("not", None, r) }
| EqualityExpr
EqualityExpr:
| r=EqualityExpr '=' s=RelationalExpr { Op("==", r, s) }
| r=EqualityExpr '!=' s=RelationalExpr { Op("!=", r, s)}
| RelationalExpr
RelationalExpr:
| r=RelationalExpr '<' s=AdditiveExpr { Op("<", r, s) }
| r=RelationalExpr '>' s=AdditiveExpr { Op(">", r, s) }
| r=RelationalExpr '<=' s=AdditiveExpr { Op("<=", r, s) }
| r=RelationalExpr '>=' s=AdditiveExpr { Op(">=", r, s) }
| AdditiveExpr
AdditiveExpr: UnionExpr
UnionExpr: PathExpr
PathExpr:
| FilterExpr
| LocationPath
FilterExpr: PrimaryExpr
FunctionCall: r=FunctionName '(' s=Arguments? ')' { Func(r, s or []) }
FunctionName: r=NAME { r.string }
Arguments: r=','.Argument+ { r }
Argument: Expr
Literal: r=STRING { r.string }