oql-lexer.plex 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. /*!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. regexp = "REGEXP"
  93. eq = "="
  94. not_eq = "!="
  95. gt = ">"
  96. lt = "<"
  97. ge = ">="
  98. le = "<="
  99. like = "LIKE"
  100. not_like = "NOT LIKE"
  101. in = "IN"
  102. not_in = "NOT IN"
  103. interval = "INTERVAL"
  104. f_if = "IF"
  105. f_elt = "ELT"
  106. f_coalesce = "COALESCE"
  107. f_isnull = "ISNULL"
  108. f_concat = "CONCAT"
  109. f_substr = "SUBSTR"
  110. f_trim = "TRIM"
  111. f_date = "DATE"
  112. f_date_format = "DATE_FORMAT"
  113. f_current_date = "CURRENT_DATE"
  114. f_now = "NOW"
  115. f_time = "TIME"
  116. f_to_days = "TO_DAYS"
  117. f_from_days = "FROM_DAYS"
  118. f_year = "YEAR"
  119. f_month = "MONTH"
  120. f_day = "DAY"
  121. f_hour = "HOUR"
  122. f_minute = "MINUTE"
  123. f_second = "SECOND"
  124. f_date_add = "DATE_ADD"
  125. f_date_sub = "DATE_SUB"
  126. f_round = "ROUND"
  127. f_floor = "FLOOR"
  128. f_inet_aton = "INET_ATON"
  129. f_inet_ntoa = "INET_NTOA"
  130. below = "BELOW"
  131. below_strict = "BELOW STRICT"
  132. not_below = "NOT BELOW"
  133. not_below_strict = "NOT BELOW STRICT"
  134. numval = /[0-9]+|0x[0-9a-fA-F]+/
  135. strval = /"([^\\"]|\\"|\\\\)*"|'.chr(94).chr(39).'([^\\'.chr(39).']|\\'.chr(39).'|\\\\)*'.chr(39).'/
  136. name = /([_a-zA-Z][_a-zA-Z0-9]*|`[^`]+`)/
  137. varname = /:([_a-zA-Z][_a-zA-Z0-9]*->[_a-zA-Z][_a-zA-Z0-9]*|[_a-zA-Z][_a-zA-Z0-9]*)/
  138. dot = "."
  139. */
  140. /*!lex2php
  141. whitespace {
  142. return false;
  143. }
  144. select {
  145. $this->token = OQLParser::SELECT;
  146. }
  147. from {
  148. $this->token = OQLParser::FROM;
  149. }
  150. as_alias {
  151. $this->token = OQLParser::AS_ALIAS;
  152. }
  153. where {
  154. $this->token = OQLParser::WHERE;
  155. }
  156. join {
  157. $this->token = OQLParser::JOIN;
  158. }
  159. on {
  160. $this->token = OQLParser::ON;
  161. }
  162. math_div {
  163. $this->token = OQLParser::MATH_DIV;
  164. }
  165. math_mult {
  166. $this->token = OQLParser::MATH_MULT;
  167. }
  168. math_plus {
  169. $this->token = OQLParser::MATH_PLUS;
  170. }
  171. math_minus {
  172. $this->token = OQLParser::MATH_MINUS;
  173. }
  174. log_and {
  175. $this->token = OQLParser::LOG_AND;
  176. }
  177. log_or {
  178. $this->token = OQLParser::LOG_OR;
  179. }
  180. coma {
  181. $this->token = OQLParser::COMA;
  182. }
  183. par_open {
  184. $this->token = OQLParser::PAR_OPEN;
  185. }
  186. par_close {
  187. $this->token = OQLParser::PAR_CLOSE;
  188. }
  189. regexp {
  190. $this->token = OQLParser::REGEXP;
  191. }
  192. eq {
  193. $this->token = OQLParser::EQ;
  194. }
  195. not_eq {
  196. $this->token = OQLParser::NOT_EQ;
  197. }
  198. gt {
  199. $this->token = OQLParser::GT;
  200. }
  201. lt {
  202. $this->token = OQLParser::LT;
  203. }
  204. ge {
  205. $this->token = OQLParser::GE;
  206. }
  207. le {
  208. $this->token = OQLParser::LE;
  209. }
  210. like {
  211. $this->token = OQLParser::LIKE;
  212. }
  213. not_like {
  214. $this->token = OQLParser::NOT_LIKE;
  215. }
  216. in {
  217. $this->token = OQLParser::IN;
  218. }
  219. not_in {
  220. $this->token = OQLParser::NOT_IN;
  221. }
  222. interval {
  223. $this->token = OQLParser::INTERVAL;
  224. }
  225. f_if {
  226. $this->token = OQLParser::F_IF;
  227. }
  228. f_elt {
  229. $this->token = OQLParser::F_ELT;
  230. }
  231. f_coalesce {
  232. $this->token = OQLParser::F_COALESCE;
  233. }
  234. f_isnull {
  235. $this->token = OQLParser::F_ISNULL;
  236. }
  237. f_concat {
  238. $this->token = OQLParser::F_CONCAT;
  239. }
  240. f_substr {
  241. $this->token = OQLParser::F_SUBSTR;
  242. }
  243. f_trim {
  244. $this->token = OQLParser::F_TRIM;
  245. }
  246. f_date {
  247. $this->token = OQLParser::F_DATE;
  248. }
  249. f_date_format {
  250. $this->token = OQLParser::F_DATE_FORMAT;
  251. }
  252. f_current_date {
  253. $this->token = OQLParser::F_CURRENT_DATE;
  254. }
  255. f_now {
  256. $this->token = OQLParser::F_NOW;
  257. }
  258. f_time {
  259. $this->token = OQLParser::F_TIME;
  260. }
  261. f_to_days {
  262. $this->token = OQLParser::F_TO_DAYS;
  263. }
  264. f_from_days {
  265. $this->token = OQLParser::F_FROM_DAYS;
  266. }
  267. f_year {
  268. $this->token = OQLParser::F_YEAR;
  269. }
  270. f_month {
  271. $this->token = OQLParser::F_MONTH;
  272. }
  273. f_day {
  274. $this->token = OQLParser::F_DAY;
  275. }
  276. f_hour {
  277. $this->token = OQLParser::F_HOUR;
  278. }
  279. f_minute {
  280. $this->token = OQLParser::F_MINUTE;
  281. }
  282. f_second {
  283. $this->token = OQLParser::F_SECOND;
  284. }
  285. f_date_add {
  286. $this->token = OQLParser::F_DATE_ADD;
  287. }
  288. f_date_sub {
  289. $this->token = OQLParser::F_DATE_SUB;
  290. }
  291. f_round {
  292. $this->token = OQLParser::F_ROUND;
  293. }
  294. f_floor {
  295. $this->token = OQLParser::F_FLOOR;
  296. }
  297. f_inet_aton {
  298. $this->token = OQLParser::F_INET_ATON;
  299. }
  300. f_inet_ntoa {
  301. $this->token = OQLParser::F_INET_NTOA;
  302. }
  303. below {
  304. $this->token = OQLParser::BELOW;
  305. }
  306. below_strict {
  307. $this->token = OQLParser::BELOW_STRICT;
  308. }
  309. not_below {
  310. $this->token = OQLParser::NOT_BELOW;
  311. }
  312. not_below_strict {
  313. $this->token = OQLParser::NOT_BELOW_STRICT;
  314. }
  315. numval {
  316. $this->token = OQLParser::NUMVAL;
  317. }
  318. strval {
  319. $this->token = OQLParser::STRVAL;
  320. }
  321. name {
  322. $this->token = OQLParser::NAME;
  323. }
  324. varname {
  325. $this->token = OQLParser::VARNAME;
  326. }
  327. dot {
  328. $this->token = OQLParser::DOT;
  329. }
  330. */
  331. }
  332. define('UNEXPECTED_INPUT_AT_LINE', 'Unexpected input at line');
  333. class OQLLexerException extends OQLException
  334. {
  335. public function __construct($sInput, $iLine, $iCol, $sUnexpected)
  336. {
  337. parent::__construct("Syntax error", $sInput, $iLine, $iCol, $sUnexpected);
  338. }
  339. }
  340. class OQLLexer extends OQLLexerRaw
  341. {
  342. public function getTokenPos()
  343. {
  344. return max(0, $this->count - strlen($this->value));
  345. }
  346. function yylex()
  347. {
  348. try
  349. {
  350. return parent::yylex();
  351. }
  352. catch (Exception $e)
  353. {
  354. $sMessage = $e->getMessage();
  355. if (substr($sMessage, 0, strlen(UNEXPECTED_INPUT_AT_LINE)) == UNEXPECTED_INPUT_AT_LINE)
  356. {
  357. $sLineAndChar = substr($sMessage, strlen(UNEXPECTED_INPUT_AT_LINE));
  358. if (preg_match('#^([0-9]+): (.+)$#', $sLineAndChar, $aMatches))
  359. {
  360. $iLine = $aMatches[1];
  361. $sUnexpected = $aMatches[2];
  362. throw new OQLLexerException($this->data, $iLine, $this->count, $sUnexpected);
  363. }
  364. }
  365. // Default: forward the exception
  366. throw $e;
  367. }
  368. }
  369. }
  370. ?>