Browse Source

Merge pull request #2165 from svaarala/improve-syntax-error-end-of-input

Improve SyntaxError "end of input" detection
pull/2166/head
Sami Vaarala 5 years ago
committed by GitHub
parent
commit
637e6f3154
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      RELEASES.rst
  2. 27
      src-input/duk_error_augment.c
  3. 48
      tests/ecmascript/test-dev-syntax-error-end-of-input.js

3
RELEASES.rst

@ -3562,7 +3562,8 @@ Planned
* Fix some C++ compile warnings (GH-2161)
* Include "end of input" in error message if SyntaxError occurs at end of file (GH-2152)
* Include "end of input" in error message if SyntaxError occurs at end of
file (GH-2152, GH-2165)
3.0.0 (XXXX-XX-XX)
------------------

27
src-input/duk_error_augment.c

@ -433,11 +433,28 @@ DUK_LOCAL void duk__add_compiler_error_line(duk_hthread *thr) {
(duk_tval *) duk_get_tval(thr, -1)));
if (duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_MESSAGE)) {
if (thr->compile_ctx->curr_token.start_offset + 1 >= thr->compile_ctx->lex.input_length) {
duk_push_sprintf(thr, " (line %ld, end of input)", (long) thr->compile_ctx->curr_token.start_line);
} else {
duk_push_sprintf(thr, " (line %ld)", (long) thr->compile_ctx->curr_token.start_line);
}
duk_bool_t at_end;
/* Best guesstimate that error occurred at end of input, token
* truncated by end of input, etc.
*/
#if 0
at_end = (thr->compile_ctx->curr_token.start_offset + 1 >= thr->compile_ctx->lex.input_length);
at_end = (thr->compile_ctx->lex.window[0].codepoint < 0 || thr->compile_ctx->lex.window[1].codepoint < 0);
#endif
at_end = (thr->compile_ctx->lex.window[0].codepoint < 0);
DUK_D(DUK_DPRINT("syntax error, determined at_end=%ld; curr_token.start_offset=%ld, "
"lex.input_length=%ld, window[0].codepoint=%ld, window[1].codepoint=%ld",
(long) at_end,
(long) thr->compile_ctx->curr_token.start_offset,
(long) thr->compile_ctx->lex.input_length,
(long) thr->compile_ctx->lex.window[0].codepoint,
(long) thr->compile_ctx->lex.window[1].codepoint));
duk_push_sprintf(thr, " (line %ld%s)",
(long) thr->compile_ctx->curr_token.start_line,
at_end ? ", end of input" : "");
duk_concat(thr, 2);
duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE);
} else {

48
tests/ecmascript/test-dev-syntax-error-end-of-input.js

@ -0,0 +1,48 @@
/*---
{
"custom": true
}
---*/
/*===
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": false
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": true
ERROR, contains "end of input": false
SUCCESS: undefined
SUCCESS: 123
===*/
[
'var x =',
'var x =;',
'var x =; var y = 1;', // error not at end of input
'var x = 1e',
'var x = 1 +',
'var x = "',
'var x = "foo bar',
'var x = { foo: 123',
'var x = { foo: 123, ',
'var x = { foo: 123, bar',
'var x = { foo: 123, bar:',
'var x = { foo: 123, bar: 234',
'var x = { foo: 123, bar: 234 var z = 1;', // error not at end of input
'var x = { foo: 123, bar: 234 }', // no error
'123' // no error
].forEach(function (v) {
try {
print('SUCCESS: ' + eval(v));
} catch (e) {
//print(e.message);
print('ERROR, contains "end of input": ' + (e.message.indexOf('end of input') >= 0));
}
});
Loading…
Cancel
Save