2 * XSLPattern parser (XSLPattern => XPath)
4 * Copyright 2010 Adam Martinson for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
26 #include "xslpattern.h"
27 #include <libxml/xpathInternals.h>
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
33 static const xmlChar NameTest_mod_pre[] = "*[name()='";
34 static const xmlChar NameTest_mod_post[] = "']";
36 #define U(str) BAD_CAST str
38 static inline BOOL is_literal(xmlChar const* tok)
40 return (tok && tok[0] && tok[1] &&
41 tok[0]== tok[xmlStrlen(tok)-1] &&
42 (tok[0] == '\'' || tok[0] == '"'));
45 static void xslpattern_error(parser_param* param, void const* scanner, char const* msg)
58 msg, param->yyscanner, param->ctx, param->in, param->pos,
59 param->len, param->out, ++param->err, scanner);
64 %token TOK_Parent TOK_Self TOK_DblFSlash TOK_FSlash TOK_Axis TOK_Colon
65 %token TOK_OpAnd TOK_OpOr TOK_OpNot
66 %token TOK_OpEq TOK_OpIEq TOK_OpNEq TOK_OpINEq
67 %token TOK_OpLt TOK_OpILt TOK_OpGt TOK_OpIGt TOK_OpLEq TOK_OpILEq TOK_OpGEq TOK_OpIGEq
68 %token TOK_OpAll TOK_OpAny
69 %token TOK_NCName TOK_Literal TOK_Number
74 %parse-param {parser_param* p}
75 %parse-param {void* scanner}
76 %lex-param {yyscan_t* scanner}
78 %left TOK_OpAnd TOK_OpOr
79 %left TOK_OpEq TOK_OpIEq TOK_OpNEq TOK_OpINEq
80 %left TOK_OpLt TOK_OpILt TOK_OpGt TOK_OpIGt TOK_OpLEq TOK_OpILEq TOK_OpGEq TOK_OpIGEq
92 /* Mostly verbatim from the w3c XML Namespaces standard.
93 * <http://www.w3.org/TR/REC-xml-names/> */
95 /* [4] Qualified Names */
99 PrefixedName : TOK_NCName TOK_Colon TOK_NCName
101 TRACE("Got PrefixedName: \"%s:%s\"\n", $1, $3);
103 $$=xmlStrcat($$,U(":"));
108 UnprefixedName : TOK_NCName
110 TRACE("Got UnprefixedName: \"%s\"\n", $1);
115 /* Based on the w3c XPath standard, adapted where needed.
116 * <http://www.w3.org/TR/xpath/> */
118 /* [2] Location Paths */
119 LocationPath : RelativeLocationPath
120 | AbsoluteLocationPath
122 AbsoluteLocationPath : TOK_FSlash RelativeLocationPath
124 TRACE("Got AbsoluteLocationPath: \"/%s\"\n", $2);
125 $$=xmlStrdup(U("/"));
131 TRACE("Got AbsoluteLocationPath: \"/\"\n");
132 $$=xmlStrdup(U("/"));
134 | AbbreviatedAbsoluteLocationPath
136 RelativeLocationPath : Step
137 | RelativeLocationPath TOK_FSlash Step
139 TRACE("Got RelativeLocationPath: \"%s/%s\"\n", $1, $3);
141 $$=xmlStrcat($$,U("/"));
145 | AbbreviatedRelativeLocationPath
147 /* [2.1] Location Steps */
148 Step : AxisSpecifier NodeTest Predicates
150 TRACE("Got Step: \"%s%s%s\"\n", $1, $2, $3);
157 | NodeTest Predicates
159 TRACE("Got Step: \"%s%s\"\n", $1, $2);
164 | AxisSpecifier NodeTest
166 TRACE("Got Step: \"%s%s\"\n", $1, $2);
175 AxisSpecifier : TOK_NCName TOK_Axis
177 TRACE("Got AxisSpecifier: \"%s::\"\n", $1);
179 $$=xmlStrcat($$,U("::"));
182 Attribute : '@' QName
184 TRACE("Got Attribute: \"@%s\"\n", $2);
185 $$=xmlStrdup(U("@"));
191 /* [2.3] Node Tests */
197 TRACE("Got NameTest: \"*\"\n");
198 $$=xmlStrdup(U("*"));
200 | TOK_NCName TOK_Colon '*'
202 TRACE("Got NameTest: \"%s:*\"\n", $1);
204 $$=xmlStrcat($$,U(":*"));
206 | TOK_NCName TOK_Colon TOK_NCName
208 xmlChar const* registeredNsURI = xmlXPathNsLookup(p->ctx, $1);
209 TRACE("Got PrefixedName: \"%s:%s\"\n", $1, $3);
214 $$=xmlStrdup(NameTest_mod_pre);
218 $$=xmlStrcat($$,U(":"));
222 if (!registeredNsURI)
223 $$=xmlStrcat($$,NameTest_mod_post);
227 $$=xmlStrdup(NameTest_mod_pre);
230 $$=xmlStrcat($$,NameTest_mod_post);
232 /* [2.4] Predicates */
233 Predicates : Predicates Predicate
241 Predicate : '[' PredicateExpr ']'
243 TRACE("Got Predicate: \"[%s]\"\n", $2);
244 $$=xmlStrdup(U("["));
247 $$=xmlStrcat($$,U("]"));
250 PredicateExpr : TOK_Number
252 $$=xmlStrdup(U("index()="));
259 /* [2.5] Abbreviated Syntax */
260 AbbreviatedAbsoluteLocationPath : TOK_DblFSlash RelativeLocationPath
262 TRACE("Got AbbreviatedAbsoluteLocationPath: \"//%s\"\n", $2);
263 $$=xmlStrdup(U("//"));
268 AbbreviatedRelativeLocationPath : RelativeLocationPath TOK_DblFSlash Step
270 TRACE("Got AbbreviatedRelativeLocationPath: \"%s//%s\"\n", $1, $3);
272 $$=xmlStrcat($$,U("//"));
277 AbbreviatedStep : TOK_Parent
279 TRACE("Got AbbreviatedStep: \"..\"\n");
280 $$=xmlStrdup(U(".."));
284 TRACE("Got AbbreviatedStep: \".\"\n");
285 $$=xmlStrdup(U("."));
289 /* [3] Expressions */
293 BoolExpr : FunctionCall
300 PrimaryExpr : '(' Expr ')'
302 TRACE("Got PrimaryExpr: \"(%s)\"\n", $1);
303 $$=xmlStrdup(U("("));
306 $$=xmlStrcat($$,U(")"));
308 | PathExpr '!' FunctionCall
310 TRACE("Got PrimaryExpr: \"%s!%s\"\n", $1, $3);
312 $$=xmlStrcat($$,U("/"));
319 /* [3.2] Function Calls */
320 FunctionCall : QName '(' Arguments ')'
322 TRACE("Got FunctionCall: \"%s(%s)\"\n", $1, $3);
323 if (xmlStrEqual($1,U("ancestor")))
326 $$=xmlStrcat($$,U("::"));
330 else if (xmlStrEqual($1,U("attribute")))
334 $$=xmlStrdup(U("@*[name()="));
338 $$=xmlStrcat($$,U("]"));
342 /* XML_XPATH_INVALID_TYPE */
343 $$=xmlStrdup(U("error(1211, 'Error: attribute("));
347 $$=xmlStrcat($$,U("): invalid argument')"));
350 else if (xmlStrEqual($1,U("element")))
354 $$=xmlStrdup(U("node()[nodeType()=1][name()="));
358 $$=xmlStrcat($$,U("]"));
362 /* XML_XPATH_INVALID_TYPE */
363 $$=xmlStrdup(U("error(1211, 'Error: element("));
367 $$=xmlStrcat($$,U("): invalid argument')"));
373 $$=xmlStrcat($$,U("("));
376 $$=xmlStrcat($$,U(")"));
381 TRACE("Got FunctionCall: \"%s()\"\n", $1);
382 /* comment() & node() work the same in XPath */
383 if (xmlStrEqual($1,U("attribute")))
385 $$=xmlStrdup(U("@*"));
388 else if (xmlStrEqual($1,U("element")))
390 $$=xmlStrdup(U("node()[nodeType()=1]"));
393 else if (xmlStrEqual($1,U("pi")))
395 $$=xmlStrdup(U("processing-instruction()"));
398 else if (xmlStrEqual($1,U("textnode")))
400 $$=xmlStrdup(U("text()"));
406 $$=xmlStrcat($$,U("()"));
410 Arguments : Argument ',' Arguments
413 $$=xmlStrcat($$,U(","));
421 /* [3.3] Node-sets */
423 | UnionExpr '|' PathExpr
425 TRACE("Got UnionExpr: \"%s|%s\"\n", $1, $3);
427 $$=xmlStrcat($$,U("|"));
432 PathExpr : LocationPath
433 | FilterExpr TOK_FSlash RelativeLocationPath
435 TRACE("Got PathExpr: \"%s/%s\"\n", $1, $3);
437 $$=xmlStrcat($$,U("/"));
441 | FilterExpr TOK_DblFSlash RelativeLocationPath
443 TRACE("Got PathExpr: \"%s//%s\"\n", $1, $3);
445 $$=xmlStrcat($$,U("//"));
451 FilterExpr : PrimaryExpr
452 | FilterExpr Predicate
454 TRACE("Got FilterExpr: \"%s%s\"\n", $1, $2);
464 BoolOrExpr : OrExpr TOK_OpOr AndExpr
466 TRACE("Got OrExpr: \"%s $or$ %s\"\n", $1, $3);
468 $$=xmlStrcat($$,U(" or "));
473 AndExpr : EqualityExpr
476 BoolAndExpr : AndExpr TOK_OpAnd EqualityExpr
478 TRACE("Got AndExpr: \"%s $and$ %s\"\n", $1, $3);
480 $$=xmlStrcat($$,U(" and "));
485 EqualityExpr : RelationalExpr
488 BoolEqualityExpr : EqualityExpr TOK_OpEq RelationalExpr
490 TRACE("Got EqualityExpr: \"%s $eq$ %s\"\n", $1, $3);
492 $$=xmlStrcat($$,U("="));
496 | EqualityExpr TOK_OpIEq RelationalExpr
498 TRACE("Got EqualityExpr: \"%s $ieq$ %s\"\n", $1, $3);
499 $$=xmlStrdup(U("OP_IEq("));
502 $$=xmlStrcat($$,U(","));
505 $$=xmlStrcat($$,U(")"));
507 | EqualityExpr TOK_OpNEq RelationalExpr
509 TRACE("Got EqualityExpr: \"%s $ne$ %s\"\n", $1, $3);
511 $$=xmlStrcat($$,U("!="));
515 | EqualityExpr TOK_OpINEq RelationalExpr
517 TRACE("Got EqualityExpr: \"%s $ine$ %s\"\n", $1, $3);
518 $$=xmlStrdup(U("OP_INEq("));
521 $$=xmlStrcat($$,U(","));
524 $$=xmlStrcat($$,U(")"));
527 RelationalExpr : UnaryExpr
530 BoolRelationalExpr : RelationalExpr TOK_OpLt UnaryExpr
532 TRACE("Got RelationalExpr: \"%s $lt$ %s\"\n", $1, $3);
534 $$=xmlStrcat($$,U("<"));
538 | RelationalExpr TOK_OpILt UnaryExpr
540 TRACE("Got RelationalExpr: \"%s $ilt$ %s\"\n", $1, $3);
541 $$=xmlStrdup(U("OP_ILt("));
544 $$=xmlStrcat($$,U(","));
547 $$=xmlStrcat($$,U(")"));
549 | RelationalExpr TOK_OpGt UnaryExpr
551 TRACE("Got RelationalExpr: \"%s $gt$ %s\"\n", $1, $3);
553 $$=xmlStrcat($$,U(">"));
557 | RelationalExpr TOK_OpIGt UnaryExpr
559 TRACE("Got RelationalExpr: \"%s $igt$ %s\"\n", $1, $3);
560 $$=xmlStrdup(U("OP_IGt("));
563 $$=xmlStrcat($$,U(","));
566 $$=xmlStrcat($$,U(")"));
568 | RelationalExpr TOK_OpLEq UnaryExpr
570 TRACE("Got RelationalExpr: \"%s $le$ %s\"\n", $1, $3);
572 $$=xmlStrcat($$,U("<="));
576 | RelationalExpr TOK_OpILEq UnaryExpr
578 TRACE("Got RelationalExpr: \"%s $ile$ %s\"\n", $1, $3);
579 $$=xmlStrdup(U("OP_ILEq("));
582 $$=xmlStrcat($$,U(","));
585 $$=xmlStrcat($$,U(")"));
587 | RelationalExpr TOK_OpGEq UnaryExpr
589 TRACE("Got RelationalExpr: \"%s $ge$ %s\"\n", $1, $3);
591 $$=xmlStrcat($$,U(">="));
595 | RelationalExpr TOK_OpIGEq UnaryExpr
597 TRACE("Got RelationalExpr: \"%s $ige$ %s\"\n", $1, $3);
598 $$=xmlStrdup(U("OP_IGEq("));
601 $$=xmlStrcat($$,U(","));
604 $$=xmlStrcat($$,U(")"));
609 UnaryExpr : UnionExpr
612 BoolUnaryExpr : TOK_OpNot UnaryExpr
614 TRACE("Got UnaryExpr: \"$not$ %s\"\n", $2);
615 $$=xmlStrdup(U(" not("));
618 $$=xmlStrcat($$,U(")"));
622 TRACE("Got UnaryExpr: \"$any$ %s\"\n", $2);
623 $$=xmlStrdup(U("boolean("));
626 $$=xmlStrcat($$,U(")"));
630 TRACE("Got UnaryExpr: \"$all$ %s\"\n", $2);
631 $$=xmlStrdup(U("not("));
634 $$=xmlStrcat($$,U(")"));
638 FIXME("Unrecognized $all$ expression - ignoring\n");
642 AllExpr : PathExpr TOK_OpEq PathExpr
645 $$=xmlStrcat($$,U("!="));
649 | PathExpr TOK_OpNEq PathExpr
652 $$=xmlStrcat($$,U("="));
656 | PathExpr TOK_OpLt PathExpr
659 $$=xmlStrcat($$,U(">="));
663 | PathExpr TOK_OpLEq PathExpr
666 $$=xmlStrcat($$,U(">"));
670 | PathExpr TOK_OpGt PathExpr
673 $$=xmlStrcat($$,U("<="));
677 | PathExpr TOK_OpGEq PathExpr
680 $$=xmlStrcat($$,U("<"));
684 | PathExpr TOK_OpIEq PathExpr
686 $$=xmlStrdup(U("OP_INEq("));
689 $$=xmlStrcat($$,U(","));
692 $$=xmlStrcat($$,U(")"));
694 | PathExpr TOK_OpINEq PathExpr
696 $$=xmlStrdup(U("OP_IEq("));
699 $$=xmlStrcat($$,U(","));
702 $$=xmlStrcat($$,U(")"));
704 | PathExpr TOK_OpILt PathExpr
706 $$=xmlStrdup(U("OP_IGEq("));
709 $$=xmlStrcat($$,U(","));
712 $$=xmlStrcat($$,U(")"));
714 | PathExpr TOK_OpILEq PathExpr
716 $$=xmlStrdup(U("OP_IGt("));
719 $$=xmlStrcat($$,U(","));
722 $$=xmlStrcat($$,U(")"));
724 | PathExpr TOK_OpIGt PathExpr
726 $$=xmlStrdup(U("OP_ILEq("));
729 $$=xmlStrcat($$,U(","));
732 $$=xmlStrcat($$,U(")"));
734 | PathExpr TOK_OpIGEq PathExpr
736 $$=xmlStrdup(U("OP_ILt("));
739 $$=xmlStrcat($$,U(","));
742 $$=xmlStrcat($$,U(")"));
748 #endif /* HAVE_LIBXML2 */