oql-lexer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * OQL syntax analyzer, to be used prior to run the lexical analyzer
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. '/\G[ \t\n\r]+/ ',
  96. '/\GSELECT/ ',
  97. '/\GFROM/ ',
  98. '/\GAS/ ',
  99. '/\GWHERE/ ',
  100. '/\GJOIN/ ',
  101. '/\GON/ ',
  102. '/\G\// ',
  103. '/\G\\*/ ',
  104. '/\G\\+/ ',
  105. '/\G-/ ',
  106. '/\GAND/ ',
  107. '/\GOR/ ',
  108. '/\G\\|/ ',
  109. '/\G&/ ',
  110. '/\G\\^/ ',
  111. '/\G<</ ',
  112. '/\G>>/ ',
  113. '/\G,/ ',
  114. '/\G\\(/ ',
  115. '/\G\\)/ ',
  116. '/\GREGEXP/ ',
  117. '/\G=/ ',
  118. '/\G!=/ ',
  119. '/\G>/ ',
  120. '/\G</ ',
  121. '/\G>=/ ',
  122. '/\G<=/ ',
  123. '/\GLIKE/ ',
  124. '/\GNOT LIKE/ ',
  125. '/\GIN/ ',
  126. '/\GNOT IN/ ',
  127. '/\GINTERVAL/ ',
  128. '/\GIF/ ',
  129. '/\GELT/ ',
  130. '/\GCOALESCE/ ',
  131. '/\GISNULL/ ',
  132. '/\GCONCAT/ ',
  133. '/\GSUBSTR/ ',
  134. '/\GTRIM/ ',
  135. '/\GDATE/ ',
  136. '/\GDATE_FORMAT/ ',
  137. '/\GCURRENT_DATE/ ',
  138. '/\GNOW/ ',
  139. '/\GTIME/ ',
  140. '/\GTO_DAYS/ ',
  141. '/\GFROM_DAYS/ ',
  142. '/\GYEAR/ ',
  143. '/\GMONTH/ ',
  144. '/\GDAY/ ',
  145. '/\GHOUR/ ',
  146. '/\GMINUTE/ ',
  147. '/\GSECOND/ ',
  148. '/\GDATE_ADD/ ',
  149. '/\GDATE_SUB/ ',
  150. '/\GROUND/ ',
  151. '/\GFLOOR/ ',
  152. '/\GINET_ATON/ ',
  153. '/\GINET_NTOA/ ',
  154. '/\GBELOW/ ',
  155. '/\GBELOW STRICT/ ',
  156. '/\GNOT BELOW/ ',
  157. '/\GNOT BELOW STRICT/ ',
  158. '/\GABOVE/ ',
  159. '/\GABOVE STRICT/ ',
  160. '/\GNOT ABOVE/ ',
  161. '/\GNOT ABOVE STRICT/ ',
  162. '/\G(0x[0-9a-fA-F]+)/ ',
  163. '/\G([0-9]+)/ ',
  164. '/\G\"([^\\\\\"]|\\\\\"|\\\\\\\\)*\"|'.chr(94).chr(39).'([^\\\\'.chr(39).']|\\\\'.chr(39).'|\\\\\\\\)*'.chr(39).'/ ',
  165. '/\G([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/ ',
  166. '/\G:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/ ',
  167. '/\G\\./ ',
  168. );
  169. $match = false;
  170. foreach ($rules as $index => $rule) {
  171. if (preg_match($rule, substr($this->data, $this->count), $yymatches)) {
  172. if ($match) {
  173. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  174. $match = array($yymatches, $index); // matches, token
  175. }
  176. } else {
  177. $match = array($yymatches, $index);
  178. }
  179. }
  180. }
  181. if (!$match) {
  182. throw new Exception('Unexpected input at line ' . $this->line .
  183. ': ' . $this->data[$this->count]);
  184. }
  185. $this->token = $match[1];
  186. $this->value = $match[0][0];
  187. $yysubmatches = $match[0];
  188. array_shift($yysubmatches);
  189. if (!$yysubmatches) {
  190. $yysubmatches = array();
  191. }
  192. $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
  193. if ($r === null) {
  194. $this->count += strlen($this->value);
  195. $this->line += substr_count($this->value, "\n");
  196. // accept this token
  197. return true;
  198. } elseif ($r === true) {
  199. // we have changed state
  200. // process this token in the new state
  201. return $this->yylex();
  202. } elseif ($r === false) {
  203. $this->count += strlen($this->value);
  204. $this->line += substr_count($this->value, "\n");
  205. if ($this->count >= strlen($this->data)) {
  206. return false; // end of input
  207. }
  208. // skip this token
  209. continue;
  210. } else {
  211. $yy_yymore_patterns = array_slice($rules, $this->token, true);
  212. // yymore is needed
  213. do {
  214. if (!isset($yy_yymore_patterns[$this->token])) {
  215. throw new Exception('cannot do yymore for the last token');
  216. }
  217. $match = false;
  218. foreach ($yy_yymore_patterns[$this->token] as $index => $rule) {
  219. if (preg_match('/' . $rule . '/',
  220. $this->data, $yymatches, null, $this->count)) {
  221. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  222. if ($match) {
  223. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  224. $match = array($yymatches, $index); // matches, token
  225. }
  226. } else {
  227. $match = array($yymatches, $index);
  228. }
  229. }
  230. }
  231. if (!$match) {
  232. throw new Exception('Unexpected input at line ' . $this->line .
  233. ': ' . $this->data[$this->count]);
  234. }
  235. $this->token = $match[1];
  236. $this->value = $match[0][0];
  237. $yysubmatches = $match[0];
  238. array_shift($yysubmatches);
  239. if (!$yysubmatches) {
  240. $yysubmatches = array();
  241. }
  242. $this->line = substr_count($this->value, "\n");
  243. $r = $this->{'yy_r1_' . $this->token}();
  244. } while ($r !== null || !$r);
  245. if ($r === true) {
  246. // we have changed state
  247. // process this token in the new state
  248. return $this->yylex();
  249. } else {
  250. // accept
  251. $this->count += strlen($this->value);
  252. $this->line += substr_count($this->value, "\n");
  253. return true;
  254. }
  255. }
  256. } while (true);
  257. } // end function
  258. function yy_r1_0($yy_subpatterns)
  259. {
  260. return false;
  261. }
  262. function yy_r1_1($yy_subpatterns)
  263. {
  264. $this->token = OQLParser::SELECT;
  265. }
  266. function yy_r1_2($yy_subpatterns)
  267. {
  268. $this->token = OQLParser::FROM;
  269. }
  270. function yy_r1_3($yy_subpatterns)
  271. {
  272. $this->token = OQLParser::AS_ALIAS;
  273. }
  274. function yy_r1_4($yy_subpatterns)
  275. {
  276. $this->token = OQLParser::WHERE;
  277. }
  278. function yy_r1_5($yy_subpatterns)
  279. {
  280. $this->token = OQLParser::JOIN;
  281. }
  282. function yy_r1_6($yy_subpatterns)
  283. {
  284. $this->token = OQLParser::ON;
  285. }
  286. function yy_r1_7($yy_subpatterns)
  287. {
  288. $this->token = OQLParser::MATH_DIV;
  289. }
  290. function yy_r1_8($yy_subpatterns)
  291. {
  292. $this->token = OQLParser::MATH_MULT;
  293. }
  294. function yy_r1_9($yy_subpatterns)
  295. {
  296. $this->token = OQLParser::MATH_PLUS;
  297. }
  298. function yy_r1_10($yy_subpatterns)
  299. {
  300. $this->token = OQLParser::MATH_MINUS;
  301. }
  302. function yy_r1_11($yy_subpatterns)
  303. {
  304. $this->token = OQLParser::LOG_AND;
  305. }
  306. function yy_r1_12($yy_subpatterns)
  307. {
  308. $this->token = OQLParser::LOG_OR;
  309. }
  310. function yy_r1_13($yy_subpatterns)
  311. {
  312. $this->token = OQLParser::BITWISE_OR;
  313. }
  314. function yy_r1_14($yy_subpatterns)
  315. {
  316. $this->token = OQLParser::BITWISE_AND;
  317. }
  318. function yy_r1_15($yy_subpatterns)
  319. {
  320. $this->token = OQLParser::BITWISE_XOR;
  321. }
  322. function yy_r1_16($yy_subpatterns)
  323. {
  324. $this->token = OQLParser::BITWISE_LEFT_SHIFT;
  325. }
  326. function yy_r1_17($yy_subpatterns)
  327. {
  328. $this->token = OQLParser::BITWISE_RIGHT_SHIFT;
  329. }
  330. function yy_r1_18($yy_subpatterns)
  331. {
  332. $this->token = OQLParser::COMA;
  333. }
  334. function yy_r1_19($yy_subpatterns)
  335. {
  336. $this->token = OQLParser::PAR_OPEN;
  337. }
  338. function yy_r1_20($yy_subpatterns)
  339. {
  340. $this->token = OQLParser::PAR_CLOSE;
  341. }
  342. function yy_r1_21($yy_subpatterns)
  343. {
  344. $this->token = OQLParser::REGEXP;
  345. }
  346. function yy_r1_22($yy_subpatterns)
  347. {
  348. $this->token = OQLParser::EQ;
  349. }
  350. function yy_r1_23($yy_subpatterns)
  351. {
  352. $this->token = OQLParser::NOT_EQ;
  353. }
  354. function yy_r1_24($yy_subpatterns)
  355. {
  356. $this->token = OQLParser::GT;
  357. }
  358. function yy_r1_25($yy_subpatterns)
  359. {
  360. $this->token = OQLParser::LT;
  361. }
  362. function yy_r1_26($yy_subpatterns)
  363. {
  364. $this->token = OQLParser::GE;
  365. }
  366. function yy_r1_27($yy_subpatterns)
  367. {
  368. $this->token = OQLParser::LE;
  369. }
  370. function yy_r1_28($yy_subpatterns)
  371. {
  372. $this->token = OQLParser::LIKE;
  373. }
  374. function yy_r1_29($yy_subpatterns)
  375. {
  376. $this->token = OQLParser::NOT_LIKE;
  377. }
  378. function yy_r1_30($yy_subpatterns)
  379. {
  380. $this->token = OQLParser::IN;
  381. }
  382. function yy_r1_31($yy_subpatterns)
  383. {
  384. $this->token = OQLParser::NOT_IN;
  385. }
  386. function yy_r1_32($yy_subpatterns)
  387. {
  388. $this->token = OQLParser::INTERVAL;
  389. }
  390. function yy_r1_33($yy_subpatterns)
  391. {
  392. $this->token = OQLParser::F_IF;
  393. }
  394. function yy_r1_34($yy_subpatterns)
  395. {
  396. $this->token = OQLParser::F_ELT;
  397. }
  398. function yy_r1_35($yy_subpatterns)
  399. {
  400. $this->token = OQLParser::F_COALESCE;
  401. }
  402. function yy_r1_36($yy_subpatterns)
  403. {
  404. $this->token = OQLParser::F_ISNULL;
  405. }
  406. function yy_r1_37($yy_subpatterns)
  407. {
  408. $this->token = OQLParser::F_CONCAT;
  409. }
  410. function yy_r1_38($yy_subpatterns)
  411. {
  412. $this->token = OQLParser::F_SUBSTR;
  413. }
  414. function yy_r1_39($yy_subpatterns)
  415. {
  416. $this->token = OQLParser::F_TRIM;
  417. }
  418. function yy_r1_40($yy_subpatterns)
  419. {
  420. $this->token = OQLParser::F_DATE;
  421. }
  422. function yy_r1_41($yy_subpatterns)
  423. {
  424. $this->token = OQLParser::F_DATE_FORMAT;
  425. }
  426. function yy_r1_42($yy_subpatterns)
  427. {
  428. $this->token = OQLParser::F_CURRENT_DATE;
  429. }
  430. function yy_r1_43($yy_subpatterns)
  431. {
  432. $this->token = OQLParser::F_NOW;
  433. }
  434. function yy_r1_44($yy_subpatterns)
  435. {
  436. $this->token = OQLParser::F_TIME;
  437. }
  438. function yy_r1_45($yy_subpatterns)
  439. {
  440. $this->token = OQLParser::F_TO_DAYS;
  441. }
  442. function yy_r1_46($yy_subpatterns)
  443. {
  444. $this->token = OQLParser::F_FROM_DAYS;
  445. }
  446. function yy_r1_47($yy_subpatterns)
  447. {
  448. $this->token = OQLParser::F_YEAR;
  449. }
  450. function yy_r1_48($yy_subpatterns)
  451. {
  452. $this->token = OQLParser::F_MONTH;
  453. }
  454. function yy_r1_49($yy_subpatterns)
  455. {
  456. $this->token = OQLParser::F_DAY;
  457. }
  458. function yy_r1_50($yy_subpatterns)
  459. {
  460. $this->token = OQLParser::F_HOUR;
  461. }
  462. function yy_r1_51($yy_subpatterns)
  463. {
  464. $this->token = OQLParser::F_MINUTE;
  465. }
  466. function yy_r1_52($yy_subpatterns)
  467. {
  468. $this->token = OQLParser::F_SECOND;
  469. }
  470. function yy_r1_53($yy_subpatterns)
  471. {
  472. $this->token = OQLParser::F_DATE_ADD;
  473. }
  474. function yy_r1_54($yy_subpatterns)
  475. {
  476. $this->token = OQLParser::F_DATE_SUB;
  477. }
  478. function yy_r1_55($yy_subpatterns)
  479. {
  480. $this->token = OQLParser::F_ROUND;
  481. }
  482. function yy_r1_56($yy_subpatterns)
  483. {
  484. $this->token = OQLParser::F_FLOOR;
  485. }
  486. function yy_r1_57($yy_subpatterns)
  487. {
  488. $this->token = OQLParser::F_INET_ATON;
  489. }
  490. function yy_r1_58($yy_subpatterns)
  491. {
  492. $this->token = OQLParser::F_INET_NTOA;
  493. }
  494. function yy_r1_59($yy_subpatterns)
  495. {
  496. $this->token = OQLParser::BELOW;
  497. }
  498. function yy_r1_60($yy_subpatterns)
  499. {
  500. $this->token = OQLParser::BELOW_STRICT;
  501. }
  502. function yy_r1_61($yy_subpatterns)
  503. {
  504. $this->token = OQLParser::NOT_BELOW;
  505. }
  506. function yy_r1_62($yy_subpatterns)
  507. {
  508. $this->token = OQLParser::NOT_BELOW_STRICT;
  509. }
  510. function yy_r1_63($yy_subpatterns)
  511. {
  512. $this->token = OQLParser::ABOVE;
  513. }
  514. function yy_r1_64($yy_subpatterns)
  515. {
  516. $this->token = OQLParser::ABOVE_STRICT;
  517. }
  518. function yy_r1_65($yy_subpatterns)
  519. {
  520. $this->token = OQLParser::NOT_ABOVE;
  521. }
  522. function yy_r1_66($yy_subpatterns)
  523. {
  524. $this->token = OQLParser::NOT_ABOVE_STRICT;
  525. }
  526. function yy_r1_67($yy_subpatterns)
  527. {
  528. $this->token = OQLParser::HEXVAL;
  529. }
  530. function yy_r1_68($yy_subpatterns)
  531. {
  532. $this->token = OQLParser::NUMVAL;
  533. }
  534. function yy_r1_69($yy_subpatterns)
  535. {
  536. $this->token = OQLParser::STRVAL;
  537. }
  538. function yy_r1_70($yy_subpatterns)
  539. {
  540. $this->token = OQLParser::NAME;
  541. }
  542. function yy_r1_71($yy_subpatterns)
  543. {
  544. $this->token = OQLParser::VARNAME;
  545. }
  546. function yy_r1_72($yy_subpatterns)
  547. {
  548. $this->token = OQLParser::DOT;
  549. }
  550. }
  551. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  552. class OQLLexerException extends OQLException
  553. {
  554. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  555. {
  556. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  557. }
  558. }
  559. class OQLLexer extends OQLLexerRaw
  560. {
  561. public function getTokenPos()
  562. {
  563. return max(0, $this->count - strlen($this->value));
  564. }
  565. function yylex()
  566. {
  567. try
  568. {
  569. return parent::yylex();
  570. }
  571. catch (Exception $e)
  572. {
  573. $sMessage = $e->getMessage();
  574. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  575. {
  576. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  577. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  578. {
  579. $iLine = $aMatches[1];
  580. $sUnexpected = $aMatches[2];
  581. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  582. }
  583. }
  584. // Default: forward the exception
  585. throw $e;
  586. }
  587. }
  588. }
  589. ?>