Skip to content

Commit 5cd750f

Browse files
author
Roland Takacs
committed
Support ES6 based octal literals
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs [email protected]
1 parent 7f6f562 commit 5cd750f

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

jerry-core/parser/js/js-lexer.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,28 @@ lexer_parse_string (parser_context_t *context_p) /**< context */
917917
context_p->column = (parser_line_counter_t) (column + 1);
918918
} /* lexer_parse_string */
919919

920+
/**
921+
* Parse octal number.
922+
*/
923+
static inline void
924+
lexer_parse_octal_number (parser_context_t *context_p, /** context */
925+
const uint8_t **source_p) /**< current source position */
926+
{
927+
do
928+
{
929+
(*source_p)++;
930+
}
931+
while (*source_p < context_p->source_end_p
932+
&& *source_p[0] >= LIT_CHAR_0
933+
&& *source_p[0] <= LIT_CHAR_7);
934+
935+
if (*source_p < context_p->source_end_p
936+
&& (*source_p[0] == LIT_CHAR_8 || *source_p[0] == LIT_CHAR_9))
937+
{
938+
parser_raise_error (context_p, PARSER_ERR_INVALID_NUMBER);
939+
}
940+
} /* lexer_parse_octal_number */
941+
920942
/**
921943
* Parse number.
922944
*/
@@ -956,6 +978,23 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
956978
while (source_p < source_end_p
957979
&& lit_char_is_hex_digit (source_p[0]));
958980
}
981+
#if ENABLED (JERRY_ES2015)
982+
else if (LEXER_TO_ASCII_LOWERCASE (source_p[1]) == LIT_CHAR_LOWERCASE_O)
983+
{
984+
context_p->token.extra_value = LEXER_NUMBER_OCTAL;
985+
context_p->token.lit_location.char_p++;
986+
context_p->source_p++;
987+
source_p += 2;
988+
989+
if (source_p >= source_end_p
990+
|| !lit_char_is_octal_digit (source_p[0]))
991+
{
992+
parser_raise_error (context_p, PARSER_ERR_INVALID_OCTAL_DIGIT);
993+
}
994+
995+
lexer_parse_octal_number (context_p, &source_p);
996+
}
997+
#endif /* ENABLED (JERRY_ES2015) */
959998
else if (source_p[1] >= LIT_CHAR_0
960999
&& source_p[1] <= LIT_CHAR_7)
9611000
{
@@ -966,20 +1005,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
9661005
parser_raise_error (context_p, PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED);
9671006
}
9681007

969-
do
970-
{
971-
source_p++;
972-
}
973-
while (source_p < source_end_p
974-
&& source_p[0] >= LIT_CHAR_0
975-
&& source_p[0] <= LIT_CHAR_7);
976-
977-
if (source_p < source_end_p
978-
&& source_p[0] >= LIT_CHAR_8
979-
&& source_p[0] <= LIT_CHAR_9)
980-
{
981-
parser_raise_error (context_p, PARSER_ERR_INVALID_NUMBER);
982-
}
1008+
lexer_parse_octal_number (context_p, &source_p);
9831009
}
9841010
else if (source_p[1] >= LIT_CHAR_8
9851011
&& source_p[1] <= LIT_CHAR_9)

jerry-core/parser/js/js-parser-util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,12 @@ parser_error_to_string (parser_error_t error) /**< error code */
828828
{
829829
return "Invalid (unexpected) character.";
830830
}
831+
#if ENABLED (JERRY_ES2015)
832+
case PARSER_ERR_INVALID_OCTAL_DIGIT:
833+
{
834+
return "Invalid octal digit.";
835+
}
836+
#endif /* ENABLED (JERRY_ES2015) */
831837
case PARSER_ERR_INVALID_HEX_DIGIT:
832838
{
833839
return "Invalid hexadecimal digit.";

jerry-core/parser/js/js-parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ typedef enum
4242
PARSER_ERR_STACK_LIMIT_REACHED, /**< maximum function stack size reached */
4343

4444
PARSER_ERR_INVALID_CHARACTER, /**< unexpected character */
45+
#if ENABLED (JERRY_ES2015)
46+
PARSER_ERR_INVALID_OCTAL_DIGIT, /**< invalid octal digit */
47+
#endif /* ENABLED (JERRY_ES2015) */
4548
PARSER_ERR_INVALID_HEX_DIGIT, /**< invalid hexadecimal digit */
4649
PARSER_ERR_INVALID_ESCAPE_SEQUENCE, /**< invalid escape sequence */
4750
PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE, /**< invalid unicode escape sequence */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
function checkSyntaxError (str) {
17+
try {
18+
eval (str);
19+
assert (false);
20+
} catch (e) {
21+
assert (e instanceof SyntaxError);
22+
}
23+
}
24+
25+
checkSyntaxError ("0p");
26+
checkSyntaxError ("0o");
27+
checkSyntaxError ("0o0123456789");
28+
checkSyntaxError ("0o9");
29+
30+
checkSyntaxError ("0p");
31+
checkSyntaxError ("0O");
32+
checkSyntaxError ("0O9");
33+
34+
// Check strict mode
35+
checkSyntaxError ("'use strict'; 0777");
36+
assert (eval ("'use strict'; 0o777") === 511);
37+
38+
assert (0o123 === 83);
39+
assert (0o77777777 === 16777215);
40+
assert (0o767 === parseInt ("767", 8));
41+
assert (0767 === 0o767);
42+
43+
assert (0O123 === 83);
44+
assert (0O77777777 === 16777215);
45+
assert (0O767 === parseInt ("767", 8));
46+
assert (0767 === 0O767);

0 commit comments

Comments
 (0)