Skip to content

Fix RegExp literal parsing #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions jerry-core/parser/js/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "lit-strings.h"
#include "jsp-early-error.h"

static token saved_token, prev_token, sent_token, empty_token;
static token saved_token, prev_token, sent_token, empty_token, prev_non_lf_token;

static bool allow_dump_lines = false, strict_mode;
static size_t buffer_size = 0;
Expand Down Expand Up @@ -1090,22 +1090,23 @@ lexer_parse_regexp (void)
token result;
bool is_char_class = false;

/* Eat up '/' */
JERRY_ASSERT (LA (0) == LIT_CHAR_SLASH);
consume_char ();
new_token ();

/* Eat up '/' */
consume_char ();

while (true)
{
ecma_char_t c = (ecma_char_t) LA (0);

if (c == LIT_CHAR_NULL)
if (lit_utf8_iterator_is_eos (&src_iter))
{
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unclosed string", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unterminated RegExp literal", token_start_pos);
}
else if (lit_char_is_line_terminator (c))

ecma_char_t c = (ecma_char_t) LA (0);
if (lit_char_is_line_terminator (c))
{
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "RegExp literal shall not contain newline character", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "RegExp literal should not contain newline character", token_start_pos);
}
else if (c == LIT_CHAR_BACKSLASH)
{
Expand Down Expand Up @@ -1140,16 +1141,15 @@ lexer_parse_regexp (void)
{
ecma_char_t c = (ecma_char_t) LA (0);

if (c == LIT_CHAR_NULL
|| !lit_char_is_word_char (c)
|| lit_char_is_line_terminator (c))
if (!lit_char_is_word_char (c) || lit_char_is_line_terminator (c))
{
break;
}

consume_char ();
}

result = lexer_create_token_for_charset (TOK_REGEXP, TOK_START (), TOK_SIZE ());
result = lexer_create_token_for_charset (TOK_REGEXP, TOK_START () + 1, TOK_SIZE () - 1);

is_token_parse_in_progress = false;
return result;
Expand Down Expand Up @@ -1294,16 +1294,16 @@ lexer_parse_token (void)
}

if (c == LIT_CHAR_SLASH
&& !(sent_token.type == TOK_NAME
|| sent_token.type == TOK_NULL
|| sent_token.type == TOK_BOOL
|| sent_token.type == TOK_CLOSE_BRACE
|| sent_token.type == TOK_CLOSE_SQUARE
|| sent_token.type == TOK_CLOSE_PAREN
|| sent_token.type == TOK_SMALL_INT
|| sent_token.type == TOK_NUMBER
|| sent_token.type == TOK_STRING
|| sent_token.type == TOK_REGEXP))
&& !(prev_non_lf_token.type == TOK_NAME
|| prev_non_lf_token.type == TOK_NULL
|| prev_non_lf_token.type == TOK_BOOL
|| prev_non_lf_token.type == TOK_CLOSE_BRACE
|| prev_non_lf_token.type == TOK_CLOSE_SQUARE
|| prev_non_lf_token.type == TOK_CLOSE_PAREN
|| prev_non_lf_token.type == TOK_SMALL_INT
|| prev_non_lf_token.type == TOK_NUMBER
|| prev_non_lf_token.type == TOK_STRING
|| prev_non_lf_token.type == TOK_REGEXP))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOK_CLOSE_PAREN and TOK_CLOSE_BRACE could precede regular expression, for example:

if (true) /abc/.exec("abc");
if (true) {} /abc/.exec("abc");

This case is harder to support, possibly, we could implement it in another patch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another case:

var a
/abc/.exec("abc");

Could you, please, create an issue for the mentioned tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue can be found here: #563

{
return lexer_parse_regexp ();
}
Expand Down Expand Up @@ -1506,6 +1506,10 @@ lexer_next_token (void)
{
dump_current_line ();
}
else
{
prev_non_lf_token = sent_token;
}

end:
return sent_token;
Expand All @@ -1516,6 +1520,7 @@ lexer_save_token (token tok)
{
JERRY_ASSERT (is_empty (saved_token));
saved_token = tok;
prev_non_lf_token = tok;
}

token
Expand All @@ -1531,6 +1536,7 @@ lexer_seek (lit_utf8_iterator_pos_t locus)

lit_utf8_iterator_seek (&src_iter, locus);
saved_token = empty_token;
prev_non_lf_token = empty_token;
}

/**
Expand Down Expand Up @@ -1833,7 +1839,7 @@ lexer_init (const jerry_api_char_t *source, /**< script source */
empty_token.uid = 0;
empty_token.loc = LIT_ITERATOR_POS_ZERO;

saved_token = prev_token = sent_token = empty_token;
saved_token = prev_token = sent_token = prev_non_lf_token = empty_token;

if (!lit_is_utf8_string_valid (source, (lit_utf8_size_t) source_size))
{
Expand Down
2 changes: 2 additions & 0 deletions tests/jerry/arithmetics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ assert (1 / (-1 % -1) < 0);
assert (1 / (-1 % 1) < 0);
assert (1 / (1 % -1) > 0);
assert (1 / (1 % 1) > 0);

assert (eval ("x\n\n=\n\n6\n\n/\n\n3") === 2)
42 changes: 42 additions & 0 deletions tests/jerry/regexp-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,45 @@ assert ("a"+/x/+"b" == "a/x/b");

t = /\/\[[\]/]/.exec("/[/");
assert (t == "/[/");

t = /\u0000/.exec("\u0000");
assert (t == "\u0000");

try {
eval("/" + String.fromCharCode("0x0000") + "/");
} catch (e) {
assert (false);
}

try {
eval("var x = 5\n\n/foo/");
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}

try {
eval("var x = 5;\n\n/foo/");
} catch (e) {
assert(false);
}

try {
eval("for (;false;/abc/.exec(\"abc\")) {5}");
} catch (e) {
assert(false);
}

try {
eval("var a = [] /foo/");
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}

try {
eval("/");
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}