2008-02-03 20:16:12 +01:00
|
|
|
/*
|
|
|
|
* 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 "../jrd/common.h"
|
|
|
|
#include "../dsql/dsql.h"
|
2008-02-28 19:42:30 +01:00
|
|
|
#include "../dsql/node.h"
|
2008-05-19 15:47:48 +02:00
|
|
|
#include "../dsql/DdlNodes.h"
|
2010-02-13 21:29:29 +01:00
|
|
|
#include "../dsql/ExprNodes.h"
|
|
|
|
#include "../dsql/AggNodes.h"
|
2010-02-14 20:08:22 +01:00
|
|
|
#include "../dsql/WinNodes.h"
|
2009-10-21 02:42:38 +02:00
|
|
|
#include "../dsql/PackageNodes.h"
|
2008-05-19 15:47:48 +02:00
|
|
|
#include "../dsql/StmtNodes.h"
|
2009-10-21 02:42:38 +02:00
|
|
|
#include "../common/classes/TriState.h"
|
2008-04-15 21:45:19 +02:00
|
|
|
#include "../common/classes/stack.h"
|
2008-02-28 14:48:16 +01:00
|
|
|
|
2009-10-21 02:42:38 +02:00
|
|
|
#define _yacc_defines_keywords
|
|
|
|
#include "../dsql/dsql.tab.h"
|
|
|
|
|
2008-02-28 14:48:16 +01:00
|
|
|
namespace Jrd {
|
|
|
|
|
|
|
|
class dsql_nod;
|
2008-02-03 20:16:12 +01:00
|
|
|
|
2008-04-15 04:18:38 +02:00
|
|
|
class Parser : public Firebird::PermanentStorage
|
2008-02-03 20:16:12 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef int Yshort;
|
|
|
|
typedef int YYPOSN; // user-defined text position type
|
|
|
|
|
|
|
|
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
|
|
|
|
dsql_fld* g_field;
|
|
|
|
dsql_fil* g_file;
|
2009-10-21 02:42:38 +02:00
|
|
|
dsql_nod* g_field_name;
|
2008-02-03 20:16:12 +01:00
|
|
|
int dsql_debug;
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-02-03 20:16:12 +01:00
|
|
|
// Actual lexer state begins from here
|
2008-04-15 21:45:19 +02:00
|
|
|
|
|
|
|
// hvlad: if at some day 16 levels of nesting would be not enough
|
2008-12-05 01:56:15 +01:00
|
|
|
// then someone must add LexerState constructor and pass memory
|
|
|
|
// pool into Stack's constructor or change Capacity value in template
|
2008-04-15 21:45:19 +02:00
|
|
|
// instantiation below
|
|
|
|
Firebird::Stack<const TEXT*> beginnings;
|
2008-02-03 20:16:12 +01:00
|
|
|
const TEXT* ptr;
|
|
|
|
const TEXT* end;
|
|
|
|
const TEXT* last_token;
|
2009-08-30 04:26:50 +02:00
|
|
|
const TEXT* start;
|
2008-02-03 20:16:12 +01:00
|
|
|
const TEXT* line_start;
|
|
|
|
const TEXT* last_token_bk;
|
|
|
|
const TEXT* line_start_bk;
|
|
|
|
SSHORT lines, att_charset;
|
|
|
|
SSHORT lines_bk;
|
|
|
|
int prev_keyword;
|
|
|
|
USHORT param_number;
|
|
|
|
};
|
|
|
|
|
2009-12-19 02:32:00 +01:00
|
|
|
struct StrMark
|
2009-08-30 04:26:50 +02:00
|
|
|
{
|
2009-12-19 02:32:00 +01:00
|
|
|
StrMark()
|
|
|
|
: introduced(false),
|
|
|
|
pos(0),
|
|
|
|
length(0),
|
|
|
|
str(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator >(const StrMark& o) const
|
|
|
|
{
|
|
|
|
return pos > o.pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool introduced;
|
2009-08-30 04:26:50 +02:00
|
|
|
unsigned pos;
|
|
|
|
unsigned length;
|
2009-11-07 20:02:46 +01:00
|
|
|
dsql_str* str;
|
2009-08-30 04:26:50 +02:00
|
|
|
};
|
|
|
|
|
2008-02-03 20:16:12 +01:00
|
|
|
public:
|
2008-04-15 04:18:38 +02:00
|
|
|
Parser(MemoryPool& pool, USHORT aClientDialect, USHORT aDbDialect, USHORT aParserVersion,
|
2010-03-21 14:38:52 +01:00
|
|
|
const TEXT* string, size_t length, SSHORT characterSet);
|
2008-02-03 20:16:12 +01:00
|
|
|
~Parser();
|
|
|
|
|
|
|
|
public:
|
2009-10-21 02:42:38 +02:00
|
|
|
dsql_nod* parse();
|
2008-02-03 20:16:12 +01:00
|
|
|
|
2009-09-02 06:23:02 +02:00
|
|
|
const Firebird::string& getTransformedString() const
|
2009-08-30 04:26:50 +02:00
|
|
|
{
|
|
|
|
return transformedString;
|
|
|
|
}
|
|
|
|
|
2009-01-08 10:26:06 +01:00
|
|
|
bool isStmtAmbiguous() const
|
2008-02-03 20:16:12 +01:00
|
|
|
{
|
|
|
|
return stmt_ambiguous;
|
|
|
|
}
|
|
|
|
|
2009-08-30 04:26:50 +02:00
|
|
|
private:
|
|
|
|
void transformString(const char* start, unsigned length, Firebird::string& dest);
|
|
|
|
|
2010-07-06 02:49:33 +02:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
using namespace Firebird;
|
|
|
|
if (isDuplicateClause(clause))
|
|
|
|
{
|
|
|
|
status_exception::raise(
|
|
|
|
Arg::Gds(isc_sqlerr) << Arg::Num(-637) <<
|
|
|
|
Arg::Gds(isc_dsql_duplicate_spec) << duplicateMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
clause = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Delete>
|
|
|
|
void setClause(Firebird::AutoPtr<T, Delete>& clause, const char* duplicateMsg, T* value)
|
|
|
|
{
|
|
|
|
using namespace Firebird;
|
|
|
|
if (isDuplicateClause(clause))
|
|
|
|
{
|
|
|
|
status_exception::raise(
|
|
|
|
Arg::Gds(isc_sqlerr) << Arg::Num(-637) <<
|
|
|
|
Arg::Gds(isc_dsql_duplicate_spec) << duplicateMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
clause = value;
|
|
|
|
}
|
|
|
|
|
2010-07-28 18:14:20 +02:00
|
|
|
template <typename T>
|
|
|
|
void setClause(TriStateType<T>& clause, const char* duplicateMsg, const T& value)
|
|
|
|
{
|
|
|
|
using namespace Firebird;
|
|
|
|
if (clause.specified)
|
|
|
|
{
|
|
|
|
status_exception::raise(
|
|
|
|
Arg::Gds(isc_sqlerr) << Arg::Num(-637) <<
|
|
|
|
Arg::Gds(isc_dsql_duplicate_spec) << duplicateMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
clause = value;
|
|
|
|
}
|
|
|
|
|
2010-07-06 02:49:33 +02:00
|
|
|
void setClause(bool& clause, const char* duplicateMsg)
|
|
|
|
{
|
|
|
|
setClause(clause, duplicateMsg, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool isDuplicateClause(const T& clause)
|
|
|
|
{
|
|
|
|
return clause != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDuplicateClause(const Firebird::MetaName& clause)
|
|
|
|
{
|
|
|
|
return clause.hasData();
|
|
|
|
}
|
|
|
|
|
2008-02-03 20:16:12 +01:00
|
|
|
// 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 yyMoreStack(yyparsestate* yyps);
|
|
|
|
static yyparsestate* yyNewState(int size);
|
|
|
|
static void yyFreeState(yyparsestate* p);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int parseAux();
|
|
|
|
int yylex1();
|
|
|
|
int yyexpand();
|
|
|
|
// end - defined in btyacc_fb.ske
|
|
|
|
|
|
|
|
// start - defined in parse.y
|
|
|
|
private:
|
|
|
|
int yylex();
|
|
|
|
int yylexAux();
|
|
|
|
|
|
|
|
void yyerror(const TEXT* error_string);
|
|
|
|
void yyerror_detailed(const TEXT* error_string, int yychar, YYSTYPE&, YYPOSN&);
|
|
|
|
|
|
|
|
const TEXT* lex_position();
|
2009-10-21 02:42:38 +02:00
|
|
|
dsql_nod* make_list (dsql_nod* node);
|
|
|
|
dsql_nod* make_parameter();
|
|
|
|
dsql_nod* make_node(Dsql::nod_t type, int count, ...);
|
2010-02-13 21:29:29 +01:00
|
|
|
dsql_nod* makeClassNode(ExprNode* node);
|
|
|
|
dsql_nod* makeClassNode(DdlNode* node);
|
|
|
|
dsql_nod* makeClassNode(StmtNode* node);
|
2009-10-21 02:42:38 +02:00
|
|
|
dsql_nod* make_flag_node(Dsql::nod_t type, SSHORT flag, int count, ...);
|
2008-02-03 20:16:12 +01:00
|
|
|
// end - defined in parse.y
|
|
|
|
|
|
|
|
private:
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::string compilingText;
|
2008-02-03 20:16:12 +01:00
|
|
|
USHORT client_dialect;
|
|
|
|
USHORT db_dialect;
|
|
|
|
USHORT parser_version;
|
|
|
|
|
2009-08-30 04:26:50 +02:00
|
|
|
Firebird::string transformedString;
|
2009-12-19 02:32:00 +01:00
|
|
|
Firebird::GenericMap<Firebird::NonPooled<dsql_str*, StrMark> > strMarks;
|
2008-02-03 20:16:12 +01:00
|
|
|
bool stmt_ambiguous;
|
2009-10-21 02:42:38 +02:00
|
|
|
dsql_nod* DSQL_parse;
|
2008-02-03 20:16:12 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
int 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
|
|
|
|
int* yylpsns;
|
|
|
|
// Current posistion at lexical position queue
|
|
|
|
int* yylpp;
|
|
|
|
// End position of lexical position queue
|
|
|
|
int* yylpe;
|
|
|
|
// The last allocated position at the lexical position queue
|
|
|
|
int* yylplim;
|
|
|
|
// Current position at lexical token queue
|
|
|
|
Yshort* yylexp;
|
|
|
|
Yshort* yylexemes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LexerState lex;
|
|
|
|
};
|
|
|
|
|
2008-02-29 09:45:02 +01:00
|
|
|
} // namespace
|
2008-02-03 20:16:12 +01:00
|
|
|
|
|
|
|
#endif // DSQL_PARSER_H
|