CUBRID Engine  latest
esql_lexer.l
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * esql_lexer.l : esql lexer file
21  */
22 
23 %{
24 
25 #include "esql_grammar.h"
26 #include "esql_scanner_support.h"
27 
28 #if defined (SUPPRESS_STRLEN_WARNING)
29 #define strlen(s1) ((int) strlen(s1))
30 #endif /* defined (SUPPRESS_STRLEN_WARNING) */
31 
32 //#define LEXER_DEBUG
33 
34 
35 #ifdef LEXER_DEBUG
36 #define PRINT(a, b) printf(a, b)
37 #else
38 #define PRINT(a, b)
39 #endif
40 
41 
42 static void parser_c_comment(void);
43 static void parser_line_comment(void);
44 static void parser_ignore_comment(void);
45 static char* parser_quoted_string(char end);
46 static int esql_yyinput(char *buff, int max_size);
47 
48 #define JP_MAXNAME 256
49 
50 
51 #undef YY_INPUT
52 #define YY_INPUT(buffer, result, max_size) (result = esql_yyinput(buffer, max_size))
53 
54 
55 %}
56 
57 
58 %%
59 
60 [ \t\r]+ {{
61 
62  if (esql_yy_mode () != BUFFER_mode)
63  ECHO;
64  else
65  ECHO_STR (" ", strlen (" "));
66 
67  }}
68 
69 \n {{
70 
71  int mode = esql_yy_mode ();
72  if (mode != BUFFER_mode)
73  ECHO;
74  else
75  ECHO_STR (" ", strlen (" "));
76 
77  esql_yylineno++;
78 
79  if (mode == ECHO_mode || mode == C_mode)
80  {
81  CHECK_LINENO;
82  }
83 
84  }}
85 
86 "//" {
87  PRINT("tok: %s\n", yytext);
88  ECHO;
89  parser_line_comment();
90 
91  }
92 
93 
94 "/*" {
95  PRINT("tok: %s\n", yytext);
96  ECHO;
97  parser_c_comment();
98  }
99 
100 "#" {
101  PRINT("tok: %s\n", yytext);
102  ECHO;
103  parser_line_comment();
104  }
105 
106 "--" {{
107  int mode = esql_yy_mode();
108  PRINT ("tok: %s\n", yytext);
109 
110  if (mode == C_mode)
111  {
112  ECHO;
113  return GENERIC_TOKEN;
114  }
115  else if (mode == ECHO_mode)
116  {
117  ECHO;
118  }
119  else if (mode == BUFFER_mode)
120  {
121  parser_ignore_comment ();
122 
123  }
124  else
125  {
126  ECHO;
127  parser_line_comment ();
128  }
129 
130  }}
131 
132 \" {{
133  const char *quote = "\"";
134  char *buff = parser_quoted_string ('\"');
135  PRINT ("tok: %s\n", yytext);
136 
137  ECHO_STR (quote, strlen (quote));
138  ECHO_STR (buff, strlen (buff));
139  ECHO_STR (quote, strlen (quote));
140 
141  if (esql_yy_mode () == EXPR_mode)
142  {
143  return GENERIC_TOKEN;
144 
145  }
146 
147  }}
148 
149 
150 EXEC[ \t] {
151  PRINT("tok: %s\n", yytext);
152  return EXEC;
153  }
154 
155 SQLX[ \t\r\n] {
156  PRINT("tok: %s\n", yytext);
157  return SQLX;
158  }
159 
160 SQL[ \t\r\n] {
161  PRINT("tok: %s\n", yytext);
162  return SQLX;
163  }
164 
165 [0-9]+ {{
166  int mode = esql_yy_mode();
167  PRINT ("tok: %s\n", yytext);
168 
169  if (mode == ECHO_mode)
170  {
171  ECHO;
172  }
173  else if (mode == C_mode)
174  {
175  ECHO;
176  return GENERIC_TOKEN;
177  }
178  else if (mode == EXPR_mode)
179  {
180  ECHO;
181  return GENERIC_TOKEN;
182  }
183  else if (mode == HV_mode)
184  {
185  vs_strcat (&pp_subscript_buf, yytext);
186 
187  ECHO;
188  }
189  else
190  {
191  ECHO;
192  }
193 
194  }}
195 
196 [a-zA-Z_][a-zA-Z_0-9]* {{
197  int mode = esql_yy_mode ();
198  int code;
199  PRINT ("tok: %s\n", yytext);
200  esql_yylval.ptr = mm_strdup (yytext);
201 
202  if (mode == ECHO_mode)
203  {
204  ECHO;
205  }
206  else if (mode == C_mode)
207  {
208  code = check_c_identifier (yytext);
209  ECHO;
210 
211  return code;
212  }
213  else if (mode == EXPR_mode)
214  {
215  ECHO;
216  return GENERIC_TOKEN;
217  }
218  else if (mode == CSQL_mode)
219  {
220  code = check_identifier (&csql_table, yytext);
221  ECHO;
222 
223  return code;
224  }
225  else if (mode == VAR_mode)
226  {
227  if (recognize_keywords)
228  {
229  code = check_identifier (&preprocessor_table, yytext);
230  }
231  else
232  {
233  code = check_c_identifier (yytext);
234  }
235 
236  recognize_keywords = (code != INDICATOR);
237 
238  if (code != IDENTIFIER)
239  {
240  sprintf (g_delay, "%s", yytext);
241  }
242 
243  else if (intl_mbs_casecmp (yytext, "WHERE") == 0)
244  {
245  sprintf (g_delay, "%s", yytext);
246 
247  }
248  else if (intl_mbs_casecmp (yytext, "AND") == 0)
249  {
250  sprintf (g_delay, "%s", yytext);
251 
252  }
253  else if (intl_mbs_casecmp (yytext, "OR") == 0)
254  {
255  sprintf (g_delay, "%s", yytext);
256 
257  }
258  else if (intl_mbs_casecmp (yytext, "ORDER") == 0)
259  {
260  sprintf (g_delay, "%s", yytext);
261 
262  }
263  else if (intl_mbs_casecmp (yytext, "GROUP") == 0)
264  {
265  sprintf (g_delay, "%s", yytext);
266 
267  }
268  else if (intl_mbs_casecmp (yytext, "UNION") == 0)
269  {
270  sprintf (g_delay, "%s", yytext);
271 
272  }
273  else if (intl_mbs_casecmp (yytext, "USING") == 0)
274  {
275  sprintf (g_delay, "%s", yytext);
276 
277  }
278 
279 
280  return code;
281  }
282  else if (mode == BUFFER_mode)
283  {
284  ECHO;
285  code = IDENTIFIER;
286  if (intl_mbs_casecmp (yytext, "DESCRIPTOR") == 0)
287  {
288  code = DESCRIPTOR;
289  }
290  else if (intl_mbs_casecmp (yytext, "INTO") == 0)
291  {
292  code = INTO;
293  }
294  else if (intl_mbs_casecmp (yytext, "TO") == 0)
295  {
296  code = INTO;
297  }
298  else if (intl_mbs_casecmp (yytext, "VALUES") == 0)
299  {
300  code = VALUES;
301  }
302  else if (intl_mbs_casecmp (yytext, "SELECT") == 0)
303  {
304  code = SELECT;
305  }
306 
307  return code;
308  }
309  else if (mode == HV_mode)
310  {
311  vs_strcat (&pp_subscript_buf, yytext);
312 
313  ECHO;
314  }
315  else
316  {
317  ECHO;
318  return IDENTIFIER;
319  }
320 
321  }}
322 
323 \'((\'\')|([^\']))*\' {{
324  int mode = esql_yy_mode ();
325  PRINT ("tok: %s\n", yytext);
326 
327  if (mode == ECHO_mode)
328  {
329  ECHO;
330  }
331  else if (mode == CSQL_mode)
332  {
333  ECHO;
334  esql_yylval.ptr = mm_strdup (yytext);
335  return STRING_LIT;
336  }
337  else
338  {
339  ECHO;
340  return GENERIC_TOKEN;
341  }
342 
343  }}
344 
345 . {{
346  int mode = esql_yy_mode ();
347  PRINT ("tok: %s\n", yytext);
348 
349  if (mode == ECHO_mode)
350  {
351  ECHO;
352  switch (yytext[0])
353  {
354  case '{':
355  case '}':
356  return yytext[0];
357  }
358  }
359  else if (mode == C_mode)
360  {
361  ECHO;
362  return yytext[0];
363  }
364  else if (mode == EXPR_mode)
365  {
366  ECHO;
367 
368  switch (yytext[0])
369  {
370  case ';':
371  case ':':
372  case '(':
373  case ')':
374  case '[':
375  case ']':
376  case '{':
377  case '}':
378  return yytext[0];
379  }
380  }
381  else if (mode == VAR_mode)
382  {
383  switch (yytext[0])
384  {
385  case ',':
386  case '.':
387  case ':':
388  recognize_keywords = false;
389  }
390 
391 
392  switch (yytext[0])
393  {
394  case '.':
395  case '&':
396  case '(':
397  case '[':
398  case ']':
399  case ';':
400  case '{':
401  case '}':
402  return yytext[0];
403 
404  case ')':
405  case ',':
406  case '*':
407  sprintf (g_delay, "%s", yytext);
408  return yytext[0];
409 
410  case ':':
411  if (g_indicator)
412  return '#';
413  else
414  return yytext[0];
415 
416  default:
417  sprintf (g_delay, "%s", yytext);
418  return GENERIC_TOKEN;
419  }
420  }
421  else if (mode == BUFFER_mode)
422  {
423  char c = yytext[0];
424 
425  switch (c)
426  {
427  case ',':
428  case '.':
429  case ':':
430  recognize_keywords = false;
431  }
432 
433 
434  if (c == ':' || c == ';')
435  return c;
436 
437  ECHO;
438  }
439  else
440  {
441  return yytext[0];
442  }
443 
444  }}
445 
446 
447 %%
448 
449 
450 
451 
452 
453 
454 int
455 esql_yywrap ()
456 {
457 #ifdef LEXER_DEBUG
458  printf ("Parsing done.....\n");
459 #endif
460  return 1;
461 }
462 
463 static void
464 parser_c_comment (void)
465 {
466  char c, c1;
467 
468 loop:
469  while ((c = input ()) != '*' && c != 0 && c != -1)
470  {
471  if (c == '\n')
472  esql_yylineno++;
473  (*echo_fn) (&c, 1);
474  }
475 
476  if ((c1 = input ()) != '/' && c != 0 && c != -1)
477  {
478  if (c == '\n')
479  esql_yylineno++;
480  (*echo_fn) (&c, 1);
481  unput (c1);
482  goto loop;
483  }
484 
485  (*echo_fn) (&c, 1);
486  (*echo_fn) (&c1, 1);
487 }
488 
489 static void
490 parser_ignore_comment (void)
491 {
492  char c;
493 
494  while ((c = input ()) != '\r' && c != '\n' && c != 0 && c != -1)
495  {
496  // ignore
497  }
498 
499  esql_yylineno++;
500 }
501 
502 static void
503 parser_line_comment (void)
504 {
505  char c;
506 
507  while ((c = input ()) != '\r' && c != '\n' && c != 0 && c != -1)
508  {
509  (*echo_fn) (&c, 1);
510  }
511 
512  c = '\n';
513  (*echo_fn) (&c, 1);
514  esql_yylineno++;
515 }
516 
517 
518 static char *
519 parser_quoted_string (char end)
520 {
521  char c, c1;
522  char buff[8192];
523  int i = 0;
524 
525  int size = 8192;
526  char *bp = buff;
527 
528 loop:
529  while ((c = input ()) != end && c != 0 && c != -1)
530  {
531  bp[i++] = c;
532  if (i >= size)
533  {
534  char *bp_new = mm_malloc (size * 2);
535  memcpy (bp_new, bp, size);
536  bp = bp_new;
537  size *= 2;
538  }
539  }
540 
541  if (c == 0 || c == -1)
542  {
543  bp[i] = 0;
544  return (bp == buff) ? mm_strdup (bp) : bp;
545  }
546 
547  if ((c1 = input ()) == end && c != 0 && c != -1)
548  {
549  bp[i++] = c1;
550  if (i >= size)
551  {
552  char *bp_new = mm_malloc (size * 2);
553  memcpy (bp_new, bp, size);
554  bp = bp_new;
555  size *= 2;
556  }
557  goto loop;
558  }
559 
560  unput (c1);
561  bp[i] = 0;
562  return (bp == buff) ? mm_strdup (bp) : bp;
563 }
564 
565 
566 
567 int
568 esql_yyinput (char *buff, int max_size)
569 {
570  int c;
571  int i = 0;
572 
573  do
574  {
575  c = fgetc (esql_yyin);
576  buff[i++] = c;
577  if (i >= max_size)
578  {
579  PRINT ("partial input: %s\n", buff);
580  return i;
581  }
582 
583  }
584  while (c != -1);
585 
586  if (c == -1)
587  buff[--i] = 0;
588 
589  buff[i] = 0;
590  buff[i + 1] = 0;
591  buff[i + 2] = 0;
592  buff[i + 3] = 0;
593  buff[i + 4] = -1;
594  PRINT ("input: %s\n", buff);
595  return i;
596 }
597 
598 
599