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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/* Generated by re2c */
#line 1 "c/state/push.re"
// re2c $INPUT -o $OUTPUT -f
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define DEBUG 0
#define LOG(...) if (DEBUG) fprintf(stderr, __VA_ARGS__);
// Use a small buffer to cover the case when a lexeme doesn't fit.
// In real world use a larger buffer.
#define BUFSIZE 10
typedef struct {
FILE *file;
char buf[BUFSIZE + 1], *lim, *cur, *mar, *tok;
int state;
} State;
typedef enum {END, READY, WAITING, BAD_PACKET, BIG_PACKET} Status;
static Status fill(State *st) {
const size_t shift = st->tok - st->buf;
const size_t used = st->lim - st->tok;
const size_t free = BUFSIZE - used;
// Error: no space. In real life can reallocate a larger buffer.
if (free < 1) return BIG_PACKET;
// Shift buffer contents (discard already processed data).
memmove(st->buf, st->tok, used);
st->lim -= shift;
st->cur -= shift;
st->mar -= shift;
st->tok -= shift;
// Fill free space at the end of buffer with new data.
const size_t read = fread(st->lim, 1, free, st->file);
st->lim += read;
st->lim[0] = 0; // append sentinel symbol
return READY;
}
static Status lex(State *st, unsigned int *recv) {
char yych;
#line 50 "c/state/push.c"
switch (st->state) {
case 0:
if (st->lim <= st->cur) goto yy8;
goto yyFillLabel0;
case 1:
if (st->lim <= st->cur) goto yy3;
goto yyFillLabel1;
case 2:
if (st->lim <= st->cur) goto yy7;
goto yyFillLabel2;
default: goto yy0;
}
#line 46 "c/state/push.re"
for (;;) {
st->tok = st->cur;
#line 69 "c/state/push.c"
yy0:
yyFillLabel0:
yych = *st->cur;
switch (yych) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy4;
default:
if (st->lim <= st->cur) {
st->state = 0;
return WAITING;
}
goto yy2;
}
yy2:
++st->cur;
yy3:
st->state = -1;
#line 63 "c/state/push.re"
{ return BAD_PACKET; }
#line 113 "c/state/push.c"
yy4:
st->mar = ++st->cur;
yyFillLabel1:
yych = *st->cur;
switch (yych) {
case ';': goto yy5;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy6;
default:
if (st->lim <= st->cur) {
st->state = 1;
return WAITING;
}
goto yy3;
}
yy5:
++st->cur;
st->state = -1;
#line 65 "c/state/push.re"
{ *recv = *recv + 1; continue; }
#line 158 "c/state/push.c"
yy6:
++st->cur;
yyFillLabel2:
yych = *st->cur;
switch (yych) {
case ';': goto yy5;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': goto yy6;
default:
if (st->lim <= st->cur) {
st->state = 2;
return WAITING;
}
goto yy7;
}
yy7:
st->cur = st->mar;
goto yy3;
yy8:
st->state = -1;
#line 64 "c/state/push.re"
{ return END; }
#line 205 "c/state/push.c"
#line 66 "c/state/push.re"
}
}
void test(const char **packets, Status expect) {
// Create a "socket" (open the same file for reading and writing).
const char *fname = "pipe";
FILE *fw = fopen(fname, "w");
FILE *fr = fopen(fname, "r");
setvbuf(fw, NULL, _IONBF, 0);
setvbuf(fr, NULL, _IONBF, 0);
// Initialize lexer state: `state` value is -1, all pointers are at the end
// of buffer.
State st;
st.file = fr;
st.cur = st.mar = st.tok = st.lim = st.buf + BUFSIZE;
// Sentinel (at YYLIMIT pointer) is set to zero, which triggers YYFILL.
st.lim[0] = 0;
st.state = -1;
// Main loop. The buffer contains incomplete data which appears packet by
// packet. When the lexer needs more input it saves its internal state and
// returns to the caller which should provide more input and resume lexing.
Status status;
unsigned int send = 0, recv = 0;
for (;;) {
status = lex(&st, &recv);
if (status == END) {
LOG("done: got %u packets\n", recv);
break;
} else if (status == WAITING) {
LOG("waiting...\n");
if (*packets) {
LOG("sent packet %u\n", send);
fprintf(fw, "%s", *packets++);
++send;
}
status = fill(&st);
LOG("queue: '%s'\n", st.buf);
if (status == BIG_PACKET) {
LOG("error: packet too big\n");
break;
}
assert(status == READY);
} else {
assert(status == BAD_PACKET);
LOG("error: ill-formed packet\n");
break;
}
}
// Check results.
assert(status == expect);
if (status == END) assert(recv == send);
// Cleanup: remove input file.
fclose(fw);
fclose(fr);
remove(fname);
}
int main() {
const char *packets1[] = {0};
const char *packets2[] = {"zero;", "one;", "two;", "three;", "four;", 0};
const char *packets3[] = {"zer0;", 0};
const char *packets4[] = {"looooooooooong;", 0};
test(packets1, END);
test(packets2, END);
test(packets3, BAD_PACKET);
test(packets4, BIG_PACKET);
return 0;
}
|