Parser.jay 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. using Test.Xml.XPath;
  12. namespace Mono.Xml.XPath
  13. {
  14. public class XPathParser
  15. {
  16. internal object yyparseDebug (Tokenizer tok)
  17. {
  18. return yyparse (tok, new yydebug.yyDebugSimple ());
  19. }
  20. %}
  21. %token ERROR
  22. %token EOF
  23. %token SLASH
  24. %token SLASH2
  25. %token DOT
  26. %token DOT2
  27. %token COLON
  28. %token COLON2
  29. %token COMMA
  30. %token AT
  31. %token FUNCTION_NAME
  32. %token BRACKET_OPEN
  33. %token BRACKET_CLOSE
  34. %token PAREN_OPEN
  35. %token PAREN_CLOSE
  36. %token AND
  37. %token OR
  38. %token DIV
  39. %token MOD
  40. %token PLUS
  41. %token MINUS
  42. %token ASTERISK
  43. %token DOLLAR
  44. %token BAR
  45. %token EQ
  46. %token NE
  47. %token LE
  48. %token GE
  49. %token LT
  50. %token GT
  51. %token ANCESTOR
  52. %token ANCESTOR_OR_SELF
  53. %token ATTRIBUTE
  54. %token CHILD
  55. %token DESCENDANT
  56. %token DESCENDANT_OR_SELF
  57. %token FOLLOWING
  58. %token FOLLOWING_SIBLING
  59. %token NAMESPACE
  60. %token PARENT
  61. %token PRECEDING
  62. %token PRECEDING_SIBLING
  63. %token SELF
  64. %token COMMENT
  65. %token TEXT
  66. %token PROCESSING_INSTRUCTION
  67. %token NODE
  68. %token NUMBER
  69. %token LITERAL
  70. %token NCName
  71. %start Expr
  72. %left AND
  73. %left OR
  74. %left EQ
  75. %left NE
  76. %left LE
  77. %left GE
  78. %left LT
  79. %left GT
  80. %left DIV
  81. %left MOD
  82. %left PLUS
  83. %left MINUS
  84. %%
  85. Expr
  86. : OrExpr
  87. ;
  88. OrExpr
  89. : AndExpr
  90. | OrExpr OR AndExpr
  91. {
  92. $$ = new ExprOR ((Expression) $1, (Expression) $3);
  93. }
  94. ;
  95. AndExpr
  96. : EqualityExpr
  97. | AndExpr AND EqualityExpr
  98. {
  99. $$ = new ExprAND ((Expression) $1, (Expression) $3);
  100. }
  101. ;
  102. EqualityExpr
  103. : RelationalExpr
  104. | EqualityExpr EQ RelationalExpr
  105. {
  106. $$ = new ExprEQ ((Expression) $1, (Expression) $3);
  107. }
  108. | EqualityExpr NE RelationalExpr
  109. {
  110. $$ = new ExprNE ((Expression) $1, (Expression) $3);
  111. }
  112. ;
  113. RelationalExpr
  114. : AdditiveExpr
  115. | RelationalExpr LT AdditiveExpr
  116. {
  117. $$ = new ExprLT ((Expression) $1, (Expression) $3);
  118. }
  119. | RelationalExpr GT AdditiveExpr
  120. {
  121. $$ = new ExprGT ((Expression) $1, (Expression) $3);
  122. }
  123. | RelationalExpr LE AdditiveExpr
  124. {
  125. $$ = new ExprLE ((Expression) $1, (Expression) $3);
  126. }
  127. | RelationalExpr GE AdditiveExpr
  128. {
  129. $$ = new ExprGE ((Expression) $1, (Expression) $3);
  130. }
  131. ;
  132. AdditiveExpr
  133. : MultiplicativeExpr
  134. | AdditiveExpr PLUS MultiplicativeExpr
  135. {
  136. $$ = new ExprPLUS ((Expression) $1, (Expression) $3);
  137. }
  138. | AdditiveExpr MINUS MultiplicativeExpr
  139. {
  140. $$ = new ExprMINUS ((Expression) $1, (Expression) $3);
  141. }
  142. ;
  143. MultiplicativeExpr
  144. : UnaryExpr
  145. | MultiplicativeExpr ASTERISK UnaryExpr
  146. {
  147. $$ = new ExprMULT ((Expression) $1, (Expression) $3);
  148. }
  149. | MultiplicativeExpr DIV UnaryExpr
  150. {
  151. $$ = new ExprDIV ((Expression) $1, (Expression) $3);
  152. }
  153. | MultiplicativeExpr MOD UnaryExpr
  154. {
  155. $$ = new ExprMOD ((Expression) $1, (Expression) $3);
  156. }
  157. ;
  158. UnaryExpr
  159. : UnionExpr
  160. | MINUS UnaryExpr
  161. {
  162. $$ = new ExprNEG ((Expression) $2);
  163. }
  164. ;
  165. UnionExpr
  166. : PathExpr
  167. | UnionExpr BAR PathExpr
  168. {
  169. $$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
  170. }
  171. ;
  172. PathExpr
  173. : RelativeLocationPath
  174. | SLASH
  175. {
  176. $$ = new ExprRoot ();
  177. }
  178. | SLASH RelativeLocationPath
  179. {
  180. $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
  181. }
  182. | SLASH2 RelativeLocationPath
  183. {
  184. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  185. $$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), exprStep), (NodeSet) $2);
  186. }
  187. | FilterExpr
  188. | FilterExpr SLASH RelativeLocationPath
  189. {
  190. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  191. }
  192. | FilterExpr SLASH2 RelativeLocationPath
  193. {
  194. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  195. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  196. }
  197. ;
  198. RelativeLocationPath
  199. : Step
  200. | RelativeLocationPath SLASH Step
  201. {
  202. $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
  203. }
  204. | RelativeLocationPath SLASH2 Step
  205. {
  206. ExprStep exprStep = new ExprStep (new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All));
  207. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, exprStep), (NodeSet) $3);
  208. }
  209. ;
  210. Step
  211. : AxisSpecifier QName ZeroOrMorePredicates
  212. {
  213. $$ = new ExprStep (new NodeNameTest ((Axes) $1, (QName) $2), (ExprPredicates) $3);
  214. }
  215. | AxisSpecifier ASTERISK ZeroOrMorePredicates
  216. {
  217. $$ = new ExprStep (new NodeTypeTest ((Axes) $1), (ExprPredicates) $3);
  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. QName
  331. : NCName
  332. {
  333. $$ = new NCName ((String) $1);
  334. }
  335. | NCName COLON ASTERISK
  336. {
  337. $$ = new QName ((String) $1, null);
  338. }
  339. | NCName COLON NCName
  340. {
  341. $$ = new QName ((String) $1, (String) $3);
  342. }
  343. ;
  344. %%
  345. }