oql-lexer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. // Notes (from the source file: oql-lexer.plex) - Romain
  3. //
  4. // The strval rule is a little bit cryptic.
  5. // This is due to both a bug in the lexer generator and the complexity of our need
  6. // The rule means: either a quoted string with ", or a quoted string with '
  7. // literal " (resp. ') must be escaped by a \
  8. // \ must be escaped by an additional \
  9. //
  10. // Here are the issues and limitation found in the lexer generator:
  11. // * Matching simple quotes is an issue, because regexp are not correctly escaped (and the ESC code is escaped itself)
  12. // Workaround: insert '.chr(39).' which will be a real ' in the end
  13. // * Matching an alternate regexp is an issue because you must specify "|^...."
  14. // and the regexp parser will not accept that syntax
  15. // Workaround: insert '.chr(94).' which will be a real ^
  16. //
  17. // Let's analyze an overview of the regexp, we have
  18. // 1) The strval rule in the lexer definition
  19. // /"([^\\"]|\\"|\\\\)*"|'.chr(94).chr(39).'([^\\'.chr(39).']|\\'.chr(39).'|\\\\)*'.chr(39).'/
  20. // 2) Becomes the php expression in the lexer
  21. // (note the escaped double quotes, hopefully having no effect, but showing where the issue is!)
  22. // $myRegexp = '/^\"([^\\\\\"]|\\\\\"|\\\\\\\\)*\"|'.chr(94).chr(39).'([^\\\\'.chr(39).']|\\\\'.chr(39).'|\\\\\\\\)*'.chr(39).'/';
  23. //
  24. // To be fixed in LexerGenerator/Parser.y, in doLongestMatch (doFirstMatch is ok)
  25. //
  26. //
  27. // Now, let's explain how the regexp has been designed.
  28. // Here is a simplified version, dealing with simple quotes, and based on the assumption that the lexer generator has been fixed!
  29. // The strval rule in the lexer definition
  30. // /'([^\\']*(\\')*(\\\\)*)*'/
  31. // This means anything containing \\ or \' or any other char but a standalone ' or \
  32. // This means ' or \ could not be found without a preceding \
  33. //
  34. class OQLLexerRaw
  35. {
  36. protected $data; // input string
  37. public $token; // token id
  38. public $value; // token string representation
  39. protected $line; // current line
  40. protected $count; // current column
  41. function __construct($data)
  42. {
  43. $this->data = $data;
  44. $this->count = 0;
  45. $this->line = 1;
  46. }
  47. private $_yy_state = 1;
  48. private $_yy_stack = array();
  49. function yylex()
  50. {
  51. return $this->{'yylex' . $this->_yy_state}();
  52. }
  53. function yypushstate($state)
  54. {
  55. array_push($this->_yy_stack, $this->_yy_state);
  56. $this->_yy_state = $state;
  57. }
  58. function yypopstate()
  59. {
  60. $this->_yy_state = array_pop($this->_yy_stack);
  61. }
  62. function yybegin($state)
  63. {
  64. $this->_yy_state = $state;
  65. }
  66. function yylex1()
  67. {
  68. if ($this->count >= strlen($this->data)) {
  69. return false; // end of input
  70. }
  71. do {
  72. $rules = array(
  73. '/^[ \t\n]+/',
  74. '/^SELECT/',
  75. '/^FROM/',
  76. '/^AS/',
  77. '/^WHERE/',
  78. '/^JOIN/',
  79. '/^ON/',
  80. '/^\//',
  81. '/^\\*/',
  82. '/^\\+/',
  83. '/^-/',
  84. '/^AND/',
  85. '/^OR/',
  86. '/^,/',
  87. '/^\\(/',
  88. '/^\\)/',
  89. '/^=/',
  90. '/^!=/',
  91. '/^>/',
  92. '/^</',
  93. '/^>=/',
  94. '/^<=/',
  95. '/^LIKE/',
  96. '/^NOT LIKE/',
  97. '/^IN/',
  98. '/^NOT IN/',
  99. '/^INTERVAL/',
  100. '/^IF/',
  101. '/^ELT/',
  102. '/^COALESCE/',
  103. '/^CONCAT/',
  104. '/^SUBSTR/',
  105. '/^TRIM/',
  106. '/^DATE/',
  107. '/^DATE_FORMAT/',
  108. '/^CURRENT_DATE/',
  109. '/^NOW/',
  110. '/^TIME/',
  111. '/^TO_DAYS/',
  112. '/^FROM_DAYS/',
  113. '/^YEAR/',
  114. '/^MONTH/',
  115. '/^DAY/',
  116. '/^DATE_ADD/',
  117. '/^DATE_SUB/',
  118. '/^ROUND/',
  119. '/^FLOOR/',
  120. '/^[0-9]+|0x[0-9a-fA-F]+/',
  121. '/^\"([^\\\\\"]|\\\\\"|\\\\\\\\)*\"|'.chr(94).chr(39).'([^\\\\'.chr(39).']|\\\\'.chr(39).'|\\\\\\\\)*'.chr(39).'/',
  122. '/^([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/',
  123. '/^:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/',
  124. '/^\\./',
  125. );
  126. $match = false;
  127. foreach ($rules as $index => $rule) {
  128. if (preg_match($rule, substr($this->data, $this->count), $yymatches)) {
  129. if ($match) {
  130. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  131. $match = array($yymatches, $index); // matches, token
  132. }
  133. } else {
  134. $match = array($yymatches, $index);
  135. }
  136. }
  137. }
  138. if (!$match) {
  139. throw new Exception('Unexpected input at line' . $this->line .
  140. ': ' . $this->data[$this->count]);
  141. }
  142. $this->token = $match[1];
  143. $this->value = $match[0][0];
  144. $yysubmatches = $match[0];
  145. array_shift($yysubmatches);
  146. if (!$yysubmatches) {
  147. $yysubmatches = array();
  148. }
  149. $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
  150. if ($r === null) {
  151. $this->count += strlen($this->value);
  152. $this->line += substr_count($this->value, "\n");
  153. // accept this token
  154. return true;
  155. } elseif ($r === true) {
  156. // we have changed state
  157. // process this token in the new state
  158. return $this->yylex();
  159. } elseif ($r === false) {
  160. $this->count += strlen($this->value);
  161. $this->line += substr_count($this->value, "\n");
  162. if ($this->count >= strlen($this->data)) {
  163. return false; // end of input
  164. }
  165. // skip this token
  166. continue;
  167. } else {
  168. $yy_yymore_patterns = array_slice($rules, $this->token, true);
  169. // yymore is needed
  170. do {
  171. if (!isset($yy_yymore_patterns[$this->token])) {
  172. throw new Exception('cannot do yymore for the last token');
  173. }
  174. $match = false;
  175. foreach ($yy_yymore_patterns[$this->token] as $index => $rule) {
  176. if (preg_match('/' . $rule . '/',
  177. substr($this->data, $this->count), $yymatches)) {
  178. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  179. if ($match) {
  180. if (strlen($yymatches[0]) > strlen($match[0][0])) {
  181. $match = array($yymatches, $index); // matches, token
  182. }
  183. } else {
  184. $match = array($yymatches, $index);
  185. }
  186. }
  187. }
  188. if (!$match) {
  189. throw new Exception('Unexpected input at line' . $this->line .
  190. ': ' . $this->data[$this->count]);
  191. }
  192. $this->token = $match[1];
  193. $this->value = $match[0][0];
  194. $yysubmatches = $match[0];
  195. array_shift($yysubmatches);
  196. if (!$yysubmatches) {
  197. $yysubmatches = array();
  198. }
  199. $this->line = substr_count($this->value, "\n");
  200. $r = $this->{'yy_r1_' . $this->token}();
  201. } while ($r !== null || !$r);
  202. if ($r === true) {
  203. // we have changed state
  204. // process this token in the new state
  205. return $this->yylex();
  206. } else {
  207. // accept
  208. $this->count += strlen($this->value);
  209. $this->line += substr_count($this->value, "\n");
  210. return true;
  211. }
  212. }
  213. } while (true);
  214. } // end function
  215. function yy_r1_0($yy_subpatterns)
  216. {
  217. return false;
  218. }
  219. function yy_r1_1($yy_subpatterns)
  220. {
  221. $this->token = OQLParser::SELECT;
  222. }
  223. function yy_r1_2($yy_subpatterns)
  224. {
  225. $this->token = OQLParser::FROM;
  226. }
  227. function yy_r1_3($yy_subpatterns)
  228. {
  229. $this->token = OQLParser::AS_ALIAS;
  230. }
  231. function yy_r1_4($yy_subpatterns)
  232. {
  233. $this->token = OQLParser::WHERE;
  234. }
  235. function yy_r1_5($yy_subpatterns)
  236. {
  237. $this->token = OQLParser::JOIN;
  238. }
  239. function yy_r1_6($yy_subpatterns)
  240. {
  241. $this->token = OQLParser::ON;
  242. }
  243. function yy_r1_7($yy_subpatterns)
  244. {
  245. $this->token = OQLParser::MATH_DIV;
  246. }
  247. function yy_r1_8($yy_subpatterns)
  248. {
  249. $this->token = OQLParser::MATH_MULT;
  250. }
  251. function yy_r1_9($yy_subpatterns)
  252. {
  253. $this->token = OQLParser::MATH_PLUS;
  254. }
  255. function yy_r1_10($yy_subpatterns)
  256. {
  257. $this->token = OQLParser::MATH_MINUS;
  258. }
  259. function yy_r1_11($yy_subpatterns)
  260. {
  261. $this->token = OQLParser::LOG_AND;
  262. }
  263. function yy_r1_12($yy_subpatterns)
  264. {
  265. $this->token = OQLParser::LOG_OR;
  266. }
  267. function yy_r1_13($yy_subpatterns)
  268. {
  269. $this->token = OQLParser::COMA;
  270. }
  271. function yy_r1_14($yy_subpatterns)
  272. {
  273. $this->token = OQLParser::PAR_OPEN;
  274. }
  275. function yy_r1_15($yy_subpatterns)
  276. {
  277. $this->token = OQLParser::PAR_CLOSE;
  278. }
  279. function yy_r1_16($yy_subpatterns)
  280. {
  281. $this->token = OQLParser::EQ;
  282. }
  283. function yy_r1_17($yy_subpatterns)
  284. {
  285. $this->token = OQLParser::NOT_EQ;
  286. }
  287. function yy_r1_18($yy_subpatterns)
  288. {
  289. $this->token = OQLParser::GT;
  290. }
  291. function yy_r1_19($yy_subpatterns)
  292. {
  293. $this->token = OQLParser::LT;
  294. }
  295. function yy_r1_20($yy_subpatterns)
  296. {
  297. $this->token = OQLParser::GE;
  298. }
  299. function yy_r1_21($yy_subpatterns)
  300. {
  301. $this->token = OQLParser::LE;
  302. }
  303. function yy_r1_22($yy_subpatterns)
  304. {
  305. $this->token = OQLParser::LIKE;
  306. }
  307. function yy_r1_23($yy_subpatterns)
  308. {
  309. $this->token = OQLParser::NOT_LIKE;
  310. }
  311. function yy_r1_24($yy_subpatterns)
  312. {
  313. $this->token = OQLParser::IN;
  314. }
  315. function yy_r1_25($yy_subpatterns)
  316. {
  317. $this->token = OQLParser::NOT_IN;
  318. }
  319. function yy_r1_26($yy_subpatterns)
  320. {
  321. $this->token = OQLParser::INTERVAL;
  322. }
  323. function yy_r1_27($yy_subpatterns)
  324. {
  325. $this->token = OQLParser::F_IF;
  326. }
  327. function yy_r1_28($yy_subpatterns)
  328. {
  329. $this->token = OQLParser::F_ELT;
  330. }
  331. function yy_r1_29($yy_subpatterns)
  332. {
  333. $this->token = OQLParser::F_COALESCE;
  334. }
  335. function yy_r1_30($yy_subpatterns)
  336. {
  337. $this->token = OQLParser::F_CONCAT;
  338. }
  339. function yy_r1_31($yy_subpatterns)
  340. {
  341. $this->token = OQLParser::F_SUBSTR;
  342. }
  343. function yy_r1_32($yy_subpatterns)
  344. {
  345. $this->token = OQLParser::F_TRIM;
  346. }
  347. function yy_r1_33($yy_subpatterns)
  348. {
  349. $this->token = OQLParser::F_DATE;
  350. }
  351. function yy_r1_34($yy_subpatterns)
  352. {
  353. $this->token = OQLParser::F_DATE_FORMAT;
  354. }
  355. function yy_r1_35($yy_subpatterns)
  356. {
  357. $this->token = OQLParser::F_CURRENT_DATE;
  358. }
  359. function yy_r1_36($yy_subpatterns)
  360. {
  361. $this->token = OQLParser::F_NOW;
  362. }
  363. function yy_r1_37($yy_subpatterns)
  364. {
  365. $this->token = OQLParser::F_TIME;
  366. }
  367. function yy_r1_38($yy_subpatterns)
  368. {
  369. $this->token = OQLParser::F_TO_DAYS;
  370. }
  371. function yy_r1_39($yy_subpatterns)
  372. {
  373. $this->token = OQLParser::F_FROM_DAYS;
  374. }
  375. function yy_r1_40($yy_subpatterns)
  376. {
  377. $this->token = OQLParser::F_YEAR;
  378. }
  379. function yy_r1_41($yy_subpatterns)
  380. {
  381. $this->token = OQLParser::F_MONTH;
  382. }
  383. function yy_r1_42($yy_subpatterns)
  384. {
  385. $this->token = OQLParser::F_DAY;
  386. }
  387. function yy_r1_43($yy_subpatterns)
  388. {
  389. $this->token = OQLParser::F_DATE_ADD;
  390. }
  391. function yy_r1_44($yy_subpatterns)
  392. {
  393. $this->token = OQLParser::F_DATE_SUB;
  394. }
  395. function yy_r1_45($yy_subpatterns)
  396. {
  397. $this->token = OQLParser::F_ROUND;
  398. }
  399. function yy_r1_46($yy_subpatterns)
  400. {
  401. $this->token = OQLParser::F_FLOOR;
  402. }
  403. function yy_r1_47($yy_subpatterns)
  404. {
  405. $this->token = OQLParser::NUMVAL;
  406. }
  407. function yy_r1_48($yy_subpatterns)
  408. {
  409. $this->token = OQLParser::STRVAL;
  410. }
  411. function yy_r1_49($yy_subpatterns)
  412. {
  413. $this->token = OQLParser::NAME;
  414. }
  415. function yy_r1_50($yy_subpatterns)
  416. {
  417. $this->token = OQLParser::VARNAME;
  418. }
  419. function yy_r1_51($yy_subpatterns)
  420. {
  421. $this->token = OQLParser::DOT;
  422. }
  423. }
  424. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  425. class OQLLexerException extends OQLException
  426. {
  427. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  428. {
  429. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  430. }
  431. }
  432. class OQLLexer extends OQLLexerRaw
  433. {
  434. public function getTokenPos()
  435. {
  436. return max(0, $this->count - strlen($this->value));
  437. }
  438. function yylex()
  439. {
  440. try
  441. {
  442. return parent::yylex();
  443. }
  444. catch (Exception $e)
  445. {
  446. $sMessage = $e->getMessage();
  447. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  448. {
  449. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  450. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  451. {
  452. $iLine = $aMatches[1];
  453. $sUnexpected = $aMatches[2];
  454. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  455. }
  456. }
  457. // Default: forward the exception
  458. throw $e;
  459. }
  460. }
  461. }
  462. ?>