Skip to content

Commit c3acb74

Browse files
committed
Fix RegExp literal parsing
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
1 parent f10a48d commit c3acb74

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

jerry-core/parser/js/lexer.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "lit-strings.h"
2424
#include "jsp-early-error.h"
2525

26-
static token saved_token, prev_token, sent_token, empty_token;
26+
static token saved_token, prev_token, sent_token, empty_token, prev_non_lf_token;
2727

2828
static bool allow_dump_lines = false, strict_mode;
2929
static size_t buffer_size = 0;
@@ -1092,18 +1092,15 @@ lexer_parse_regexp (void)
10921092

10931093
/* Eat up '/' */
10941094
JERRY_ASSERT (LA (0) == LIT_CHAR_SLASH);
1095-
consume_char ();
10961095
new_token ();
10971096

1097+
consume_char ();
1098+
10981099
while (true)
10991100
{
11001101
ecma_char_t c = (ecma_char_t) LA (0);
11011102

1102-
if (c == LIT_CHAR_NULL)
1103-
{
1104-
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unclosed string", token_start_pos);
1105-
}
1106-
else if (lit_char_is_line_terminator (c))
1103+
if (lit_char_is_line_terminator (c))
11071104
{
11081105
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "RegExp literal shall not contain newline character", token_start_pos);
11091106
}
@@ -1140,16 +1137,15 @@ lexer_parse_regexp (void)
11401137
{
11411138
ecma_char_t c = (ecma_char_t) LA (0);
11421139

1143-
if (c == LIT_CHAR_NULL
1144-
|| !lit_char_is_word_char (c)
1145-
|| lit_char_is_line_terminator (c))
1140+
if (!lit_char_is_word_char (c) || lit_char_is_line_terminator (c))
11461141
{
11471142
break;
11481143
}
1144+
11491145
consume_char ();
11501146
}
11511147

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

11541150
is_token_parse_in_progress = false;
11551151
return result;
@@ -1294,16 +1290,16 @@ lexer_parse_token (void)
12941290
}
12951291

12961292
if (c == LIT_CHAR_SLASH
1297-
&& !(sent_token.type == TOK_NAME
1298-
|| sent_token.type == TOK_NULL
1299-
|| sent_token.type == TOK_BOOL
1300-
|| sent_token.type == TOK_CLOSE_BRACE
1301-
|| sent_token.type == TOK_CLOSE_SQUARE
1302-
|| sent_token.type == TOK_CLOSE_PAREN
1303-
|| sent_token.type == TOK_SMALL_INT
1304-
|| sent_token.type == TOK_NUMBER
1305-
|| sent_token.type == TOK_STRING
1306-
|| sent_token.type == TOK_REGEXP))
1293+
&& !(prev_non_lf_token.type == TOK_NAME
1294+
|| prev_non_lf_token.type == TOK_NULL
1295+
|| prev_non_lf_token.type == TOK_BOOL
1296+
|| prev_non_lf_token.type == TOK_CLOSE_BRACE
1297+
|| prev_non_lf_token.type == TOK_CLOSE_SQUARE
1298+
|| prev_non_lf_token.type == TOK_CLOSE_PAREN
1299+
|| prev_non_lf_token.type == TOK_SMALL_INT
1300+
|| prev_non_lf_token.type == TOK_NUMBER
1301+
|| prev_non_lf_token.type == TOK_STRING
1302+
|| prev_non_lf_token.type == TOK_REGEXP))
13071303
{
13081304
return lexer_parse_regexp ();
13091305
}
@@ -1482,6 +1478,7 @@ lexer_next_token (void)
14821478
if (!is_empty (saved_token))
14831479
{
14841480
sent_token = saved_token;
1481+
prev_non_lf_token = sent_token;
14851482
saved_token = empty_token;
14861483
goto end;
14871484
}
@@ -1499,6 +1496,11 @@ lexer_next_token (void)
14991496
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unexpected EOF", lit_utf8_iterator_get_pos (&src_iter));
15001497
}
15011498

1499+
if (sent_token.type != TOK_NEWLINE)
1500+
{
1501+
prev_non_lf_token = sent_token;
1502+
}
1503+
15021504
prev_token = sent_token;
15031505
sent_token = lexer_parse_token ();
15041506

tests/jerry/arithmetics.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ assert (1 / (-1 % -1) < 0);
3737
assert (1 / (-1 % 1) < 0);
3838
assert (1 / (1 % -1) > 0);
3939
assert (1 / (1 % 1) > 0);
40+
41+
assert (eval ("x\n\n=\n\n6\n\n/\n\n3") === 2)

tests/jerry/regexp-literal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ assert ("a"+/x/+"b" == "a/x/b");
2323

2424
t = /\/\[[\]/]/.exec("/[/");
2525
assert (t == "/[/");
26+
27+
try {
28+
eval("/" + String.fromCharCode("0x0000") + "/");
29+
} catch (e) {
30+
assert (false);
31+
}

0 commit comments

Comments
 (0)