Skip to content

Commit a6ace5e

Browse files
rerobikaLaszloLango
authored andcommitted
Improve lexer_expect_object_literal_id option handling (#2436)
This patch allows to add further options flags to `lexer_expect_object_literal_id` to handle unique behaviors more easily also substitutes `PARSER_IS_CLASS` flag hence it is removed. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 8482fef commit a6ace5e

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,13 +2264,13 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */
22642264
*/
22652265
void
22662266
lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
2267-
bool must_be_identifier) /**< only identifiers are accepted */
2267+
uint32_t ident_opts) /**< lexer_obj_ident_opts_t option bits */
22682268
{
22692269
lexer_skip_spaces (context_p);
22702270

22712271
#ifndef CONFIG_DISABLE_ES2015_CLASS
2272-
int is_class_method = ((context_p->status_flags & PARSER_IS_CLASS)
2273-
&& !must_be_identifier
2272+
int is_class_method = ((ident_opts & LEXER_OBJ_IDENT_CLASS_METHOD)
2273+
&& !(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS)
22742274
&& (context_p->token.type != LEXER_KEYW_STATIC));
22752275
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
22762276

@@ -2285,7 +2285,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
22852285
{
22862286
lexer_parse_identifier (context_p, false);
22872287

2288-
if (!must_be_identifier
2288+
if (!(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS)
22892289
&& context_p->token.lit_location.length == 3)
22902290
{
22912291
lexer_skip_spaces (context_p);
@@ -2323,7 +2323,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
23232323
lexer_parse_string (context_p);
23242324
create_literal_object = true;
23252325
}
2326-
else if (!must_be_identifier && context_p->source_p[0] == LIT_CHAR_RIGHT_BRACE)
2326+
else if (!(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS) && context_p->source_p[0] == LIT_CHAR_RIGHT_BRACE)
23272327
{
23282328
context_p->token.type = LEXER_RIGHT_BRACE;
23292329
context_p->source_p += 1;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ typedef enum
208208
LEXER_NO_SKIP_SPACES = (1u << 1) /**< ignore skip spaces */
209209
} lexer_newline_flags_t;
210210

211+
/**
212+
* Lexer object identifier parse options.
213+
*/
214+
typedef enum
215+
{
216+
LEXER_OBJ_IDENT_NO_OPTS = (1u << 0), /**< no options */
217+
LEXER_OBJ_IDENT_ONLY_IDENTIFIERS = (1u << 1), /**< only identifiers are accepted */
218+
LEXER_OBJ_IDENT_CLASS_METHOD = (1u << 2), /**< expect identifier inside a class body */
219+
} lexer_obj_ident_opts_t;
220+
221+
211222
/**
212223
* Lexer literal object types.
213224
*/

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
379379
lexer_skip_empty_statements (context_p);
380380
}
381381

382-
lexer_expect_object_literal_id (context_p, false);
382+
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_CLASS_METHOD);
383383

384384
if (context_p->token.type == LEXER_RIGHT_BRACE)
385385
{
@@ -403,7 +403,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
403403
opcode = is_static ? CBC_EXT_SET_STATIC_SETTER : CBC_EXT_SET_SETTER;
404404
}
405405

406-
lexer_expect_object_literal_id (context_p, true);
406+
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_CLASS_METHOD | LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
407407
literal_index = context_p->lit_object.index;
408408

409409
if (!is_static && lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
@@ -556,7 +556,7 @@ parser_parse_class (parser_context_t *context_p, /**< context */
556556
bool is_strict = context_p->status_flags & PARSER_IS_STRICT;
557557

558558
/* 14.5. A ClassBody is always strict code. */
559-
context_p->status_flags |= PARSER_IS_STRICT | PARSER_IS_CLASS;
559+
context_p->status_flags |= PARSER_IS_STRICT;
560560

561561
/* ClassDeclaration is parsed. Continue with class body. */
562562
parser_parse_class_literal (context_p, constructor_literal_p);
@@ -586,8 +586,6 @@ parser_parse_class (parser_context_t *context_p, /**< context */
586586

587587
parser_flush_cbc (context_p);
588588

589-
context_p->status_flags &= (uint32_t) ~PARSER_IS_CLASS;
590-
591589
if (!is_strict)
592590
{
593591
/* Restore flag */
@@ -612,7 +610,7 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
612610

613611
while (true)
614612
{
615-
lexer_expect_object_literal_id (context_p, false);
613+
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_NO_OPTS);
616614

617615
if (context_p->token.type == LEXER_RIGHT_BRACE)
618616
{
@@ -640,7 +638,7 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
640638
item_type = PARSER_OBJECT_PROPERTY_SETTER;
641639
}
642640

643-
lexer_expect_object_literal_id (context_p, true);
641+
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
644642
literal_index = context_p->lit_object.index;
645643

646644
parser_append_object_literal_item (context_p, literal_index, item_type);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ typedef enum
6868
PARSER_ARROW_PARSE_ARGS = (1u << 19), /**< parse the argument list of an arrow function */
6969
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
7070
#ifndef CONFIG_DISABLE_ES2015_CLASS
71-
PARSER_IS_CLASS = (1u << 20), /**< statements parsed inside class body */
72-
PARSER_CLASS_CONSTRUCTOR = (1u << 21), /**< a class constructor is parsed */
71+
PARSER_CLASS_CONSTRUCTOR = (1u << 20), /**< a class constructor is parsed */
7372
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
7473
} parser_general_flags_t;
7574

@@ -447,7 +446,7 @@ void lexer_parse_string (parser_context_t *context_p);
447446
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
448447
void lexer_scan_identifier (parser_context_t *context_p, bool propety_name);
449448
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
450-
void lexer_expect_object_literal_id (parser_context_t *context_p, bool must_be_identifier);
449+
void lexer_expect_object_literal_id (parser_context_t *context_p, uint32_t ident_opts);
451450
void lexer_construct_literal_object (parser_context_t *context_p, lexer_lit_location_t *literal_p,
452451
uint8_t literal_type);
453452
bool lexer_construct_number_object (parser_context_t *context_p, bool is_expr, bool is_negative_number);

0 commit comments

Comments
 (0)