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
|
#include <stdlib.h>
#include <string.h>
#include "tools/re2c/scanner.h"
#include "tools/re2c/parse.h"
#include "tools/re2c/globals.h"
#include "tools/re2c/parser.h"
#ifndef MAX
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif
#define BSIZE 8192
#define YYCTYPE unsigned char
#define YYCURSOR cursor
#define YYLIMIT s->lim
#define YYMARKER s->ptr
#define YYFILL(n) {cursor = fill(s, cursor);}
#define RETURN(i) {s->cur = cursor; return i;}
static unsigned char *fill(Scanner*, unsigned char*);
void
Scanner_init(Scanner *s, FILE *i)
{
s->in = i;
s->bot = s->tok = s->ptr = s->cur = s->pos = s->lim = s->top =
s->eof = NULL;
s->tchar = s->tline = 0;
s->cline = 1;
}
static unsigned char *
fill(Scanner *s, unsigned char *cursor)
{
if(!s->eof){
unsigned int cnt = s->tok - s->bot;
if(cnt){
memcpy(s->bot, s->tok, s->lim - s->tok);
s->tok = s->bot;
s->ptr -= cnt;
cursor -= cnt;
s->pos -= cnt;
s->lim -= cnt;
}
if((s->top - s->lim) < BSIZE){
unsigned char *buf = malloc(((s->lim - s->bot) + BSIZE) + 1);
memcpy(buf, s->tok, s->lim - s->tok);
s->tok = buf;
s->ptr = &buf[s->ptr - s->bot];
cursor = &buf[cursor - s->bot];
s->pos = &buf[s->pos - s->bot];
s->lim = &buf[s->lim - s->bot];
s->top = &s->lim[BSIZE];
if (s->bot)
free(s->bot);
s->bot = buf;
}
if((cnt = fread(s->lim, 1, BSIZE, s->in)) != BSIZE){
s->eof = &s->lim[cnt]; *s->eof++ = '\0';
}
s->lim += cnt;
}
return cursor;
}
/*!re2c
zero = "\000";
any = [\000-\377];
dot = any \ [\n];
esc = dot \ [\\];
istring = "[" "^" ((esc \ [\]]) | "\\" dot)* "]" ;
cstring = "[" ((esc \ [\]]) | "\\" dot)* "]" ;
dstring = "\"" ((esc \ ["] ) | "\\" dot)* "\"";
sstring = "'" ((esc \ ['] ) | "\\" dot)* "'" ;
letter = [a-zA-Z];
digit = [0-9];
*/
int
Scanner_echo(Scanner *s, FILE *out)
{
unsigned char *cursor = s->cur;
int ignore_eoc = 0;
/* Catch EOF */
if (s->eof && cursor == s->eof)
return 0;
s->tok = cursor;
echo:
/*!re2c
"/*!re2c" { fwrite(s->tok, 1, &cursor[-7] - s->tok, out);
s->tok = cursor;
RETURN(1); }
"/*!max:re2c" {
fprintf(out, "#define YYMAXFILL %u\n", maxFill);
s->tok = s->pos = cursor;
ignore_eoc = 1;
goto echo;
}
"*" "/" {
if (ignore_eoc) {
ignore_eoc = 0;
} else {
fwrite(s->tok, 1, cursor - s->tok, out);
}
s->tok = s->pos = cursor;
goto echo;
}
"\n" { fwrite(s->tok, 1, cursor - s->tok, out);
s->tok = s->pos = cursor; s->cline++; oline++;
goto echo; }
zero { fwrite(s->tok, 1, cursor - s->tok - 1, out); /* -1 so we don't write out the \0 */
if(cursor == s->eof) { RETURN(0); } }
any { goto echo; }
*/
}
int
Scanner_scan(Scanner *s)
{
unsigned char *cursor = s->cur;
unsigned int depth;
scan:
s->tchar = cursor - s->pos;
s->tline = s->cline;
s->tok = cursor;
/*!re2c
"{" { depth = 1;
goto code;
}
"/*" { depth = 1;
goto comment; }
"*/" { s->tok = cursor;
RETURN(0); }
dstring { s->cur = cursor;
yylval.regexp = strToRE(Scanner_token(s));
return STRING; }
sstring { s->cur = cursor;
yylval.regexp = strToCaseInsensitiveRE(Scanner_token(s));
return STRING; }
"\"" { Scanner_fatal(s, "unterminated string constant (missing \")"); }
"'" { Scanner_fatal(s, "unterminated string constant (missing ')"); }
istring { s->cur = cursor;
yylval.regexp = invToRE(Scanner_token(s));
return RANGE; }
cstring { s->cur = cursor;
yylval.regexp = ranToRE(Scanner_token(s));
return RANGE; }
"[" { Scanner_fatal(s, "unterminated range (missing ])"); }
[()|=;/\\] { RETURN(*s->tok); }
[*+?] { yylval.op = *s->tok;
RETURN(CLOSE); }
"{" [0-9]+ "}" { yylval.extop.minsize = atoi((char *)s->tok+1);
yylval.extop.maxsize = atoi((char *)s->tok+1);
RETURN(CLOSESIZE); }
"{" [0-9]+ "," [0-9]+ "}" { yylval.extop.minsize = atoi((char *)s->tok+1);
yylval.extop.maxsize = MAX(yylval.extop.minsize,atoi(strchr((char *)s->tok, ',')+1));
RETURN(CLOSESIZE); }
"{" [0-9]+ ",}" { yylval.extop.minsize = atoi((char *)s->tok+1);
yylval.extop.maxsize = -1;
RETURN(CLOSESIZE); }
letter (letter|digit)* { SubStr substr;
s->cur = cursor;
substr = Scanner_token(s);
yylval.symbol = Symbol_find(&substr);
return ID; }
[ \t]+ { goto scan; }
"\n" { if(cursor == s->eof) RETURN(0);
s->pos = cursor; s->cline++;
goto scan;
}
"." { s->cur = cursor;
yylval.regexp = mkDot();
return RANGE;
}
any { fprintf(stderr, "unexpected character: '%c'\n", *s->tok);
goto scan;
}
*/
code:
/*!re2c
"}" { if(--depth == 0){
s->cur = cursor;
yylval.token = Token_new(Scanner_token(s), s->tline);
return CODE;
}
goto code; }
"{" { ++depth;
goto code; }
"\n" { if(cursor == s->eof) Scanner_fatal(s, "missing '}'");
s->pos = cursor; s->cline++;
goto code;
}
dstring | sstring | any { goto code; }
*/
comment:
/*!re2c
"*/" { if(--depth == 0)
goto scan;
else
goto comment; }
"/*" { ++depth;
goto comment; }
"\n" { if(cursor == s->eof) RETURN(0);
s->tok = s->pos = cursor; s->cline++;
goto comment;
}
any { goto comment; }
*/
}
void
Scanner_fatal(Scanner *s, const char *msg)
{
fprintf(stderr, "line %d, column %d: %s\n", s->tline, s->tchar + 1, msg);
exit(1);
}
|