Parser.jay 7.2 KB

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