Parser.jay 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 NUMBER
  87. %token LITERAL
  88. %token NCName
  89. %start Expr
  90. %left AND
  91. %left OR
  92. %left EQ
  93. %left NE
  94. %left LE
  95. %left GE
  96. %left LT
  97. %left GT
  98. %left DIV
  99. %left MOD
  100. %left PLUS
  101. %left MINUS
  102. %%
  103. Expr
  104. : OrExpr
  105. ;
  106. OrExpr
  107. : AndExpr
  108. | OrExpr OR AndExpr
  109. {
  110. $$ = new ExprOR ((Expression) $1, (Expression) $3);
  111. }
  112. ;
  113. AndExpr
  114. : EqualityExpr
  115. | AndExpr AND EqualityExpr
  116. {
  117. $$ = new ExprAND ((Expression) $1, (Expression) $3);
  118. }
  119. ;
  120. EqualityExpr
  121. : RelationalExpr
  122. | EqualityExpr EQ RelationalExpr
  123. {
  124. $$ = new ExprEQ ((Expression) $1, (Expression) $3);
  125. }
  126. | EqualityExpr NE RelationalExpr
  127. {
  128. $$ = new ExprNE ((Expression) $1, (Expression) $3);
  129. }
  130. ;
  131. RelationalExpr
  132. : AdditiveExpr
  133. | RelationalExpr LT AdditiveExpr
  134. {
  135. $$ = new ExprLT ((Expression) $1, (Expression) $3);
  136. }
  137. | RelationalExpr GT AdditiveExpr
  138. {
  139. $$ = new ExprGT ((Expression) $1, (Expression) $3);
  140. }
  141. | RelationalExpr LE AdditiveExpr
  142. {
  143. $$ = new ExprLE ((Expression) $1, (Expression) $3);
  144. }
  145. | RelationalExpr GE AdditiveExpr
  146. {
  147. $$ = new ExprGE ((Expression) $1, (Expression) $3);
  148. }
  149. ;
  150. AdditiveExpr
  151. : MultiplicativeExpr
  152. | AdditiveExpr PLUS MultiplicativeExpr
  153. {
  154. $$ = new ExprPLUS ((Expression) $1, (Expression) $3);
  155. }
  156. | AdditiveExpr MINUS MultiplicativeExpr
  157. {
  158. $$ = new ExprMINUS ((Expression) $1, (Expression) $3);
  159. }
  160. ;
  161. MultiplicativeExpr
  162. : UnaryExpr
  163. | MultiplicativeExpr ASTERISK UnaryExpr
  164. {
  165. $$ = new ExprMULT ((Expression) $1, (Expression) $3);
  166. }
  167. | MultiplicativeExpr DIV UnaryExpr
  168. {
  169. $$ = new ExprDIV ((Expression) $1, (Expression) $3);
  170. }
  171. | MultiplicativeExpr MOD UnaryExpr
  172. {
  173. $$ = new ExprMOD ((Expression) $1, (Expression) $3);
  174. }
  175. ;
  176. UnaryExpr
  177. : UnionExpr
  178. | MINUS UnaryExpr
  179. {
  180. $$ = new ExprNEG ((Expression) $2);
  181. }
  182. ;
  183. UnionExpr
  184. : PathExpr
  185. | UnionExpr BAR PathExpr
  186. {
  187. $$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
  188. }
  189. ;
  190. PathExpr
  191. : RelativeLocationPath
  192. | SLASH
  193. {
  194. $$ = new ExprRoot ();
  195. }
  196. | SLASH RelativeLocationPath
  197. {
  198. $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
  199. }
  200. | SLASH2 RelativeLocationPath
  201. {
  202. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  203. $$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), exprStep), (NodeSet) $2);
  204. }
  205. | FilterExpr
  206. | FilterExpr SLASH RelativeLocationPath
  207. {
  208. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  209. }
  210. | FilterExpr SLASH2 RelativeLocationPath
  211. {
  212. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  213. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  214. }
  215. ;
  216. RelativeLocationPath
  217. : Step
  218. | RelativeLocationPath SLASH Step
  219. {
  220. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  221. }
  222. | RelativeLocationPath SLASH2 Step
  223. {
  224. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  225. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  226. }
  227. ;
  228. Step
  229. : AxisSpecifier QName ZeroOrMorePredicates
  230. {
  231. $$ = new ExprStep (new NodeNameTest ((Axes) $1, (QName) $2), (ExprPredicates) $3);
  232. }
  233. | AxisSpecifier ASTERISK ZeroOrMorePredicates
  234. {
  235. $$ = new ExprStep (new NodeTypeTest ((Axes) $1), (ExprPredicates) $3);
  236. }
  237. | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE ZeroOrMorePredicates
  238. {
  239. $$ = new ExprStep (new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4), (ExprPredicates) $6);
  240. }
  241. | DOT
  242. {
  243. $$ = new ExprStep (new NodeTypeTest (Axes.Self, XPathNodeType.All));
  244. }
  245. | DOT2
  246. {
  247. $$ = new ExprStep (new NodeTypeTest (Axes.Parent, XPathNodeType.All));
  248. }
  249. ;
  250. AxisSpecifier
  251. : /* empty */
  252. {
  253. $$ = Axes.Child;
  254. }
  255. | AT
  256. {
  257. $$ = Axes.Attribute;
  258. }
  259. | AxisName COLON2
  260. {
  261. $$ = $1;
  262. }
  263. ;
  264. NodeType
  265. : COMMENT { $$ = XPathNodeType.Comment; }
  266. | TEXT { $$ = XPathNodeType.Text; }
  267. | PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
  268. | NODE { $$ = XPathNodeType.All; }
  269. ;
  270. FilterExpr
  271. : PrimaryExpr
  272. | FilterExpr Predicate
  273. {
  274. $$ = new ExprFilter ((Expression) $1, (Expression) $2);
  275. }
  276. ;
  277. PrimaryExpr
  278. : DOLLAR QName
  279. {
  280. $$ = new ExprVariable ((QName) $2);
  281. }
  282. | PAREN_OPEN Expr PAREN_CLOSE
  283. {
  284. $$ = $2;
  285. }
  286. | LITERAL
  287. {
  288. $$ = new ExprLiteral ((String) $1);
  289. }
  290. | NUMBER
  291. {
  292. $$ = new ExprNumber ((double) $1);
  293. }
  294. | FunctionCall
  295. ;
  296. FunctionCall
  297. : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
  298. {
  299. $$ = new ExprFunctionCall ((String) $1, (FunctionArguments) $3);
  300. }
  301. ;
  302. OptionalArgumentList
  303. : /* empty */
  304. | Expr OptionalArgumentListTail
  305. {
  306. $$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
  307. }
  308. ;
  309. OptionalArgumentListTail
  310. : /* empty */
  311. | COMMA Expr OptionalArgumentListTail
  312. {
  313. $$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
  314. }
  315. ;
  316. ZeroOrMorePredicates
  317. : /* empty */
  318. | Predicate ZeroOrMorePredicates
  319. {
  320. $$ = new ExprPredicates ((Expression) $1, (ExprPredicates) $2);
  321. }
  322. ;
  323. Predicate
  324. : BRACKET_OPEN Expr BRACKET_CLOSE
  325. {
  326. $$ = $2;
  327. }
  328. ;
  329. AxisName
  330. : ANCESTOR { $$ = Axes.Ancestor; }
  331. | ANCESTOR_OR_SELF { $$ = Axes.AncestorOrSelf; }
  332. | ATTRIBUTE { $$ = Axes.Attribute; }
  333. | CHILD { $$ = Axes.Child; }
  334. | DESCENDANT { $$ = Axes.Descendant; }
  335. | DESCENDANT_OR_SELF { $$ = Axes.DescendantOrSelf; }
  336. | FOLLOWING { $$ = Axes.Following; }
  337. | FOLLOWING_SIBLING { $$ = Axes.FollowingSibling; }
  338. | NAMESPACE { $$ = Axes.Namespace; }
  339. | PARENT { $$ = Axes.Parent; }
  340. | PRECEDING { $$ = Axes.Preceding; }
  341. | PRECEDING_SIBLING { $$ = Axes.PrecedingSibling; }
  342. | SELF { $$ = Axes.Self; }
  343. ;
  344. OptionalLiteral
  345. : /* empty */
  346. | LITERAL
  347. ;
  348. QName
  349. : NCName
  350. {
  351. $$ = new NCName ((String) $1);
  352. }
  353. | NCName COLON ASTERISK
  354. {
  355. $$ = new QName ((String) $1, null);
  356. }
  357. | NCName COLON NCName
  358. {
  359. $$ = new QName ((String) $1, (String) $3);
  360. }
  361. ;
  362. %%
  363. }