2 * Copyright 2008 Search Solution Corporation
3 * Copyright 2016 CUBRID Corporation
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * esql_lexer.l : esql lexer file
25 #include "esql_grammar.h"
26 #include "esql_scanner_support.h"
28 #if defined (SUPPRESS_STRLEN_WARNING)
29 #define strlen(s1) ((int) strlen(s1))
30 #endif /* defined (SUPPRESS_STRLEN_WARNING) */
36 #define PRINT(a, b) printf(a, b)
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);
48 #define JP_MAXNAME 256
52 #define YY_INPUT(buffer, result, max_size) (result = esql_yyinput(buffer, max_size))
62 if (esql_yy_mode () != BUFFER_mode)
65 ECHO_STR (" ", strlen (" "));
71 int mode = esql_yy_mode ();
72 if (mode != BUFFER_mode)
75 ECHO_STR (" ", strlen (" "));
79 if (mode == ECHO_mode || mode == C_mode)
87 PRINT("tok: %s\n", yytext);
89 parser_line_comment();
95 PRINT("tok: %s\n", yytext);
101 PRINT("tok: %s\n", yytext);
103 parser_line_comment();
107 int mode = esql_yy_mode();
108 PRINT ("tok: %s\n", yytext);
113 return GENERIC_TOKEN;
115 else if (mode == ECHO_mode)
119 else if (mode == BUFFER_mode)
121 parser_ignore_comment ();
127 parser_line_comment ();
133 const char *quote = "\"";
134 char *buff = parser_quoted_string ('\"');
135 PRINT ("tok: %s\n", yytext);
137 ECHO_STR (quote, strlen (quote));
138 ECHO_STR (buff, strlen (buff));
139 ECHO_STR (quote, strlen (quote));
141 if (esql_yy_mode () == EXPR_mode)
143 return GENERIC_TOKEN;
151 PRINT("tok: %s\n", yytext);
156 PRINT("tok: %s\n", yytext);
161 PRINT("tok: %s\n", yytext);
166 int mode = esql_yy_mode();
167 PRINT ("tok: %s\n", yytext);
169 if (mode == ECHO_mode)
173 else if (mode == C_mode)
176 return GENERIC_TOKEN;
178 else if (mode == EXPR_mode)
181 return GENERIC_TOKEN;
183 else if (mode == HV_mode)
185 vs_strcat (&pp_subscript_buf, yytext);
196 [a-zA-Z_][a-zA-Z_0-9]* {{
197 int mode = esql_yy_mode ();
199 PRINT ("tok: %s\n", yytext);
200 esql_yylval.ptr = mm_strdup (yytext);
202 if (mode == ECHO_mode)
206 else if (mode == C_mode)
208 code = check_c_identifier (yytext);
213 else if (mode == EXPR_mode)
216 return GENERIC_TOKEN;
218 else if (mode == CSQL_mode)
220 code = check_identifier (&csql_table, yytext);
225 else if (mode == VAR_mode)
227 if (recognize_keywords)
229 code = check_identifier (&preprocessor_table, yytext);
233 code = check_c_identifier (yytext);
236 recognize_keywords = (code != INDICATOR);
238 if (code != IDENTIFIER)
240 sprintf (g_delay, "%s", yytext);
243 else if (intl_mbs_casecmp (yytext, "WHERE") == 0)
245 sprintf (g_delay, "%s", yytext);
248 else if (intl_mbs_casecmp (yytext, "AND") == 0)
250 sprintf (g_delay, "%s", yytext);
253 else if (intl_mbs_casecmp (yytext, "OR") == 0)
255 sprintf (g_delay, "%s", yytext);
258 else if (intl_mbs_casecmp (yytext, "ORDER") == 0)
260 sprintf (g_delay, "%s", yytext);
263 else if (intl_mbs_casecmp (yytext, "GROUP") == 0)
265 sprintf (g_delay, "%s", yytext);
268 else if (intl_mbs_casecmp (yytext, "UNION") == 0)
270 sprintf (g_delay, "%s", yytext);
273 else if (intl_mbs_casecmp (yytext, "USING") == 0)
275 sprintf (g_delay, "%s", yytext);
282 else if (mode == BUFFER_mode)
286 if (intl_mbs_casecmp (yytext, "DESCRIPTOR") == 0)
290 else if (intl_mbs_casecmp (yytext, "INTO") == 0)
294 else if (intl_mbs_casecmp (yytext, "TO") == 0)
298 else if (intl_mbs_casecmp (yytext, "VALUES") == 0)
302 else if (intl_mbs_casecmp (yytext, "SELECT") == 0)
309 else if (mode == HV_mode)
311 vs_strcat (&pp_subscript_buf, yytext);
323 \'((\'\')|([^\']))*\' {{
324 int mode = esql_yy_mode ();
325 PRINT ("tok: %s\n", yytext);
327 if (mode == ECHO_mode)
331 else if (mode == CSQL_mode)
334 esql_yylval.ptr = mm_strdup (yytext);
340 return GENERIC_TOKEN;
346 int mode = esql_yy_mode ();
347 PRINT ("tok: %s\n", yytext);
349 if (mode == ECHO_mode)
359 else if (mode == C_mode)
364 else if (mode == EXPR_mode)
381 else if (mode == VAR_mode)
388 recognize_keywords = false;
407 sprintf (g_delay, "%s", yytext);
417 sprintf (g_delay, "%s", yytext);
418 return GENERIC_TOKEN;
421 else if (mode == BUFFER_mode)
430 recognize_keywords = false;
434 if (c == ':' || c == ';')
458 printf ("Parsing done.....\n");
464 parser_c_comment (void)
469 while ((c = input ()) != '*' && c != 0 && c != -1)
476 if ((c1 = input ()) != '/' && c != 0 && c != -1)
490 parser_ignore_comment (void)
494 while ((c = input ()) != '\r' && c != '\n' && c != 0 && c != -1)
503 parser_line_comment (void)
507 while ((c = input ()) != '\r' && c != '\n' && c != 0 && c != -1)
519 parser_quoted_string (char end)
529 while ((c = input ()) != end && c != 0 && c != -1)
534 char *bp_new = mm_malloc (size * 2);
535 memcpy (bp_new, bp, size);
541 if (c == 0 || c == -1)
544 return (bp == buff) ? mm_strdup (bp) : bp;
547 if ((c1 = input ()) == end && c != 0 && c != -1)
552 char *bp_new = mm_malloc (size * 2);
553 memcpy (bp_new, bp, size);
562 return (bp == buff) ? mm_strdup (bp) : bp;
568 esql_yyinput (char *buff, int max_size)
575 c = fgetc (esql_yyin);
579 PRINT ("partial input: %s\n", buff);
594 PRINT ("input: %s\n", buff);