Skip to content

Commit 820fd7e

Browse files
committed
Frontported fix for CORE-6466 - Comments before the first line of code are removed.
1 parent 8aa4958 commit 820fd7e

4 files changed

Lines changed: 33 additions & 41 deletions

File tree

src/dsql/Parser.cpp

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Parser::Parser(thread_db* tdbb, MemoryPool& pool, DsqlCompilerScratch* aScratch,
123123
yylexemes = 0;
124124

125125
lex.start = string;
126-
lex.line_start = lex.last_token = lex.ptr = string;
126+
lex.line_start = lex.last_token = lex.ptr = lex.leadingPtr = string;
127127
lex.end = string + length;
128128
lex.lines = 1;
129129
lex.att_charset = characterSet;
@@ -242,11 +242,12 @@ void Parser::transformString(const char* start, unsigned length, string& dest)
242242
// Make a substring from the command text being parsed.
243243
string Parser::makeParseStr(const Position& p1, const Position& p2)
244244
{
245-
const char* start = p1.firstPos;
246-
const char* end = p2.lastPos;
245+
const char* start = p1.leadingFirstPos;
246+
const char* end = p2.trailingLastPos;
247247

248248
string str;
249249
transformString(start, end - start, str);
250+
str.trim(" \t\r\n");
250251

251252
string ret;
252253

@@ -278,16 +279,19 @@ void Parser::yyReducePosn(YYPOSN& ret, YYPOSN* termPosns, YYSTYPE* /*termVals*/,
278279
// Accessing termPosns[-1] seems to be the only way to get correct positions in this case.
279280
ret.firstLine = ret.lastLine = termPosns[termNo - 1].lastLine;
280281
ret.firstColumn = ret.lastColumn = termPosns[termNo - 1].lastColumn;
281-
ret.firstPos = ret.lastPos = termPosns[termNo - 1].lastPos;
282+
ret.firstPos = ret.lastPos = ret.trailingLastPos = termPosns[termNo - 1].trailingLastPos;
283+
ret.leadingFirstPos = termPosns[termNo - 1].lastPos;
282284
}
283285
else
284286
{
285287
ret.firstLine = termPosns[0].firstLine;
286288
ret.firstColumn = termPosns[0].firstColumn;
287289
ret.firstPos = termPosns[0].firstPos;
290+
ret.leadingFirstPos = termPosns[0].leadingFirstPos;
288291
ret.lastLine = termPosns[termNo - 1].lastLine;
289292
ret.lastColumn = termPosns[termNo - 1].lastColumn;
290293
ret.lastPos = termPosns[termNo - 1].lastPos;
294+
ret.trailingLastPos = termPosns[termNo - 1].trailingLastPos;
291295
}
292296

293297
/*** This allows us to see colored output representing the position reductions.
@@ -308,29 +312,24 @@ int Parser::yylex()
308312
yyposn.firstLine = lex.lines;
309313
yyposn.firstColumn = lex.ptr - lex.line_start;
310314
yyposn.firstPos = lex.ptr - 1;
315+
yyposn.leadingFirstPos = lex.leadingPtr;
311316

312317
lex.prev_keyword = yylexAux();
313318

314-
const TEXT* ptr = lex.ptr;
315-
const TEXT* last_token = lex.last_token;
316-
const TEXT* line_start = lex.line_start;
317-
const SLONG lines = lex.lines;
319+
yyposn.lastPos = lex.ptr;
320+
lex.leadingPtr = lex.ptr;
318321

319322
// Lets skip spaces before store lastLine/lastColumn. This is necessary to avoid yyReducePosn
320323
// produce invalid line/column information - CORE-4381.
321-
yylexSkipSpaces();
324+
bool spacesSkipped = yylexSkipSpaces();
322325

323326
yyposn.lastLine = lex.lines;
324327
yyposn.lastColumn = lex.ptr - lex.line_start;
325328

326-
lex.ptr = ptr;
327-
lex.last_token = last_token;
328-
lex.line_start = line_start;
329-
lex.lines = lines;
329+
if (spacesSkipped)
330+
--lex.ptr;
330331

331-
// But the correct value for lastPos is the old (before the second yyLexSkipSpaces)
332-
// value of lex.ptr.
333-
yyposn.lastPos = ptr;
332+
yyposn.trailingLastPos = lex.ptr;
334333

335334
return lex.prev_keyword;
336335
}
@@ -1269,7 +1268,7 @@ int Parser::yylexAux()
12691268
}
12701269

12711270

1272-
void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&, YYPOSN&)
1271+
void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&, YYPOSN& posn)
12731272
{
12741273
/**************************************
12751274
*
@@ -1281,29 +1280,21 @@ void Parser::yyerror_detailed(const TEXT* /*error_string*/, int yychar, YYSTYPE&
12811280
* Print a syntax error.
12821281
*
12831282
**************************************/
1284-
const TEXT* line_start = lex.line_start;
1285-
SLONG lines = lex.lines;
1286-
if (lex.last_token < line_start)
1287-
{
1288-
line_start = lex.line_start_bk;
1289-
lines--;
1290-
}
1291-
12921283
if (yychar < 1)
12931284
{
12941285
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
12951286
// Unexpected end of command
1296-
Arg::Gds(isc_command_end_err2) << Arg::Num(lines) <<
1297-
Arg::Num(lex.last_token - line_start + 1));
1287+
Arg::Gds(isc_command_end_err2) << Arg::Num(posn.firstLine) <<
1288+
Arg::Num(posn.firstColumn));
12981289
}
12991290
else
13001291
{
13011292
ERRD_post (Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
13021293
// Token unknown - line %d, column %d
1303-
Arg::Gds(isc_dsql_token_unk_err) << Arg::Num(lines) <<
1304-
Arg::Num(lex.last_token - line_start + 1) << // CVC: +1
1294+
Arg::Gds(isc_dsql_token_unk_err) << Arg::Num(posn.firstLine) <<
1295+
Arg::Num(posn.firstColumn) <<
13051296
// Show the token
1306-
Arg::Gds(isc_random) << Arg::Str(string(lex.last_token, lex.ptr - lex.last_token)));
1297+
Arg::Gds(isc_random) << Arg::Str(string(posn.firstPos, posn.lastPos - posn.firstPos)));
13071298
}
13081299
}
13091300

@@ -1317,15 +1308,12 @@ void Parser::yyerror(const TEXT* error_string)
13171308
yyerror_detailed(error_string, -1, errt_value, errt_posn);
13181309
}
13191310

1320-
void Parser::yyerrorIncompleteCmd()
1311+
void Parser::yyerrorIncompleteCmd(const YYPOSN& pos)
13211312
{
1322-
const TEXT* line_start = lex.line_start;
1323-
SLONG lines = lex.lines;
1324-
13251313
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
13261314
// Unexpected end of command
1327-
Arg::Gds(isc_command_end_err2) << Arg::Num(lines) <<
1328-
Arg::Num(lex.ptr - line_start + 1));
1315+
Arg::Gds(isc_command_end_err2) << Arg::Num(pos.lastLine) <<
1316+
Arg::Num(pos.lastColumn + 1));
13291317
}
13301318

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

src/dsql/Parser.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Parser : public Firebird::PermanentStorage
5959
ULONG lastColumn;
6060
const char* firstPos;
6161
const char* lastPos;
62+
const char* leadingFirstPos;
63+
const char* trailingLastPos;
6264
};
6365

6466
typedef Position YYPOSN;
@@ -89,6 +91,7 @@ class Parser : public Firebird::PermanentStorage
8991

9092
// Actual lexer state begins from here
9193

94+
const TEXT* leadingPtr;
9295
const TEXT* ptr;
9396
const TEXT* end;
9497
const TEXT* last_token;
@@ -245,7 +248,7 @@ class Parser : public Firebird::PermanentStorage
245248

246249
void yyerror(const TEXT* error_string);
247250
void yyerror_detailed(const TEXT* error_string, int yychar, YYSTYPE&, YYPOSN&);
248-
void yyerrorIncompleteCmd();
251+
void yyerrorIncompleteCmd(const YYPOSN& pos);
249252

250253
void check_bound(const char* const to, const char* const string);
251254
void check_copy_incr(char*& to, const char ch, const char* const string);
@@ -393,7 +396,7 @@ class Parser : public Firebird::PermanentStorage
393396
Position yyretposn;
394397

395398
int yynerrs;
396-
int yym;
399+
int yym; // ASF: moved from local variable of Parser::parseAux()
397400

398401
// Current parser state
399402
yyparsestate* yyps;

src/dsql/btyacc_fb.ske

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ void Parser::setNodeLineColumn(Node* node)
189189

190190
int Parser::parseAux()
191191
{
192-
int yyn, yystate, yychar;
192+
// ASF: yym moved to Parser.h
193+
int /*yym, */yyn, yystate, yychar, yynewerrflag;
193194
yyparsestate *yyerrctx = NULL;
194195
int reduce_posn;
195196

src/dsql/parse.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ replace_sequence_clause
17991799
{
18001800
// Remove this to implement CORE-5137
18011801
if (!$2->restartSpecified && !$2->step.specified)
1802-
yyerrorIncompleteCmd();
1802+
yyerrorIncompleteCmd(YYPOSNARG(3));
18031803
$$ = $2;
18041804
}
18051805
;
@@ -1832,7 +1832,7 @@ alter_sequence_clause
18321832
alter_sequence_options($2)
18331833
{
18341834
if (!$2->restartSpecified && !$2->value.specified && !$2->step.specified)
1835-
yyerrorIncompleteCmd();
1835+
yyerrorIncompleteCmd(YYPOSNARG(3));
18361836
$$ = $2;
18371837
}
18381838

0 commit comments

Comments
 (0)