Skip to content

Commit 4f78065

Browse files
authored
Merge pull request #3495 from masatake/fix-broken-promise-filter
main,Fypp: stop applying the line filter if there is no more input
2 parents 132379f + 092da29 commit 4f78065

File tree

9 files changed

+49
-21
lines changed

9 files changed

+49
-21
lines changed

Units/parser-fypp.r/run-alt-guest.d/expected.tags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ F input.fy /^ F(1), \/\/ should be 'e', not missing$/;" e enum:e file: end:22
77
debug_code input.fy /^#:def debug_code(code)$/;" m end:8
88
e input.fy /^ e() {}$/;" m class:e end:32
99
e input.fy /^ e(int x) {}$/;" m class:e end:31
10-
e input.fy /^public enum e {$/;" g
10+
e input.fy /^public enum e {$/;" g end:51
1111
getShape input.fy /^ public final Shape getShape() {$/;" m class:e end:46
1212
getString input.fy /^ public String getString() {$/;" m class:e end:41
1313
repeat_code input.fy /^#:def repeat_code(code, repeat)$/;" m end:29

Units/parser-fypp.r/segv.d/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a crash test.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--extras=+g
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
#:

docs/internal.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,15 @@ Available parser names can be listed by running ctags with
494494
the 3rd and 5th argument. They are byte offsets of the start and the end of the
495495
C language area from the beginning of the line which is 0 in this case. In
496496
general, the guest language's section does not have to start at the beginning of
497-
the line in which case the two offsets have to be provided. Compilers reading
497+
the line in which case the two offsets have to be provided. Parsers reading
498498
the input character by character can obtain the current offset by calling
499499
``getInputLineOffset()``.
500500

501+
In some cases, you may want to specifying the offset of the end of
502+
line (EOL). A macro ``EOL_CHAR_OFFSET`` defined in ``main/promise.h``
503+
can be used for specying EOL in abstracted way; you don't have to find
504+
the real offset for the EOL.
505+
501506
Internal design
502507
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
503508

main/promise.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int makePromise (const char *parser,
7171
int r;
7272
langType lang = LANG_IGNORE;
7373

74-
verbose("makePromise: %s start(line: %lu, offset: %lu, srcline: %lu), end(line: %lu, offset: %lu)\n",
74+
verbose("makePromise: %s start(line: %lu, offset: %ld, srcline: %lu), end(line: %lu, offset: %ld)\n",
7575
parser? parser: "*", startLine, startCharOffset, sourceLineOffset,
7676
endLine, endCharOffset);
7777

@@ -197,7 +197,7 @@ int getLastPromise (void)
197197
return promise_count - 1;
198198
}
199199

200-
static unsigned char* fill_or_skip (unsigned char *input, unsigned char *input_end, bool filling)
200+
static unsigned char* fill_or_skip (unsigned char *input, const unsigned char *const input_end, const bool filling)
201201
{
202202
if ( !(input < input_end))
203203
return NULL;
@@ -221,53 +221,59 @@ static unsigned char* fill_or_skip (unsigned char *input, unsigned char *input_e
221221
}
222222
}
223223

224-
static void line_filler (unsigned char *input, size_t size,
225-
unsigned long startLine, long startCharOffset,
226-
unsigned long endLine, long endCharOffset,
224+
static void line_filler (unsigned char *input, size_t const size,
225+
unsigned long const startLine, long const startCharOffset,
226+
unsigned long const endLine, long const endCharOffset,
227227
void *data)
228228
{
229-
ulongArray *lines = data;
229+
const ulongArray *lines = data;
230+
const size_t count = ulongArrayCount (lines);
230231
unsigned int start_index, end_index;
231232
unsigned int i;
232233

233-
for (i = 0; i < ulongArrayCount (lines); i++)
234+
for (i = 0; i < count; i++)
234235
{
235-
unsigned long line = ulongArrayItem (lines, i);
236+
const unsigned long line = ulongArrayItem (lines, i);
236237
if (line >= startLine)
238+
{
239+
if (line > endLine)
240+
return; /* Not over-wrapping */
237241
break;
242+
}
238243
}
239-
if (i == ulongArrayCount (lines))
240-
return;
241-
if (i > endLine)
242-
return;
244+
if (i == count)
245+
return; /* Not over-wrapping */
243246
start_index = i;
244247

245-
for (; i < ulongArrayCount (lines); i++)
248+
for (; i < count; i++)
246249
{
247-
unsigned long line = ulongArrayItem (lines, i);
250+
const unsigned long line = ulongArrayItem (lines, i);
248251
if (line > endLine)
249252
break;
250253
}
251254
end_index = i;
252255

253256
unsigned long input_line = startLine;
257+
const unsigned char *const input_end = input + size;
254258
for (i = start_index; i < end_index; i++)
255259
{
256-
unsigned long line = ulongArrayItem (lines, i);
260+
const unsigned long line = ulongArrayItem (lines, i);
257261

258262
while (1)
259263
{
260264
if (input_line == line)
261265
{
262-
input = fill_or_skip (input, input + size, true);
266+
input = fill_or_skip (input, input_end, true);
263267
input_line++;
264268
break;
265269
}
266270
else
267271
{
268-
input = fill_or_skip (input, input + size, false);
272+
input = fill_or_skip (input, input_end, false);
269273
input_line++;
270274
}
275+
if (input == NULL)
276+
return;
271277
}
272278
}
273279
}

main/promise.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "parse.h"
1818
#include "numarray.h"
1919

20+
#define EOL_CHAR_OFFSET -1
21+
2022
/* parser can be NULL; give a name with promiseUpdateLanguage()
2123
* when the name can be determined. */
2224
int makePromise (const char *parser,

main/read.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "routines_p.h"
2727
#include "options_p.h"
2828
#include "parse_p.h"
29+
#include "promise.h"
2930
#include "promise_p.h"
3031
#include "stats_p.h"
3132
#include "trace.h"
@@ -1133,7 +1134,17 @@ extern void pushNarrowedInputStream (
11331134

11341135
tmp = getInputFilePositionForLine (endLine);
11351136
mio_setpos (File.mio, &tmp);
1136-
mio_seek (File.mio, endCharOffset, SEEK_CUR);
1137+
if (endCharOffset == EOL_CHAR_OFFSET)
1138+
{
1139+
long line_start = mio_tell (File.mio);
1140+
vString *tmpstr = vStringNew ();
1141+
readLine (tmpstr, File.mio);
1142+
endCharOffset = mio_tell (File.mio) - line_start;
1143+
vStringDelete (tmpstr);
1144+
Assert (endCharOffset >= 0);
1145+
}
1146+
else
1147+
mio_seek (File.mio, endCharOffset, SEEK_CUR);
11371148
q = mio_tell (File.mio);
11381149

11391150
mio_setpos (File.mio, &original);

parsers/fypp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static void findFyppTags (void)
373373
{
374374
promise = makePromise (vStringValue(fyppGuestParser),
375375
1, 0,
376-
getInputLineNumber(), 0,
376+
getInputLineNumber(), EOL_CHAR_OFFSET,
377377
0);
378378
if (promise >= 0)
379379
promiseAttachLineFiller (promise, parseCtx.fypp_lines);

0 commit comments

Comments
 (0)