Parser.jay 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
  201. $$ = new ExprSLASH (new ExprSLASH ((Expression) $1, test), (NodeSet) $3);
  202. }
  203. ;
  204. LocationPath
  205. : RelativeLocationPath
  206. | AbsoluteLocationPath
  207. ;
  208. AbsoluteLocationPath
  209. : SLASH
  210. {
  211. $$ = new ExprRoot ();
  212. }
  213. | SLASH RelativeLocationPath
  214. {
  215. $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
  216. }
  217. | SLASH2 RelativeLocationPath
  218. {
  219. NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
  220. $$ = new ExprSLASH (new ExprSLASH (new ExprRoot (), test), (NodeSet) $2);
  221. }
  222. ;
  223. RelativeLocationPath
  224. : Step
  225. | RelativeLocationPath SLASH Step
  226. {
  227. $$ = new ExprSLASH ((NodeSet) $1, (NodeSet) $3);
  228. }
  229. | RelativeLocationPath SLASH2 Step
  230. {
  231. NodeTest test = new NodeTypeTest (Axes.DescendantOrSelf, XPathNodeType.All);
  232. $$ = new ExprSLASH (new ExprSLASH ((NodeSet) $1, test), (NodeSet) $3);
  233. }
  234. ;
  235. Step
  236. : PredicatedStep
  237. | DOT
  238. {
  239. $$ = new NodeTypeTest (Axes.Self, XPathNodeType.All);
  240. }
  241. | DOT2
  242. {
  243. $$ = new NodeTypeTest (Axes.Parent, XPathNodeType.All);
  244. }
  245. ;
  246. PredicatedStep
  247. : AxisTest
  248. | PredicatedStep Predicate
  249. {
  250. $$ = new ExprFilter ((NodeSet) $1, (Expression) $2);
  251. }
  252. ;
  253. AxisTest
  254. : AxisSpecifier QName
  255. {
  256. $$ = new NodeNameTest ((Axes) $1, (XmlQualifiedName) $2);
  257. }
  258. | AxisSpecifier ASTERISK
  259. {
  260. $$ = new NodeTypeTest ((Axes) $1);
  261. }
  262. | AxisSpecifier NodeType PAREN_OPEN OptionalLiteral PAREN_CLOSE
  263. {
  264. $$ = new NodeTypeTest ((Axes) $1, (XPathNodeType) $2, (String) $4);
  265. }
  266. ;
  267. AxisSpecifier
  268. : /* empty */
  269. {
  270. $$ = Axes.Child;
  271. }
  272. | AT
  273. {
  274. $$ = Axes.Attribute;
  275. }
  276. | AxisName COLON2
  277. {
  278. $$ = $1;
  279. }
  280. ;
  281. NodeType
  282. : COMMENT { $$ = XPathNodeType.Comment; }
  283. | TEXT { $$ = XPathNodeType.Text; }
  284. | PROCESSING_INSTRUCTION { $$ = XPathNodeType.ProcessingInstruction; }
  285. | NODE { $$ = XPathNodeType.All; }
  286. ;
  287. FilterExpr
  288. : PrimaryExpr
  289. | FilterExpr Predicate
  290. {
  291. $$ = new ExprFilter ((Expression) $1, (Expression) $2);
  292. }
  293. ;
  294. PrimaryExpr
  295. : DOLLAR QName
  296. {
  297. $$ = new ExprVariable ((XmlQualifiedName) $2);
  298. }
  299. | PAREN_OPEN Expr PAREN_CLOSE
  300. {
  301. $$ = $2;
  302. }
  303. | LITERAL
  304. {
  305. $$ = new ExprLiteral ((String) $1);
  306. }
  307. | NUMBER
  308. {
  309. $$ = new ExprNumber ((double) $1);
  310. }
  311. | FunctionCall
  312. ;
  313. FunctionCall
  314. : FUNCTION_NAME PAREN_OPEN OptionalArgumentList PAREN_CLOSE
  315. {
  316. $$ = new ExprFunctionCall ((XmlQualifiedName) $1, (FunctionArguments) $3);
  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. }