1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
%{
// XPath/XSLT Pattern parser
//
// Author: Piers Haken <piersh@friskit.com>
// Atsushi Enomoto <atsushi@ximian.com>
//
// IMPORTANT:
// Do not edit "PatternParser.jay". It is autogenerated from
// Parser.jay. It will be overwritten!
//
using System;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
#if XSLT_PATTERN
namespace Mono.Xml.Xsl
#else
namespace Mono.Xml.XPath
#endif
{
#if XSLT_PATTERN
internal class XsltPatternParser
#else
internal class XPathParser
#endif
{
internal System.Xml.Xsl.IStaticXsltContext Context;
#if XSLT_PATTERN
public XsltPatternParser () : this (null) {}
internal XsltPatternParser (System.Xml.Xsl.IStaticXsltContext context)
#else
public XPathParser () : this (null) {}
internal XPathParser (System.Xml.Xsl.IStaticXsltContext context)
#endif
{
Context = context;
ErrorOutput = System.IO.TextWriter.Null;
// debug = new yydebug.yyDebugSimple ();
}
internal Expression Compile (string xpath)
{
try {
Tokenizer tokenizer = new Tokenizer (xpath);
return (Expression) yyparse (tokenizer);
} catch (XPathException) {
throw;
} catch (Exception e) {
throw new XPathException ("Error during parse of " + xpath, e);
}
}
#pragma warning disable 649
static readonly int yacc_verbose_flag;
#pragma warning restore 649
private NodeSet CreateNodeTest (Axes axis, object nodeTest, ArrayList plist)
{
NodeSet test = CreateNodeTest (axis, nodeTest);
if (plist != null) {
for (int i = 0; i < plist.Count; i++)
test = new ExprFilter (test,
(Expression) plist [i]);
}
return test;
}
private NodeTest CreateNodeTest (Axes axis, object test)
{
if (test is XPathNodeType)
return new NodeTypeTest (axis,
(XPathNodeType) test, null);
else if (test is string || test == null)
return new NodeTypeTest (axis,
XPathNodeType.ProcessingInstruction,
(string) test);
XmlQualifiedName q = (XmlQualifiedName) test;
if (q == XmlQualifiedName.Empty)
return new NodeTypeTest (axis);
else
return new NodeNameTest (axis, q, Context);
}
%}
%token ERROR
%token EOF
%token SLASH "/"
%token SLASH2 "//"
%token DOT "."
%token DOT2 ".."
%token COLON2 "::"
%token COMMA ","
%token AT "@"
%token FUNCTION_NAME
%token BRACKET_OPEN "["
%token BRACKET_CLOSE "]"
%token PAREN_OPEN "("
%token PAREN_CLOSE ")"
%token AND "and"
%token OR "or"
%token DIV "div"
%token MOD "mod"
%token PLUS "+"
%token MINUS "-"
%token ASTERISK "*"
%token DOLLAR "$"
%token BAR "|"
%token EQ "="
%token NE "!="
%token LE "<="
%token GE ">="
%token LT "<"
%token GT ">"
%token ANCESTOR "ancestor"
%token ANCESTOR_OR_SELF "ancstor-or-self"
%token ATTRIBUTE "attribute"
%token CHILD "child"
%token DESCENDANT "descendant"
%token DESCENDANT_OR_SELF "descendant-or-self"
%token FOLLOWING "following"
%token FOLLOWING_SIBLING "sibling"
%token NAMESPACE "NameSpace"
%token PARENT "parent"
%token PRECEDING "preceding"
%token PRECEDING_SIBLING "preceding-sibling"
%token SELF "self"
%token COMMENT "comment"
%token TEXT "text"
%token PROCESSING_INSTRUCTION "processing-instruction"
%token NODE "node"
%token MULTIPLY "*"
%token NUMBER
%token LITERAL
%token QName
%start Expr
%left AND
%left OR
%left EQ
%left NE
%left LE
%left GE
%left LT
%left GT
%left DIV
%left MOD
%left PLUS
%left MINUS
%%
/* XSLT Pattern */
Pattern
: LocationPathPattern
| Pattern BAR LocationPathPattern
{
$$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
}
;
LocationPathPattern
: SLASH
{
$$ = new ExprRoot ();
}
| SLASH RelativePathPattern
{
$$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
}
| IdKeyPattern
| IdKeyPattern SLASH RelativePathPattern
{
$$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
}
| IdKeyPattern SLASH2 RelativePathPattern
{
$$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
}
| SLASH2 RelativePathPattern
{
$$ = new ExprSLASH2 (new ExprRoot (), (NodeSet) $2);
}
| RelativePathPattern
;
// to avoid context-sensitive tokenizer, I just reuse FUNCTION_NAME
IdKeyPattern
: FUNCTION_NAME PAREN_OPEN LITERAL PAREN_CLOSE
{
XmlQualifiedName name = (XmlQualifiedName) $1;
if (name.Name != "id" || name.Namespace != String.Empty)
throw new XPathException (String.Format ("Expected 'id' but got '{0}'", name));
$$ = ExprFunctionCall.Factory (name,
new FunctionArguments (
new ExprLiteral ((string) $3),
null),
Context);
}
| FUNCTION_NAME PAREN_OPEN LITERAL COMMA LITERAL PAREN_CLOSE
{
XmlQualifiedName name = (XmlQualifiedName) $1;
if (name.Name != "key" || name.Namespace != String.Empty)
throw new XPathException (String.Format ("Expected 'key' but got '{0}'", name));
$$ = Context.TryGetFunction (name,
new FunctionArguments (
new ExprLiteral ((string) $3),
new FunctionArguments (
new ExprLiteral ((string) $5),
null)));
}
;
RelativePathPattern
: StepPattern
| RelativePathPattern SLASH StepPattern
{
$$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
}
| RelativePathPattern SLASH2 StepPattern
{
$$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
}
;
StepPattern
: ChildOrAttributeAxisSpecifier NodeTest Predicates
{
$$ = CreateNodeTest ((Axes) $1, $2, (ArrayList) $3);
}
;
ChildOrAttributeAxisSpecifier
: AbbreviatedAxisSpecifier
| CHILD COLON2
{
$$ = Axes.Child;
}
| ATTRIBUTE COLON2
{
$$ = Axes.Attribute;
}
;
Predicates
: // empty
{
$$ = null;
}
| Predicates Predicate
{
ArrayList al = (ArrayList) $1;
if (al == null)
al = new ArrayList ();
al.Add ((Expression) $2);
$$ = al;
}
;
/* ---- end of XSLT Pattern ---- */
Expr
: OrExpr
;
OrExpr
: AndExpr
| OrExpr OR AndExpr
{
$$ = new ExprOR ((Expression) $1, (Expression) $3);
}
;
AndExpr
: EqualityExpr
| AndExpr AND EqualityExpr
{
$$ = new ExprAND ((Expression) $1, (Expression) $3);
}
;
EqualityExpr
: RelationalExpr
| EqualityExpr EQ RelationalExpr
{
$$ = new ExprEQ ((Expression) $1, (Expression) $3);
}
| EqualityExpr NE RelationalExpr
{
$$ = new ExprNE ((Expression) $1, (Expression) $3);
}
;
RelationalExpr
: AdditiveExpr
| RelationalExpr LT AdditiveExpr
{
$$ = new ExprLT ((Expression) $1, (Expression) $3);
}
| RelationalExpr GT AdditiveExpr
{
$$ = new ExprGT ((Expression) $1, (Expression) $3);
}
| RelationalExpr LE AdditiveExpr
{
$$ = new ExprLE ((Expression) $1, (Expression) $3);
}
| RelationalExpr GE AdditiveExpr
{
$$ = new ExprGE ((Expression) $1, (Expression) $3);
}
;
AdditiveExpr
: MultiplicativeExpr
| AdditiveExpr PLUS MultiplicativeExpr
{
$$ = new ExprPLUS ((Expression) $1, (Expression) $3);
}
| AdditiveExpr MINUS MultiplicativeExpr
{
$$ = new ExprMINUS ((Expression) $1, (Expression) $3);
}
;
MultiplicativeExpr
: UnaryExpr
| MultiplicativeExpr MULTIPLY UnaryExpr
{
$$ = new ExprMULT ((Expression) $1, (Expression) $3);
}
| MultiplicativeExpr DIV UnaryExpr
{
$$ = new ExprDIV ((Expression) $1, (Expression) $3);
}
| MultiplicativeExpr MOD UnaryExpr
{
$$ = new ExprMOD ((Expression) $1, (Expression) $3);
}
;
UnaryExpr
: UnionExpr
| MINUS UnaryExpr
{
$$ = new ExprNEG ((Expression) $2);
}
;
UnionExpr
: PathExpr
| UnionExpr BAR PathExpr
{
$$ = new ExprUNION ((Expression) $1, (Expression) $3);
}
;
PathExpr
: LocationPath
| FilterExpr
| FilterExpr SLASH RelativeLocationPath
{
$$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
}
| FilterExpr SLASH2 RelativeLocationPath
{
$$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
}
;
LocationPath
: RelativeLocationPath
| AbsoluteLocationPath
;
AbsoluteLocationPath
: SLASH
{
$$ = new ExprRoot ();
}
| SLASH RelativeLocationPath
{
$$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
}
| SLASH2 RelativeLocationPath
{
$$ = new ExprSLASH2 (new ExprRoot (), (NodeSet) $2);
}
;
RelativeLocationPath
: Step
| RelativeLocationPath SLASH Step
{
$$ = new ExprSLASH ((NodeSet) $1, (NodeSet) $3);
}
| RelativeLocationPath SLASH2 Step
{
$$ = new ExprSLASH2 ((NodeSet) $1, (NodeSet) $3);
}
;
Step
: AxisSpecifier NodeTest Predicates
{
$$ = CreateNodeTest ((Axes) $1, $2, (ArrayList) $3);
}
| AbbreviatedStep
;
NodeTest // QName, XPathNodeType or string
: NameTest
| NodeType PAREN_OPEN PAREN_CLOSE
{
$$ = (XPathNodeType) $1;
}
| PROCESSING_INSTRUCTION PAREN_OPEN OptionalLiteral PAREN_CLOSE
{
$$ = (string) $3;
}
;
NameTest
: ASTERISK
{
$$ = XmlQualifiedName.Empty;
}
| QName // token QName also contains "blah:*"
;
AbbreviatedStep
: DOT
{
$$ = new NodeTypeTest (Axes.Self, XPathNodeType.All);
}
| DOT2
{
$$ = new NodeTypeTest (Axes.Parent, XPathNodeType.All);
}
;
Predicates
: /* empty */
{
$$ = null;
}
| Predicates Predicate
{
ArrayList al = (ArrayList) $1;
if (al == null)
al = new ArrayList ();
al.Add ($2);
$$ = al;
}
;
AxisSpecifier
: AxisName COLON2
{
$$ = $1;
}
| AbbreviatedAxisSpecifier
;
AbbreviatedAxisSpecifier
: /* empty */
{
$$ = Axes.Child;
}
| AT
{
$$ = Axes.Attribute;
}
;
NodeType
: COMMENT { $$ = XPathNodeType.Comment; }
| TEXT { $$ = XPathNodeType.Text; }
| PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
| NODE { $$ = XPathNodeType.All; }
;
FilterExpr
: PrimaryExpr
| FilterExpr Predicate
{
$$ = new ExprFilter ((Expression) $1, (Expression) $2);
}
;
PrimaryExpr
: DOLLAR QName
{
Expression ret = null;
if (Context != null)
ret = Context.TryGetVariable (((XmlQualifiedName) $2).ToString ());
if (ret == null)
ret = new ExprVariable ((XmlQualifiedName) $2, Context);
$$ = ret;
}
| PAREN_OPEN Expr PAREN_CLOSE
{
$$ = new ExprParens ((Expression) $2);
}
| LITERAL
{
$$ = new ExprLiteral ((String) $1);
}
| NUMBER
{
$$ = new ExprNumber ((double) $1);
}
| FunctionCall
;
FunctionCall
: FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
{
Expression ret = null;
if (Context != null)
ret = Context.TryGetFunction ((XmlQualifiedName) $1, (FunctionArguments) $3);
if (ret == null)
ret = ExprFunctionCall.Factory ((XmlQualifiedName) $1, (FunctionArguments) $3, Context);
$$ = ret;
}
;
OptionalArgumentList
: /* empty */
| Expr OptionalArgumentListTail
{
$$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
}
;
OptionalArgumentListTail
: /* empty */
| COMMA Expr OptionalArgumentListTail
{
$$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
}
;
Predicate
: BRACKET_OPEN Expr BRACKET_CLOSE
{
$$ = $2;
}
;
AxisName
: ANCESTOR { $$ = Axes.Ancestor; }
| ANCESTOR_OR_SELF { $$ = Axes.AncestorOrSelf; }
| ATTRIBUTE { $$ = Axes.Attribute; }
| CHILD { $$ = Axes.Child; }
| DESCENDANT { $$ = Axes.Descendant; }
| DESCENDANT_OR_SELF { $$ = Axes.DescendantOrSelf; }
| FOLLOWING { $$ = Axes.Following; }
| FOLLOWING_SIBLING { $$ = Axes.FollowingSibling; }
| NAMESPACE { $$ = Axes.Namespace; }
| PARENT { $$ = Axes.Parent; }
| PRECEDING { $$ = Axes.Preceding; }
| PRECEDING_SIBLING { $$ = Axes.PrecedingSibling; }
| SELF { $$ = Axes.Self; }
;
OptionalLiteral
: /* empty */
| LITERAL
;
%%
}
|