XPathGrammar.y 12 KB

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