oql-lexer.plex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /*!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. bitwise_and = "&"
  93. bitwise_or = "|"
  94. bitwise_xor = "^"
  95. bitwise_leftshift = "<<"
  96. bitwise_rightshift = ">>"
  97. regexp = "REGEXP"
  98. eq = "="
  99. not_eq = "!="
  100. gt = ">"
  101. lt = "<"
  102. ge = ">="
  103. le = "<="
  104. like = "LIKE"
  105. not_like = "NOT LIKE"
  106. in = "IN"
  107. not_in = "NOT IN"
  108. interval = "INTERVAL"
  109. f_if = "IF"
  110. f_elt = "ELT"
  111. f_coalesce = "COALESCE"
  112. f_isnull = "ISNULL"
  113. f_concat = "CONCAT"
  114. f_substr = "SUBSTR"
  115. f_trim = "TRIM"
  116. f_date = "DATE"
  117. f_date_format = "DATE_FORMAT"
  118. f_current_date = "CURRENT_DATE"
  119. f_now = "NOW"
  120. f_time = "TIME"
  121. f_to_days = "TO_DAYS"
  122. f_from_days = "FROM_DAYS"
  123. f_year = "YEAR"
  124. f_month = "MONTH"
  125. f_day = "DAY"
  126. f_hour = "HOUR"
  127. f_minute = "MINUTE"
  128. f_second = "SECOND"
  129. f_date_add = "DATE_ADD"
  130. f_date_sub = "DATE_SUB"
  131. f_round = "ROUND"
  132. f_floor = "FLOOR"
  133. f_inet_aton = "INET_ATON"
  134. f_inet_ntoa = "INET_NTOA"
  135. below = "BELOW"
  136. below_strict = "BELOW STRICT"
  137. not_below = "NOT BELOW"
  138. not_below_strict = "NOT BELOW STRICT"
  139. above = "ABOVE"
  140. above_strict = "ABOVE STRICT"
  141. not_above = "NOT ABOVE"
  142. not_above_strict = "NOT ABOVE STRICT"
  143. //
  144. // WARNING: there seems to be a bug in the Lexer about matching the longest pattern
  145. // when there are alternates in the regexp.
  146. //
  147. // For instance:
  148. // numval = /[0-9]+|0x[0-9a-fA-F]+/
  149. // Does not work: SELECT Toto WHERE name = 'Text0xCTest' => Fails because 0xC is recongnized as a numval (inside the string) instead of a strval !!
  150. //
  151. // Inserting a ^ after the alternate (see comment at the top of this file) does not work either
  152. // numval = /[0-9]+|'.chr(94).'0x[0-9a-fA-F]+/
  153. // SELECT Toto WHERE name = 'Text0xCTest' => works but
  154. // SELECT Toto WHERE id = 0xC => does not work, 'xC' is found as a name (apparently 0 is recognized as a numval and the remaining is a name !)
  155. //
  156. // numval = /([0-9]+|0x[0-9a-fA-F]+)/
  157. // Does not work either, the hexadecimal numbers are not matched properly
  158. // Anyhow let's distinguish the hexadecimal values from decimal integers, hex numbers will be stored as strings
  159. // and passed as-is to MySQL which enables us to pass 64-bit values without messing with them in PHP
  160. //
  161. hexval = /(0x[0-9a-fA-F]+)/
  162. numval = /([0-9]+)/
  163. strval = /"([^\\"]|\\"|\\\\)*"|'.chr(94).chr(39).'([^\\'.chr(39).']|\\'.chr(39).'|\\\\)*'.chr(39).'/
  164. name = /([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/
  165. varname = /:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/
  166. dot = "."
  167. */
  168. /*!lex2php
  169. whitespace {
  170. return false;
  171. }
  172. select {
  173. $this->token = OQLParser::SELECT;
  174. }
  175. from {
  176. $this->token = OQLParser::FROM;
  177. }
  178. as_alias {
  179. $this->token = OQLParser::AS_ALIAS;
  180. }
  181. where {
  182. $this->token = OQLParser::WHERE;
  183. }
  184. join {
  185. $this->token = OQLParser::JOIN;
  186. }
  187. on {
  188. $this->token = OQLParser::ON;
  189. }
  190. math_div {
  191. $this->token = OQLParser::MATH_DIV;
  192. }
  193. math_mult {
  194. $this->token = OQLParser::MATH_MULT;
  195. }
  196. math_plus {
  197. $this->token = OQLParser::MATH_PLUS;
  198. }
  199. math_minus {
  200. $this->token = OQLParser::MATH_MINUS;
  201. }
  202. log_and {
  203. $this->token = OQLParser::LOG_AND;
  204. }
  205. log_or {
  206. $this->token = OQLParser::LOG_OR;
  207. }
  208. bitwise_or {
  209. $this->token = OQLParser::BITWISE_OR;
  210. }
  211. bitwise_and {
  212. $this->token = OQLParser::BITWISE_AND;
  213. }
  214. bitwise_xor {
  215. $this->token = OQLParser::BITWISE_XOR;
  216. }
  217. bitwise_leftshift {
  218. $this->token = OQLParser::BITWISE_LEFT_SHIFT;
  219. }
  220. bitwise_rightshift {
  221. $this->token = OQLParser::BITWISE_RIGHT_SHIFT;
  222. }
  223. coma {
  224. $this->token = OQLParser::COMA;
  225. }
  226. par_open {
  227. $this->token = OQLParser::PAR_OPEN;
  228. }
  229. par_close {
  230. $this->token = OQLParser::PAR_CLOSE;
  231. }
  232. regexp {
  233. $this->token = OQLParser::REGEXP;
  234. }
  235. eq {
  236. $this->token = OQLParser::EQ;
  237. }
  238. not_eq {
  239. $this->token = OQLParser::NOT_EQ;
  240. }
  241. gt {
  242. $this->token = OQLParser::GT;
  243. }
  244. lt {
  245. $this->token = OQLParser::LT;
  246. }
  247. ge {
  248. $this->token = OQLParser::GE;
  249. }
  250. le {
  251. $this->token = OQLParser::LE;
  252. }
  253. like {
  254. $this->token = OQLParser::LIKE;
  255. }
  256. not_like {
  257. $this->token = OQLParser::NOT_LIKE;
  258. }
  259. in {
  260. $this->token = OQLParser::IN;
  261. }
  262. not_in {
  263. $this->token = OQLParser::NOT_IN;
  264. }
  265. interval {
  266. $this->token = OQLParser::INTERVAL;
  267. }
  268. f_if {
  269. $this->token = OQLParser::F_IF;
  270. }
  271. f_elt {
  272. $this->token = OQLParser::F_ELT;
  273. }
  274. f_coalesce {
  275. $this->token = OQLParser::F_COALESCE;
  276. }
  277. f_isnull {
  278. $this->token = OQLParser::F_ISNULL;
  279. }
  280. f_concat {
  281. $this->token = OQLParser::F_CONCAT;
  282. }
  283. f_substr {
  284. $this->token = OQLParser::F_SUBSTR;
  285. }
  286. f_trim {
  287. $this->token = OQLParser::F_TRIM;
  288. }
  289. f_date {
  290. $this->token = OQLParser::F_DATE;
  291. }
  292. f_date_format {
  293. $this->token = OQLParser::F_DATE_FORMAT;
  294. }
  295. f_current_date {
  296. $this->token = OQLParser::F_CURRENT_DATE;
  297. }
  298. f_now {
  299. $this->token = OQLParser::F_NOW;
  300. }
  301. f_time {
  302. $this->token = OQLParser::F_TIME;
  303. }
  304. f_to_days {
  305. $this->token = OQLParser::F_TO_DAYS;
  306. }
  307. f_from_days {
  308. $this->token = OQLParser::F_FROM_DAYS;
  309. }
  310. f_year {
  311. $this->token = OQLParser::F_YEAR;
  312. }
  313. f_month {
  314. $this->token = OQLParser::F_MONTH;
  315. }
  316. f_day {
  317. $this->token = OQLParser::F_DAY;
  318. }
  319. f_hour {
  320. $this->token = OQLParser::F_HOUR;
  321. }
  322. f_minute {
  323. $this->token = OQLParser::F_MINUTE;
  324. }
  325. f_second {
  326. $this->token = OQLParser::F_SECOND;
  327. }
  328. f_date_add {
  329. $this->token = OQLParser::F_DATE_ADD;
  330. }
  331. f_date_sub {
  332. $this->token = OQLParser::F_DATE_SUB;
  333. }
  334. f_round {
  335. $this->token = OQLParser::F_ROUND;
  336. }
  337. f_floor {
  338. $this->token = OQLParser::F_FLOOR;
  339. }
  340. f_inet_aton {
  341. $this->token = OQLParser::F_INET_ATON;
  342. }
  343. f_inet_ntoa {
  344. $this->token = OQLParser::F_INET_NTOA;
  345. }
  346. below {
  347. $this->token = OQLParser::BELOW;
  348. }
  349. below_strict {
  350. $this->token = OQLParser::BELOW_STRICT;
  351. }
  352. not_below {
  353. $this->token = OQLParser::NOT_BELOW;
  354. }
  355. not_below_strict {
  356. $this->token = OQLParser::NOT_BELOW_STRICT;
  357. }
  358. above {
  359. $this->token = OQLParser::ABOVE;
  360. }
  361. above_strict {
  362. $this->token = OQLParser::ABOVE_STRICT;
  363. }
  364. not_above {
  365. $this->token = OQLParser::NOT_ABOVE;
  366. }
  367. not_above_strict {
  368. $this->token = OQLParser::NOT_ABOVE_STRICT;
  369. }
  370. hexval {
  371. $this->token = OQLParser::HEXVAL;
  372. }
  373. numval {
  374. $this->token = OQLParser::NUMVAL;
  375. }
  376. strval {
  377. $this->token = OQLParser::STRVAL;
  378. }
  379. name {
  380. $this->token = OQLParser::NAME;
  381. }
  382. varname {
  383. $this->token = OQLParser::VARNAME;
  384. }
  385. dot {
  386. $this->token = OQLParser::DOT;
  387. }
  388. */
  389. }
  390. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  391. class OQLLexerException extends OQLException
  392. {
  393. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  394. {
  395. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  396. }
  397. }
  398. class OQLLexer extends OQLLexerRaw
  399. {
  400. public function getTokenPos()
  401. {
  402. return max(0, $this->count - strlen($this->value));
  403. }
  404. function yylex()
  405. {
  406. try
  407. {
  408. return parent::yylex();
  409. }
  410. catch (Exception $e)
  411. {
  412. $sMessage = $e->getMessage();
  413. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  414. {
  415. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  416. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  417. {
  418. $iLine = $aMatches[1];
  419. $sUnexpected = $aMatches[2];
  420. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  421. }
  422. }
  423. // Default: forward the exception
  424. throw $e;
  425. }
  426. }
  427. }
  428. ?>