oql-lexer.plex 7.1 KB

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