oql-lexer.plex 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * OQL syntax analyzer, to be used prior to run the lexical analyzer
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. // Notes (from the source file: oql-lexer.plex) - Romain
  25. //
  26. // The strval rule is a little bit cryptic.
  27. // This is due to both a bug in the lexer generator and the complexity of our need
  28. // The rule means: either a quoted string with ", or a quoted string with '
  29. // literal " (resp. ') must be escaped by a \
  30. // \ must be escaped by an additional \
  31. //
  32. // Here are the issues and limitation found in the lexer generator:
  33. // * Matching simple quotes is an issue, because regexp are not correctly escaped (and the ESC code is escaped itself)
  34. // Workaround: insert '.chr(39).' which will be a real ' in the end
  35. // * Matching an alternate regexp is an issue because you must specify "|^...."
  36. // and the regexp parser will not accept that syntax
  37. // Workaround: insert '.chr(94).' which will be a real ^
  38. //
  39. // Let's analyze an overview of the regexp, we have
  40. // 1) The strval rule in the lexer definition
  41. // /"([^\\"]|\\"|\\\\)*"|'.chr(94).chr(39).'([^\\'.chr(39).']|\\'.chr(39).'|\\\\)*'.chr(39).'/
  42. // 2) Becomes the php expression in the lexer
  43. // (note the escaped double quotes, hopefully having no effect, but showing where the issue is!)
  44. // $myRegexp = '/^\"([^\\\\\"]|\\\\\"|\\\\\\\\)*\"|'.chr(94).chr(39).'([^\\\\'.chr(39).']|\\\\'.chr(39).'|\\\\\\\\)*'.chr(39).'/';
  45. //
  46. // To be fixed in LexerGenerator/Parser.y, in doLongestMatch (doFirstMatch is ok)
  47. //
  48. //
  49. // Now, let's explain how the regexp has been designed.
  50. // Here is a simplified version, dealing with simple quotes, and based on the assumption that the lexer generator has been fixed!
  51. // The strval rule in the lexer definition
  52. // /'([^\\']*(\\')*(\\\\)*)*'/
  53. // This means anything containing \\ or \' or any other char but a standalone ' or \
  54. // This means ' or \ could not be found without a preceding \
  55. //
  56. class OQLLexerRaw
  57. {
  58. protected $data; // input string
  59. public $token; // token id
  60. public $value; // token string representation
  61. protected $line; // current line
  62. protected $count; // current column
  63. function __construct($data)
  64. {
  65. $this->data = $data;
  66. $this->count = 0;
  67. $this->line = 1;
  68. }
  69. /*!lex2php
  70. %input $this->data
  71. %counter $this->count
  72. %token $this->token
  73. %value $this->value
  74. %line $this->line
  75. %matchlongest 1
  76. whitespace = /[ \t\n\r]+/
  77. select = "SELECT"
  78. from = "FROM"
  79. as_alias = "AS"
  80. where = "WHERE"
  81. join = "JOIN"
  82. on = "ON"
  83. coma = ","
  84. par_open = "("
  85. par_close = ")"
  86. math_div = "/"
  87. math_mult = "*"
  88. math_plus = "+"
  89. math_minus = "-"
  90. log_and = "AND"
  91. log_or = "OR"
  92. regexp = "REGEXP"
  93. eq = "="
  94. not_eq = "!="
  95. gt = ">"
  96. lt = "<"
  97. ge = ">="
  98. le = "<="
  99. like = "LIKE"
  100. not_like = "NOT LIKE"
  101. in = "IN"
  102. not_in = "NOT IN"
  103. interval = "INTERVAL"
  104. f_if = "IF"
  105. f_elt = "ELT"
  106. f_coalesce = "COALESCE"
  107. f_isnull = "ISNULL"
  108. f_concat = "CONCAT"
  109. f_substr = "SUBSTR"
  110. f_trim = "TRIM"
  111. f_date = "DATE"
  112. f_date_format = "DATE_FORMAT"
  113. f_current_date = "CURRENT_DATE"
  114. f_now = "NOW"
  115. f_time = "TIME"
  116. f_to_days = "TO_DAYS"
  117. f_from_days = "FROM_DAYS"
  118. f_year = "YEAR"
  119. f_month = "MONTH"
  120. f_day = "DAY"
  121. f_hour = "HOUR"
  122. f_minute = "MINUTE"
  123. f_second = "SECOND"
  124. f_date_add = "DATE_ADD"
  125. f_date_sub = "DATE_SUB"
  126. f_round = "ROUND"
  127. f_floor = "FLOOR"
  128. f_inet_aton = "INET_ATON"
  129. f_inet_ntoa = "INET_NTOA"
  130. numval = /[0-9]+|0x[0-9a-fA-F]+/
  131. strval = /"([^\\"]|\\"|\\\\)*"|'.chr(94).chr(39).'([^\\'.chr(39).']|\\'.chr(39).'|\\\\)*'.chr(39).'/
  132. name = /([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/
  133. varname = /:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/
  134. dot = "."
  135. */
  136. /*!lex2php
  137. whitespace {
  138. return false;
  139. }
  140. select {
  141. $this->token = OQLParser::SELECT;
  142. }
  143. from {
  144. $this->token = OQLParser::FROM;
  145. }
  146. as_alias {
  147. $this->token = OQLParser::AS_ALIAS;
  148. }
  149. where {
  150. $this->token = OQLParser::WHERE;
  151. }
  152. join {
  153. $this->token = OQLParser::JOIN;
  154. }
  155. on {
  156. $this->token = OQLParser::ON;
  157. }
  158. math_div {
  159. $this->token = OQLParser::MATH_DIV;
  160. }
  161. math_mult {
  162. $this->token = OQLParser::MATH_MULT;
  163. }
  164. math_plus {
  165. $this->token = OQLParser::MATH_PLUS;
  166. }
  167. math_minus {
  168. $this->token = OQLParser::MATH_MINUS;
  169. }
  170. log_and {
  171. $this->token = OQLParser::LOG_AND;
  172. }
  173. log_or {
  174. $this->token = OQLParser::LOG_OR;
  175. }
  176. coma {
  177. $this->token = OQLParser::COMA;
  178. }
  179. par_open {
  180. $this->token = OQLParser::PAR_OPEN;
  181. }
  182. par_close {
  183. $this->token = OQLParser::PAR_CLOSE;
  184. }
  185. regexp {
  186. $this->token = OQLParser::REGEXP;
  187. }
  188. eq {
  189. $this->token = OQLParser::EQ;
  190. }
  191. not_eq {
  192. $this->token = OQLParser::NOT_EQ;
  193. }
  194. gt {
  195. $this->token = OQLParser::GT;
  196. }
  197. lt {
  198. $this->token = OQLParser::LT;
  199. }
  200. ge {
  201. $this->token = OQLParser::GE;
  202. }
  203. le {
  204. $this->token = OQLParser::LE;
  205. }
  206. like {
  207. $this->token = OQLParser::LIKE;
  208. }
  209. not_like {
  210. $this->token = OQLParser::NOT_LIKE;
  211. }
  212. in {
  213. $this->token = OQLParser::IN;
  214. }
  215. not_in {
  216. $this->token = OQLParser::NOT_IN;
  217. }
  218. interval {
  219. $this->token = OQLParser::INTERVAL;
  220. }
  221. f_if {
  222. $this->token = OQLParser::F_IF;
  223. }
  224. f_elt {
  225. $this->token = OQLParser::F_ELT;
  226. }
  227. f_coalesce {
  228. $this->token = OQLParser::F_COALESCE;
  229. }
  230. f_isnull {
  231. $this->token = OQLParser::F_ISNULL;
  232. }
  233. f_concat {
  234. $this->token = OQLParser::F_CONCAT;
  235. }
  236. f_substr {
  237. $this->token = OQLParser::F_SUBSTR;
  238. }
  239. f_trim {
  240. $this->token = OQLParser::F_TRIM;
  241. }
  242. f_date {
  243. $this->token = OQLParser::F_DATE;
  244. }
  245. f_date_format {
  246. $this->token = OQLParser::F_DATE_FORMAT;
  247. }
  248. f_current_date {
  249. $this->token = OQLParser::F_CURRENT_DATE;
  250. }
  251. f_now {
  252. $this->token = OQLParser::F_NOW;
  253. }
  254. f_time {
  255. $this->token = OQLParser::F_TIME;
  256. }
  257. f_to_days {
  258. $this->token = OQLParser::F_TO_DAYS;
  259. }
  260. f_from_days {
  261. $this->token = OQLParser::F_FROM_DAYS;
  262. }
  263. f_year {
  264. $this->token = OQLParser::F_YEAR;
  265. }
  266. f_month {
  267. $this->token = OQLParser::F_MONTH;
  268. }
  269. f_day {
  270. $this->token = OQLParser::F_DAY;
  271. }
  272. f_hour {
  273. $this->token = OQLParser::F_HOUR;
  274. }
  275. f_minute {
  276. $this->token = OQLParser::F_MINUTE;
  277. }
  278. f_second {
  279. $this->token = OQLParser::F_SECOND;
  280. }
  281. f_date_add {
  282. $this->token = OQLParser::F_DATE_ADD;
  283. }
  284. f_date_sub {
  285. $this->token = OQLParser::F_DATE_SUB;
  286. }
  287. f_round {
  288. $this->token = OQLParser::F_ROUND;
  289. }
  290. f_floor {
  291. $this->token = OQLParser::F_FLOOR;
  292. }
  293. f_inet_aton {
  294. $this->token = OQLParser::F_INET_ATON;
  295. }
  296. f_inet_ntoa {
  297. $this->token = OQLParser::F_INET_NTOA;
  298. }
  299. numval {
  300. $this->token = OQLParser::NUMVAL;
  301. }
  302. strval {
  303. $this->token = OQLParser::STRVAL;
  304. }
  305. name {
  306. $this->token = OQLParser::NAME;
  307. }
  308. varname {
  309. $this->token = OQLParser::VARNAME;
  310. }
  311. dot {
  312. $this->token = OQLParser::DOT;
  313. }
  314. */
  315. }
  316. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  317. class OQLLexerException extends OQLException
  318. {
  319. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  320. {
  321. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  322. }
  323. }
  324. class OQLLexer extends OQLLexerRaw
  325. {
  326. public function getTokenPos()
  327. {
  328. return max(0, $this->count - strlen($this->value));
  329. }
  330. function yylex()
  331. {
  332. try
  333. {
  334. return parent::yylex();
  335. }
  336. catch (Exception $e)
  337. {
  338. $sMessage = $e->getMessage();
  339. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  340. {
  341. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  342. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  343. {
  344. $iLine = $aMatches[1];
  345. $sUnexpected = $aMatches[2];
  346. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  347. }
  348. }
  349. // Default: forward the exception
  350. throw $e;
  351. }
  352. }
  353. }
  354. ?>