4 // Author - Piers Haken <piersh@friskit.com>
9 using System.Xml.XPath;
11 namespace Mono.Xml.XPath
13 internal class XPathParser
16 internal System.Xml.Xsl.IStaticXsltContext Context;
18 public XPathParser () : this (null) {}
19 internal XPathParser (System.Xml.Xsl.IStaticXsltContext context)
22 ErrorOutput = System.IO.TextWriter.Null;
25 internal Expression Compile (string xpath)
28 Tokenizer tokenizer = new Tokenizer (xpath);
29 return (Expression) yyparse (tokenizer);
30 } catch (XPathException e) {
32 } catch (Exception e) {
33 throw new XPathException ("Error during parse of " + xpath, e);
51 %token BRACKET_OPEN "["
52 %token BRACKET_CLOSE "]"
54 %token PAREN_CLOSE ")"
72 %token ANCESTOR "ancestor"
73 %token ANCESTOR_OR_SELF "ancstor-or-self"
74 %token ATTRIBUTE "attribute"
76 %token DESCENDANT "descendant"
77 %token DESCENDANT_OR_SELF "descendant-or-self"
78 %token FOLLOWING "following"
79 %token FOLLOWING_SIBLING "sibling"
80 %token NAMESPACE "NameSpace"
81 %token PARENT "parent"
82 %token PRECEDING "preceding"
83 %token PRECEDING_SIBLING "preceding-sibling"
86 %token COMMENT "comment"
88 %token PROCESSING_INSTRUCTION "processing-instruction"
126 $$ = new ExprOR ((Expression) $1, (Expression) $3);
132 | AndExpr AND EqualityExpr
134 $$ = new ExprAND ((Expression) $1, (Expression) $3);
140 | EqualityExpr EQ RelationalExpr
142 $$ = new ExprEQ ((Expression) $1, (Expression) $3);
144 | EqualityExpr NE RelationalExpr
146 $$ = new ExprNE ((Expression) $1, (Expression) $3);
152 | RelationalExpr LT AdditiveExpr
154 $$ = new ExprLT ((Expression) $1, (Expression) $3);
156 | RelationalExpr GT AdditiveExpr
158 $$ = new ExprGT ((Expression) $1, (Expression) $3);
160 | RelationalExpr LE AdditiveExpr
162 $$ = new ExprLE ((Expression) $1, (Expression) $3);
164 | RelationalExpr GE AdditiveExpr
166 $$ = new ExprGE ((Expression) $1, (Expression) $3);
172 | AdditiveExpr PLUS MultiplicativeExpr
174 $$ = new ExprPLUS ((Expression) $1, (Expression) $3);
176 | AdditiveExpr MINUS MultiplicativeExpr
178 $$ = new ExprMINUS ((Expression) $1, (Expression) $3);
184 | MultiplicativeExpr MULTIPLY UnaryExpr
186 $$ = new ExprMULT ((Expression) $1, (Expression) $3);
188 | MultiplicativeExpr DIV UnaryExpr
190 $$ = new ExprDIV ((Expression) $1, (Expression) $3);
192 | MultiplicativeExpr MOD UnaryExpr
194 $$ = new ExprMOD ((Expression) $1, (Expression) $3);
202 $$ = new ExprNEG ((Expression) $2);
208 | UnionExpr BAR PathExpr
210 $$ = new ExprUNION ((Expression) $1, (Expression) $3);
217 | FilterExpr SLASH RelativeLocationPath
219 $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
221 | FilterExpr SLASH2 RelativeLocationPath
223 $$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
228 : RelativeLocationPath
229 | AbsoluteLocationPath
235 $$ = new ExprRoot ();
237 | SLASH RelativeLocationPath
239 $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
241 | SLASH2 RelativeLocationPath
243 $$ = new ExprSLASH2 (new ExprRoot (), (NodeSet) $2);
249 | RelativeLocationPath SLASH Step
251 $$ = new ExprSLASH ((NodeSet) $1, (NodeSet) $3);
253 | RelativeLocationPath SLASH2 Step
255 $$ = new ExprSLASH2 ((NodeSet) $1, (NodeSet) $3);
263 $$ = new NodeTypeTest (Axes.Self, XPathNodeType.All);
267 $$ = new NodeTypeTest (Axes.Parent, XPathNodeType.All);
273 | PredicatedStep Predicate
275 $$ = new ExprFilter ((NodeSet) $1, (Expression) $2);
280 : AxisSpecifier QName
282 $$ = new NodeNameTest ((Axes) $1, (XmlQualifiedName) $2, Context);
284 | AxisSpecifier ASTERISK
286 $$ = new NodeTypeTest ((Axes) $1);
288 | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE
290 $$ = new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4);
310 : COMMENT { $$ = XPathNodeType.Comment; }
311 | TEXT { $$ = XPathNodeType.Text; }
312 | PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
313 | NODE { $$ = XPathNodeType.All; }
319 | FilterExpr Predicate
321 $$ = new ExprFilter ((Expression) $1, (Expression) $2);
328 Expression ret = null;
330 ret = Context.TryGetVariable (((XmlQualifiedName) $2).ToString ());
333 ret = new ExprVariable ((XmlQualifiedName) $2, Context);
337 | PAREN_OPEN Expr PAREN_CLOSE
339 $$ = new ExprParens ((Expression) $2);
343 $$ = new ExprLiteral ((String) $1);
347 $$ = new ExprNumber ((double) $1);
353 : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
355 Expression ret = null;
357 ret = Context.TryGetFunction ((XmlQualifiedName) $1, (FunctionArguments) $3);
359 ret = ExprFunctionCall.Factory ((XmlQualifiedName) $1, (FunctionArguments) $3, Context);
367 | Expr OptionalArgumentListTail
369 $$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
373 OptionalArgumentListTail
375 | COMMA Expr OptionalArgumentListTail
377 $$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
382 : BRACKET_OPEN Expr BRACKET_CLOSE
389 : ANCESTOR { $$ = Axes.Ancestor; }
390 | ANCESTOR_OR_SELF { $$ = Axes.AncestorOrSelf; }
391 | ATTRIBUTE { $$ = Axes.Attribute; }
392 | CHILD { $$ = Axes.Child; }
393 | DESCENDANT { $$ = Axes.Descendant; }
394 | DESCENDANT_OR_SELF { $$ = Axes.DescendantOrSelf; }
395 | FOLLOWING { $$ = Axes.Following; }
396 | FOLLOWING_SIBLING { $$ = Axes.FollowingSibling; }
397 | NAMESPACE { $$ = Axes.Namespace; }
398 | PARENT { $$ = Axes.Parent; }
399 | PRECEDING { $$ = Axes.Preceding; }
400 | PRECEDING_SIBLING { $$ = Axes.PrecedingSibling; }
401 | SELF { $$ = Axes.Self; }