Add [no line-terminator here] context handling in lexer.

Return a semicolon if a newline is encountered following the
"break", "continue", "return" and "throw" keywords.

Add special tokens for ++ and -- with preceding line terminators,
so that they can be handled in the postfix expression grammar rules.
This commit is contained in:
Tor Andersson
2013-12-28 17:39:47 +01:00
parent e928c1410c
commit dde8db59b8
2 changed files with 22 additions and 4 deletions
+20 -4
View File
@@ -283,8 +283,7 @@ static inline int lexstring(js_State *J, const char **sp, int q)
/* the ugliest language wart ever... */
static int isregexpcontext(int last)
{
switch (last)
{
switch (last) {
case ']':
case ')':
case TK_IDENTIFIER:
@@ -344,6 +343,20 @@ static int lexregexp(js_State *J, const char **sp)
return TK_REGEXP;
}
/* simple "return [no Line Terminator here] ..." contexts */
static inline int isnlthcontext(int last)
{
switch (last) {
case TK_BREAK:
case TK_CONTINUE:
case TK_RETURN:
case TK_THROW:
return 1;
default:
return 0;
}
}
static int lex(js_State *J, const char **sp)
{
J->newline = 0;
@@ -360,6 +373,8 @@ static int lex(js_State *J, const char **sp)
NEXT();
J->yyline++;
J->newline = 1;
if (isnlthcontext(J->lasttoken))
return ';';
continue;
}
@@ -468,14 +483,14 @@ static int lex(js_State *J, const char **sp)
case '+':
if (LOOK('+'))
return TK_INC;
return J->newline ? TK_INC : TK_NLTH_INC;
if (LOOK('='))
return TK_ADD_ASS;
return '+';
case '-':
if (LOOK('-'))
return TK_DEC;
return J->newline ? TK_DEC : TK_NLTH_DEC;
if (LOOK('='))
return TK_SUB_ASS;
return '-';
@@ -529,6 +544,7 @@ void jsP_initlex(js_State *J, const char *source)
int jsP_lex(js_State *J)
{
int t = lex(J, &J->yysource);
// TODO: move yytext/yynumber into jsP_lval
J->lasttoken = t;
return t;
}
+2
View File
@@ -33,7 +33,9 @@ enum {
TK_OR_ASS,
TK_XOR_ASS,
TK_INC,
TK_NLTH_INC,
TK_DEC,
TK_NLTH_DEC,
/* keywords */
TK_BREAK,