Code

frontend: Always reset the current command after handling it.
[sysdb.git] / src / frontend / scanner.l
index f300e46d73160d906f841dd3c421f125d3a93fcc..dd2a86cf5273b4e5e3c450c02306820b282a1a46 100644 (file)
 
 %{
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "frontend/connection.h"
 #include "frontend/parser.h"
 #include "frontend/grammar.h"
@@ -70,6 +74,8 @@ csc_inside    ([^*/]+|[^*]\/|\*[^/])
 csc_end                \*\/
 
 identifier     ([A-Za-z_][A-Za-z_0-9$]*)
+/* TODO: fully support SQL strings */
+string         ('[^']*')
 
 %%
 
@@ -80,27 +86,53 @@ identifier  ([A-Za-z_][A-Za-z_0-9$]*)
 <CSC>{csc_inside}      { /* ignore */ }
 <CSC>{csc_end}         { BEGIN(INITIAL); }
 <CSC><<EOF>> {
-               sdb_fe_yyerror(yylval, yyscanner, "unterminated C-style comment");
+               sdb_fe_yyerror(yylloc, yyscanner, "unterminated C-style comment");
                return SCANNER_ERROR;
        }
 
 {identifier} {
-               /* XXX */
-               if (! strcasecmp(yytext, "LIST"))
+               /* XXX: simplify handling of reserved words */
+               if (! strcasecmp(yytext, "AND"))
+                       return AND;
+               else if (! strcasecmp(yytext, "FETCH"))
+                       return FETCH;
+               else if (! strcasecmp(yytext, "LIST"))
                        return LIST;
-
+               else if (! strcasecmp(yytext, "LOOKUP"))
+                       return LOOKUP;
+               else if (! strcasecmp(yytext, "NOT"))
+                       return NOT;
+               else if (! strcasecmp(yytext, "OR"))
+                       return OR;
+               else if (! strcasecmp(yytext, "WHERE"))
+                       return WHERE;
+
+               yylval->str = strdup(yytext);
                return IDENTIFIER;
        }
+{string} {
+               yytext[yyleng - 1] = '\0';
+               yylval->str = strdup(yytext + 1);
+               return STRING;
+       }
+
+=      { return CMP_EQUAL; }
+!=     { return CMP_NEQUAL; }
+=~     { return CMP_REGEX; }
+!~     { return CMP_NREGEX; }
 
-.      { /* do nothing for now */ }
+.      { /* XXX: */ return yytext[0]; }
 
 %%
 
 sdb_fe_yyscan_t
-sdb_fe_scanner_init(const char *str, sdb_fe_yyextra_t *yyext)
+sdb_fe_scanner_init(const char *str, int len, sdb_fe_yyextra_t *yyext)
 {
        yyscan_t scanner;
 
+       if (! str)
+               return NULL;
+
        if (sdb_fe_yylex_init(&scanner)) {
                char errbuf[1024];
                sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
@@ -110,9 +142,12 @@ sdb_fe_scanner_init(const char *str, sdb_fe_yyextra_t *yyext)
 
        sdb_fe_yyset_extra(yyext, scanner);
 
+       if (len < 0)
+               len = strlen(str);
+
        /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
         * scanner and, thus, will be freed by yylex_destroy */
-       yy_scan_string(str, scanner);
+       sdb_fe_yy_scan_bytes(str, len, scanner);
        return scanner;
 } /* sdb_fe_scanner_init */