oql-lexer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. private $_yy_state = 1;
  70. private $_yy_stack = array();
  71. function yylex()
  72. {
  73. return $this->{'yylex' . $this->_yy_state}();
  74. }
  75. function yypushstate($state)
  76. {
  77. array_push($this->_yy_stack, $this->_yy_state);
  78. $this->_yy_state = $state;
  79. }
  80. function yypopstate()
  81. {
  82. $this->_yy_state = array_pop($this->_yy_stack);
  83. }
  84. function yybegin($state)
  85. {
  86. $this->_yy_state = $state;
  87. }
  88. function yylex1()
  89. {
  90. if ($this->count >= strlen($this->data)) {
  91. return false; // end of input
  92. }
  93. do {
  94. $rules = array(
  95. '/^[ \t\n\r]+/',
  96. '/^SELECT/',
  97. '/^FROM/',
  98. '/^AS/',
  99. '/^WHERE/',
  100. '/^JOIN/',
  101. '/^ON/',
  102. '/^\//',
  103. '/^\\*/',
  104. '/^\\+/',
  105. '/^-/',
  106. '/^AND/',
  107. '/^OR/',
  108. '/^,/',
  109. '/^\\(/',
  110. '/^\\)/',
  111. '/^REGEXP/',
  112. '/^=/',
  113. '/^!=/',
  114. '/^>/',
  115. '/^</',
  116. '/^>=/',
  117. '/^<=/',
  118. '/^LIKE/',
  119. '/^NOT LIKE/',
  120. '/^IN/',
  121. '/^NOT IN/',
  122. '/^INTERVAL/',
  123. '/^IF/',
  124. '/^ELT/',
  125. '/^COALESCE/',
  126. '/^ISNULL/',
  127. '/^CONCAT/',
  128. '/^SUBSTR/',
  129. '/^TRIM/',
  130. '/^DATE/',
  131. '/^DATE_FORMAT/',
  132. '/^CURRENT_DATE/',
  133. '/^NOW/',
  134. '/^TIME/',
  135. '/^TO_DAYS/',
  136. '/^FROM_DAYS/',
  137. '/^YEAR/',
  138. '/^MONTH/',
  139. '/^DAY/',
  140. '/^HOUR/',
  141. '/^MINUTE/',
  142. '/^SECOND/',
  143. '/^DATE_ADD/',
  144. '/^DATE_SUB/',
  145. '/^ROUND/',
  146. '/^FLOOR/',
  147. '/^INET_ATON/',
  148. '/^INET_NTOA/',
  149. '/^[0-9]+|0x[0-9a-fA-F]+/',
  150. '/^\"([^\\\\\"]|\\\\\"|\\\\\\\\)*\"|'.chr(94).chr(39).'([^\\\\'.chr(39).']|\\\\'.chr(39).'|\\\\\\\\)*'.chr(39).'/',
  151. '/^([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/',
  152. '/^:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/',
  153. '/^\\./',
  154. );
  155. $match = false;
  156. foreach ($rules as $index => $rule) {
  157. if (preg_match($rule, substr($this->data, $this->count), $yymatches)) {
  158. if ($match) {
  159. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  160. $match = array($yymatches, $index); // matches, token
  161. }
  162. } else {
  163. $match = array($yymatches, $index);
  164. }
  165. }
  166. }
  167. if (!$match) {
  168. throw new Exception('Unexpected input at line' . $this->line .
  169. ': ' . $this->data[$this->count]);
  170. }
  171. $this->token = $match[1];
  172. $this->value = $match[0][0];
  173. $yysubmatches = $match[0];
  174. array_shift($yysubmatches);
  175. if (!$yysubmatches) {
  176. $yysubmatches = array();
  177. }
  178. $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
  179. if ($r === null) {
  180. $this->count += strlen($this->value);
  181. $this->line += substr_count($this->value, "\n");
  182. // accept this token
  183. return true;
  184. } elseif ($r === true) {
  185. // we have changed state
  186. // process this token in the new state
  187. return $this->yylex();
  188. } elseif ($r === false) {
  189. $this->count += strlen($this->value);
  190. $this->line += substr_count($this->value, "\n");
  191. if ($this->count >= strlen($this->data)) {
  192. return false; // end of input
  193. }
  194. // skip this token
  195. continue;
  196. } else {
  197. $yy_yymore_patterns = array_slice($rules, $this->token, true);
  198. // yymore is needed
  199. do {
  200. if (!isset($yy_yymore_patterns[$this->token])) {
  201. throw new Exception('cannot do yymore for the last token');
  202. }
  203. $match = false;
  204. foreach ($yy_yymore_patterns[$this->token] as $index => $rule) {
  205. if (preg_match('/' . $rule . '/',
  206. substr($this->data, $this->count), $yymatches)) {
  207. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  208. if ($match) {
  209. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  210. $match = array($yymatches, $index); // matches, token
  211. }
  212. } else {
  213. $match = array($yymatches, $index);
  214. }
  215. }
  216. }
  217. if (!$match) {
  218. throw new Exception('Unexpected input at line' . $this->line .
  219. ': ' . $this->data[$this->count]);
  220. }
  221. $this->token = $match[1];
  222. $this->value = $match[0][0];
  223. $yysubmatches = $match[0];
  224. array_shift($yysubmatches);
  225. if (!$yysubmatches) {
  226. $yysubmatches = array();
  227. }
  228. $this->line = substr_count($this->value, "\n");
  229. $r = $this->{'yy_r1_' . $this->token}();
  230. } while ($r !== null || !$r);
  231. if ($r === true) {
  232. // we have changed state
  233. // process this token in the new state
  234. return $this->yylex();
  235. } else {
  236. // accept
  237. $this->count += strlen($this->value);
  238. $this->line += substr_count($this->value, "\n");
  239. return true;
  240. }
  241. }
  242. } while (true);
  243. } // end function
  244. function yy_r1_0($yy_subpatterns)
  245. {
  246. return false;
  247. }
  248. function yy_r1_1($yy_subpatterns)
  249. {
  250. $this->token = OQLParser::SELECT;
  251. }
  252. function yy_r1_2($yy_subpatterns)
  253. {
  254. $this->token = OQLParser::FROM;
  255. }
  256. function yy_r1_3($yy_subpatterns)
  257. {
  258. $this->token = OQLParser::AS_ALIAS;
  259. }
  260. function yy_r1_4($yy_subpatterns)
  261. {
  262. $this->token = OQLParser::WHERE;
  263. }
  264. function yy_r1_5($yy_subpatterns)
  265. {
  266. $this->token = OQLParser::JOIN;
  267. }
  268. function yy_r1_6($yy_subpatterns)
  269. {
  270. $this->token = OQLParser::ON;
  271. }
  272. function yy_r1_7($yy_subpatterns)
  273. {
  274. $this->token = OQLParser::MATH_DIV;
  275. }
  276. function yy_r1_8($yy_subpatterns)
  277. {
  278. $this->token = OQLParser::MATH_MULT;
  279. }
  280. function yy_r1_9($yy_subpatterns)
  281. {
  282. $this->token = OQLParser::MATH_PLUS;
  283. }
  284. function yy_r1_10($yy_subpatterns)
  285. {
  286. $this->token = OQLParser::MATH_MINUS;
  287. }
  288. function yy_r1_11($yy_subpatterns)
  289. {
  290. $this->token = OQLParser::LOG_AND;
  291. }
  292. function yy_r1_12($yy_subpatterns)
  293. {
  294. $this->token = OQLParser::LOG_OR;
  295. }
  296. function yy_r1_13($yy_subpatterns)
  297. {
  298. $this->token = OQLParser::COMA;
  299. }
  300. function yy_r1_14($yy_subpatterns)
  301. {
  302. $this->token = OQLParser::PAR_OPEN;
  303. }
  304. function yy_r1_15($yy_subpatterns)
  305. {
  306. $this->token = OQLParser::PAR_CLOSE;
  307. }
  308. function yy_r1_16($yy_subpatterns)
  309. {
  310. $this->token = OQLParser::REGEXP;
  311. }
  312. function yy_r1_17($yy_subpatterns)
  313. {
  314. $this->token = OQLParser::EQ;
  315. }
  316. function yy_r1_18($yy_subpatterns)
  317. {
  318. $this->token = OQLParser::NOT_EQ;
  319. }
  320. function yy_r1_19($yy_subpatterns)
  321. {
  322. $this->token = OQLParser::GT;
  323. }
  324. function yy_r1_20($yy_subpatterns)
  325. {
  326. $this->token = OQLParser::LT;
  327. }
  328. function yy_r1_21($yy_subpatterns)
  329. {
  330. $this->token = OQLParser::GE;
  331. }
  332. function yy_r1_22($yy_subpatterns)
  333. {
  334. $this->token = OQLParser::LE;
  335. }
  336. function yy_r1_23($yy_subpatterns)
  337. {
  338. $this->token = OQLParser::LIKE;
  339. }
  340. function yy_r1_24($yy_subpatterns)
  341. {
  342. $this->token = OQLParser::NOT_LIKE;
  343. }
  344. function yy_r1_25($yy_subpatterns)
  345. {
  346. $this->token = OQLParser::IN;
  347. }
  348. function yy_r1_26($yy_subpatterns)
  349. {
  350. $this->token = OQLParser::NOT_IN;
  351. }
  352. function yy_r1_27($yy_subpatterns)
  353. {
  354. $this->token = OQLParser::INTERVAL;
  355. }
  356. function yy_r1_28($yy_subpatterns)
  357. {
  358. $this->token = OQLParser::F_IF;
  359. }
  360. function yy_r1_29($yy_subpatterns)
  361. {
  362. $this->token = OQLParser::F_ELT;
  363. }
  364. function yy_r1_30($yy_subpatterns)
  365. {
  366. $this->token = OQLParser::F_COALESCE;
  367. }
  368. function yy_r1_31($yy_subpatterns)
  369. {
  370. $this->token = OQLParser::F_ISNULL;
  371. }
  372. function yy_r1_32($yy_subpatterns)
  373. {
  374. $this->token = OQLParser::F_CONCAT;
  375. }
  376. function yy_r1_33($yy_subpatterns)
  377. {
  378. $this->token = OQLParser::F_SUBSTR;
  379. }
  380. function yy_r1_34($yy_subpatterns)
  381. {
  382. $this->token = OQLParser::F_TRIM;
  383. }
  384. function yy_r1_35($yy_subpatterns)
  385. {
  386. $this->token = OQLParser::F_DATE;
  387. }
  388. function yy_r1_36($yy_subpatterns)
  389. {
  390. $this->token = OQLParser::F_DATE_FORMAT;
  391. }
  392. function yy_r1_37($yy_subpatterns)
  393. {
  394. $this->token = OQLParser::F_CURRENT_DATE;
  395. }
  396. function yy_r1_38($yy_subpatterns)
  397. {
  398. $this->token = OQLParser::F_NOW;
  399. }
  400. function yy_r1_39($yy_subpatterns)
  401. {
  402. $this->token = OQLParser::F_TIME;
  403. }
  404. function yy_r1_40($yy_subpatterns)
  405. {
  406. $this->token = OQLParser::F_TO_DAYS;
  407. }
  408. function yy_r1_41($yy_subpatterns)
  409. {
  410. $this->token = OQLParser::F_FROM_DAYS;
  411. }
  412. function yy_r1_42($yy_subpatterns)
  413. {
  414. $this->token = OQLParser::F_YEAR;
  415. }
  416. function yy_r1_43($yy_subpatterns)
  417. {
  418. $this->token = OQLParser::F_MONTH;
  419. }
  420. function yy_r1_44($yy_subpatterns)
  421. {
  422. $this->token = OQLParser::F_DAY;
  423. }
  424. function yy_r1_45($yy_subpatterns)
  425. {
  426. $this->token = OQLParser::F_HOUR;
  427. }
  428. function yy_r1_46($yy_subpatterns)
  429. {
  430. $this->token = OQLParser::F_MINUTE;
  431. }
  432. function yy_r1_47($yy_subpatterns)
  433. {
  434. $this->token = OQLParser::F_SECOND;
  435. }
  436. function yy_r1_48($yy_subpatterns)
  437. {
  438. $this->token = OQLParser::F_DATE_ADD;
  439. }
  440. function yy_r1_49($yy_subpatterns)
  441. {
  442. $this->token = OQLParser::F_DATE_SUB;
  443. }
  444. function yy_r1_50($yy_subpatterns)
  445. {
  446. $this->token = OQLParser::F_ROUND;
  447. }
  448. function yy_r1_51($yy_subpatterns)
  449. {
  450. $this->token = OQLParser::F_FLOOR;
  451. }
  452. function yy_r1_52($yy_subpatterns)
  453. {
  454. $this->token = OQLParser::F_INET_ATON;
  455. }
  456. function yy_r1_53($yy_subpatterns)
  457. {
  458. $this->token = OQLParser::F_INET_NTOA;
  459. }
  460. function yy_r1_54($yy_subpatterns)
  461. {
  462. $this->token = OQLParser::NUMVAL;
  463. }
  464. function yy_r1_55($yy_subpatterns)
  465. {
  466. $this->token = OQLParser::STRVAL;
  467. }
  468. function yy_r1_56($yy_subpatterns)
  469. {
  470. $this->token = OQLParser::NAME;
  471. }
  472. function yy_r1_57($yy_subpatterns)
  473. {
  474. $this->token = OQLParser::VARNAME;
  475. }
  476. function yy_r1_58($yy_subpatterns)
  477. {
  478. $this->token = OQLParser::DOT;
  479. }
  480. }
  481. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  482. class OQLLexerException extends OQLException
  483. {
  484. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  485. {
  486. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  487. }
  488. }
  489. class OQLLexer extends OQLLexerRaw
  490. {
  491. public function getTokenPos()
  492. {
  493. return max(0, $this->count - strlen($this->value));
  494. }
  495. function yylex()
  496. {
  497. try
  498. {
  499. return parent::yylex();
  500. }
  501. catch (Exception $e)
  502. {
  503. $sMessage = $e->getMessage();
  504. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  505. {
  506. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  507. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  508. {
  509. $iLine = $aMatches[1];
  510. $sUnexpected = $aMatches[2];
  511. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  512. }
  513. }
  514. // Default: forward the exception
  515. throw $e;
  516. }
  517. }
  518. }
  519. ?>