oql-lexer.plex 7.4 KB

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