Parser.jay 6.9 KB

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