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