oql-parser.y 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. This is a LALR(1) grammar
  3. (seek for Lemon grammar to get some documentation from the Net)
  4. That doc was helpful: http://www.hwaci.com/sw/lemon/lemon.html
  5. To handle operators precedence we could have used the %left directive
  6. (we took another option, because that one was discovered right after...
  7. which option is the best for us?)
  8. Example:
  9. %left LOG_AND.
  10. %left LOG_OR.
  11. %nonassoc EQ NE GT GE LT LE.
  12. %left PLUS MINUS.
  13. %left TIMES DIVIDE MOD.
  14. %right EXP NOT.
  15. TODO : solve the 2 remaining shift-reduce conflicts (JOIN)
  16. */
  17. %name OQLParser_
  18. %declare_class {class OQLParserRaw}
  19. %syntax_error {
  20. throw new OQLParserException($this->m_sSourceQuery, $this->m_iLine, $this->m_iCol, $this->tokenName($yymajor), $TOKEN);
  21. }
  22. result ::= query(X). { $this->my_result = X; }
  23. result ::= condition(X). { $this->my_result = X; }
  24. query(A) ::= SELECT class_name(X) join_statement(J) where_statement(W). {
  25. A = new OqlQuery(X, X, W, J);
  26. }
  27. query(A) ::= SELECT class_name(X) AS_ALIAS class_name(Y) join_statement(J) where_statement(W). {
  28. A = new OqlQuery(X, Y, W, J);
  29. }
  30. where_statement(A) ::= WHERE condition(C). { A = C;}
  31. where_statement(A) ::= . { A = null;}
  32. join_statement(A) ::= join_item(J) join_statement(S). {
  33. // insert the join statement on top of the existing list
  34. array_unshift(S, J);
  35. // and return the updated array
  36. A = S;
  37. }
  38. join_statement(A) ::= join_item(J). {
  39. A = Array(J);
  40. }
  41. join_statement(A) ::= . { A = null;}
  42. join_item(A) ::= JOIN class_name(X) AS_ALIAS class_name(Y) ON join_condition(C).
  43. {
  44. // create an array with one single item
  45. A = new OqlJoinSpec(X, Y, C);
  46. }
  47. join_item(A) ::= JOIN class_name(X) ON join_condition(C).
  48. {
  49. // create an array with one single item
  50. A = new OqlJoinSpec(X, X, C);
  51. }
  52. join_condition(A) ::= field_id(X) EQ field_id(Y). { A = new BinaryOqlExpression(X, '=', Y); }
  53. condition(A) ::= expression_prio4(X). { A = X; }
  54. expression_basic(A) ::= scalar(X). { A = X; }
  55. expression_basic(A) ::= field_id(X). { A = X; }
  56. expression_basic(A) ::= func_name(X) PAR_OPEN arg_list(Y) PAR_CLOSE. { A = new FunctionOqlExpression(X, Y); }
  57. expression_basic(A) ::= PAR_OPEN expression_prio4(X) PAR_CLOSE. { A = X; }
  58. expression_basic(A) ::= expression_basic(X) list_operator(Y) list(Z). { A = new BinaryOqlExpression(X, Y, Z); }
  59. expression_prio1(A) ::= expression_basic(X). { A = X; }
  60. expression_prio1(A) ::= expression_prio1(X) operator1(Y) expression_basic(Z). { A = new BinaryOqlExpression(X, Y, Z); }
  61. expression_prio2(A) ::= expression_prio1(X). { A = X; }
  62. expression_prio2(A) ::= expression_prio2(X) operator2(Y) expression_prio1(Z). { A = new BinaryOqlExpression(X, Y, Z); }
  63. expression_prio3(A) ::= expression_prio2(X). { A = X; }
  64. expression_prio3(A) ::= expression_prio3(X) operator3(Y) expression_prio2(Z). { A = new BinaryOqlExpression(X, Y, Z); }
  65. expression_prio4(A) ::= expression_prio3(X). { A = X; }
  66. expression_prio4(A) ::= expression_prio4(X) operator4(Y) expression_prio3(Z). { A = new BinaryOqlExpression(X, Y, Z); }
  67. list(A) ::= PAR_OPEN scalar_list(X) PAR_CLOSE. {
  68. A = new ListOqlExpression(X);
  69. }
  70. scalar_list(A) ::= scalar(X). {
  71. A = array(X);
  72. }
  73. scalar_list(A) ::= scalar_list(L) COMA scalar(X). {
  74. array_push(L, X);
  75. A = L;
  76. }
  77. arg_list(A) ::= . {
  78. A = array();
  79. }
  80. arg_list(A) ::= argument(X). {
  81. A = array(X);
  82. }
  83. arg_list(A) ::= arg_list(L) COMA argument(X). {
  84. array_push(L, X);
  85. A = L;
  86. }
  87. argument(A) ::= expression_prio4(X). { A = X; }
  88. argument(A) ::= INTERVAL expression_prio4(X) interval_unit(Y). { A = new IntervalOqlExpression(X, Y); }
  89. interval_unit(A) ::= F_DAY(X). { A = X; }
  90. interval_unit(A) ::= F_MONTH(X). { A = X; }
  91. interval_unit(A) ::= F_YEAR(X). { A = X; }
  92. scalar(A) ::= num_scalar(X). { A = X; }
  93. scalar(A) ::= str_scalar(X). { A = X; }
  94. num_scalar(A) ::= num_value(X). { A = new ScalarOqlExpression(X); }
  95. str_scalar(A) ::= str_value(X). { A = new ScalarOqlExpression(X); }
  96. field_id(A) ::= name(X). { A = new FieldOqlExpression(X); }
  97. field_id(A) ::= class_name(X) DOT name(Y). { A = new FieldOqlExpression(Y, X); }
  98. class_name(A) ::= name(X). { A=X; }
  99. name(A) ::= NAME(X). {
  100. if (X[0] == '`')
  101. {
  102. $name = substr(X, 1, strlen(X) - 2);
  103. }
  104. else
  105. {
  106. $name = X;
  107. }
  108. A = new OqlName($name, $this->m_iColPrev);
  109. }
  110. num_value(A) ::= NUMVAL(X). {A=X;}
  111. str_value(A) ::= STRVAL(X). {A=stripslashes(substr(X, 1, strlen(X) - 2));}
  112. operator1(A) ::= num_operator1(X). {A=X;}
  113. operator2(A) ::= num_operator2(X). {A=X;}
  114. operator2(A) ::= str_operator(X). {A=X;}
  115. operator2(A) ::= EQ(X). {A=X;}
  116. operator2(A) ::= NOT_EQ(X). {A=X;}
  117. operator3(A) ::= LOG_AND(X). {A=X;}
  118. operator4(A) ::= LOG_OR(X). {A=X;}
  119. num_operator1(A) ::= MATH_DIV(X). {A=X;}
  120. num_operator1(A) ::= MATH_MULT(X). {A=X;}
  121. num_operator2(A) ::= MATH_PLUS(X). {A=X;}
  122. num_operator2(A) ::= MATH_MINUS(X). {A=X;}
  123. num_operator2(A) ::= GT(X). {A=X;}
  124. num_operator2(A) ::= LT(X). {A=X;}
  125. num_operator2(A) ::= GE(X). {A=X;}
  126. num_operator2(A) ::= LE(X). {A=X;}
  127. str_operator(A) ::= LIKE(X). {A=X;}
  128. str_operator(A) ::= NOT_LIKE(X). {A=X;}
  129. list_operator(A) ::= IN(X). {A=X;}
  130. list_operator(A) ::= NOT_IN(X). {A=X;}
  131. func_name(A) ::= F_IF(X). { A=X; }
  132. func_name(A) ::= F_ELT(X). { A=X; }
  133. func_name(A) ::= F_COALESCE(X). { A=X; }
  134. func_name(A) ::= F_CONCAT(X). { A=X; }
  135. func_name(A) ::= F_SUBSTR(X). { A=X; }
  136. func_name(A) ::= F_TRIM(X). { A=X; }
  137. func_name(A) ::= F_DATE(X). { A=X; }
  138. func_name(A) ::= F_DATE_FORMAT(X). { A=X; }
  139. func_name(A) ::= F_CURRENT_DATE(X). { A=X; }
  140. func_name(A) ::= F_NOW(X). { A=X; }
  141. func_name(A) ::= F_TIME(X). { A=X; }
  142. func_name(A) ::= F_TO_DAYS(X). { A=X; }
  143. func_name(A) ::= F_FROM_DAYS(X). { A=X; }
  144. func_name(A) ::= F_YEAR(X). { A=X; }
  145. func_name(A) ::= F_MONTH(X). { A=X; }
  146. func_name(A) ::= F_DAY(X). { A=X; }
  147. func_name(A) ::= F_DATE_ADD(X). { A=X; }
  148. func_name(A) ::= F_DATE_SUB(X). { A=X; }
  149. func_name(A) ::= F_ROUND(X). { A=X; }
  150. func_name(A) ::= F_FLOOR(X). { A=X; }
  151. %code {
  152. class OQLParserException extends OQLException
  153. {
  154. public function __construct($sInput, $iLine, $iCol, $sTokenName, $sTokenValue)
  155. {
  156. $sIssue = "Unexpected token $sTokenName";
  157. parent::__construct($sIssue, $sInput, $iLine, $iCol, $sTokenValue);
  158. }
  159. }
  160. class OQLParser extends OQLParserRaw
  161. {
  162. // dirty, but working for us (no other mean to get the final result :-(
  163. protected $my_result;
  164. public function GetResult()
  165. {
  166. return $this->my_result;
  167. }
  168. // More info on the source query and the current position while parsing it
  169. // Data used when an exception is raised
  170. protected $m_iLine; // still not used
  171. protected $m_iCol;
  172. protected $m_iColPrev; // this is the interesting one, because the parser will reduce on the next token
  173. protected $m_sSourceQuery;
  174. public function __construct($sQuery)
  175. {
  176. $this->m_iLine = 0;
  177. $this->m_iCol = 0;
  178. $this->m_iColPrev = 0;
  179. $this->m_sSourceQuery = $sQuery;
  180. // no constructor - parent::__construct();
  181. }
  182. public function doParse($token, $value, $iCurrPosition = 0)
  183. {
  184. $this->m_iColPrev = $this->m_iCol;
  185. $this->m_iCol = $iCurrPosition;
  186. return parent::DoParse($token, $value);
  187. }
  188. public function doFinish()
  189. {
  190. $this->doParse(0, 0);
  191. return $this->my_result;
  192. }
  193. public function __destruct()
  194. {
  195. // Bug in the original destructor, causing an infinite loop !
  196. // This is a real issue when a fatal error occurs on the first token (the error could not be seen)
  197. if (is_null($this->yyidx))
  198. {
  199. $this->yyidx = -1;
  200. }
  201. parent::__destruct();
  202. }
  203. }
  204. }