This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
qgssqllexer.ll
239 lines (188 loc) · 6.54 KB
/
qgssqllexer.ll
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/***************************************************************************
qgssqllexer.ll
--------------------
begin : Feb. 2015
copyright : (C) 2015 by Hugo Mercier / Oslandia
email : hugo dot mercier at oslandia dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
%option noyywrap
%option case-insensitive
%option never-interactive
%option nounput
%option prefix="sqlp_"
// this makes flex generate lexer with context + init/destroy functions
%option reentrant
// this makes Bison send yylex another argument to use instead of using the global variable yylval
%option bison-bridge
// ensure that lexer will be 8-bit (and not just 7-bit)
%option 8bit
%{
#include <stdlib.h> // atof()
#include "qgsexpression.h"
#include "qgssql.h"
struct expression_parser_context;
#include "qgssqlparser.hpp"
#include <QRegExp>
#include <QLocale>
// if not defined, searches for isatty()
// which doesn't in MSVC compiler
#define YY_NEVER_INTERACTIVE 1
#ifndef YY_NO_UNPUT
#define YY_NO_UNPUT // unused
#endif
#define YY_DECL int sqlp_lex (YYSTYPE * yylval_param, YYLTYPE* yylloc, yyscan_t yyscanner)
#define YY_USER_ACTION yylloc->first_line = yylloc->last_line = yylineno; \
yylloc->first_column = yycolumn; yylloc->last_column = yycolumn+yyleng-1; \
yycolumn += yyleng;
#ifdef _MSC_VER
#define YY_NO_UNISTD_H
#endif
#define B_OP(x) yylval->b_op = QgsExpression::x
#define U_OP(x) yylval->u_op = QgsExpression::x
#define TEXT yylval->text = new QString(); *yylval->text = QString::fromUtf8(yytext);
#define TEXT_FILTER(filter_fn) yylval->text = new QString(); *yylval->text = filter_fn( QString::fromUtf8(yytext) );
static QString stripText(QString text)
{
// strip single quotes on start,end
text = text.mid( 1, text.length() - 2 );
// make single "single quotes" from double "single quotes"
text.replace( QRegExp( "''" ), "'" );
// strip \n \' etc.
int index = 0;
while (( index = text.indexOf( '\\', index ) ) != -1 )
{
text.remove( index, 1 ); // delete backslash
QChar chr;
switch ( text[index].toLatin1() ) // evaluate backslashed character
{
case 'n': chr = '\n'; break;
case 't': chr = '\t'; break;
case '\\': chr = '\\'; break;
case '\'': chr = '\''; break;
default: chr = '?'; break;
}
text[index++] = chr; // set new character and push index +1
}
return text;
}
static QString stripColumnRef(QString text)
{
// strip double quotes on start,end
text = text.mid( 1, text.length() - 2 );
// make single "double quotes" from double "double quotes"
text.replace( QRegExp( "\"\"" ), "\"" );
return text;
}
// C locale for correct parsing of numbers even if the system locale is different
static QLocale cLocale("C");
%}
%s BLOCK_COMMENT
line_comment \-\-[^\r\n]*[\r\n]?
white [ \t\r\n]+
non_ascii [\x80-\xFF]
col_first [A-Za-z_]|{non_ascii}
col_next [A-Za-z0-9_]|{non_ascii}
identifier {col_first}{col_next}*
special_col "$"{identifier}
col_str_char "\"\""|[^\"]
identifier_quoted "\""{col_str_char}*"\""
dig [0-9]
num_int {dig}+
num_float {dig}*(\.{dig}+([eE][-+]?{dig}+)?|[eE][-+]?{dig}+)
str_char ('')|(\\.)|[^'\\]
string "'"{str_char}*"'"
%%
<INITIAL>{
"/*" BEGIN(BLOCK_COMMENT);
}
<BLOCK_COMMENT>{
"*/" BEGIN(INITIAL);
[^*\n]+ // eat comment in chunks
"*" // eat the lone star
\n yylineno++;
}
"NOT" { U_OP(uoNot); return NOT; }
"AND" { B_OP(boAnd); return AND; }
"OR" { B_OP(boOr); return OR; }
"=" { B_OP(boEQ); return EQ; }
"!=" { B_OP(boNE); return NE; }
"<=" { B_OP(boLE); return LE; }
">=" { B_OP(boGE); return GE; }
"<>" { B_OP(boNE); return NE; }
"<" { B_OP(boLT); return LT; }
">" { B_OP(boGT); return GT; }
"~" { B_OP(boRegexp); return REGEXP; }
"LIKE" { B_OP(boLike); return LIKE; }
"NOT LIKE" { B_OP(boNotLike); return LIKE; }
"ILIKE" { B_OP(boILike); return LIKE; }
"NOT ILIKE" { B_OP(boNotILike); return LIKE; }
"IS" { B_OP(boIs); return IS; }
"IS NOT" { B_OP(boIsNot); return IS; }
"||" { B_OP(boConcat); return CONCAT; }
"+" { B_OP(boPlus); return PLUS; }
"-" { B_OP(boMinus); return MINUS; }
"*" { B_OP(boMul); return MUL; }
"/" { B_OP(boDiv); return DIV; }
"%" { B_OP(boMod); return MOD; }
"IN" { return IN; }
"NULL" { return NULLVALUE; }
"CASE" { return CASE; }
"WHEN" { return WHEN; }
"THEN" { return THEN; }
"ELSE" { return ELSE; }
"END" { return END; }
"INNER" { return INNER; }
"LEFT" { return LEFT; }
"OUTER" { return OUTER; }
"CROSS" { return CROSS; }
"JOIN" { return JOIN; }
"NATURAL" { return NATURAL; }
"USING" { return USING; }
"ON" { return ON; }
"SELECT" { return SELECT; }
"FROM" { return FROM; }
"AS" { return AS; }
"WHERE" { return WHERE; }
"ALL" { return ALL; }
"DISTINCT" { return DISTINCT; }
"UNION" { return UNION; }
"EXCEPT" { return EXCEPT; }
"INTERSECT" { return INTERSECT; }
"ORDER" { return ORDER; }
"GROUP" { return GROUP; }
"BY" { return BY; }
"HAVING" { return HAVING; }
"EXISTS" { return EXISTS; }
"CAST" { return CAST; }
"LIMIT" { return LIMIT; }
"OFFSET" { return OFFSET; }
"ASC" { return ASC; }
"DESC" { return DESC; }
[()\.] { return yytext[0]; }
"," { return COMMA; }
{num_float} { yylval->numberFloat = cLocale.toDouble( QString::fromAscii(yytext) ); return NUMBER_FLOAT; }
{num_int} {
bool ok;
yylval->numberInt = cLocale.toInt( QString::fromAscii(yytext), &ok );
if( ok )
return NUMBER_INT;
yylval->numberFloat = cLocale.toDouble( QString::fromAscii(yytext), &ok );
if( ok )
return NUMBER_FLOAT;
return Unknown_CHARACTER;
}
{string} { TEXT_FILTER(stripText); return STRING; }
{special_col} { TEXT; return SPECIAL_COL; }
{identifier} { TEXT; return IDENTIFIER; }
{identifier_quoted} { TEXT_FILTER(stripColumnRef); return IDENTIFIER; }
{white} /* skip blanks and tabs */
{line_comment} /* skip line comments */
. { return Unknown_CHARACTER; }
%%