Parser.jay 6.8 KB

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