Parser.jay 7.2 KB

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