ref: dde8db59b846ec59f809b3d885fd1c82a567b67a
parent: e928c1410c38c4befb60f103c4246323f2a59e02
author: Tor Andersson <tor.andersson@artifex.com>
date: Sat Dec 28 12:39:47 EST 2013
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.
--- a/js-lex.c
+++ b/js-lex.c
@@ -283,8 +283,7 @@
/* the ugliest language wart ever... */
static int isregexpcontext(int last)
{
- switch (last)
- {
+ switch (last) {
case ']':
case ')':
case TK_IDENTIFIER:
@@ -344,6 +343,20 @@
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 @@
NEXT();
J->yyline++;
J->newline = 1;
+ if (isnlthcontext(J->lasttoken))
+ return ';';
continue;
}
@@ -468,7 +483,7 @@
case '+':
if (LOOK('+'))
- return TK_INC;
+ return J->newline ? TK_INC : TK_NLTH_INC;
if (LOOK('='))
return TK_ADD_ASS;
return '+';
@@ -475,7 +490,7 @@
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 @@
int jsP_lex(js_State *J)
{
int t = lex(J, &J->yysource);
+ // TODO: move yytext/yynumber into jsP_lval
J->lasttoken = t;
return t;
}
--- a/js-parse.h
+++ b/js-parse.h
@@ -33,7 +33,9 @@
TK_OR_ASS,
TK_XOR_ASS,
TK_INC,
+ TK_NLTH_INC,
TK_DEC,
+ TK_NLTH_DEC,
/* keywords */
TK_BREAK,