ref: 8a1c1c24ed0998efd90a24827df512325207d983
parent: f36eb04aeec2b5b81c55dadc8effac349b6369fc
author: Tor Andersson <tor@ccxvii.net>
date: Sat Jan 11 13:06:17 EST 2014
Add warning function to parser. Warn but accept function expressions as expression statements.
--- a/jslex.h
+++ b/jslex.h
@@ -69,5 +69,6 @@
int jsP_lex(js_State *J);
const char *jsP_tokenstring(int token);
int jsP_error(js_State *J, const char *fmt, ...);
+void jsP_warning(js_State *J, const char *fmt, ...);
#endif
--- a/jsparse.c
+++ b/jsparse.c
@@ -764,12 +764,13 @@
}
/* expression statement */
- if (J->lookahead != TK_FUNCTION) {
- a = expression(J, 0);
- semicolon(J);
- return a;
- }
+ if (J->lookahead == TK_FUNCTION)
+ jsP_warning(J, "naked function expression");
+ a = expression(J, 0);
+ semicolon(J);
+ return a;
+
jsP_error(J, "unexpected token in statement: %s", TOKSTR);
return NULL;
}
@@ -808,6 +809,17 @@
a = chunklist(J);
expect(J, '}');
return a;
+}
+
+void jsP_warning(js_State *J, const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s:%d: warning:", J->filename, J->line);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
}
int jsP_error(js_State *J, const char *fmt, ...)