error on invalid escape sequences

This commit is contained in:
Roberto Ierusalimschy 2011-07-08 16:17:30 -03:00
parent b5084fdafe
commit df19931ddc
1 changed files with 38 additions and 31 deletions

69
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.50 2011/07/05 19:13:45 roberto Exp roberto $ ** $Id: llex.c,v 2.51 2011/07/06 16:45:14 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -286,45 +286,51 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
} }
static void escerror (LexState *ls, int *c, int n, const char *msg) {
int i;
luaZ_resetbuffer(ls->buff); /* prepare error message */
save(ls, '\\');
for (i = 0; i < n && c[i] != EOZ; i++)
save(ls, c[i]);
lexerror(ls, msg, TK_STRING);
}
static int readhexaesc (LexState *ls) { static int readhexaesc (LexState *ls) {
int c1 = next(ls); int c[3]; /* keep input for error message */
int c2 = EOZ; int i = 2; /* at least 'x?' will go to error message */
if (lisxdigit(c1)) { c[0] = 'x';
c2 = next(ls); c[1] = next(ls); /* first hexa digit */
if (lisxdigit(c2)) if (lisxdigit(c[1])) {
return (luaO_hexavalue(c1) << 4) + luaO_hexavalue(c2); c[i++] = next(ls); /* second hexa digit */
if (lisxdigit(c[2]))
return (luaO_hexavalue(c[1]) << 4) + luaO_hexavalue(c[2]);
/* else go through to error */ /* else go through to error */
} }
luaZ_resetbuffer(ls->buff); /* prepare error message */ escerror(ls, c, i, "hexadecimal digit expected");
save(ls, '\\'); save(ls, 'x');
if (c1 != EOZ) save(ls, c1);
if (c2 != EOZ) save(ls, c2);
lexerror(ls, "hexadecimal digit expected", TK_STRING);
return 0; /* to avoid warnings */ return 0; /* to avoid warnings */
} }
static int readdecesc (LexState *ls) { static int readdecesc (LexState *ls) {
int c1 = ls->current; /* first char must be a digit */ int c[3], r;
int c2 = next(ls); /* read second char */ int i = 0;
int c = c1 - '0'; /* partial result */ c[i++] = ls->current; /* first char must be a digit */
if (lisdigit(c2)) { c[i++] = next(ls); /* read second char */
int c3 = next(ls); /* read third char */ r = c[0] - '0'; /* partial result */
c = 10*c + c2 - '0'; /* update result */ if (lisdigit(c[1])) {
if (lisdigit(c3)) { c[i++] = next(ls); /* read third char */
c = 10*c + c3 - '0'; /* update result */ r = 10*r + c[1] - '0'; /* update result */
if (c > UCHAR_MAX) { if (lisdigit(c[2])) {
luaZ_resetbuffer(ls->buff); /* prepare error message */ r = 10*r + c[2] - '0'; /* update result */
save(ls, '\\'); if (r > UCHAR_MAX)
save(ls, c1); save(ls, c2); save(ls, c3); escerror(ls, c, i, "decimal escape too large");
lexerror(ls, "decimal escape too large", TK_STRING); return r;
}
return c;
} }
} }
/* else, has read one character that was not a digit */ /* else, has read one character that was not a digit */
zungetc(ls->z); /* return it to input stream */ zungetc(ls->z); /* return it to input stream */
return c; return r;
} }
@ -353,6 +359,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
case 'x': c = readhexaesc(ls); break; case 'x': c = readhexaesc(ls); break;
case '\n': case '\n':
case '\r': save(ls, '\n'); inclinenumber(ls); continue; case '\r': save(ls, '\n'); inclinenumber(ls); continue;
case '\\': case '\"': case '\'': c = ls->current; break;
case EOZ: continue; /* will raise an error next loop */ case EOZ: continue; /* will raise an error next loop */
case 'z': { /* zap following span of spaces */ case 'z': { /* zap following span of spaces */
next(ls); /* skip the 'z' */ next(ls); /* skip the 'z' */
@ -364,9 +371,9 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
} }
default: { default: {
if (!lisdigit(ls->current)) if (!lisdigit(ls->current))
c = ls->current; /* handles \\, \", and \' */ escerror(ls, &ls->current, 1, "invalid escape sequence");
else /* digital escape \ddd */ /* digital escape \ddd */
c = readdecesc(ls); c = readdecesc(ls);
break; break;
} }
} }