implementation of numerical escape sequences in strings ("\12");

better error messages for wrong tokens
This commit is contained in:
Roberto Ierusalimschy 1998-02-11 18:56:46 -02:00
parent 29b7b8e52c
commit aafa106d10
1 changed files with 41 additions and 20 deletions

61
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 1.13 1998/01/09 14:44:55 roberto Exp roberto $ ** $Id: llex.c,v 1.14 1998/01/19 20:18:02 roberto Exp roberto $
** Lexical Analizer ** Lexical Analizer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -126,7 +126,7 @@ static void ifskip (LexState *LS)
if (LS->current == '\n') if (LS->current == '\n')
inclinenumber(LS); inclinenumber(LS);
else if (LS->current == EOZ) else if (LS->current == EOZ)
luaY_syntaxerror("input ends inside a $if", ""); luaY_error("input ends inside a $if");
else next(LS); else next(LS);
} }
} }
@ -218,8 +218,8 @@ static int read_long_string (LexState *LS, YYSTYPE *l)
while (1) { while (1) {
switch (LS->current) { switch (LS->current) {
case EOZ: case EOZ:
save(0); luaY_error("unfinished long string");
return WRONGTOKEN; return 0; /* to avoid warnings */
case '[': case '[':
save_and_next(LS); save_and_next(LS);
if (LS->current == '[') { if (LS->current == '[') {
@ -319,8 +319,8 @@ int luaY_lex (YYSTYPE *l)
switch (LS->current) { switch (LS->current) {
case EOZ: case EOZ:
case '\n': case '\n':
save(0); luaY_error("unfinished string");
return WRONGTOKEN; return 0; /* to avoid warnings */
case '\\': case '\\':
next(LS); /* do not save the '\' */ next(LS); /* do not save the '\' */
switch (LS->current) { switch (LS->current) {
@ -328,7 +328,29 @@ int luaY_lex (YYSTYPE *l)
case 't': save('\t'); next(LS); break; case 't': save('\t'); next(LS); break;
case 'r': save('\r'); next(LS); break; case 'r': save('\r'); next(LS); break;
case '\n': save('\n'); inclinenumber(LS); break; case '\n': save('\n'); inclinenumber(LS); break;
default : save_and_next(LS); break; case '\\': case '"': case '\'': {
save(LS->current);
next(LS);
break;
}
default : {
if (isdigit(LS->current)) {
int c = 0;
int i = 0;
do {
c = 10*c + (LS->current-'0');
i++;
next(LS);
} while (i<3 && isdigit(LS->current));
save(c);
}
else {
save('\\');
save(LS->current);
luaY_error("invalid escape sequence");
}
break;
}
} }
break; break;
default: default:
@ -363,13 +385,13 @@ int luaY_lex (YYSTYPE *l)
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
a=0.0; a=0.0;
do { do {
a=10.0*a+(LS->current-'0'); a = 10.0*a + (LS->current-'0');
save_and_next(LS); save_and_next(LS);
} while (isdigit(LS->current)); } while (isdigit(LS->current));
if (LS->current == '.') { if (LS->current == '.') {
save_and_next(LS); save_and_next(LS);
if (LS->current == '.') { if (LS->current == '.') {
save(0); save('.');
luaY_error( luaY_error(
"ambiguous syntax (decimal point x string concatenation)"); "ambiguous syntax (decimal point x string concatenation)");
} }
@ -378,27 +400,27 @@ int luaY_lex (YYSTYPE *l)
{ double da=0.1; { double da=0.1;
while (isdigit(LS->current)) while (isdigit(LS->current))
{ {
a+=(LS->current-'0')*da; a += (LS->current-'0')*da;
da/=10.0; da /= 10.0;
save_and_next(LS); save_and_next(LS);
} }
if (toupper(LS->current) == 'E') { if (toupper(LS->current) == 'E') {
int e=0; int e = 0;
int neg; int neg;
double ea; double ea;
save_and_next(LS); save_and_next(LS);
neg=(LS->current=='-'); neg = (LS->current=='-');
if (LS->current == '+' || LS->current == '-') save_and_next(LS); if (LS->current == '+' || LS->current == '-') save_and_next(LS);
if (!isdigit(LS->current)) { if (!isdigit(LS->current))
save(0); return WRONGTOKEN; } luaY_error("invalid numeral format");
do { do {
e=10.0*e+(LS->current-'0'); e = 10.0*e + (LS->current-'0');
save_and_next(LS); save_and_next(LS);
} while (isdigit(LS->current)); } while (isdigit(LS->current));
for (ea=neg?0.1:10.0; e>0; e>>=1) for (ea=neg?0.1:10.0; e>0; e>>=1)
{ {
if (e & 1) a*=ea; if (e & 1) a *= ea;
ea*=ea; ea *= ea;
} }
} }
l->vReal = a; l->vReal = a;
@ -406,9 +428,8 @@ int luaY_lex (YYSTYPE *l)
} }
case EOZ: case EOZ:
save(0);
if (LS->iflevel > 0) if (LS->iflevel > 0)
luaY_syntaxerror("input ends inside a $if", ""); luaY_error("input ends inside a $if");
return 0; return 0;
default: default: