opengl32: Fix some style issues.
[wine.git] / dlls / msxml3 / xslpattern.y
blob4e3b699e550b564a043069a8b2f150f7b694411c
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #ifdef HAVE_LIBXML2
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)
47 FIXME("%s:\n"
48 " param {\n"
49 " yyscanner=%p\n"
50 " ctx=%p\n"
51 " in=\"%s\"\n"
52 " pos=%i\n"
53 " len=%i\n"
54 " out=\"%s\"\n"
55 " err=%i\n"
56 " }\n"
57 " scanner=%p\n",
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
71 %start XSLPattern
73 %pure_parser
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
82 %expect 14
86 XSLPattern : Expr
88 p->out = $1;
92 /* Mostly verbatim from the w3c XML Namespaces standard.
93 * <http://www.w3.org/TR/REC-xml-names/> */
95 /* [4] Qualified Names */
96 QName : PrefixedName
97 | UnprefixedName
99 PrefixedName : TOK_NCName TOK_Colon TOK_NCName
101 TRACE("Got PrefixedName: \"%s:%s\"\n", $1, $3);
102 $$=$1;
103 $$=xmlStrcat($$,U(":"));
104 $$=xmlStrcat($$,$3);
105 xmlFree($3);
108 UnprefixedName : TOK_NCName
110 TRACE("Got UnprefixedName: \"%s\"\n", $1);
111 $$=$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("/"));
126 $$=xmlStrcat($$,$2);
127 xmlFree($2);
129 | TOK_FSlash
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);
140 $$=$1;
141 $$=xmlStrcat($$,U("/"));
142 $$=xmlStrcat($$,$3);
143 xmlFree($3);
145 | AbbreviatedRelativeLocationPath
147 /* [2.1] Location Steps */
148 Step : AxisSpecifier NodeTest Predicates
150 TRACE("Got Step: \"%s%s%s\"\n", $1, $2, $3);
151 $$=$1;
152 $$=xmlStrcat($$,$2);
153 xmlFree($2);
154 $$=xmlStrcat($$,$3);
155 xmlFree($3);
157 | NodeTest Predicates
159 TRACE("Got Step: \"%s%s\"\n", $1, $2);
160 $$=$1;
161 $$=xmlStrcat($$,$2);
162 xmlFree($2);
164 | AxisSpecifier NodeTest
166 TRACE("Got Step: \"%s%s\"\n", $1, $2);
167 $$=$1;
168 $$=xmlStrcat($$,$2);
169 xmlFree($2);
171 | NodeTest
172 | Attribute
173 | AbbreviatedStep
175 AxisSpecifier : TOK_NCName TOK_Axis
177 TRACE("Got AxisSpecifier: \"%s::\"\n", $1);
178 $$=$1;
179 $$=xmlStrcat($$,U("::"));
182 Attribute : '@' QName
184 TRACE("Got Attribute: \"@%s\"\n", $2);
185 $$=xmlStrdup(U("@"));
186 $$=xmlStrcat($$,$2);
187 xmlFree($2);
191 /* [2.3] Node Tests */
192 NodeTest : NameTest
193 | FunctionCall
195 NameTest : '*'
197 TRACE("Got NameTest: \"*\"\n");
198 $$=xmlStrdup(U("*"));
200 | TOK_NCName TOK_Colon '*'
202 TRACE("Got NameTest: \"%s:*\"\n", $1);
203 $$=$1;
204 $$=xmlStrcat($$,U(":*"));
206 | TOK_NCName TOK_Colon TOK_NCName
207 { /* PrefixedName */
208 xmlChar const* registeredNsURI = xmlXPathNsLookup(p->ctx, $1);
209 TRACE("Got PrefixedName: \"%s:%s\"\n", $1, $3);
211 if (registeredNsURI)
212 $$=xmlStrdup(U(""));
213 else
214 $$=xmlStrdup(NameTest_mod_pre);
216 $$=xmlStrcat($$,$1);
217 xmlFree($1);
218 $$=xmlStrcat($$,U(":"));
219 $$=xmlStrcat($$,$3);
220 xmlFree($3);
222 if (!registeredNsURI)
223 $$=xmlStrcat($$,NameTest_mod_post);
225 | UnprefixedName
227 $$=xmlStrdup(NameTest_mod_pre);
228 $$=xmlStrcat($$,$1);
229 xmlFree($1);
230 $$=xmlStrcat($$,NameTest_mod_post);
232 /* [2.4] Predicates */
233 Predicates : Predicates Predicate
235 $$=$1;
236 $$=xmlStrcat($$,$2);
237 xmlFree($2);
239 | Predicate
241 Predicate : '[' PredicateExpr ']'
243 TRACE("Got Predicate: \"[%s]\"\n", $2);
244 $$=xmlStrdup(U("["));
245 $$=xmlStrcat($$,$2);
246 xmlFree($2);
247 $$=xmlStrcat($$,U("]"));
250 PredicateExpr : TOK_Number
252 $$=xmlStrdup(U("index()="));
253 $$=xmlStrcat($$,$1);
254 xmlFree($1);
256 | BoolExpr
257 | Attribute
258 | TOK_NCName
260 /* [2.5] Abbreviated Syntax */
261 AbbreviatedAbsoluteLocationPath : TOK_DblFSlash RelativeLocationPath
263 TRACE("Got AbbreviatedAbsoluteLocationPath: \"//%s\"\n", $2);
264 $$=xmlStrdup(U("//"));
265 $$=xmlStrcat($$,$2);
266 xmlFree($2);
269 AbbreviatedRelativeLocationPath : RelativeLocationPath TOK_DblFSlash Step
271 TRACE("Got AbbreviatedRelativeLocationPath: \"%s//%s\"\n", $1, $3);
272 $$=$1;
273 $$=xmlStrcat($$,U("//"));
274 $$=xmlStrcat($$,$3);
275 xmlFree($3);
278 AbbreviatedStep : TOK_Parent
280 TRACE("Got AbbreviatedStep: \"..\"\n");
281 $$=xmlStrdup(U(".."));
283 | TOK_Self
285 TRACE("Got AbbreviatedStep: \".\"\n");
286 $$=xmlStrdup(U("."));
290 /* [3] Expressions */
291 /* [3.1] Basics */
292 Expr : OrExpr
294 BoolExpr : FunctionCall
295 | BoolUnaryExpr
296 | BoolRelationalExpr
297 | BoolEqualityExpr
298 | BoolAndExpr
299 | BoolOrExpr
301 PrimaryExpr : '(' Expr ')'
303 TRACE("Got PrimaryExpr: \"(%s)\"\n", $1);
304 $$=xmlStrdup(U("("));
305 $$=xmlStrcat($$,$2);
306 xmlFree($2);
307 $$=xmlStrcat($$,U(")"));
309 | PathExpr '!' FunctionCall
311 TRACE("Got PrimaryExpr: \"%s!%s\"\n", $1, $3);
312 $$=$1;
313 $$=xmlStrcat($$,U("/"));
314 $$=xmlStrcat($$,$3);
315 xmlFree($3);
317 | TOK_Literal
318 | TOK_Number
320 /* [3.2] Function Calls */
321 FunctionCall : QName '(' Arguments ')'
323 TRACE("Got FunctionCall: \"%s(%s)\"\n", $1, $3);
324 if (xmlStrEqual($1,U("ancestor")))
326 $$=$1;
327 $$=xmlStrcat($$,U("::"));
328 $$=xmlStrcat($$,$3);
329 xmlFree($3);
331 else if (xmlStrEqual($1,U("attribute")))
333 if (is_literal($3))
335 $$=xmlStrdup(U("@*[name()="));
336 xmlFree($1);
337 $$=xmlStrcat($$,$3);
338 xmlFree($3);
339 $$=xmlStrcat($$,U("]"));
341 else
343 /* XML_XPATH_INVALID_TYPE */
344 $$=xmlStrdup(U("error(1211, 'Error: attribute("));
345 xmlFree($1);
346 $$=xmlStrcat($$,$3);
347 xmlFree($3);
348 $$=xmlStrcat($$,U("): invalid argument')"));
351 else if (xmlStrEqual($1,U("element")))
353 if (is_literal($3))
355 $$=xmlStrdup(U("node()[nodeType()=1][name()="));
356 xmlFree($1);
357 $$=xmlStrcat($$,$3);
358 xmlFree($3);
359 $$=xmlStrcat($$,U("]"));
361 else
363 /* XML_XPATH_INVALID_TYPE */
364 $$=xmlStrdup(U("error(1211, 'Error: element("));
365 xmlFree($1);
366 $$=xmlStrcat($$,$3);
367 xmlFree($3);
368 $$=xmlStrcat($$,U("): invalid argument')"));
371 else
373 $$=$1;
374 $$=xmlStrcat($$,U("("));
375 $$=xmlStrcat($$,$3);
376 xmlFree($3);
377 $$=xmlStrcat($$,U(")"));
380 | QName '(' ')'
382 TRACE("Got FunctionCall: \"%s()\"\n", $1);
383 /* comment() & node() work the same in XPath */
384 if (xmlStrEqual($1,U("attribute")))
386 $$=xmlStrdup(U("@*"));
387 xmlFree($1);
389 else if (xmlStrEqual($1,U("element")))
391 $$=xmlStrdup(U("node()[nodeType()=1]"));
392 xmlFree($1);
394 else if (xmlStrEqual($1,U("pi")))
396 $$=xmlStrdup(U("processing-instruction()"));
397 xmlFree($1);
399 else if (xmlStrEqual($1,U("textnode")))
401 $$=xmlStrdup(U("text()"));
402 xmlFree($1);
404 else
406 $$=$1;
407 $$=xmlStrcat($$,U("()"));
411 Arguments : Argument ',' Arguments
413 $$=$1;
414 $$=xmlStrcat($$,U(","));
415 $$=xmlStrcat($$,$3);
416 xmlFree($3);
418 | Argument
420 Argument : Expr
422 /* [3.3] Node-sets */
423 UnionExpr : PathExpr
424 | UnionExpr '|' PathExpr
426 TRACE("Got UnionExpr: \"%s|%s\"\n", $1, $3);
427 $$=$1;
428 $$=xmlStrcat($$,U("|"));
429 $$=xmlStrcat($$,$3);
430 xmlFree($3);
433 PathExpr : LocationPath
434 | FilterExpr TOK_FSlash RelativeLocationPath
436 TRACE("Got PathExpr: \"%s/%s\"\n", $1, $3);
437 $$=$1;
438 $$=xmlStrcat($$,U("/"));
439 $$=xmlStrcat($$,$3);
440 xmlFree($3);
442 | FilterExpr TOK_DblFSlash RelativeLocationPath
444 TRACE("Got PathExpr: \"%s//%s\"\n", $1, $3);
445 $$=$1;
446 $$=xmlStrcat($$,U("//"));
447 $$=xmlStrcat($$,$3);
448 xmlFree($3);
450 | FilterExpr
452 FilterExpr : PrimaryExpr
453 | FilterExpr Predicate
455 TRACE("Got FilterExpr: \"%s%s\"\n", $1, $2);
456 $$=$1;
457 $$=xmlStrcat($$,$2);
458 xmlFree($2);
461 /* [3.4] Booleans */
462 OrExpr : AndExpr
463 | BoolOrExpr
465 BoolOrExpr : OrExpr TOK_OpOr AndExpr
467 TRACE("Got OrExpr: \"%s $or$ %s\"\n", $1, $3);
468 $$=$1;
469 $$=xmlStrcat($$,U(" or "));
470 $$=xmlStrcat($$,$3);
471 xmlFree($3);
474 AndExpr : EqualityExpr
475 | BoolAndExpr
477 BoolAndExpr : AndExpr TOK_OpAnd EqualityExpr
479 TRACE("Got AndExpr: \"%s $and$ %s\"\n", $1, $3);
480 $$=$1;
481 $$=xmlStrcat($$,U(" and "));
482 $$=xmlStrcat($$,$3);
483 xmlFree($3);
486 EqualityExpr : RelationalExpr
487 | BoolEqualityExpr
489 BoolEqualityExpr : EqualityExpr TOK_OpEq RelationalExpr
491 TRACE("Got EqualityExpr: \"%s $eq$ %s\"\n", $1, $3);
492 $$=$1;
493 $$=xmlStrcat($$,U("="));
494 $$=xmlStrcat($$,$3);
495 xmlFree($3);
497 | EqualityExpr TOK_OpIEq RelationalExpr
499 TRACE("Got EqualityExpr: \"%s $ieq$ %s\"\n", $1, $3);
500 $$=xmlStrdup(U("OP_IEq("));
501 $$=xmlStrcat($$,$1);
502 xmlFree($1);
503 $$=xmlStrcat($$,U(","));
504 $$=xmlStrcat($$,$3);
505 xmlFree($3);
506 $$=xmlStrcat($$,U(")"));
508 | EqualityExpr TOK_OpNEq RelationalExpr
510 TRACE("Got EqualityExpr: \"%s $ne$ %s\"\n", $1, $3);
511 $$=$1;
512 $$=xmlStrcat($$,U("!="));
513 $$=xmlStrcat($$,$3);
514 xmlFree($3);
516 | EqualityExpr TOK_OpINEq RelationalExpr
518 TRACE("Got EqualityExpr: \"%s $ine$ %s\"\n", $1, $3);
519 $$=xmlStrdup(U("OP_INEq("));
520 $$=xmlStrcat($$,$1);
521 xmlFree($1);
522 $$=xmlStrcat($$,U(","));
523 $$=xmlStrcat($$,$3);
524 xmlFree($3);
525 $$=xmlStrcat($$,U(")"));
528 RelationalExpr : UnaryExpr
529 | BoolRelationalExpr
531 BoolRelationalExpr : RelationalExpr TOK_OpLt UnaryExpr
533 TRACE("Got RelationalExpr: \"%s $lt$ %s\"\n", $1, $3);
534 $$=$1;
535 $$=xmlStrcat($$,U("<"));
536 $$=xmlStrcat($$,$3);
537 xmlFree($3);
539 | RelationalExpr TOK_OpILt UnaryExpr
541 TRACE("Got RelationalExpr: \"%s $ilt$ %s\"\n", $1, $3);
542 $$=xmlStrdup(U("OP_ILt("));
543 $$=xmlStrcat($$,$1);
544 xmlFree($1);
545 $$=xmlStrcat($$,U(","));
546 $$=xmlStrcat($$,$3);
547 xmlFree($3);
548 $$=xmlStrcat($$,U(")"));
550 | RelationalExpr TOK_OpGt UnaryExpr
552 TRACE("Got RelationalExpr: \"%s $gt$ %s\"\n", $1, $3);
553 $$=$1;
554 $$=xmlStrcat($$,U(">"));
555 $$=xmlStrcat($$,$3);
556 xmlFree($3);
558 | RelationalExpr TOK_OpIGt UnaryExpr
560 TRACE("Got RelationalExpr: \"%s $igt$ %s\"\n", $1, $3);
561 $$=xmlStrdup(U("OP_IGt("));
562 $$=xmlStrcat($$,$1);
563 xmlFree($1);
564 $$=xmlStrcat($$,U(","));
565 $$=xmlStrcat($$,$3);
566 xmlFree($3);
567 $$=xmlStrcat($$,U(")"));
569 | RelationalExpr TOK_OpLEq UnaryExpr
571 TRACE("Got RelationalExpr: \"%s $le$ %s\"\n", $1, $3);
572 $$=$1;
573 $$=xmlStrcat($$,U("<="));
574 $$=xmlStrcat($$,$3);
575 xmlFree($3);
577 | RelationalExpr TOK_OpILEq UnaryExpr
579 TRACE("Got RelationalExpr: \"%s $ile$ %s\"\n", $1, $3);
580 $$=xmlStrdup(U("OP_ILEq("));
581 $$=xmlStrcat($$,$1);
582 xmlFree($1);
583 $$=xmlStrcat($$,U(","));
584 $$=xmlStrcat($$,$3);
585 xmlFree($3);
586 $$=xmlStrcat($$,U(")"));
588 | RelationalExpr TOK_OpGEq UnaryExpr
590 TRACE("Got RelationalExpr: \"%s $ge$ %s\"\n", $1, $3);
591 $$=$1;
592 $$=xmlStrcat($$,U(">="));
593 $$=xmlStrcat($$,$3);
594 xmlFree($3);
596 | RelationalExpr TOK_OpIGEq UnaryExpr
598 TRACE("Got RelationalExpr: \"%s $ige$ %s\"\n", $1, $3);
599 $$=xmlStrdup(U("OP_IGEq("));
600 $$=xmlStrcat($$,$1);
601 xmlFree($1);
602 $$=xmlStrcat($$,U(","));
603 $$=xmlStrcat($$,$3);
604 xmlFree($3);
605 $$=xmlStrcat($$,U(")"));
609 /* [3.5] Numbers */
610 UnaryExpr : UnionExpr
611 | BoolUnaryExpr
613 BoolUnaryExpr : TOK_OpNot UnaryExpr
615 TRACE("Got UnaryExpr: \"$not$ %s\"\n", $2);
616 $$=xmlStrdup(U(" not("));
617 $$=xmlStrcat($$,$2);
618 xmlFree($2);
619 $$=xmlStrcat($$,U(")"));
621 | TOK_OpAny Expr
623 TRACE("Got UnaryExpr: \"$any$ %s\"\n", $2);
624 $$=xmlStrdup(U("boolean("));
625 $$=xmlStrcat($$,$2);
626 xmlFree($2);
627 $$=xmlStrcat($$,U(")"));
629 | TOK_OpAll AllExpr
631 TRACE("Got UnaryExpr: \"$all$ %s\"\n", $2);
632 $$=xmlStrdup(U("not("));
633 $$=xmlStrcat($$,$2);
634 xmlFree($2);
635 $$=xmlStrcat($$,U(")"));
637 | TOK_OpAll
639 FIXME("Unrecognized $all$ expression - ignoring\n");
640 $$=xmlStrdup(U(""));
643 AllExpr : PathExpr TOK_OpEq PathExpr
645 $$=$1;
646 $$=xmlStrcat($$,U("!="));
647 $$=xmlStrcat($$,$3);
648 xmlFree($3);
650 | PathExpr TOK_OpNEq PathExpr
652 $$=$1;
653 $$=xmlStrcat($$,U("="));
654 $$=xmlStrcat($$,$3);
655 xmlFree($3);
657 | PathExpr TOK_OpLt PathExpr
659 $$=$1;
660 $$=xmlStrcat($$,U(">="));
661 $$=xmlStrcat($$,$3);
662 xmlFree($3);
664 | PathExpr TOK_OpLEq PathExpr
666 $$=$1;
667 $$=xmlStrcat($$,U(">"));
668 $$=xmlStrcat($$,$3);
669 xmlFree($3);
671 | PathExpr TOK_OpGt PathExpr
673 $$=$1;
674 $$=xmlStrcat($$,U("<="));
675 $$=xmlStrcat($$,$3);
676 xmlFree($3);
678 | PathExpr TOK_OpGEq PathExpr
680 $$=$1;
681 $$=xmlStrcat($$,U("<"));
682 $$=xmlStrcat($$,$3);
683 xmlFree($3);
685 | PathExpr TOK_OpIEq PathExpr
687 $$=xmlStrdup(U("OP_INEq("));
688 $$=xmlStrcat($$,$1);
689 xmlFree($1);
690 $$=xmlStrcat($$,U(","));
691 $$=xmlStrcat($$,$3);
692 xmlFree($3);
693 $$=xmlStrcat($$,U(")"));
695 | PathExpr TOK_OpINEq PathExpr
697 $$=xmlStrdup(U("OP_IEq("));
698 $$=xmlStrcat($$,$1);
699 xmlFree($1);
700 $$=xmlStrcat($$,U(","));
701 $$=xmlStrcat($$,$3);
702 xmlFree($3);
703 $$=xmlStrcat($$,U(")"));
705 | PathExpr TOK_OpILt PathExpr
707 $$=xmlStrdup(U("OP_IGEq("));
708 $$=xmlStrcat($$,$1);
709 xmlFree($1);
710 $$=xmlStrcat($$,U(","));
711 $$=xmlStrcat($$,$3);
712 xmlFree($3);
713 $$=xmlStrcat($$,U(")"));
715 | PathExpr TOK_OpILEq PathExpr
717 $$=xmlStrdup(U("OP_IGt("));
718 $$=xmlStrcat($$,$1);
719 xmlFree($1);
720 $$=xmlStrcat($$,U(","));
721 $$=xmlStrcat($$,$3);
722 xmlFree($3);
723 $$=xmlStrcat($$,U(")"));
725 | PathExpr TOK_OpIGt PathExpr
727 $$=xmlStrdup(U("OP_ILEq("));
728 $$=xmlStrcat($$,$1);
729 xmlFree($1);
730 $$=xmlStrcat($$,U(","));
731 $$=xmlStrcat($$,$3);
732 xmlFree($3);
733 $$=xmlStrcat($$,U(")"));
735 | PathExpr TOK_OpIGEq PathExpr
737 $$=xmlStrdup(U("OP_ILt("));
738 $$=xmlStrcat($$,$1);
739 xmlFree($1);
740 $$=xmlStrcat($$,U(","));
741 $$=xmlStrcat($$,$3);
742 xmlFree($3);
743 $$=xmlStrcat($$,U(")"));
749 #endif /* HAVE_LIBXML2 */