1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
/* Generated by re2c */
// re2c $INPUT -o $OUTPUT --recursive-functions -i
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define YYMAXFILL 1
struct State {
const char* cur;
const char* lim;
int count;
};
int yy0(State& st);
int yy1(State& st);
int yy2(State& st);
int yy3(State& st);
int yy4(State& st);
int yy5(State& st);
int yy6(State& st);
int yy7(State& st);
int lex(State& st);
int yy0(State& st) {
if (st.lim <= st.cur) return -1;
char yych = *st.cur++;
switch (yych) {
case 0x00: return yy1(st);
case ' ': return yy3(st);
case '\'': return yy5(st);
default: return yy2(st);
}
}
int yy1(State& st) {
return st.cur + YYMAXFILL - 1 == st.lim ? st.count : -1;
}
int yy2(State& st) {
return -1;
}
int yy3(State& st) {
if (st.lim <= st.cur) return -1;
char yych = *st.cur;
switch (yych) {
case ' ':
++st.cur;
return yy3(st);
default: return yy4(st);
}
}
int yy4(State& st) {
return lex(st);
}
int yy5(State& st) {
if (st.lim <= st.cur) return -1;
char yych = *st.cur++;
switch (yych) {
case '\'': return yy6(st);
case '\\': return yy7(st);
default: return yy5(st);
}
}
int yy6(State& st) {
++st.count; return lex(st);
}
int yy7(State& st) {
if (st.lim <= st.cur) return -1;
++st.cur;
return yy5(st);
}
int lex(State& st) {
return yy0(st);
}
// make a copy of the string with YYMAXFILL zeroes at the end
static void test(const char *str, unsigned int len, int res) {
char *s = (char*) malloc(len + YYMAXFILL);
memcpy(s, str, len);
memset(s + len, 0, YYMAXFILL);
State st{s, s + len + YYMAXFILL, 0};
int r = lex(st);
free(s);
assert(r == res);
}
#define TEST(s, r) test(s, sizeof(s) - 1, r)
int main() {
TEST("", 0);
TEST("'qu\0tes' 'are' 'fine: \\'' ", 3);
TEST("'unterminated\\'", -1);
return 0;
}
|