Parser.jay 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. %{
  2. // XPath parser
  3. //
  4. // Author - Piers Haken <[email protected]>
  5. //
  6. // TODO: FUNCTION_CALL should be a QName, not just a NCName
  7. // TODO: PROCESSING_INSTRUCTION's optional parameter
  8. // TODO: flatten argument/predicate lists in place
  9. using System;
  10. using System.Xml.XPath;
  11. namespace Mono.Xml.XPath
  12. {
  13. public class XPathParser
  14. {
  15. internal object yyparseSafe (Tokenizer tok)
  16. {
  17. return yyparseSafe (tok, null);
  18. }
  19. internal object yyparseSafe (Tokenizer tok, object yyDebug)
  20. {
  21. try
  22. {
  23. return yyparse (tok, yyDebug);
  24. }
  25. catch (XPathException e)
  26. {
  27. throw e;
  28. }
  29. catch (Exception e)
  30. {
  31. throw new XPathException ("Error during parse", e);
  32. }
  33. }
  34. internal object yyparseDebug (Tokenizer tok)
  35. {
  36. return yyparseSafe (tok, new yydebug.yyDebugSimple ());
  37. }
  38. %}
  39. %token ERROR
  40. %token EOF
  41. %token SLASH "/"
  42. %token SLASH2 "//"
  43. %token DOT "."
  44. %token DOT2 ".."
  45. %token COLON ":"
  46. %token COLON2 "::"
  47. %token COMMA ","
  48. %token AT "@"
  49. %token FUNCTION_NAME
  50. %token BRACKET_OPEN "["
  51. %token BRACKET_CLOSE "]"
  52. %token PAREN_OPEN "("
  53. %token PAREN_CLOSE ")"
  54. %token AND "and"
  55. %token OR "or"
  56. %token DIV "div"
  57. %token MOD "mod"
  58. %token PLUS "+"
  59. %token MINUS "-"
  60. %token ASTERISK "*"
  61. %token DOLLAR "$"
  62. %token BAR "|"
  63. %token EQ "="
  64. %token NE "!="
  65. %token LE "<="
  66. %token GE ">="
  67. %token LT "<"
  68. %token GT ">"
  69. %token ANCESTOR "ancestor"
  70. %token ANCESTOR_OR_SELF "ancstor-or-self"
  71. %token ATTRIBUTE "attribute"
  72. %token CHILD "child"
  73. %token DESCENDANT "descendant"
  74. %token DESCENDANT_OR_SELF "descendant-or-self"
  75. %token FOLLOWING "following"
  76. %token FOLLOWING_SIBLING "sibling"
  77. %token NAMESPACE "NameSpace"
  78. %token PARENT "parent"
  79. %token PRECEDING "preceding"
  80. %token PRECEDING_SIBLING "preceding-sibling"
  81. %token SELF "self"
  82. %token COMMENT "comment"
  83. %token TEXT "text"
  84. %token PROCESSING_INSTRUCTION "processing-instruction"
  85. %token NODE "node"
  86. %token MULTIPLY "*"
  87. %token NUMBER
  88. %token LITERAL
  89. %token NCName
  90. %start Expr
  91. %left AND
  92. %left OR
  93. %left EQ
  94. %left NE
  95. %left LE
  96. %left GE
  97. %left LT
  98. %left GT
  99. %left DIV
  100. %left MOD
  101. %left PLUS
  102. %left MINUS
  103. %%
  104. Expr
  105. : OrExpr
  106. ;
  107. OrExpr
  108. : AndExpr
  109. | OrExpr OR AndExpr
  110. {
  111. $$ = new ExprOR ((Expression) $1, (Expression) $3);
  112. }
  113. ;
  114. AndExpr
  115. : EqualityExpr
  116. | AndExpr AND EqualityExpr
  117. {
  118. $$ = new ExprAND ((Expression) $1, (Expression) $3);
  119. }
  120. ;
  121. EqualityExpr
  122. : RelationalExpr
  123. | EqualityExpr EQ RelationalExpr
  124. {
  125. $$ = new ExprEQ ((Expression) $1, (Expression) $3);
  126. }
  127. | EqualityExpr NE RelationalExpr
  128. {
  129. $$ = new ExprNE ((Expression) $1, (Expression) $3);
  130. }
  131. ;
  132. RelationalExpr
  133. : AdditiveExpr
  134. | RelationalExpr LT AdditiveExpr
  135. {
  136. $$ = new ExprLT ((Expression) $1, (Expression) $3);
  137. }
  138. | RelationalExpr GT AdditiveExpr
  139. {
  140. $$ = new ExprGT ((Expression) $1, (Expression) $3);
  141. }
  142. | RelationalExpr LE AdditiveExpr
  143. {
  144. $$ = new ExprLE ((Expression) $1, (Expression) $3);
  145. }
  146. | RelationalExpr GE AdditiveExpr
  147. {
  148. $$ = new ExprGE ((Expression) $1, (Expression) $3);
  149. }
  150. ;
  151. AdditiveExpr
  152. : MultiplicativeExpr
  153. | AdditiveExpr PLUS MultiplicativeExpr
  154. {
  155. $$ = new ExprPLUS ((Expression) $1, (Expression) $3);
  156. }
  157. | AdditiveExpr MINUS MultiplicativeExpr
  158. {
  159. $$ = new ExprMINUS ((Expression) $1, (Expression) $3);
  160. }
  161. ;
  162. MultiplicativeExpr
  163. : UnaryExpr
  164. | MultiplicativeExpr MULTIPLY UnaryExpr
  165. {
  166. $$ = new ExprMULT ((Expression) $1, (Expression) $3);
  167. }
  168. | MultiplicativeExpr DIV UnaryExpr
  169. {
  170. $$ = new ExprDIV ((Expression) $1, (Expression) $3);
  171. }
  172. | MultiplicativeExpr MOD UnaryExpr
  173. {
  174. $$ = new ExprMOD ((Expression) $1, (Expression) $3);
  175. }
  176. ;
  177. UnaryExpr
  178. : UnionExpr
  179. | MINUS UnaryExpr
  180. {
  181. $$ = new ExprNEG ((Expression) $2);
  182. }
  183. ;
  184. UnionExpr
  185. : PathExpr
  186. | UnionExpr BAR PathExpr
  187. {
  188. $$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
  189. }
  190. ;
  191. PathExpr
  192. : RelativeLocationPath
  193. | SLASH
  194. {
  195. $$ = new ExprRoot ();
  196. }
  197. | SLASH RelativeLocationPath
  198. {
  199. $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
  200. }
  201. | SLASH2 RelativeLocationPath
  202. {
  203. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  204. $$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), exprStep), (NodeSet) $2);
  205. }
  206. | FilterExpr
  207. | FilterExpr SLASH RelativeLocationPath
  208. {
  209. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  210. }
  211. | FilterExpr SLASH2 RelativeLocationPath
  212. {
  213. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  214. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  215. }
  216. ;
  217. RelativeLocationPath
  218. : Step
  219. | RelativeLocationPath SLASH Step
  220. {
  221. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  222. }
  223. | RelativeLocationPath SLASH2 Step
  224. {
  225. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  226. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  227. }
  228. ;
  229. Step
  230. : AxisSpecifier QName ZeroOrMorePredicates
  231. {
  232. $$ = new ExprStep (new NodeNameTest ((Axes) $1, (QName) $2), (ExprPredicates) $3);
  233. }
  234. | AxisSpecifier ASTERISK ZeroOrMorePredicates
  235. {
  236. $$ = new ExprStep (new NodeTypeTest ((Axes) $1), (ExprPredicates) $3);
  237. }
  238. | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE ZeroOrMorePredicates
  239. {
  240. $$ = new ExprStep (new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4), (ExprPredicates) $6);
  241. }
  242. | DOT
  243. {
  244. $$ = new ExprStep (new NodeTypeTest (Axes.Self, XPathNodeType.All));
  245. }
  246. | DOT2
  247. {
  248. $$ = new ExprStep (new NodeTypeTest (Axes.Parent, XPathNodeType.All));
  249. }
  250. ;
  251. AxisSpecifier
  252. : /* empty */
  253. {
  254. $$ = Axes.Child;
  255. }
  256. | AT
  257. {
  258. $$ = Axes.Attribute;
  259. }
  260. | AxisName COLON2
  261. {
  262. $$ = $1;
  263. }
  264. ;
  265. NodeType
  266. : COMMENT { $$ = XPathNodeType.Comment; }
  267. | TEXT { $$ = XPathNodeType.Text; }
  268. | PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
  269. | NODE { $$ = XPathNodeType.All; }
  270. ;
  271. FilterExpr
  272. : PrimaryExpr
  273. | FilterExpr Predicate
  274. {
  275. $$ = new ExprFilter ((Expression) $1, (Expression) $2);
  276. }
  277. ;
  278. PrimaryExpr
  279. : DOLLAR QName
  280. {
  281. $$ = new ExprVariable ((QName) $2);
  282. }
  283. | PAREN_OPEN Expr PAREN_CLOSE
  284. {
  285. $$ = $2;
  286. }
  287. | LITERAL
  288. {
  289. $$ = new ExprLiteral ((String) $1);
  290. }
  291. | NUMBER
  292. {
  293. $$ = new ExprNumber ((double) $1);
  294. }
  295. | FunctionCall
  296. ;
  297. FunctionCall
  298. : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
  299. {
  300. $$ = new ExprFunctionCall ((String) $1, (FunctionArguments) $3);
  301. }
  302. ;
  303. OptionalArgumentList
  304. : /* empty */
  305. | Expr OptionalArgumentListTail
  306. {
  307. $$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
  308. }
  309. ;
  310. OptionalArgumentListTail
  311. : /* empty */
  312. | COMMA Expr OptionalArgumentListTail
  313. {
  314. $$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
  315. }
  316. ;
  317. ZeroOrMorePredicates
  318. : /* empty */
  319. | Predicate ZeroOrMorePredicates
  320. {
  321. $$ = new ExprPredicates ((Expression) $1, (ExprPredicates) $2);
  322. }
  323. ;
  324. Predicate
  325. : BRACKET_OPEN Expr BRACKET_CLOSE
  326. {
  327. $$ = $2;
  328. }
  329. ;
  330. AxisName
  331. : ANCESTOR { $$ = Axes.Ancestor; }
  332. | ANCESTOR_OR_SELF { $$ = Axes.AncestorOrSelf; }
  333. | ATTRIBUTE { $$ = Axes.Attribute; }
  334. | CHILD { $$ = Axes.Child; }
  335. | DESCENDANT { $$ = Axes.Descendant; }
  336. | DESCENDANT_OR_SELF { $$ = Axes.DescendantOrSelf; }
  337. | FOLLOWING { $$ = Axes.Following; }
  338. | FOLLOWING_SIBLING { $$ = Axes.FollowingSibling; }
  339. | NAMESPACE { $$ = Axes.Namespace; }
  340. | PARENT { $$ = Axes.Parent; }
  341. | PRECEDING { $$ = Axes.Preceding; }
  342. | PRECEDING_SIBLING { $$ = Axes.PrecedingSibling; }
  343. | SELF { $$ = Axes.Self; }
  344. ;
  345. OptionalLiteral
  346. : /* empty */
  347. | LITERAL
  348. ;
  349. QName
  350. : NCName
  351. {
  352. $$ = new NCName ((String) $1);
  353. }
  354. | NCName COLON ASTERISK
  355. {
  356. $$ = new QName ((String) $1, null);
  357. }
  358. | NCName COLON NCName
  359. {
  360. $$ = new QName ((String) $1, (String) $3);
  361. }
  362. ;
  363. %%
  364. }