4bf327cfe167085ddba680497d03ac6296d7436a
[yangtools.git] / yang / 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   |  '@'?
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   ;
129
130 variableReference
131   :  '$' qName
132   ;
133
134 nameTest:  '*'
135   |  nCName ':' '*'
136   |  qName
137   ;
138
139 nCName  :  NCName
140   |  AxisName
141   |  NodeType
142   ;
143
144 NodeType:  'comment'
145   |  'text'
146   |  'processing-instruction'
147   |  'node'
148   ;
149   
150 Number  :  Digits ('.' Digits?)?
151   |  '.' Digits
152   ;
153
154 fragment
155 Digits  :  ('0'..'9')+
156   ;
157
158 AxisName:  'ancestor'
159   |  'ancestor-or-self'
160   |  'attribute'
161   |  'child'
162   |  'descendant'
163   |  'descendant-or-self'
164   |  'following'
165   |  'following-sibling'
166   |  'namespace'
167   |  'parent'
168   |  'preceding'
169   |  'preceding-sibling'
170   |  'self'
171   ;
172
173
174   PATHSEP 
175        :'/';
176   ABRPATH   
177        : '//';
178   LPAR   
179        : '(';
180   RPAR   
181        : ')';
182   LBRAC   
183        :  '[';
184   RBRAC   
185        :  ']';
186   MINUS   
187        :  '-';
188   PLUS   
189        :  '+';
190   DOT   
191        :  '.';
192   MUL   
193        : '*';
194   DOTDOT   
195        :  '..';
196   AT   
197        : '@';
198   COMMA  
199        : ',';
200   PIPE   
201        :  '|';
202   LESS   
203        :  '<';
204   MORE_ 
205        :  '>';
206   LE   
207        :  '<=';
208   GE   
209        :  '>=';
210   COLON   
211        :  ':';
212   CC   
213        :  '::';
214   APOS   
215        :  '\'';
216   QUOT   
217        :  '"';
218   
219 Literal  :  '"' ~'"'* '"'
220   |  '\'' ~'\''* '\''
221   ;
222
223 Whitespace
224   :  (' '|'\t'|'\n'|'\r')+ ->skip
225   ;
226
227 NCName  :  NCNameStartChar NCNameChar*
228   ;
229
230 fragment
231 NCNameStartChar
232   :  'A'..'Z'
233   |   '_'
234   |  'a'..'z'
235   |  '\u00C0'..'\u00D6'
236   |  '\u00D8'..'\u00F6'
237   |  '\u00F8'..'\u02FF'
238   |  '\u0370'..'\u037D'
239   |  '\u037F'..'\u1FFF'
240   |  '\u200C'..'\u200D'
241   |  '\u2070'..'\u218F'
242   |  '\u2C00'..'\u2FEF'
243   |  '\u3001'..'\uD7FF'
244   |  '\uF900'..'\uFDCF'
245   |  '\uFDF0'..'\uFFFD'
246 // Unfortunately, java escapes can't handle this conveniently,
247 // as they're limited to 4 hex digits. TODO.
248 //  |  '\U010000'..'\U0EFFFF'
249   ;
250
251 fragment
252 NCNameChar
253   :  NCNameStartChar | '-' | '.' | '0'..'9'
254   |  '\u00B7' | '\u0300'..'\u036F'
255   |  '\u203F'..'\u2040'
256   ;
257
258