Parser.jay 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 ((NodeSet) $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 ((NodeSet) $1, exprStep), (NodeSet) $3);
  196. }
  197. ;
  198. RelativeLocationPath
  199. : Step
  200. | RelativeLocationPath SLASH Step
  201. {
  202. $$ = new ExprSLASH ((NodeSet) $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 ((NodeSet) $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 NCName COLON ASTERISK ZeroOrMorePredicates
  220. {
  221. $$ = new ExprStep (new NodeNameTestAny ((Axes) $1, (NCName) $2), (ExprPredicates) $5);
  222. }
  223. */
  224. | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE ZeroOrMorePredicates
  225. {
  226. $$ = new ExprStep (new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4), (ExprPredicates) $6);
  227. }
  228. | DOT
  229. {
  230. $$ = new ExprStep (new NodeTypeTest (Axes.Self, XPathNodeType.All));
  231. }
  232. | DOT2
  233. {
  234. $$ = new ExprStep (new NodeTypeTest (Axes.Parent, XPathNodeType.All));
  235. }
  236. ;
  237. AxisSpecifier
  238. : /* empty */
  239. {
  240. $$ = Axes.Child;
  241. }
  242. | AT
  243. {
  244. $$ = Axes.Attribute;
  245. }
  246. | AxisName COLON2
  247. {
  248. $$ = $1;
  249. }
  250. ;
  251. NodeType
  252. : COMMENT { $$ = XPathNodeType.Comment; }
  253. | TEXT { $$ = XPathNodeType.Text; }
  254. | PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
  255. | NODE { $$ = XPathNodeType.All; }
  256. ;
  257. FilterExpr
  258. : PrimaryExpr
  259. | FilterExpr Predicate
  260. {
  261. $$ = new ExprFilter ((Expression) $1, (Expression) $2);
  262. }
  263. ;
  264. PrimaryExpr
  265. : DOLLAR QName
  266. {
  267. $$ = new ExprVariable ((QName) $2);
  268. }
  269. | PAREN_OPEN Expr PAREN_CLOSE
  270. {
  271. $$ = $2;
  272. }
  273. | LITERAL
  274. {
  275. $$ = new ExprLiteral ((String) $1);
  276. }
  277. | NUMBER
  278. {
  279. $$ = new ExprNumber ((double) $1);
  280. }
  281. | FunctionCall
  282. ;
  283. FunctionCall
  284. : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
  285. {
  286. $$ = new ExprFunctionCall ((String) $1, (FunctionArguments) $3);
  287. }
  288. ;
  289. OptionalArgumentList
  290. : /* empty */
  291. | Expr OptionalArgumentListTail
  292. {
  293. $$ = new FunctionArguments ((Expression) $1, (FunctionArguments) $2);
  294. }
  295. ;
  296. OptionalArgumentListTail
  297. : /* empty */
  298. | COMMA Expr OptionalArgumentListTail
  299. {
  300. $$ = new FunctionArguments ((Expression) $2, (FunctionArguments) $3);
  301. }
  302. ;
  303. ZeroOrMorePredicates
  304. : /* empty */
  305. | Predicate ZeroOrMorePredicates
  306. {
  307. $$ = new ExprPredicates ((Expression) $1, (ExprPredicates) $2);
  308. }
  309. ;
  310. Predicate
  311. : BRACKET_OPEN Expr BRACKET_CLOSE
  312. {
  313. $$ = $2;
  314. }
  315. ;
  316. AxisName
  317. : ANCESTOR { $$ = Axes.Ancestor; }
  318. | ANCESTOR_OR_SELF { $$ = Axes.AncestorOrSelf; }
  319. | ATTRIBUTE { $$ = Axes.Attribute; }
  320. | CHILD { $$ = Axes.Child; }
  321. | DESCENDANT { $$ = Axes.Descendant; }
  322. | DESCENDANT_OR_SELF { $$ = Axes.DescendantOrSelf; }
  323. | FOLLOWING { $$ = Axes.Following; }
  324. | FOLLOWING_SIBLING { $$ = Axes.FollowingSibling; }
  325. | NAMESPACE { $$ = Axes.Namespace; }
  326. | PARENT { $$ = Axes.Parent; }
  327. | PRECEDING { $$ = Axes.Preceding; }
  328. | PRECEDING_SIBLING { $$ = Axes.PrecedingSibling; }
  329. | SELF { $$ = Axes.Self; }
  330. ;
  331. OptionalLiteral
  332. : /* empty */
  333. | LITERAL
  334. ;
  335. QName
  336. : NCName
  337. {
  338. $$ = new NCName ((String) $1);
  339. }
  340. | NCName COLON ASTERISK
  341. {
  342. $$ = new QName ((String) $1, null);
  343. }
  344. | NCName COLON NCName
  345. {
  346. $$ = new QName ((String) $1, (String) $3);
  347. }
  348. ;
  349. %%
  350. }