/src/3rdparty/webkit/Source/WebCore/xml/XPathGrammar.y

https://bitbucket.org/ultra_iter/qt-vtl · Happy · 558 lines · 511 code · 47 blank · 0 comment · 0 complexity · a8f2f931c3540743f6a98839b96ab6bd MD5 · raw file

  1. /*
  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>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  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.
  15. *
  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.
  26. */
  27. %{
  28. #include "config.h"
  29. #if ENABLE(XPATH)
  30. #include "XPathFunctions.h"
  31. #include "XPathNSResolver.h"
  32. #include "XPathParser.h"
  33. #include "XPathPath.h"
  34. #include "XPathPredicate.h"
  35. #include "XPathVariableReference.h"
  36. #include <wtf/FastMalloc.h>
  37. #define YYMALLOC fastMalloc
  38. #define YYFREE fastFree
  39. #define YYENABLE_NLS 0
  40. #define YYLTYPE_IS_TRIVIAL 1
  41. #define YYDEBUG 0
  42. #define YYMAXDEPTH 10000
  43. #define YYPARSE_PARAM parserParameter
  44. #define PARSER static_cast<Parser*>(parserParameter)
  45. using namespace WebCore;
  46. using namespace XPath;
  47. %}
  48. %pure_parser
  49. %union
  50. {
  51. Step::Axis axis;
  52. Step::NodeTest* nodeTest;
  53. NumericOp::Opcode numop;
  54. EqTestOp::Opcode eqop;
  55. String* str;
  56. Expression* expr;
  57. Vector<Predicate*>* predList;
  58. Vector<Expression*>* argList;
  59. Step* step;
  60. LocationPath* locationPath;
  61. }
  62. %{
  63. static int xpathyylex(YYSTYPE* yylval) { return Parser::current()->lex(yylval); }
  64. static void xpathyyerror(const char*) { }
  65. %}
  66. %left <numop> MULOP
  67. %left <eqop> EQOP RELOP
  68. %left PLUS MINUS
  69. %left OR AND
  70. %token <axis> AXISNAME
  71. %token <str> NODETYPE PI FUNCTIONNAME LITERAL
  72. %token <str> VARIABLEREFERENCE NUMBER
  73. %token DOTDOT SLASHSLASH
  74. %token <str> NAMETEST
  75. %token XPATH_ERROR
  76. %type <locationPath> LocationPath
  77. %type <locationPath> AbsoluteLocationPath
  78. %type <locationPath> RelativeLocationPath
  79. %type <step> Step
  80. %type <axis> AxisSpecifier
  81. %type <step> DescendantOrSelf
  82. %type <nodeTest> NodeTest
  83. %type <expr> Predicate
  84. %type <predList> OptionalPredicateList
  85. %type <predList> PredicateList
  86. %type <step> AbbreviatedStep
  87. %type <expr> Expr
  88. %type <expr> PrimaryExpr
  89. %type <expr> FunctionCall
  90. %type <argList> ArgumentList
  91. %type <expr> Argument
  92. %type <expr> UnionExpr
  93. %type <expr> PathExpr
  94. %type <expr> FilterExpr
  95. %type <expr> OrExpr
  96. %type <expr> AndExpr
  97. %type <expr> EqualityExpr
  98. %type <expr> RelationalExpr
  99. %type <expr> AdditiveExpr
  100. %type <expr> MultiplicativeExpr
  101. %type <expr> UnaryExpr
  102. %%
  103. Expr:
  104. OrExpr
  105. {
  106. PARSER->m_topExpr = $1;
  107. }
  108. ;
  109. LocationPath:
  110. RelativeLocationPath
  111. {
  112. $$->setAbsolute(false);
  113. }
  114. |
  115. AbsoluteLocationPath
  116. {
  117. $$->setAbsolute(true);
  118. }
  119. ;
  120. AbsoluteLocationPath:
  121. '/'
  122. {
  123. $$ = new LocationPath;
  124. PARSER->registerParseNode($$);
  125. }
  126. |
  127. '/' RelativeLocationPath
  128. {
  129. $$ = $2;
  130. }
  131. |
  132. DescendantOrSelf RelativeLocationPath
  133. {
  134. $$ = $2;
  135. $$->insertFirstStep($1);
  136. PARSER->unregisterParseNode($1);
  137. }
  138. ;
  139. RelativeLocationPath:
  140. Step
  141. {
  142. $$ = new LocationPath;
  143. $$->appendStep($1);
  144. PARSER->unregisterParseNode($1);
  145. PARSER->registerParseNode($$);
  146. }
  147. |
  148. RelativeLocationPath '/' Step
  149. {
  150. $$->appendStep($3);
  151. PARSER->unregisterParseNode($3);
  152. }
  153. |
  154. RelativeLocationPath DescendantOrSelf Step
  155. {
  156. $$->appendStep($2);
  157. $$->appendStep($3);
  158. PARSER->unregisterParseNode($2);
  159. PARSER->unregisterParseNode($3);
  160. }
  161. ;
  162. Step:
  163. NodeTest OptionalPredicateList
  164. {
  165. if ($2) {
  166. $$ = new Step(Step::ChildAxis, *$1, *$2);
  167. PARSER->deletePredicateVector($2);
  168. } else
  169. $$ = new Step(Step::ChildAxis, *$1);
  170. PARSER->deleteNodeTest($1);
  171. PARSER->registerParseNode($$);
  172. }
  173. |
  174. NAMETEST OptionalPredicateList
  175. {
  176. String localName;
  177. String namespaceURI;
  178. if (!PARSER->expandQName(*$1, localName, namespaceURI)) {
  179. PARSER->m_gotNamespaceError = true;
  180. YYABORT;
  181. }
  182. if ($2) {
  183. $$ = new Step(Step::ChildAxis, Step::NodeTest(Step::NodeTest::NameTest, localName, namespaceURI), *$2);
  184. PARSER->deletePredicateVector($2);
  185. } else
  186. $$ = new Step(Step::ChildAxis, Step::NodeTest(Step::NodeTest::NameTest, localName, namespaceURI));
  187. PARSER->deleteString($1);
  188. PARSER->registerParseNode($$);
  189. }
  190. |
  191. AxisSpecifier NodeTest OptionalPredicateList
  192. {
  193. if ($3) {
  194. $$ = new Step($1, *$2, *$3);
  195. PARSER->deletePredicateVector($3);
  196. } else
  197. $$ = new Step($1, *$2);
  198. PARSER->deleteNodeTest($2);
  199. PARSER->registerParseNode($$);
  200. }
  201. |
  202. AxisSpecifier NAMETEST OptionalPredicateList
  203. {
  204. String localName;
  205. String namespaceURI;
  206. if (!PARSER->expandQName(*$2, localName, namespaceURI)) {
  207. PARSER->m_gotNamespaceError = true;
  208. YYABORT;
  209. }
  210. if ($3) {
  211. $$ = new Step($1, Step::NodeTest(Step::NodeTest::NameTest, localName, namespaceURI), *$3);
  212. PARSER->deletePredicateVector($3);
  213. } else
  214. $$ = new Step($1, Step::NodeTest(Step::NodeTest::NameTest, localName, namespaceURI));
  215. PARSER->deleteString($2);
  216. PARSER->registerParseNode($$);
  217. }
  218. |
  219. AbbreviatedStep
  220. ;
  221. AxisSpecifier:
  222. AXISNAME
  223. |
  224. '@'
  225. {
  226. $$ = Step::AttributeAxis;
  227. }
  228. ;
  229. NodeTest:
  230. NODETYPE '(' ')'
  231. {
  232. if (*$1 == "node")
  233. $$ = new Step::NodeTest(Step::NodeTest::AnyNodeTest);
  234. else if (*$1 == "text")
  235. $$ = new Step::NodeTest(Step::NodeTest::TextNodeTest);
  236. else if (*$1 == "comment")
  237. $$ = new Step::NodeTest(Step::NodeTest::CommentNodeTest);
  238. PARSER->deleteString($1);
  239. PARSER->registerNodeTest($$);
  240. }
  241. |
  242. PI '(' ')'
  243. {
  244. $$ = new Step::NodeTest(Step::NodeTest::ProcessingInstructionNodeTest);
  245. PARSER->deleteString($1);
  246. PARSER->registerNodeTest($$);
  247. }
  248. |
  249. PI '(' LITERAL ')'
  250. {
  251. $$ = new Step::NodeTest(Step::NodeTest::ProcessingInstructionNodeTest, $3->stripWhiteSpace());
  252. PARSER->deleteString($1);
  253. PARSER->deleteString($3);
  254. PARSER->registerNodeTest($$);
  255. }
  256. ;
  257. OptionalPredicateList:
  258. /* empty */
  259. {
  260. $$ = 0;
  261. }
  262. |
  263. PredicateList
  264. ;
  265. PredicateList:
  266. Predicate
  267. {
  268. $$ = new Vector<Predicate*>;
  269. $$->append(new Predicate($1));
  270. PARSER->unregisterParseNode($1);
  271. PARSER->registerPredicateVector($$);
  272. }
  273. |
  274. PredicateList Predicate
  275. {
  276. $$->append(new Predicate($2));
  277. PARSER->unregisterParseNode($2);
  278. }
  279. ;
  280. Predicate:
  281. '[' Expr ']'
  282. {
  283. $$ = $2;
  284. }
  285. ;
  286. DescendantOrSelf:
  287. SLASHSLASH
  288. {
  289. $$ = new Step(Step::DescendantOrSelfAxis, Step::NodeTest(Step::NodeTest::AnyNodeTest));
  290. PARSER->registerParseNode($$);
  291. }
  292. ;
  293. AbbreviatedStep:
  294. '.'
  295. {
  296. $$ = new Step(Step::SelfAxis, Step::NodeTest(Step::NodeTest::AnyNodeTest));
  297. PARSER->registerParseNode($$);
  298. }
  299. |
  300. DOTDOT
  301. {
  302. $$ = new Step(Step::ParentAxis, Step::NodeTest(Step::NodeTest::AnyNodeTest));
  303. PARSER->registerParseNode($$);
  304. }
  305. ;
  306. PrimaryExpr:
  307. VARIABLEREFERENCE
  308. {
  309. $$ = new VariableReference(*$1);
  310. PARSER->deleteString($1);
  311. PARSER->registerParseNode($$);
  312. }
  313. |
  314. '(' Expr ')'
  315. {
  316. $$ = $2;
  317. }
  318. |
  319. LITERAL
  320. {
  321. $$ = new StringExpression(*$1);
  322. PARSER->deleteString($1);
  323. PARSER->registerParseNode($$);
  324. }
  325. |
  326. NUMBER
  327. {
  328. $$ = new Number($1->toDouble());
  329. PARSER->deleteString($1);
  330. PARSER->registerParseNode($$);
  331. }
  332. |
  333. FunctionCall
  334. ;
  335. FunctionCall:
  336. FUNCTIONNAME '(' ')'
  337. {
  338. $$ = createFunction(*$1);
  339. if (!$$)
  340. YYABORT;
  341. PARSER->deleteString($1);
  342. PARSER->registerParseNode($$);
  343. }
  344. |
  345. FUNCTIONNAME '(' ArgumentList ')'
  346. {
  347. $$ = createFunction(*$1, *$3);
  348. if (!$$)
  349. YYABORT;
  350. PARSER->deleteString($1);
  351. PARSER->deleteExpressionVector($3);
  352. PARSER->registerParseNode($$);
  353. }
  354. ;
  355. ArgumentList:
  356. Argument
  357. {
  358. $$ = new Vector<Expression*>;
  359. $$->append($1);
  360. PARSER->unregisterParseNode($1);
  361. PARSER->registerExpressionVector($$);
  362. }
  363. |
  364. ArgumentList ',' Argument
  365. {
  366. $$->append($3);
  367. PARSER->unregisterParseNode($3);
  368. }
  369. ;
  370. Argument:
  371. Expr
  372. ;
  373. UnionExpr:
  374. PathExpr
  375. |
  376. UnionExpr '|' PathExpr
  377. {
  378. $$ = new Union;
  379. $$->addSubExpression($1);
  380. $$->addSubExpression($3);
  381. PARSER->unregisterParseNode($1);
  382. PARSER->unregisterParseNode($3);
  383. PARSER->registerParseNode($$);
  384. }
  385. ;
  386. PathExpr:
  387. LocationPath
  388. {
  389. $$ = $1;
  390. }
  391. |
  392. FilterExpr
  393. |
  394. FilterExpr '/' RelativeLocationPath
  395. {
  396. $3->setAbsolute(true);
  397. $$ = new Path(static_cast<Filter*>($1), $3);
  398. PARSER->unregisterParseNode($1);
  399. PARSER->unregisterParseNode($3);
  400. PARSER->registerParseNode($$);
  401. }
  402. |
  403. FilterExpr DescendantOrSelf RelativeLocationPath
  404. {
  405. $3->insertFirstStep($2);
  406. $3->setAbsolute(true);
  407. $$ = new Path(static_cast<Filter*>($1), $3);
  408. PARSER->unregisterParseNode($1);
  409. PARSER->unregisterParseNode($2);
  410. PARSER->unregisterParseNode($3);
  411. PARSER->registerParseNode($$);
  412. }
  413. ;
  414. FilterExpr:
  415. PrimaryExpr
  416. |
  417. PrimaryExpr PredicateList
  418. {
  419. $$ = new Filter($1, *$2);
  420. PARSER->unregisterParseNode($1);
  421. PARSER->deletePredicateVector($2);
  422. PARSER->registerParseNode($$);
  423. }
  424. ;
  425. OrExpr:
  426. AndExpr
  427. |
  428. OrExpr OR AndExpr
  429. {
  430. $$ = new LogicalOp(LogicalOp::OP_Or, $1, $3);
  431. PARSER->unregisterParseNode($1);
  432. PARSER->unregisterParseNode($3);
  433. PARSER->registerParseNode($$);
  434. }
  435. ;
  436. AndExpr:
  437. EqualityExpr
  438. |
  439. AndExpr AND EqualityExpr
  440. {
  441. $$ = new LogicalOp(LogicalOp::OP_And, $1, $3);
  442. PARSER->unregisterParseNode($1);
  443. PARSER->unregisterParseNode($3);
  444. PARSER->registerParseNode($$);
  445. }
  446. ;
  447. EqualityExpr:
  448. RelationalExpr
  449. |
  450. EqualityExpr EQOP RelationalExpr
  451. {
  452. $$ = new EqTestOp($2, $1, $3);
  453. PARSER->unregisterParseNode($1);
  454. PARSER->unregisterParseNode($3);
  455. PARSER->registerParseNode($$);
  456. }
  457. ;
  458. RelationalExpr:
  459. AdditiveExpr
  460. |
  461. RelationalExpr RELOP AdditiveExpr
  462. {
  463. $$ = new EqTestOp($2, $1, $3);
  464. PARSER->unregisterParseNode($1);
  465. PARSER->unregisterParseNode($3);
  466. PARSER->registerParseNode($$);
  467. }
  468. ;
  469. AdditiveExpr:
  470. MultiplicativeExpr
  471. |
  472. AdditiveExpr PLUS MultiplicativeExpr
  473. {
  474. $$ = new NumericOp(NumericOp::OP_Add, $1, $3);
  475. PARSER->unregisterParseNode($1);
  476. PARSER->unregisterParseNode($3);
  477. PARSER->registerParseNode($$);
  478. }
  479. |
  480. AdditiveExpr MINUS MultiplicativeExpr
  481. {
  482. $$ = new NumericOp(NumericOp::OP_Sub, $1, $3);
  483. PARSER->unregisterParseNode($1);
  484. PARSER->unregisterParseNode($3);
  485. PARSER->registerParseNode($$);
  486. }
  487. ;
  488. MultiplicativeExpr:
  489. UnaryExpr
  490. |
  491. MultiplicativeExpr MULOP UnaryExpr
  492. {
  493. $$ = new NumericOp($2, $1, $3);
  494. PARSER->unregisterParseNode($1);
  495. PARSER->unregisterParseNode($3);
  496. PARSER->registerParseNode($$);
  497. }
  498. ;
  499. UnaryExpr:
  500. UnionExpr
  501. |
  502. MINUS UnaryExpr
  503. {
  504. $$ = new Negative;
  505. $$->addSubExpression($2);
  506. PARSER->unregisterParseNode($2);
  507. PARSER->registerParseNode($$);
  508. }
  509. ;
  510. %%
  511. #endif