2002-09-12 Piers Haken <piersh@friskit.com>
[mono.git] / mcs / class / System.XML / System.Xml.XPath / Parser.jay
1 %{
2 // XPath parser
3 //
4 // Author - Piers Haken <piersh@friskit.com>
5 //
6
7 // TODO: FUNCTION_CALL should be a QName, not just a NCName
8 // TODO: PROCESSING_INSTRUCTION's optional parameter
9 // TODO: flatten argument/predicate lists in place
10
11 using System;
12 using System.Xml.XPath;
13 using Test.Xml.XPath;
14
15 namespace Mono.Xml.XPath
16 {
17         public class XPathParser
18         {
19                 internal object yyparseDebug (Tokenizer tok)
20                 {
21                         return yyparse (tok, new yydebug.yyDebugSimple ());
22                 }
23
24 %}
25
26 %token ERROR
27 %token EOF
28
29 %token SLASH
30 %token SLASH2
31 %token DOT
32 %token DOT2
33 %token COLON
34 %token COLON2
35 %token COMMA
36 %token AT
37
38 %token FUNCTION_NAME
39
40 %token BRACKET_OPEN
41 %token BRACKET_CLOSE
42 %token PAREN_OPEN
43 %token PAREN_CLOSE
44
45 %token AND
46 %token OR
47 %token DIV
48 %token MOD
49 %token PLUS
50 %token MINUS
51 %token ASTERISK
52 %token DOLLAR
53 %token BAR
54 %token EQ
55 %token NE
56 %token LE
57 %token GE
58 %token LT
59 %token GT
60
61 %token ANCESTOR
62 %token ANCESTOR_OR_SELF
63 %token ATTRIBUTE
64 %token CHILD
65 %token DESCENDANT
66 %token DESCENDANT_OR_SELF
67 %token FOLLOWING
68 %token FOLLOWING_SIBLING
69 %token NAMESPACE
70 %token PARENT
71 %token PRECEDING
72 %token PRECEDING_SIBLING
73 %token SELF
74
75 %token COMMENT
76 %token TEXT
77 %token PROCESSING_INSTRUCTION
78 %token NODE
79
80 %token NUMBER
81 %token LITERAL
82 %token NCName
83
84
85 %start Expr
86
87
88 %left AND
89 %left OR
90 %left EQ
91 %left NE
92 %left LE
93 %left GE
94 %left LT
95 %left GT
96
97 %left DIV
98 %left MOD
99 %left PLUS
100 %left MINUS
101
102 %%
103
104
105 Expr
106         : OrExpr
107         ;
108         
109 OrExpr
110         : AndExpr 
111         | OrExpr OR AndExpr
112         {
113                 $$ = new ExprOR ((Expression) $1, (Expression) $3);
114         }
115         ;
116
117 AndExpr
118         : EqualityExpr 
119         | AndExpr AND EqualityExpr 
120         {
121                 $$ = new ExprAND ((Expression) $1, (Expression) $3);
122         }
123         ;
124
125 EqualityExpr
126         : RelationalExpr 
127         | EqualityExpr EQ RelationalExpr 
128         {
129                 $$ = new ExprEQ ((Expression) $1, (Expression) $3);
130         }
131         | EqualityExpr NE RelationalExpr 
132         {
133                 $$ = new ExprNE ((Expression) $1, (Expression) $3);
134         }
135         ;
136
137 RelationalExpr
138         : AdditiveExpr 
139         | RelationalExpr LT AdditiveExpr 
140         {
141                 $$ = new ExprLT ((Expression) $1, (Expression) $3);
142         }
143         | RelationalExpr GT AdditiveExpr 
144         {
145                 $$ = new ExprGT ((Expression) $1, (Expression) $3);
146         }
147         | RelationalExpr LE AdditiveExpr 
148         {
149                 $$ = new ExprLE ((Expression) $1, (Expression) $3);
150         }
151         | RelationalExpr GE AdditiveExpr 
152         {
153                 $$ = new ExprGE ((Expression) $1, (Expression) $3);
154         }
155         ;
156
157 AdditiveExpr
158         : MultiplicativeExpr 
159         | AdditiveExpr PLUS MultiplicativeExpr
160         {
161                 $$ = new ExprPLUS ((Expression) $1, (Expression) $3);
162         }
163         | AdditiveExpr MINUS MultiplicativeExpr
164         {
165                 $$ = new ExprMINUS ((Expression) $1, (Expression) $3);
166         }
167         ;
168
169 MultiplicativeExpr
170         : UnaryExpr 
171         | MultiplicativeExpr ASTERISK UnaryExpr 
172         {
173                 $$ = new ExprMULT ((Expression) $1, (Expression) $3);
174         }
175         | MultiplicativeExpr DIV UnaryExpr
176         {
177                 $$ = new ExprDIV ((Expression) $1, (Expression) $3);
178         }
179         | MultiplicativeExpr MOD UnaryExpr
180         {
181                 $$ = new ExprMOD ((Expression) $1, (Expression) $3);
182         }
183         ;
184
185 UnaryExpr
186         : UnionExpr 
187         | MINUS UnaryExpr 
188         {
189                 $$ = new ExprNEG ((Expression) $2);
190         }
191         ;
192
193 UnionExpr
194         : PathExpr 
195         | UnionExpr BAR PathExpr
196         {
197                 $$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
198         }
199         ;
200
201 PathExpr
202         : RelativeLocationPath
203         | SLASH
204         {
205                 $$ = new ExprRoot ();
206         }
207         | SLASH RelativeLocationPath
208         {
209                 $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
210         }
211         | SLASH2 RelativeLocationPath 
212         {
213                 ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
214                 $$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), exprStep), (NodeSet) $2);
215         }
216         | FilterExpr 
217         | FilterExpr SLASH RelativeLocationPath
218         {
219                 $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
220         }
221         | FilterExpr SLASH2 RelativeLocationPath
222         {
223                 ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
224                 $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
225         }
226         ;
227
228 RelativeLocationPath
229         : Step
230         | RelativeLocationPath SLASH Step 
231         {
232                 $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
233         }
234         | RelativeLocationPath SLASH2 Step 
235         {
236                 ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
237                 $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
238         }
239         ;
240
241 Step
242         : AxisSpecifier QName ZeroOrMorePredicates
243         {
244                 $$ = new ExprStep (new NodeNameTest ((Axes) $1, (QName) $2), (ExprPredicates) $3);
245         }
246         | AxisSpecifier ASTERISK ZeroOrMorePredicates
247         {
248                 $$ = new ExprStep (new NodeTypeTest ((Axes) $1), (ExprPredicates) $3);
249         }
250         | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE ZeroOrMorePredicates
251         {
252                 $$ = new ExprStep (new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4), (ExprPredicates) $6);
253         }
254         | DOT
255         {
256                 $$ = new ExprStep (new NodeTypeTest (Axes.Self, XPathNodeType.All));
257         }
258         | DOT2
259         {
260                 $$ = new ExprStep (new NodeTypeTest (Axes.Parent, XPathNodeType.All));
261         }
262         ;
263
264 AxisSpecifier
265         : /* empty */
266         {
267                 $$ = Axes.Child;
268         }
269         | AT
270         {
271                 $$ = Axes.Attribute;
272         }
273         | AxisName COLON2
274         {
275                 $$ = $1;
276         }
277         ;
278
279 NodeType
280         : COMMENT                                       { $$ = XPathNodeType.Comment; }
281         | TEXT                                          { $$ = XPathNodeType.Text; }
282         | PROCESSING_INSTRUCTION        { $$ = XPathNodeType.ProcessingInstruction; }
283         | NODE                                          { $$ = XPathNodeType.All; }
284         ;
285
286
287 FilterExpr
288         : PrimaryExpr 
289         | FilterExpr Predicate 
290         {
291                 $$ = new ExprFilter ((Expression) $1, (Expression) $2);
292         }
293         ;
294
295 PrimaryExpr
296         : DOLLAR QName
297         {
298                 $$ = new ExprVariable ((QName) $2);
299         } 
300         | PAREN_OPEN Expr PAREN_CLOSE
301         {
302                 $$ = $2;
303         }
304         | LITERAL
305         {
306                 $$ = new ExprLiteral ((String) $1);
307         }
308         | NUMBER
309         {
310                 $$ = new ExprNumber ((double) $1);
311         }
312         | FunctionCall
313         ;
314
315 FunctionCall
316         : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
317         {
318                 $$ = new ExprFunctionCall ((String) $1, (FunctionArguments) $3);
319         }
320         ;
321
322 OptionalArgumentList
323         : /* empty */
324         | Expr OptionalArgumentListTail
325         {
326                 $$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
327         }
328         ;
329
330 OptionalArgumentListTail
331         : /* empty */
332         | COMMA Expr OptionalArgumentListTail
333         {
334                 $$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
335         }
336         ;
337
338
339 ZeroOrMorePredicates
340         : /* empty */
341         | Predicate ZeroOrMorePredicates
342         {
343                 $$ = new ExprPredicates ((Expression) $1, (ExprPredicates) $2);
344         }
345         ;
346
347 Predicate
348         : BRACKET_OPEN Expr BRACKET_CLOSE
349         {
350                 $$ = $2;
351         }
352         ;
353
354 AxisName
355         : ANCESTOR                              { $$ = Axes.Ancestor; }
356         | ANCESTOR_OR_SELF              { $$ = Axes.AncestorOrSelf; }
357         | ATTRIBUTE                             { $$ = Axes.Attribute; }
358         | CHILD                                 { $$ = Axes.Child; }
359         | DESCENDANT                    { $$ = Axes.Descendant; }
360         | DESCENDANT_OR_SELF    { $$ = Axes.DescendantOrSelf; }
361         | FOLLOWING                             { $$ = Axes.Following; }
362         | FOLLOWING_SIBLING             { $$ = Axes.FollowingSibling; }
363         | NAMESPACE                             { $$ = Axes.Namespace; }
364         | PARENT                                { $$ = Axes.Parent; }
365         | PRECEDING                             { $$ = Axes.Preceding; }
366         | PRECEDING_SIBLING             { $$ = Axes.PrecedingSibling; }
367         | SELF                                  { $$ = Axes.Self; }
368         ;
369
370 OptionalLiteral
371         : /* empty */
372         | LITERAL
373         ;
374
375 QName
376         : NCName
377         {
378                 $$ = new NCName ((String) $1);
379         }
380         | NCName COLON ASTERISK
381         {
382                 $$ = new QName ((String) $1, null);
383         }
384         | NCName COLON NCName
385         {
386                 $$ = new QName ((String) $1, (String) $3);
387         }
388         ;
389
390 %%
391         }