Parser.jay 7.3 KB

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