oql-lexer.plex 7.5 KB

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