oql-lexer.plex 8.6 KB

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