Migrate yang-xpath-impl to bnd-parent
[yangtools.git] / xpath / yang-xpath-antlr / src / main / antlr4 / org / opendaylight / yangtools / yang / xpath / antlr / xpath.g4
1 grammar xpath;
2
3 /*
4 XPath 1.0 grammar. Should conform to the official spec at
5 http://www.w3.org/TR/1999/REC-xpath-19991116. The grammar
6 rules have been kept as close as possible to those in the
7 spec, but some adjustmewnts were unavoidable. These were
8 mainly removing left recursion (spec seems to be based on
9 LR), and to deal with the double nature of the '*' token
10 (node wildcard and multiplication operator). See also
11 section 3.7 in the spec. These rule changes should make
12 no difference to the strings accepted by the grammar.
13
14 Written by Jan-Willem van den Broek
15 Version 1.0
16
17 Do with this code as you will.
18 */
19 /*
20     Ported to Antlr4 by Tom Everett <tom@khubla.com>
21 */
22
23
24 main  :  expr
25   ;
26
27 locationPath 
28   :  relativeLocationPath
29   |  absoluteLocationPathNoroot
30   ;
31
32 absoluteLocationPathNoroot
33   :  '/' relativeLocationPath
34   |  '//' relativeLocationPath
35   ;
36
37 relativeLocationPath
38   :  step (('/'|'//') step)*
39   ;
40
41 step  :  axisSpecifier nodeTest predicate*
42   |  abbreviatedStep
43   ;
44
45 axisSpecifier
46   :  AxisName '::'
47   |  AT?
48   ;
49
50 nodeTest:  nameTest
51   |  NodeType '(' ')'
52   |  'processing-instruction' '(' Literal ')'
53   ;
54
55 predicate
56   :  '[' expr ']'
57   ;
58
59 abbreviatedStep
60   :  '.'
61   |  '..'
62   ;
63
64 expr  :  orExpr
65   ;
66
67 primaryExpr
68   :  variableReference
69   |  '(' expr ')'
70   |  Literal
71   |  Number  
72   |  functionCall
73   ;
74
75 functionCall
76   :  functionName '(' ( expr ( ',' expr )* )? ')'
77   ;
78
79 unionExprNoRoot
80   :  pathExprNoRoot ('|' unionExprNoRoot)?
81   |  '/' '|' unionExprNoRoot
82   ;
83
84 pathExprNoRoot
85   :  locationPath
86   |  filterExpr (('/'|'//') relativeLocationPath)?
87   ;
88
89 filterExpr
90   :  primaryExpr predicate*
91   ;
92
93 orExpr  :  andExpr (OR andExpr)*
94   ;
95
96 andExpr  :  equalityExpr (AND equalityExpr)*
97   ;
98
99 equalityExpr
100   :  relationalExpr (('='|'!=') relationalExpr)*
101   ;
102
103 relationalExpr
104   :  additiveExpr (('<'|'>'|'<='|'>=') additiveExpr)*
105   ;
106
107 additiveExpr
108   :  multiplicativeExpr (('+'|'-') multiplicativeExpr)*
109   ;
110
111 multiplicativeExpr
112   :  unaryExprNoRoot (('*'|DIV|MOD) multiplicativeExpr)?
113   |  '/' ((DIV|MOD) multiplicativeExpr)?
114   ;
115
116 unaryExprNoRoot
117   :  '-'* unionExprNoRoot
118   ;
119
120 qName  :  nCName (':' nCName)?
121   ;
122
123 // Does not match NodeType, as per spec.
124 functionName
125   :  nCName ':' nCName
126   |  NCName
127   |  AxisName
128   |  AND
129   |  OR
130   |  DIV
131   |  MOD
132   ;
133
134 variableReference
135   :  '$' qName
136   ;
137
138 nameTest:  '*'
139   |  nCName ':' '*'
140   |  qName
141   ;
142
143 nCName  :  NCName
144   |  AxisName
145   |  NodeType
146   |  AND
147   |  OR
148   |  DIV
149   |  MOD
150   ;
151
152 NodeType:  'comment'
153   |  'text'
154   |  'processing-instruction'
155   |  'node'
156   ;
157   
158 Number  :  Digits ('.' Digits?)?
159   |  '.' Digits
160   ;
161
162 fragment
163 Digits  :  ('0'..'9')+
164   ;
165
166 AxisName:  'ancestor'
167   |  'ancestor-or-self'
168   |  'attribute'
169   |  'child'
170   |  'descendant'
171   |  'descendant-or-self'
172   |  'following'
173   |  'following-sibling'
174   |  'namespace'
175   |  'parent'
176   |  'preceding'
177   |  'preceding-sibling'
178   |  'self'
179   ;
180
181
182   PATHSEP 
183        :'/';
184   ABRPATH   
185        : '//';
186   LPAR   
187        : '(';
188   RPAR   
189        : ')';
190   LBRAC   
191        :  '[';
192   RBRAC   
193        :  ']';
194   MINUS   
195        :  '-';
196   PLUS   
197        :  '+';
198   DOT   
199        :  '.';
200   MUL   
201        : '*';
202   DOTDOT   
203        :  '..';
204   AT   
205        : '@';
206   COMMA  
207        : ',';
208   PIPE   
209        :  '|';
210   LESS   
211        :  '<';
212   MORE_ 
213        :  '>';
214   LE   
215        :  '<=';
216   GE   
217        :  '>=';
218   COLON   
219        :  ':';
220   CC   
221        :  '::';
222   APOS   
223        :  '\'';
224   QUOT   
225        :  '"';
226   AND
227        :  'and';
228   OR
229        :  'or';
230   DIV
231        :  'div';
232   MOD
233        :  'mod';
234
235   
236 Literal  :  '"' ~'"'* '"'
237   |  '\'' ~'\''* '\''
238   ;
239
240 Whitespace
241   :  (' '|'\t'|'\n'|'\r')+ ->skip
242   ;
243
244 NCName  :  NCNameStartChar NCNameChar*
245   ;
246
247 fragment
248 NCNameStartChar
249   :  'A'..'Z'
250   |   '_'
251   |  'a'..'z'
252   |  '\u00C0'..'\u00D6'
253   |  '\u00D8'..'\u00F6'
254   |  '\u00F8'..'\u02FF'
255   |  '\u0370'..'\u037D'
256   |  '\u037F'..'\u1FFF'
257   |  '\u200C'..'\u200D'
258   |  '\u2070'..'\u218F'
259   |  '\u2C00'..'\u2FEF'
260   |  '\u3001'..'\uD7FF'
261   |  '\uF900'..'\uFDCF'
262   |  '\uFDF0'..'\uFFFD'
263 // Unfortunately, java escapes can't handle this conveniently,
264 // as they're limited to 4 hex digits. TODO.
265 //  |  '\U010000'..'\U0EFFFF'
266   ;
267
268 fragment
269 NCNameChar
270   :  NCNameStartChar | '-' | '.' | '0'..'9'
271   |  '\u00B7' | '\u0300'..'\u036F'
272   |  '\u203F'..'\u2040'
273   ;
274
275