This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql.l
157 lines (125 loc) · 3.49 KB
/
sql.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%{
#include "querycsv.h"
#include "sql.h"
#define YY_INPUT(buf,result,max_size) { \
myyyinput(yyin, yyextra, buf, &(result), max_size); \
}
/* macro to save the text and return a token */
#define TOK(name) { return name; }
#define LEX_WORKAROUND strReplace("]]", "]", yytext)
%}
%option outfile="lexer.c" header-file="lexer.h"
%option warn nodefault
%option reentrant noyywrap never-interactive nounistd yylineno
%option bison-bridge
%s commands
U [\x80-\xbf]
U2 [\xc2-\xdf]
U3 [\xe0-\xef]
U4 [\xf0-\xf4]
UANY {U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U}
%%
/* literal keyword tokens */
(?i:ALL) TOK(ALL)
(?i:AND) TOK(AND)
(?i:AVG) { yylval->intval = GRP_AVG;return AGGFN; }
(?i:MIN) { yylval->intval = GRP_MIN;return AGGFN; }
(?i:MAX) { yylval->intval = GRP_MAX;return AGGFN; }
(?i:SUM) { yylval->intval = GRP_SUM;return AGGFN; }
(?i:CURRENT_TIMESTAMP) { return NOW; }
(?i:COUNT) { yylval->intval = GRP_COUNT;return AGGFN; }
(?i:GROUP_CONCAT) { yylval->intval = GRP_CONCAT;return AGGFN; }
(?i:AS) TOK(AS)
(?i:ASC) TOK(ASC)
(?i:BY) TOK(BY)
(?i:ENCODING) TOK(ENCODING)
(?i:CASE) TOK(CASE)
(?i:COLLATE) TOK(COLLATE)
(?i:OUTPUT) { TOK(OUTPUT) }
(?i:COLUMNS) { TOK(COLUMNS) }
(?i:NEXT) { TOK(NEXT) }
(?i:VALUE) { TOK(VALUE) }
(?i:DATE) { TOK(DATE) }
(?i:NOW) { TOK(NOW) }
(?i:CONCAT) TOK(CONCAT)
(?i:SLICE) TOK(SLICE)
(?i:ROW_NUMBER) TOK(ROWNUMBER)
(?i:DESC) TOK(DESC)
(?i:DISTINCT) TOK(DISTINCT)
(?i:ELSE) TOK(ELSE)
(?i:END) TOK(END)
(?i:FROM) TOK(FROM)
(?i:GROUP) TOK(GROUP)
(?i:HAVING) TOK(HAVING)
(?i:IN) TOK(IN)
(?i:INTO) TOK(INTO)
(?i:IS) TOK(IS)
(?i:JOIN) TOK(JOIN)
(?i:LEFT) TOK(LEFT)
(?i:LIKE) TOK(LIKE)
(?i:NOT) TOK(NOT)
(?i:NULL) TOK(NULLX)
(?i:ON) TOK(ON)
(?i:OR) TOK(OR)
(?i:ORDER) TOK(ORDER)
(?i:OVER) TOK(OVER)
(?i:SORT) TOK(SORT)
(?i:PARTITION) TOK(PARTITION)
(?i:OPTIONS) TOK(OPTIONS)
(?i:SELECT) TOK(SELECT)
(?i:THEN) TOK(THEN)
(?i:WHEN) TOK(WHEN)
(?i:WHERE) TOK(WHERE)
(?i:MBCS) TOK(MBCS)
/* punctuation */
"=" { yylval->intval = 0;return COMPARISON; }
"<>" { yylval->intval = 1;return COMPARISON; }
"!=" { yylval->intval = 1;return COMPARISON; }
"<" { yylval->intval = 2;return COMPARISON; }
">" { yylval->intval = 3;return COMPARISON; }
"<=" { yylval->intval = 4;return COMPARISON; }
">=" { yylval->intval = 5;return COMPARISON; }
[-+*/(),.;] TOK(yytext[0])
/* names */
([A-Za-z]|{UANY})([A-Za-z0-9_]|{UANY})* {
yylval->strval = NULL;
d_sprintf(&(yylval->strval), "_" S_STRING, yytext);
return NAME;
}
\[([^\]]|\]\])+\] {
yytext[0] = '_';
yytext[strlen(yytext)-1] = '\0';
yylval->strval = LEX_WORKAROUND;
return NAME;
}
\[([^\]]|\]\])*$ { yyerror2(yylineno, yytext); }
/* numbers */
[0-9]+ {
yylval->strval = mystrdup(yytext);
return INTNUM;
}
[0-9]+"."[0-9]* |
"."[0-9]* |
[0-9]+[eE][+-]?[0-9]+ |
[0-9]+"."[0-9]*[eE][+-]?[0-9]+ |
"."[0-9]*[eE][+-]?[0-9]+ {
yylval->strval = mystrdup(yytext);
return STRING;
}
/* strings */
'([^']|'')*' {
yytext[strlen(yytext)-1] = '\0';
yylval->strval = strReplace("''", "'", yytext+1);
return STRING;
}
'([^']|'')*$ { yyerror2(yylineno, yytext); }
\"([^\"]|\"\")+\" {
yytext[strlen(yytext)-1] = '\0';
yylval->strval = strReplace("\"\"", "\"", yytext+1);
return STRING;
}
\"([^\"]|\"\")*$ { yyerror2(yylineno, yytext); }
[ \t\r\n\205]+ /* white space */
"--"[^\r\n]* /* comment */
. /* random non-SQL text. trigger a syntax error */
%%