oql-lexer.php 17 KB

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