2 * Copyright 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/xml/XPathFunctions.h"
33 #include "core/xml/XPathNSResolver.h"
34 #include "core/xml/XPathParser.h"
35 #include "core/xml/XPathPath.h"
36 #include "core/xml/XPathPredicate.h"
37 #include "core/xml/XPathStep.h"
38 #include "core/xml/XPathVariableReference.h"
39 #include "wtf/FastMalloc.h"
41 #define YYMALLOC fastMalloc
42 #define YYFREE fastFree
44 #define YYENABLE_NLS 0
45 #define YYLTYPE_IS_TRIVIAL 1
47 #define YYMAXDEPTH 10000
49 using namespace blink
;
50 using namespace XPath
;
55 %parse
-param
{ blink
::XPath
::Parser
* parser
}
59 blink
::XPath
::Step
::Axis axis
;
60 blink
::XPath
::Step
::NodeTest
* nodeTest
;
61 blink
::XPath
::NumericOp
::Opcode numop
;
62 blink
::XPath
::EqTestOp
::Opcode eqop
;
64 blink
::XPath
::Expression
* expr
;
65 blink
::HeapVector
<blink
::Member
<blink
::XPath
::Predicate
>>* predList
;
66 blink
::HeapVector
<blink
::Member
<blink
::XPath
::Expression
>>* argList
;
67 blink
::XPath
::Step
* step
;
68 blink
::XPath
::LocationPath
* locationPath
;
73 static int xpathyylex
(YYSTYPE* yylval) { return Parser
::current
()->lex
(yylval); }
74 static void xpathyyerror
(void*, const char*) { }
79 %left
<eqop
> EQOP RELOP
82 %token
<axis
> AXISNAME
83 %token
<str
> NODETYPE PI FUNCTIONNAME LITERAL
84 %token
<str
> VARIABLEREFERENCE NUMBER
85 %token DOTDOT SLASHSLASH
89 %type
<locationPath
> LocationPath
90 %type
<locationPath
> AbsoluteLocationPath
91 %type
<locationPath
> RelativeLocationPath
93 %type
<axis
> AxisSpecifier
94 %type
<step
> DescendantOrSelf
95 %type
<nodeTest
> NodeTest
96 %type
<expr
> Predicate
97 %type
<predList
> OptionalPredicateList
98 %type
<predList
> PredicateList
99 %type
<step
> AbbreviatedStep
101 %type
<expr
> PrimaryExpr
102 %type
<expr
> FunctionCall
103 %type
<argList
> ArgumentList
104 %type
<expr
> Argument
105 %type
<expr
> UnionExpr
106 %type
<expr
> PathExpr
107 %type
<expr
> FilterExpr
110 %type
<expr
> EqualityExpr
111 %type
<expr
> RelationalExpr
112 %type
<expr
> AdditiveExpr
113 %type
<expr
> MultiplicativeExpr
114 %type
<expr
> UnaryExpr
121 parser
->m_topExpr
= $1;
128 $$
->setAbsolute
(false
);
133 $$
->setAbsolute
(true
);
137 AbsoluteLocationPath:
140 $$
= new LocationPath
;
143 '/' RelativeLocationPath
148 DescendantOrSelf RelativeLocationPath
151 $$
->insertFirstStep
($1);
155 RelativeLocationPath:
158 $$
= new LocationPath
;
162 RelativeLocationPath
'/' Step
167 RelativeLocationPath DescendantOrSelf Step
175 NodeTest OptionalPredicateList
178 $$
= new Step
(Step
::ChildAxis
, *$1, *$2);
180 $$
= new Step
(Step
::ChildAxis
, *$1);
183 NAMETEST OptionalPredicateList
185 AtomicString localName
;
186 AtomicString namespaceURI
;
187 if
(!parser
->expandQName
(*$1, localName
, namespaceURI
)) {
188 parser
->m_gotNamespaceError
= true
;
193 $$
= new Step
(Step
::ChildAxis
, Step
::NodeTest
(Step
::NodeTest
::NameTest
, localName
, namespaceURI
), *$2);
195 $$
= new Step
(Step
::ChildAxis
, Step
::NodeTest
(Step
::NodeTest
::NameTest
, localName
, namespaceURI
));
196 parser
->deleteString
($1);
199 AxisSpecifier NodeTest OptionalPredicateList
202 $$
= new Step
($1, *$2, *$3);
204 $$
= new Step
($1, *$2);
207 AxisSpecifier NAMETEST OptionalPredicateList
209 AtomicString localName
;
210 AtomicString namespaceURI
;
211 if
(!parser
->expandQName
(*$2, localName
, namespaceURI
)) {
212 parser
->m_gotNamespaceError
= true
;
217 $$
= new Step
($1, Step
::NodeTest
(Step
::NodeTest
::NameTest
, localName
, namespaceURI
), *$3);
219 $$
= new Step
($1, Step
::NodeTest
(Step
::NodeTest
::NameTest
, localName
, namespaceURI
));
220 parser
->deleteString
($2);
231 $$
= Step
::AttributeAxis
;
239 $$
= new Step
::NodeTest
(Step
::NodeTest
::AnyNodeTest
);
240 else if
(*$1 == "text")
241 $$
= new Step
::NodeTest
(Step
::NodeTest
::TextNodeTest
);
242 else if
(*$1 == "comment")
243 $$
= new Step
::NodeTest
(Step
::NodeTest
::CommentNodeTest
);
245 parser
->deleteString
($1);
250 $$
= new Step
::NodeTest
(Step
::NodeTest
::ProcessingInstructionNodeTest
);
251 parser
->deleteString
($1);
256 $$
= new Step
::NodeTest
(Step
::NodeTest
::ProcessingInstructionNodeTest
, $3->stripWhiteSpace
());
257 parser
->deleteString
($1);
258 parser
->deleteString
($3);
262 OptionalPredicateList:
274 $$
= new blink
::HeapVector
<blink
::Member
<Predicate
>>;
275 $$
->append
(new Predicate
($1));
278 PredicateList Predicate
280 $$
->append
(new Predicate
($2));
294 $$
= new Step
(Step
::DescendantOrSelfAxis
, Step
::NodeTest
(Step
::NodeTest
::AnyNodeTest
));
301 $$
= new Step
(Step
::SelfAxis
, Step
::NodeTest
(Step
::NodeTest
::AnyNodeTest
));
306 $$
= new Step
(Step
::ParentAxis
, Step
::NodeTest
(Step
::NodeTest
::AnyNodeTest
));
313 $$
= new VariableReference
(*$1);
314 parser
->deleteString
($1);
324 $$
= new StringExpression
(*$1);
325 parser
->deleteString
($1);
330 $$
= new Number
($1->toDouble
());
331 parser
->deleteString
($1);
340 $$
= createFunction
(*$1);
343 parser
->deleteString
($1);
346 FUNCTIONNAME
'(' ArgumentList
')'
348 $$
= createFunction
(*$1, *$3);
351 parser
->deleteString
($1);
358 $$
= new blink
::HeapVector
<blink
::Member
<Expression
>>;
362 ArgumentList
',' Argument
375 UnionExpr
'|' PathExpr
378 $$
->addSubExpression
($1);
379 $$
->addSubExpression
($3);
391 FilterExpr
'/' RelativeLocationPath
393 $3->setAbsolute
(true
);
394 $$
= new Path
($1, $3);
397 FilterExpr DescendantOrSelf RelativeLocationPath
399 $3->insertFirstStep
($2);
400 $3->setAbsolute
(true
);
401 $$
= new Path
($1, $3);
408 PrimaryExpr PredicateList
410 $$
= new Filter
($1, *$2);
419 $$
= new LogicalOp
(LogicalOp
::OP_Or
, $1, $3);
426 AndExpr AND EqualityExpr
428 $$
= new LogicalOp
(LogicalOp
::OP_And
, $1, $3);
435 EqualityExpr EQOP RelationalExpr
437 $$
= new EqTestOp
($2, $1, $3);
444 RelationalExpr RELOP AdditiveExpr
446 $$
= new EqTestOp
($2, $1, $3);
453 AdditiveExpr PLUS MultiplicativeExpr
455 $$
= new NumericOp
(NumericOp
::OP_Add
, $1, $3);
458 AdditiveExpr MINUS MultiplicativeExpr
460 $$
= new NumericOp
(NumericOp
::OP_Sub
, $1, $3);
467 MultiplicativeExpr MULOP UnaryExpr
469 $$
= new NumericOp
($2, $1, $3);
479 $$
->addSubExpression
($2);