/
Grammar.pm6
208 lines (173 loc) · 4.68 KB
/
Grammar.pm6
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
use v6.c;
#use Grammar::Debugger;
grammar XML::XPath::Grammar {
rule TOP { <.ws>? <Expr> }
# https://www.w3.org/TR/1999/REC-xpath-19991116/
# [1]
# [2]
# [10] token AbbreviatedAbsoluteLocationPath
token LocationPath {
<StepDelim> <RelativeLocationPath>
| <RelativeLocationPath>
| <StepDelim>
}
token StepDelim { '/' }
# [3]
#token RelativeLocationPath {
# <Step>
# | <RelativeLocationPath> '/' <Step>
# | <AbbreviatedRelativeLocationPath>
#}
# [11]
#token AbbreviatedRelativeLocationPath {
# <RelativeLocationPath> '//' <Step>
#}
# rewrite wihtout infinite loop
token RelativeLocationPath {
[ <.ws>? <Step> <.ws>? ]+ % <StepDelim>
}
# [4]
token Step {
<StepDelim>? <AxisSpecifier> <NodeTest> <Predicate>*
| <AbbreviatedStep>
}
# [13]
# [5]
# [6]
token AxisSpecifier {
'ancestor::'
| 'ancestor-or-self::'
| 'attribute::'
| 'child::'
| 'descendant::'
| 'descendant-or-self::'
| 'following::'
| 'following-sibling::'
| 'namespace::'
| 'parent::'
| 'preceding::'
| 'preceding-sibling::'
| 'self::'
| '@'
| ''
}
# [7]s
token NodeTest {
<NameTest>
| <NodeType> '()'
| 'processing-instruction' '(' <Literal> ')'
}
# [8]
token Predicate { '[' <.ws>? <PredicateExpr> <.ws>? ']' }
# [9]
token PredicateExpr { <Expr> }
# [12]
token AbbreviatedStep { '.' | '..' }
# [14]
token Expr { <OrExpr> }
# [15]
token PrimaryExpr {
<VariableReference>
| '(' <Expr> ')'
| <Literal>
| <Number>
| <FunctionCall>
}
# [16]
token FunctionCall {
<FunctionName> '(' <.ws>? [ [<.ws>? <Argument> <.ws>?]* % ',' ] <.ws>? ')'
}
# [17]
token Argument { <Expr> }
# [18]
token UnionExpr { [<.ws>? <PathExpr> <.ws>? ]+ % <UnionOperator> }
token UnionOperator { '|' }
# [19]
token PathExpr {
<FilterExpr> [ <StepDelim> <RelativeLocationPath> ]?
| <LocationPath>
}
# [20]
token FilterExpr {
<PrimaryExpr> <.ws>? <Predicate> *
}
# [21]
token OrExpr { [ <.ws>? <AndExpr> <.ws>? ]+ % <OrOperator> }
token OrOperator { 'or' }
# [22]
token AndExpr { [ <.ws>? <EqualityExpr> <.ws>? ]+ % <AndOperator> }
token AndOperator { 'and' }
# [23]
token EqualityExpr { [ <.ws>? <RelationalExpr> <.ws>? ]+ % <EqualityOperator> }
token EqualityOperator { ['=' || '!=' ] }
# [24]
token RelationalExpr { [ <.ws>? <AdditiveExpr> <.ws>? ]+ % <RelationalOperator> }
token RelationalOperator { [ [ '<' | '>' ] '='? ] }
# [25]
token AdditiveExpr { [ <.ws>? <MultiplicativeExpr> <.ws>? ]+ % <AdditiveOperators> }
token AdditiveOperators { [ '+' | '-'] }
# [34]
# [26]
token MultiplicativeExpr { [ <.ws>? <UnaryExpr> <.ws>? ]+ % <MultiplicativeOperator> }
token MultiplicativeOperator { ['*' | 'div' | 'mod' ] }
# [27]
token UnaryExpr { <UnaryOperator> <UnionExpr> }
token UnaryOperator { '-'* }
# [29]
token Literal {
'"' <-[ " ]>* '"'
| "'" <-[ ' ]>* "'"
}
#'
# [30]
# [31]
token Number {
\d+ [ '.' \d *]?
| '.' \d+
}
# [35]
token FunctionName {
<QName>
# except <NodeType>
# look ahead
}
# [36]
token VariableReference { '$' <QName> }
# [37]
token NameTest {
'*'
| <NCName> ':' '*'
| <QName>
}
# [38]
token NodeType {
'comment'
| 'text'
| 'processing-instruction'
| 'node'
}
# https://www.w3.org/TR/REC-xml-names/#NT-QName
# [7]
token QName {
<PrefixedName>
| <UnprefixedName>
}
# [8]
token PrefixedName { <Prefix> ':' <LocalPart> }
# [9]
token UnprefixedName { <LocalPart> }
# [10]
token Prefix { <NCName> }
# [11]
token LocalPart { <NCName> }
# https://www.w3.org/TR/REC-xml-names/#NT-NCName
# [4]
# TODO
# [4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
# [4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
# [5] Name ::= NameStartChar (NameChar)*ken NCName {
token NCName {
# https://www.w3.org/TR/REC-xml/#NT-Name
<:L> <[\w\-\.] - [:]>*
}
}