Skip to content

Commit c26edb9

Browse files
committed
Fixed CORE-6466 - Comments before the first line of code are removed.
1 parent 6088e5d commit c26edb9

4 files changed

Lines changed: 40 additions & 48 deletions

File tree

src/dsql/Parser.cpp

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Parser::Parser(MemoryPool& pool, DsqlCompilerScratch* aScratch, USHORT aClientDi
101101
yylexemes = 0;
102102

103103
lex.start = string;
104-
lex.line_start = lex.last_token = lex.ptr = string;
104+
lex.line_start = lex.last_token = lex.ptr = lex.leadingPtr = string;
105105
lex.end = string + length;
106106
lex.lines = 1;
107107
lex.att_charset = characterSet;
@@ -217,11 +217,12 @@ void Parser::transformString(const char* start, unsigned length, string& dest)
217217
// Make a substring from the command text being parsed.
218218
string Parser::makeParseStr(const Position& p1, const Position& p2)
219219
{
220-
const char* start = p1.firstPos;
221-
const char* end = p2.lastPos;
220+
const char* start = p1.leadingFirstPos;
221+
const char* end = p2.trailingLastPos;
222222

223223
string str;
224224
transformString(start, end - start, str);
225+
str.trim(" \t\r\n");
225226

226227
string ret;
227228

@@ -254,15 +255,18 @@ void Parser::yyReducePosn(YYPOSN& ret, YYPOSN* termPosns, YYSTYPE* /*termVals*/,
254255
ret.firstLine = ret.lastLine = termPosns[termNo - 1].lastLine;
255256
ret.firstColumn = ret.lastColumn = termPosns[termNo - 1].lastColumn;
256257
ret.firstPos = ret.lastPos = termPosns[termNo - 1].lastPos;
258+
ret.leadingFirstPos = ret.trailingLastPos = termPosns[termNo - 1].trailingLastPos;
257259
}
258260
else
259261
{
260262
ret.firstLine = termPosns[0].firstLine;
261263
ret.firstColumn = termPosns[0].firstColumn;
262264
ret.firstPos = termPosns[0].firstPos;
265+
ret.leadingFirstPos = termPosns[0].leadingFirstPos;
263266
ret.lastLine = termPosns[termNo - 1].lastLine;
264267
ret.lastColumn = termPosns[termNo - 1].lastColumn;
265268
ret.lastPos = termPosns[termNo - 1].lastPos;
269+
ret.trailingLastPos = termPosns[termNo - 1].trailingLastPos;
266270
}
267271

268272
/*** This allows us to see colored output representing the position reductions.
@@ -283,29 +287,24 @@ int Parser::yylex()
283287
yyposn.firstLine = lex.lines;
284288
yyposn.firstColumn = lex.ptr - lex.line_start;
285289
yyposn.firstPos = lex.ptr - 1;
290+
yyposn.leadingFirstPos = lex.leadingPtr;
286291

287292
lex.prev_keyword = yylexAux();
288293

289-
const TEXT* ptr = lex.ptr;
290-
const TEXT* last_token = lex.last_token;
291-
const TEXT* line_start = lex.line_start;
292-
const SLONG lines = lex.lines;
294+
yyposn.lastPos = lex.ptr;
295+
lex.leadingPtr = lex.ptr;
293296

294297
// Lets skip spaces before store lastLine/lastColumn. This is necessary to avoid yyReducePosn
295298
// produce invalid line/column information - CORE-4381.
296-
yylexSkipSpaces();
299+
bool spacesSkipped = yylexSkipSpaces();
297300

298301
yyposn.lastLine = lex.lines;
299302
yyposn.lastColumn = lex.ptr - lex.line_start;
300303

301-
lex.ptr = ptr;
302-
lex.last_token = last_token;
303-
lex.line_start = line_start;
304-
lex.lines = lines;
304+
if (spacesSkipped)
305+
--lex.ptr;
305306

306-
// But the correct value for lastPos is the old (before the second yyLexSkipSpaces)
307-
// value of lex.ptr.
308-
yyposn.lastPos = ptr;
307+
yyposn.trailingLastPos = lex.ptr;
309308

310309
return lex.prev_keyword;
311310
}
@@ -1138,7 +1137,7 @@ int Parser::yylexAux()
11381137
}
11391138

11401139

1141-
void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&, YYPOSN&)
1140+
void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&, YYPOSN& posn)
11421141
{
11431142
/**************************************
11441143
*
@@ -1150,29 +1149,21 @@ void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&
11501149
* Print a syntax error.
11511150
*
11521151
**************************************/
1153-
const TEXT* line_start = lex.line_start;
1154-
SLONG lines = lex.lines;
1155-
if (lex.last_token < line_start)
1156-
{
1157-
line_start = lex.line_start_bk;
1158-
lines--;
1159-
}
1160-
11611152
if (yychar < 1)
11621153
{
11631154
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
11641155
// Unexpected end of command
1165-
Arg::Gds(isc_command_end_err2) << Arg::Num(lines) <<
1166-
Arg::Num(lex.last_token - line_start + 1));
1156+
Arg::Gds(isc_command_end_err2) << Arg::Num(posn.firstLine) <<
1157+
Arg::Num(posn.firstColumn));
11671158
}
11681159
else
11691160
{
11701161
ERRD_post (Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
11711162
// Token unknown - line %d, column %d
1172-
Arg::Gds(isc_dsql_token_unk_err) << Arg::Num(lines) <<
1173-
Arg::Num(lex.last_token - line_start + 1) << // CVC: +1
1163+
Arg::Gds(isc_dsql_token_unk_err) << Arg::Num(posn.firstLine) <<
1164+
Arg::Num(posn.firstColumn) << // CVC: +1
11741165
// Show the token
1175-
Arg::Gds(isc_random) << Arg::Str(string(lex.last_token, lex.ptr - lex.last_token)));
1166+
Arg::Gds(isc_random) << Arg::Str(string(posn.firstPos, posn.lastPos - posn.firstPos)));
11761167
}
11771168
}
11781169

@@ -1186,15 +1177,12 @@ void Parser::yyerror(const TEXT* error_string)
11861177
yyerror_detailed(error_string, -1, errt_value, errt_posn);
11871178
}
11881179

1189-
void Parser::yyerrorIncompleteCmd()
1180+
void Parser::yyerrorIncompleteCmd(const YYPOSN& pos)
11901181
{
1191-
const TEXT* line_start = lex.line_start;
1192-
SLONG lines = lex.lines;
1193-
11941182
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
11951183
// Unexpected end of command
1196-
Arg::Gds(isc_command_end_err2) << Arg::Num(lines) <<
1197-
Arg::Num(lex.ptr - line_start + 1));
1184+
Arg::Gds(isc_command_end_err2) << Arg::Num(pos.lastLine) <<
1185+
Arg::Num(pos.lastColumn + 1));
11981186
}
11991187

12001188
void Parser::check_bound(const char* const to, const char* const string)

src/dsql/Parser.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Parser : public Firebird::PermanentStorage
5151
ULONG lastColumn;
5252
const char* firstPos;
5353
const char* lastPos;
54+
const char* leadingFirstPos;
55+
const char* trailingLastPos;
5456
};
5557

5658
typedef Position YYPOSN;
@@ -81,6 +83,7 @@ class Parser : public Firebird::PermanentStorage
8183

8284
// Actual lexer state begins from here
8385

86+
const TEXT* leadingPtr;
8487
const TEXT* ptr;
8588
const TEXT* end;
8689
const TEXT* last_token;
@@ -188,8 +191,10 @@ class Parser : public Firebird::PermanentStorage
188191
private:
189192
template <typename T> T* setupNode(Node* node)
190193
{
191-
node->line = yyposn.firstLine;
192-
node->column = yyposn.firstColumn;
194+
// Get line/column from YYPOSNARG(1)
195+
const int posnArg = 1;
196+
node->line = ((yyps->psp)[1 - yym + posnArg - 1]).firstLine;
197+
node->column = ((yyps->psp)[1 - yym + posnArg - 1]).firstColumn;
193198
return static_cast<T*>(node);
194199
}
195200

@@ -228,7 +233,7 @@ class Parser : public Firebird::PermanentStorage
228233

229234
void yyerror(const TEXT* error_string);
230235
void yyerror_detailed(const TEXT* error_string, int yychar, YYSTYPE&, YYPOSN&);
231-
void yyerrorIncompleteCmd();
236+
void yyerrorIncompleteCmd(const YYPOSN& pos);
232237

233238
void check_bound(const char* const to, const char* const string);
234239
void check_copy_incr(char*& to, const char ch, const char* const string);
@@ -360,6 +365,7 @@ class Parser : public Firebird::PermanentStorage
360365
Position yyretposn;
361366

362367
int yynerrs;
368+
int yym; // ASF: moved from local variable of Parser::parseAux()
363369

364370
// Current parser state
365371
yyparsestate* yyps;

src/dsql/btyacc_fb.ske

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ void Parser::yyFreeState(Parser::yyparsestate* p)
181181

182182
int Parser::parseAux()
183183
{
184-
int yym, yyn, yystate, yychar, yynewerrflag;
184+
// ASF: yym moved to Parser.h
185+
int /*yym, */yyn, yystate, yychar, yynewerrflag;
185186
yyparsestate *yyerrctx = NULL;
186187
int reduce_posn;
187188

@@ -552,9 +553,6 @@ yyreduce:
552553

553554
reduce_posn = TRUE;
554555

555-
yyposn.firstLine = YYPOSNARG(1).firstLine;
556-
yyposn.firstColumn = YYPOSNARG(1).firstColumn;
557-
558556
switch (yyn) {
559557

560558
%% trailer

src/dsql/parse.y

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ grant0($node)
866866
***/
867867
| ddl_privileges(NOTRIAL(&$node->privileges)) object
868868
TO non_role_grantee_list(NOTRIAL(&$node->users)) grant_option granted_by
869-
{
869+
{
870870
$node->object = $2;
871871
$node->grantAdminOption = $5;
872872
$node->grantor = $6;
@@ -900,11 +900,11 @@ object
900900
{ $$ = newNode<GranteeClause>(obj_functions, get_object_name(obj_functions)); }
901901
| PACKAGE
902902
{ $$ = newNode<GranteeClause>(obj_packages, get_object_name(obj_packages)); }
903-
| GENERATOR
903+
| GENERATOR
904904
{ $$ = newNode<GranteeClause>(obj_generators, get_object_name(obj_generators)); }
905-
| SEQUENCE
905+
| SEQUENCE
906906
{ $$ = newNode<GranteeClause>(obj_generators, get_object_name(obj_generators)); }
907-
| KW_DOMAIN
907+
| KW_DOMAIN
908908
{ $$ = newNode<GranteeClause>(obj_domains, get_object_name(obj_domains)); }
909909
| EXCEPTION
910910
{ $$ = newNode<GranteeClause>(obj_exceptions, get_object_name(obj_exceptions)); }
@@ -917,7 +917,7 @@ object
917917
| FILTER
918918
{ $$ = newNode<GranteeClause>(obj_filters, get_object_name(obj_filters)); }
919919
;
920-
920+
921921
table_noise
922922
: // nothing
923923
| TABLE
@@ -1670,7 +1670,7 @@ alter_sequence_clause
16701670
: symbol_generator_name restart_value_opt2 step_option
16711671
{
16721672
if (!$2.specified && !$3.specified)
1673-
yyerrorIncompleteCmd();
1673+
yyerrorIncompleteCmd(YYPOSNARG(3));
16741674
CreateAlterSequenceNode* node = newNode<CreateAlterSequenceNode>(*$1, $2.value, $3);
16751675
node->create = false;
16761676
node->alter = true;

0 commit comments

Comments
 (0)