33%option extra-type="MysqlParser::Parser*"
44
55%{
6- #include " mysql_parser/mysql_parser.h"
7- #include " mysql_parser/mysql_ast.h"
8- #include " mysql_parser.tab.h" // Will be mysql_parser.tab.h
6+ #include " mysql_parser/mysql_parser.h" // For MysqlParser::Parser, yyscan_t
7+ #include " mysql_parser/mysql_ast.h" // For MysqlParser::AstNode, etc.
8+ #include " mysql_parser.tab.h" // Bison-generated: token enums, defines union MYSQL_YYSTYPE and YYSTYPE
99#include < string>
1010#include < vector>
1111
12- // YY_DECL to control the signature of mysql_yylex
12+ // YY_DECL to control the signature of the generated mysql_yylex function.
13+ // This signature MUST match how Bison calls it (influenced by %lex-param in .y file
14+ // and the extern declaration of mysql_yylex in the .y file's prologue).
15+ // We use the explicit union name as deduced from previous Bison error messages.
16+ // The actual definition of 'union MYSQL_YYSTYPE' comes from "mysql_parser.tab.h".
1317union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql_parser.tab.h)
1418#undef YY_DECL
1519#define YY_DECL int mysql_yylex (union MYSQL_YYSTYPE *yylval_param, yyscan_t yyscanner, MysqlParser::Parser* parser_context)
1620
17- // #define YY_USER_DATA ((MysqlParser::Parser*)yyget_extra(yyscanner)) // Alternative way to access context
21+ // YY_USER_DATA can be used to access parser_context IF it's passed as yyextra,
22+ // OR if parser_context from YY_DECL is directly used.
23+ // Since parser_context is now a direct parameter to mysql_yylex via YY_DECL,
24+ // we can use it directly in actions.
25+ // #define YY_USER_DATA ((MysqlParser::Parser*)yyget_extra(yyscanner))
1826%}
1927
20- /* Declare exclusive start conditions */
2128%x COMMENT
2229%x SQSTRING
2330%x DQSTRING
@@ -27,9 +34,9 @@ union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql
2734
2835<INITIAL>{
2936 " /*" { BEGIN (COMMENT); }
30- " -- " .* { /* MySQL -- comment (note space) */ }
31- " --\n " { /* MySQL -- comment followed by newline */ }
32- " #" .* { /* MySQL # comment */ }
37+ " -- " .* { /* MySQL -- comment (note space); no action, just ignore */ }
38+ " --\n " { /* MySQL -- comment followed by newline; no action */ }
39+ " #" .* { /* MySQL # comment; no action */ }
3340
3441 [ \t\n]+ { /* Ignore whitespace */ }
3542
@@ -50,16 +57,30 @@ union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql
5057 " DEFAULT" { return TOKEN_DEFAULT; }
5158 " COLLATE" { return TOKEN_COLLATE; }
5259
60+ " DELETE" { return TOKEN_DELETE; }
61+ " LOW_PRIORITY" { return TOKEN_LOW_PRIORITY; }
62+ " QUICK" { return TOKEN_QUICK; }
63+ " IGNORE" { return TOKEN_IGNORE_SYM; }
64+ " USING" { return TOKEN_USING; }
65+ " ORDER" { return TOKEN_ORDER; }
66+ " BY" { return TOKEN_BY; }
67+ " LIMIT" { return TOKEN_LIMIT; }
68+ " ASC" { return TOKEN_ASC; }
69+ " DESC" { return TOKEN_DESC; }
70+ " WHERE" { return TOKEN_WHERE; }
71+ " AS" { return TOKEN_AS; }
72+
73+
5374 " `" { yylval_param->str_val = new std::string (); BEGIN (BTIDENT); }
5475
5576 " @@global." { return TOKEN_GLOBAL_VAR_PREFIX; }
5677 " @@session." { return TOKEN_SESSION_VAR_PREFIX; }
5778 " @@local." { return TOKEN_SESSION_VAR_PREFIX; } /* Alias for session */
58- " @@persisted." { /* Placeholder if you add specific handling */ return TOKEN_PERSIST_VAR_PREFIX; }
79+ " @@persisted." { return TOKEN_PERSIST_VAR_PREFIX; } /* If you add specific handling */
5980 " @@" { return TOKEN_DOUBLESPECIAL; }
6081 " @" { return TOKEN_SPECIAL; }
6182
62- [a-zA-Z_][a-zA-Z0-9_]* { /* Does not include hyphen for now */
83+ [a-zA-Z_][a-zA-Z0-9_]* {
6384 yylval_param->str_val = new std::string (yytext);
6485 return TOKEN_IDENTIFIER;
6586 }
@@ -71,10 +92,18 @@ union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql
7192 " (" { return TOKEN_LPAREN; }
7293 " )" { return TOKEN_RPAREN; }
7394 " ;" { return TOKEN_SEMICOLON; }
74- " =" { return TOKEN_EQUAL; }
7595 " ." { return TOKEN_DOT; }
7696 " ," { return TOKEN_COMMA; }
7797
98+ " =" { return TOKEN_EQUAL; }
99+ " <" { return TOKEN_LESS; }
100+ " >" { return TOKEN_GREATER; }
101+ " <=" { return TOKEN_LESS_EQUAL; }
102+ " >=" { return TOKEN_GREATER_EQUAL; }
103+ " !=" { return TOKEN_NOT_EQUAL; }
104+ " <>" { return TOKEN_NOT_EQUAL; }
105+
106+
78107 [0 -9 ]+(" ." [0 -9 ]+)? { yylval_param->str_val = new std::string (yytext); return TOKEN_NUMBER_LITERAL;}
79108
80109 . {
@@ -83,7 +112,7 @@ union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql
83112 if (parser_context) {
84113 parser_context->internal_add_error (err_msg);
85114 } else {
86- fprintf (stderr, " %s\n " , err_msg);
115+ fprintf (stderr, " %s\n " , err_msg); // Fallback
87116 }
88117 }
89118}
@@ -99,27 +128,30 @@ union MYSQL_YYSTYPE; // Forward declare for YY_DECL (Bison defines this in mysql
99128 " [^'\\\\ ]+" { *(yylval_param->str_val ) += yytext; }
100129 " \\ '" { *(yylval_param->str_val ) += " '" ; }
101130 " \\\\ " { *(yylval_param->str_val ) += " \\ " ; }
102- " ''" { *(yylval_param->str_val ) += " '" ; }
131+ " ''" { *(yylval_param->str_val ) += " '" ; } /* MySQL: '' inside string is a literal ' */
103132 " '" { *(yylval_param->str_val ) += " '" ; BEGIN (INITIAL); return TOKEN_STRING_LITERAL; }
104- <<EOF>> { if (parser_context) parser_context->internal_add_error (" Unterminated single-quoted string" ); BEGIN (INITIAL); return YY_NULL; }
133+ <<EOF>> { if (parser_context) parser_context->internal_add_error (" Unterminated single-quoted string" ); BEGIN (INITIAL); return YY_NULL; /* Return 0 for EOF/error */ }
105134}
106135
107136<DQSTRING>{
108137 " [^\"\\\\ ]+" { *(yylval_param->str_val ) += yytext; }
109138 " \\\" " { *(yylval_param->str_val ) += " \" " ; }
110139 " \\\\ " { *(yylval_param->str_val ) += " \\ " ; }
111- " \"\" " { *(yylval_param->str_val ) += " \" " ; }
140+ " \"\" " { *(yylval_param->str_val ) += " \" " ; } /* MySQL: "" inside string is a literal " */
112141 " \" " { *(yylval_param->str_val ) += " \" " ; BEGIN (INITIAL); return TOKEN_STRING_LITERAL; }
113142 <<EOF>> { if (parser_context) parser_context->internal_add_error (" Unterminated double-quoted string" ); BEGIN (INITIAL); return YY_NULL; }
114143}
115144
116145<BTIDENT>{
117- " `" { BEGIN (INITIAL); return TOKEN_IDENTIFIER; } // Returns the accumulated string
118- " ``" { *(yylval_param->str_val ) += ' `' ; }
146+ " `" { BEGIN (INITIAL); return TOKEN_IDENTIFIER; } /* Returns the accumulated string */
147+ " ``" { *(yylval_param->str_val ) += ' `' ; } /* `` inside backticks is a literal ` */
119148 [^`\n]+ { *(yylval_param->str_val ) += yytext; }
120- \n { if (parser_context) parser_context->internal_add_error (" Newline in backticked identifier" ); BEGIN (INITIAL); /* No explicit return */ }
149+ \n { if (parser_context) parser_context->internal_add_error (" Newline in backticked identifier" ); BEGIN (INITIAL); /* No explicit return, lexer will find next token or error */ }
121150 <<EOF>> { if (parser_context) parser_context->internal_add_error (" Unterminated backticked identifier" ); BEGIN (INITIAL); return YY_NULL; }
122151}
123152
124153%%
125154
155+ // yywrap is not strictly needed due to %option noyywrap
156+ // int mysql_yywrap(yyscan_t scanner) { return 1; }
157+
0 commit comments