/
Parser.h
396 lines (331 loc) · 9.66 KB
/
Parser.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Adriano dos Santos Fernandes <adrianosf@uol.com.br>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#ifndef DSQL_PARSER_H
#define DSQL_PARSER_H
#include "../dsql/dsql.h"
#include "../dsql/DdlNodes.h"
#include "../dsql/BoolNodes.h"
#include "../dsql/ExprNodes.h"
#include "../dsql/AggNodes.h"
#include "../dsql/WinNodes.h"
#include "../dsql/PackageNodes.h"
#include "../dsql/StmtNodes.h"
#include "../jrd/RecordSourceNodes.h"
#include "../common/classes/Nullable.h"
#include "../common/classes/stack.h"
#include "gen/parse.h"
namespace Jrd {
class CharSet;
class Parser : public Firebird::PermanentStorage
{
private:
// User-defined text position type.
struct Position
{
ULONG firstLine;
ULONG firstColumn;
ULONG lastLine;
ULONG lastColumn;
const char* firstPos;
const char* lastPos;
};
typedef Position YYPOSN;
typedef int Yshort;
struct yyparsestate
{
yyparsestate* save; // Previously saved parser state
int state;
int errflag;
Yshort* ssp; // state stack pointer
YYSTYPE* vsp; // value stack pointer
YYPOSN* psp; // position stack pointer
YYSTYPE val; // value as returned by actions
YYPOSN pos; // position as returned by universal action
Yshort* ss; // state stack base
YYSTYPE* vs; // values stack base
YYPOSN* ps; // position stack base
int lexeme; // index of the conflict lexeme in the lexical queue
unsigned int stacksize; // current maximum stack size
Yshort ctry; // index in yyctable[] for this conflict
};
struct LexerState
{
// This is, in fact, parser state. Not used in lexer itself
int dsql_debug;
// Actual lexer state begins from here
const TEXT* ptr;
const TEXT* end;
const TEXT* last_token;
const TEXT* start;
const TEXT* line_start;
const TEXT* last_token_bk;
const TEXT* line_start_bk;
SSHORT att_charset;
SLONG lines, lines_bk;
int prev_keyword;
USHORT param_number;
};
struct StrMark
{
StrMark()
: introduced(false),
pos(0),
length(0),
str(NULL)
{
}
bool operator >(const StrMark& o) const
{
return pos > o.pos;
}
bool introduced;
unsigned pos;
unsigned length;
IntlString* str;
};
public:
static const int MAX_TOKEN_LEN = 256;
public:
Parser(thread_db* tdbb, MemoryPool& pool, DsqlCompilerScratch* aScratch, USHORT aClientDialect,
USHORT aDbDialect, const TEXT* string, size_t length,
SSHORT characterSet);
~Parser();
public:
dsql_req* parse();
const Firebird::string& getTransformedString() const
{
return transformedString;
}
bool isStmtAmbiguous() const
{
return stmt_ambiguous;
}
Firebird::string* newString(const Firebird::string& s)
{
return FB_NEW_POOL(getPool()) Firebird::string(getPool(), s);
}
IntlString* newIntlString(const Firebird::string& s, const char* charSet = NULL)
{
return FB_NEW_POOL(getPool()) IntlString(getPool(), s, charSet);
}
// newNode overloads
template <typename T>
T* newNode()
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool()));
}
template <typename T, typename T1>
T* newNode(T1 a1)
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool(), a1));
}
template <typename T, typename T1, typename T2>
T* newNode(T1 a1, T2 a2)
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool(), a1, a2));
}
template <typename T, typename T1, typename T2, typename T3>
T* newNode(T1 a1, T2 a2, T3 a3)
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool(), a1, a2, a3));
}
template <typename T, typename T1, typename T2, typename T3, typename T4>
T* newNode(T1 a1, T2 a2, T3 a3, T4 a4)
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool(), a1, a2, a3, a4));
}
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
T* newNode(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
{
return setupNode<T>(FB_NEW_POOL(getPool()) T(getPool(), a1, a2, a3, a4, a5));
}
private:
template <typename T> T* setupNode(Node* node)
{
node->line = yyposn.firstLine;
node->column = yyposn.firstColumn;
return static_cast<T*>(node);
}
// Overload to not make LineColumnNode data wrong after its construction.
template <typename T> T* setupNode(LineColumnNode* node)
{
return node;
}
// Overload for non-Node classes constructed with newNode.
template <typename T> T* setupNode(void* node)
{
return static_cast<T*>(node);
}
BoolExprNode* valueToBool(ValueExprNode* value)
{
BoolAsValueNode* node = value->as<BoolAsValueNode>();
if (node)
return node->boolean;
ComparativeBoolNode* cmpNode = newNode<ComparativeBoolNode>(
blr_eql, value, MAKE_constant("1", CONSTANT_BOOLEAN));
cmpNode->dsqlCheckBoolean = true;
return cmpNode;
}
void yyReducePosn(YYPOSN& ret, YYPOSN* termPosns, YYSTYPE* termVals,
int termNo, int stkPos, int yychar, YYPOSN& yyposn, void*);
int yylex();
bool yylexSkipSpaces();
int yylexAux();
void yyerror(const TEXT* error_string);
void yyerror_detailed(const TEXT* error_string, int yychar, YYSTYPE&, YYPOSN&);
void yyerrorIncompleteCmd();
void check_bound(const char* const to, const char* const string);
void check_copy_incr(char*& to, const char ch, const char* const string);
void yyabandon(const Position& position, SLONG, ISC_STATUS);
Firebird::MetaName optName(Firebird::MetaName* name)
{
return (name ? *name : Firebird::MetaName());
}
void transformString(const char* start, unsigned length, Firebird::string& dest);
Firebird::string makeParseStr(const Position& p1, const Position& p2);
ParameterNode* make_parameter();
// Set the value of a clause, checking if it was already specified.
template <typename T>
void setClause(T& clause, const char* duplicateMsg, const T& value)
{
checkDuplicateClause(clause, duplicateMsg);
clause = value;
}
template <typename T, typename Delete>
void setClause(Firebird::AutoPtr<T, Delete>& clause, const char* duplicateMsg, T* value)
{
checkDuplicateClause(clause, duplicateMsg);
clause = value;
}
template <typename T>
void setClause(BaseNullable<T>& clause, const char* duplicateMsg, const T& value)
{
checkDuplicateClause(clause, duplicateMsg);
clause = value;
}
template <typename T1, typename T2>
void setClause(NestConst<T1>& clause, const char* duplicateMsg, const T2& value)
{
setClause(*clause.getAddress(), duplicateMsg, value);
}
void setClause(bool& clause, const char* duplicateMsg)
{
setClause(clause, duplicateMsg, true);
}
void setClauseFlag(unsigned& clause, const unsigned flag, const char* duplicateMsg)
{
using namespace Firebird;
if (clause & flag)
{
status_exception::raise(
Arg::Gds(isc_sqlerr) << Arg::Num(-637) <<
Arg::Gds(isc_dsql_duplicate_spec) << duplicateMsg);
}
clause |= flag;
}
template <typename T>
void checkDuplicateClause(const T& clause, const char* duplicateMsg)
{
using namespace Firebird;
if (isDuplicateClause(clause))
{
status_exception::raise(
Arg::Gds(isc_sqlerr) << Arg::Num(-637) <<
Arg::Gds(isc_dsql_duplicate_spec) << duplicateMsg);
}
}
template <typename T>
bool isDuplicateClause(const T& clause)
{
return clause != 0;
}
bool isDuplicateClause(const Firebird::MetaName& clause)
{
return clause.hasData();
}
template <typename T>
bool isDuplicateClause(const BaseNullable<T>& clause)
{
return clause.specified;
}
template <typename T>
bool isDuplicateClause(const Firebird::Array<T>& clause)
{
return clause.hasData();
}
// start - defined in btyacc_fb.ske
private:
static void yySCopy(YYSTYPE* to, YYSTYPE* from, int size);
static void yyPCopy(YYPOSN* to, YYPOSN* from, int size);
static void yyFreeState(yyparsestate* p);
void yyMoreStack(yyparsestate* yyps);
yyparsestate* yyNewState(int size);
private:
int parseAux();
int yylex1();
int yyexpand();
// end - defined in btyacc_fb.ske
private:
DsqlCompilerScratch* scratch;
USHORT client_dialect;
USHORT db_dialect;
USHORT parser_version;
CharSet* metadataCharSet;
Firebird::string transformedString;
Firebird::GenericMap<Firebird::NonPooled<IntlString*, StrMark> > strMarks;
bool stmt_ambiguous;
dsql_req* DSQL_parse;
// These value/posn are taken from the lexer
YYSTYPE yylval;
YYPOSN yyposn;
// These value/posn of the root non-terminal are returned to the caller
YYSTYPE yyretlval;
Position yyretposn;
int yynerrs;
// Current parser state
yyparsestate* yyps;
// yypath!=NULL: do the full parse, starting at *yypath parser state.
yyparsestate* yypath;
// Base of the lexical value queue
YYSTYPE* yylvals;
// Current posistion at lexical value queue
YYSTYPE* yylvp;
// End position of lexical value queue
YYSTYPE* yylve;
// The last allocated position at the lexical value queue
YYSTYPE* yylvlim;
// Base of the lexical position queue
Position* yylpsns;
// Current posistion at lexical position queue
Position* yylpp;
// End position of lexical position queue
Position* yylpe;
// The last allocated position at the lexical position queue
Position* yylplim;
// Current position at lexical token queue
Yshort* yylexp;
Yshort* yylexemes;
public:
LexerState lex;
};
} // namespace
#endif // DSQL_PARSER_H