Skip to content

Commit

Permalink
Avoid new features added in 4.0 in C/C++ examples.
Browse files Browse the repository at this point in the history
Keep examples compatible with older re2c versions until most of the world has
updated to re2c-4.0 or later. When it happens, this commit should be reverted.
  • Loading branch information
skvadrik committed Nov 22, 2024
1 parent b4dc9f6 commit 5cea17c
Show file tree
Hide file tree
Showing 41 changed files with 447 additions and 512 deletions.
204 changes: 102 additions & 102 deletions bootstrap/doc/re2c.1

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/c/01_basic.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bool lex(const char *s) {
const char *YYCURSOR = s;
/*!re2c
re2c:yyfill:enable = 0;
re2c:YYCTYPE = char;
re2c:define:YYCTYPE = char;
[1-9][0-9]* { return true; }
* { return false; }
Expand Down
2 changes: 1 addition & 1 deletion examples/c/conditions/parse_u32_blocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static uint64_t parse_u32(const char *s) {

/*!re2c
re2c:yyfill:enable = 0;
re2c:YYCTYPE = char;
re2c:define:YYCTYPE = char;
end = "\x00";
Expand Down
6 changes: 3 additions & 3 deletions examples/c/conditions/parse_u32_conditions.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ static uint64_t parse_u32(const char *s) {

/*!re2c
re2c:api:style = free-form;
re2c:YYCTYPE = char;
re2c:YYGETCOND = "c";
re2c:YYSETCOND = "c = @@;";
re2c:define:YYCTYPE = char;
re2c:define:YYGETCONDITION = "c";
re2c:define:YYSETCONDITION = "c = @@;";
re2c:yyfill:enable = 0;
<*> * { return ERROR; }
Expand Down
2 changes: 1 addition & 1 deletion examples/c/encodings/unicode_identifier.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
static int lex(const char *s) {
const char *YYCURSOR = s, *YYMARKER;
/*!re2c
re2c:YYCTYPE = 'unsigned char';
re2c:define:YYCTYPE = 'unsigned char';
re2c:yyfill:enable = 0;
// Simplified "Unicode Identifier and Pattern Syntax"
Expand Down
2 changes: 1 addition & 1 deletion examples/c/eof/01_sentinel.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static int lex(const char *YYCURSOR) {

for (;;) {
/*!re2c
re2c:YYCTYPE = char;
re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0;
* { return -1; }
Expand Down
4 changes: 2 additions & 2 deletions examples/c/eof/02_bounds_checking.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static int lex(const char *str, unsigned int len) {
loop:
/*!re2c
re2c:api:style = free-form;
re2c:YYCTYPE = char;
re2c:YYFILL = "goto fail;";
re2c:define:YYCTYPE = char;
re2c:define:YYFILL = "goto fail;";
str = ['] ([^'\\] | [\\][^])* ['];
Expand Down
2 changes: 1 addition & 1 deletion examples/c/eof/03_eof_rule.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static int lex(const char *str, unsigned int len) {

for (;;) {
/*!re2c
re2c:YYCTYPE = char;
re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0;
re2c:eof = 0;
Expand Down
8 changes: 4 additions & 4 deletions examples/c/eof/04_fake_sentinel.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ static int lex(const char *str, unsigned int len) {
for (;;) {
/*!re2c
re2c:yyfill:enable = 0;
re2c:api = generic;
re2c:api = custom;
re2c:api:style = free-form;
re2c:YYCTYPE = char;
re2c:YYPEEK = "cur < lim ? *cur : 0"; // fake null
re2c:YYSKIP = "++cur;";
re2c:define:YYCTYPE = char;
re2c:define:YYPEEK = "cur < lim ? *cur : 0"; // fake null
re2c:define:YYSKIP = "++cur;";
* { count = -1; break; }
[\x00] { break;; }
Expand Down
14 changes: 7 additions & 7 deletions examples/c/eof/05_fake_sentinel_eof_rule.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ static int lex(const char *str, unsigned int len) {
/*!re2c
re2c:yyfill:enable = 0;
re2c:eof = 0;
re2c:api = generic;
re2c:api = custom;
re2c:api:style = free-form;
re2c:YYCTYPE = char;
re2c:YYLESSTHAN = "cur >= lim";
re2c:YYPEEK = "cur < lim ? *cur : 0"; // fake null
re2c:YYSKIP = "++cur;";
re2c:YYBACKUP = "mar = cur;";
re2c:YYRESTORE = "cur = mar;";
re2c:define:YYCTYPE = char;
re2c:define:YYLESSTHAN = "cur >= lim";
re2c:define:YYPEEK = "cur < lim ? *cur : 0"; // fake null
re2c:define:YYSKIP = "++cur;";
re2c:define:YYBACKUP = "mar = cur;";
re2c:define:YYRESTORE = "cur = mar;";
str = ['] ([^'\\] | [\\][^])* ['];
Expand Down
101 changes: 48 additions & 53 deletions examples/c/fill/01_fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,130 +9,125 @@

struct Input {
FILE *file;
char buffer[BUFSIZE + 1]; // +1 for sentinel
char *yylimit;
char *yycursor;
char *yymarker;
char *token;
char buf[BUFSIZE + 1], *lim, *cur, *mar, *tok; // +1 for sentinel
bool eof;
};

static int fill(Input &in) {
if (in.eof) return 1;

const size_t shift = in.token - in.buffer;
const size_t used = in.yylimit - in.token;
const size_t shift = in.tok - in.buf;
const size_t used = in.lim - in.tok;

// Error: lexeme too long. In real life could reallocate a larger buffer.
if (shift < 1) return 2;

// Shift buffer contents (discard everything up to the current token).
memmove(in.buffer, in.token, used);
in.yylimit -= shift;
in.yycursor -= shift;
in.yymarker -= shift;
in.token -= shift;
memmove(in.buf, in.tok, used);
in.lim -= shift;
in.cur -= shift;
in.mar -= shift;
in.tok -= shift;

// Fill free space at the end of buffer with new data from file.
in.yylimit += fread(in.yylimit, 1, BUFSIZE - used, in.file);
in.yylimit[0] = 0;
in.eof = in.yylimit < in.buffer + BUFSIZE;
in.lim += fread(in.lim, 1, BUFSIZE - used, in.file);
in.lim[0] = 0;
in.eof = in.lim < in.buf + BUFSIZE;
return 0;
}

static int lex(Input *yyrecord) {
static int lex(Input &in) {
int count = 0;
loop:
yyrecord->token = yyrecord->yycursor;
in.tok = in.cur;

#line 49 "c/fill/01_fill.c"
#line 45 "c/fill/01_fill.c"
{
char yych;
yyFillLabel0:
yych = *yyrecord->yycursor;
yych = *in.cur;
switch (yych) {
case ' ': goto yy3;
case '\'': goto yy5;
default:
if (yyrecord->yylimit <= yyrecord->yycursor) {
if (fill(*yyrecord) == 0) goto yyFillLabel0;
if (in.lim <= in.cur) {
if (fill(in) == 0) goto yyFillLabel0;
goto yy10;
}
goto yy1;
}
yy1:
++yyrecord->yycursor;
++in.cur;
yy2:
#line 53 "c/fill/01_fill.re"
#line 52 "c/fill/01_fill.re"
{ return -1; }
#line 69 "c/fill/01_fill.c"
#line 65 "c/fill/01_fill.c"
yy3:
++yyrecord->yycursor;
++in.cur;
yyFillLabel1:
yych = *yyrecord->yycursor;
yych = *in.cur;
switch (yych) {
case ' ': goto yy3;
default:
if (yyrecord->yylimit <= yyrecord->yycursor) {
if (fill(*yyrecord) == 0) goto yyFillLabel1;
if (in.lim <= in.cur) {
if (fill(in) == 0) goto yyFillLabel1;
}
goto yy4;
}
yy4:
#line 56 "c/fill/01_fill.re"
#line 55 "c/fill/01_fill.re"
{ goto loop; }
#line 85 "c/fill/01_fill.c"
#line 81 "c/fill/01_fill.c"
yy5:
++yyrecord->yycursor;
yyrecord->yymarker = yyrecord->yycursor;
in.mar = ++in.cur;
yyFillLabel2:
yych = *yyrecord->yycursor;
yych = *in.cur;
if (yych >= 0x01) goto yy7;
if (yyrecord->yylimit <= yyrecord->yycursor) {
if (fill(*yyrecord) == 0) goto yyFillLabel2;
if (in.lim <= in.cur) {
if (fill(in) == 0) goto yyFillLabel2;
goto yy2;
}
yy6:
++yyrecord->yycursor;
++in.cur;
yyFillLabel3:
yych = *yyrecord->yycursor;
yych = *in.cur;
yy7:
switch (yych) {
case '\'': goto yy8;
case '\\': goto yy9;
default:
if (yyrecord->yylimit <= yyrecord->yycursor) {
if (fill(*yyrecord) == 0) goto yyFillLabel3;
if (in.lim <= in.cur) {
if (fill(in) == 0) goto yyFillLabel3;
goto yy11;
}
goto yy6;
}
yy8:
++yyrecord->yycursor;
#line 55 "c/fill/01_fill.re"
++in.cur;
#line 54 "c/fill/01_fill.re"
{ ++count; goto loop; }
#line 115 "c/fill/01_fill.c"
#line 110 "c/fill/01_fill.c"
yy9:
++yyrecord->yycursor;
++in.cur;
yyFillLabel4:
yych = *yyrecord->yycursor;
yych = *in.cur;
if (yych <= 0x00) {
if (yyrecord->yylimit <= yyrecord->yycursor) {
if (fill(*yyrecord) == 0) goto yyFillLabel4;
if (in.lim <= in.cur) {
if (fill(in) == 0) goto yyFillLabel4;
goto yy11;
}
goto yy6;
}
goto yy6;
yy10:
#line 54 "c/fill/01_fill.re"
#line 53 "c/fill/01_fill.re"
{ return count; }
#line 131 "c/fill/01_fill.c"
#line 126 "c/fill/01_fill.c"
yy11:
yyrecord->yycursor = yyrecord->yymarker;
in.cur = in.mar;
goto yy2;
}
#line 57 "c/fill/01_fill.re"
#line 56 "c/fill/01_fill.re"

}

Expand All @@ -152,13 +147,13 @@ int main() {
// Initialize lexer state: all pointers are at the end of buffer.
Input in;
in.file = fopen(fname, "r");
in.yycursor = in.yymarker = in.token = in.yylimit = in.buffer + BUFSIZE;
in.cur = in.mar = in.tok = in.lim = in.buf + BUFSIZE;
in.eof = 0;
// Sentinel (at YYLIMIT pointer) is set to zero, which triggers YYFILL.
in.yylimit[0] = 0;
in.lim[0] = 0;

// Run the lexer.
assert(lex(&in) == count);
assert(lex(in) == count);

// Cleanup: remove input file.
fclose(in.file);
Expand Down
45 changes: 22 additions & 23 deletions examples/c/fill/01_fill.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,44 @@

struct Input {
FILE *file;
char buffer[BUFSIZE + 1]; // +1 for sentinel
char *yylimit;
char *yycursor;
char *yymarker;
char *token;
char buf[BUFSIZE + 1], *lim, *cur, *mar, *tok; // +1 for sentinel
bool eof;
};

static int fill(Input &in) {
if (in.eof) return 1;

const size_t shift = in.token - in.buffer;
const size_t used = in.yylimit - in.token;
const size_t shift = in.tok - in.buf;
const size_t used = in.lim - in.tok;

// Error: lexeme too long. In real life could reallocate a larger buffer.
if (shift < 1) return 2;

// Shift buffer contents (discard everything up to the current token).
memmove(in.buffer, in.token, used);
in.yylimit -= shift;
in.yycursor -= shift;
in.yymarker -= shift;
in.token -= shift;
memmove(in.buf, in.tok, used);
in.lim -= shift;
in.cur -= shift;
in.mar -= shift;
in.tok -= shift;

// Fill free space at the end of buffer with new data from file.
in.yylimit += fread(in.yylimit, 1, BUFSIZE - used, in.file);
in.yylimit[0] = 0;
in.eof = in.yylimit < in.buffer + BUFSIZE;
in.lim += fread(in.lim, 1, BUFSIZE - used, in.file);
in.lim[0] = 0;
in.eof = in.lim < in.buf + BUFSIZE;
return 0;
}

static int lex(Input *yyrecord) {
static int lex(Input &in) {
int count = 0;
loop:
yyrecord->token = yyrecord->yycursor;
in.tok = in.cur;
/*!re2c
re2c:api = record;
re2c:YYCTYPE = char;
re2c:YYFILL = "fill(*yyrecord) == 0";
re2c:api:style = free-form;
re2c:define:YYCTYPE = char;
re2c:define:YYCURSOR = in.cur;
re2c:define:YYMARKER = in.mar;
re2c:define:YYLIMIT = in.lim;
re2c:define:YYFILL = "fill(in) == 0";
re2c:eof = 0;
str = ['] ([^'\\] | [\\][^])* ['];
Expand Down Expand Up @@ -73,13 +72,13 @@ int main() {
// Initialize lexer state: all pointers are at the end of buffer.
Input in;
in.file = fopen(fname, "r");
in.yycursor = in.yymarker = in.token = in.yylimit = in.buffer + BUFSIZE;
in.cur = in.mar = in.tok = in.lim = in.buf + BUFSIZE;
in.eof = 0;
// Sentinel (at YYLIMIT pointer) is set to zero, which triggers YYFILL.
in.yylimit[0] = 0;
in.lim[0] = 0;

// Run the lexer.
assert(lex(&in) == count);
assert(lex(in) == count);

// Cleanup: remove input file.
fclose(in.file);
Expand Down
Loading

0 comments on commit 5cea17c

Please sign in to comment.