Implement optional chaining (#5164)

The work is based on PR #4843, only fixed some conflicts and cppcheck errors.

Co-authored-by: Robert Fancsik robert.fancsik@h-lab.eu
JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi gergocs@inf.u-szeged.hu
This commit is contained in:
Gergo Csizi
2024-11-20 11:57:58 +01:00
committed by GitHub
parent e9f08a7879
commit f54f2d3a7b
14 changed files with 1012 additions and 412 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ JERRY_STATIC_ASSERT (offsetof (cbc_uint8_arguments_t, script_value) == offsetof
* whenever new bytecodes are introduced or existing ones have been deleted.
*/
JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 167, number_of_cbc_ext_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 170, number_of_cbc_ext_opcodes_changed);
/** \addtogroup parser Parser
* @{
+2 -2
View File
@@ -497,9 +497,9 @@
CBC_FORWARD_BRANCH (CBC_EXT_DEFAULT_INITIALIZER, -1, VM_OC_DEFAULT_INITIALIZER) \
CBC_OPCODE (CBC_EXT_ERROR, CBC_NO_FLAG, 0, VM_OC_ERROR) \
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_IF_NULLISH, -1, VM_OC_BRANCH_IF_NULLISH) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_POP_REFERENCE, CBC_NO_FLAG, -2, VM_OC_POP_REFERENCE) \
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_OPTIONAL_CHAIN, 0, VM_OC_BRANCH_OPTIONAL_CHAIN) \
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_CREATE_ARGUMENTS, CBC_HAS_LITERAL_ARG, 0, VM_OC_CREATE_ARGUMENTS) \
CBC_OPCODE (CBC_EXT_CREATE_VAR_EVAL, CBC_HAS_LITERAL_ARG, 0, VM_OC_EXT_VAR_EVAL) \
CBC_OPCODE (CBC_EXT_CREATE_VAR_FUNC_EVAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, VM_OC_EXT_VAR_EVAL) \
+30
View File
@@ -381,6 +381,29 @@ lexer_skip_spaces (parser_context_t *context_p) /**< context */
}
} /* lexer_skip_spaces */
/**
* Checks the next token start character.
*
* @return LIT_INVALID_CP - if there is no more characters to read
* next byte - otherwise
*/
lit_code_point_t
lexer_peek_next_character (parser_context_t *context_p) /**< context */
{
if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES))
{
lexer_skip_spaces (context_p);
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
}
if (context_p->source_p < context_p->source_end_p)
{
return context_p->source_p[0];
}
return LIT_INVALID_CP;
} /* lexer_check_next_character */
/**
* Skip all the continuous empty statements.
*/
@@ -1738,6 +1761,13 @@ lexer_next_token (parser_context_t *context_p) /**< context */
length = 2;
break;
}
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_DOT
&& (length < 3 || !lit_char_is_decimal_digit (context_p->source_p[2])))
{
context_p->token.type = LEXER_QUESTION_MARK_DOT;
length = 2;
break;
}
}
context_p->token.type = LEXER_QUESTION_MARK;
+1
View File
@@ -154,6 +154,7 @@ typedef enum
LEXER_RIGHT_PAREN, /**< ")" */
LEXER_RIGHT_SQUARE, /**< "]" */
LEXER_DOT, /**< "." */
LEXER_QUESTION_MARK_DOT, /**< "?." */
LEXER_SEMICOLON, /**< ";" */
LEXER_COLON, /**< ":" */
LEXER_COMMA, /**< "," */
File diff suppressed because it is too large Load Diff
@@ -736,6 +736,7 @@ bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t ch
bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1, lit_utf8_byte_t character2);
uint8_t lexer_consume_next_character (parser_context_t *context_p);
bool lexer_check_post_primary_exp (parser_context_t *context_p);
lit_code_point_t lexer_peek_next_character (parser_context_t *context_p);
void lexer_skip_empty_statements (parser_context_t *context_p);
bool lexer_check_arrow (parser_context_t *context_p);
bool lexer_check_arrow_param (parser_context_t *context_p);
+84 -52
View File
@@ -354,6 +354,29 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
return SCAN_NEXT_TOKEN;
} /* scanner_scan_primary_expression */
/**
* Consume the ?. token
*
* @return token type to continue the post primary expression parsing
*/
static lexer_token_type_t
scanner_consume_optional_chain (parser_context_t *context_p) /**< context */
{
switch (lexer_peek_next_character (context_p))
{
case LIT_CHAR_LEFT_PAREN:
case LIT_CHAR_LEFT_SQUARE:
{
lexer_next_token (context_p);
return context_p->token.type;
}
default:
{
return LEXER_DOT;
}
}
} /* scanner_consume_optional_chain */
/**
* Scan the tokens after the primary expression.
*
@@ -365,75 +388,84 @@ scanner_scan_post_primary_expression (parser_context_t *context_p, /**< context
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top) /**< current stack top */
{
switch (type)
while (true)
{
case LEXER_DOT:
switch (type)
{
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
if (context_p->token.type == LEXER_HASHMARK)
case LEXER_QUESTION_MARK_DOT:
{
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
type = scanner_consume_optional_chain (context_p);
continue;
}
if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
case LEXER_DOT:
{
scanner_raise_error (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
if (context_p->token.type == LEXER_HASHMARK)
{
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
}
if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
scanner_raise_error (context_p);
}
return true;
}
return true;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_TEMPLATE_LITERAL:
{
if (JERRY_UNLIKELY (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT))
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
parser_stack_push_uint8 (context_p, SCAN_STACK_TAGGED_TEMPLATE_LITERAL);
return true;
}
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PROPERTY_ACCESSOR);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
if (context_p->token.flags & LEXER_WAS_NEWLINE)
case LEXER_TEMPLATE_LITERAL:
{
return false;
if (JERRY_UNLIKELY (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT))
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
parser_stack_push_uint8 (context_p, SCAN_STACK_TAGGED_TEMPLATE_LITERAL);
}
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PROPERTY_ACCESSOR);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
lexer_next_token (context_p);
type = (lexer_token_type_t) context_p->token.type;
if (context_p->token.flags & LEXER_WAS_NEWLINE)
{
return false;
}
if (type != LEXER_QUESTION_MARK)
lexer_next_token (context_p);
type = (lexer_token_type_t) context_p->token.type;
if (type != LEXER_QUESTION_MARK)
{
break;
}
/* FALLTHRU */
}
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
default:
{
break;
}
/* FALLTHRU */
}
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
default:
{
break;
}
break;
}
if (LEXER_IS_BINARY_OP_TOKEN (type) && (type != LEXER_KEYW_IN || !SCANNER_IS_FOR_START (stack_top)))
@@ -169,6 +169,7 @@ PARSER_ERROR_DEF (PARSER_ERR_IMPORT_META_REQUIRE_MODULE, "Cannot use 'import.met
#if JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_INVALID_IDENTIFIER_PART, "Character cannot be part of an identifier")
PARSER_ERROR_DEF (PARSER_ERR_EVAL_CANNOT_ASSIGNED, "Eval cannot be assigned to in strict mode")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_TAGGED_TEMPLATE_OPTIONAL_CHAIN, "Invalid tagged template on optional chain")
PARSER_ERROR_DEF (PARSER_ERR_WITH_NOT_ALLOWED, "With statement not allowed in strict mode")
PARSER_ERROR_DEF (PARSER_ERR_NEW_TARGET_NOT_ALLOWED, "new.target expression is not allowed here")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_IDENTIFIER_START, "Character cannot be start of an identifier")
@@ -140,3 +140,4 @@ PARSER_ERR_UNDECLARED_PRIVATE_FIELD = "Private field must be declared in an encl
PARSER_ERR_DELETE_PRIVATE_FIELD = "Private fields can not be deleted"
PARSER_ERR_UNEXPECTED_PRIVATE_FIELD = "Unexpected private field"
PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR = "Class constructor may not be a private method"
PARSER_ERR_INVALID_TAGGED_TEMPLATE_OPTIONAL_CHAIN = "Invalid tagged template on optional chain"