Parser.jay 7.2 KB

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