Add support of ReferenceError early error to parser.

JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
This commit is contained in:
Ruben Ayrapetyan 2015-07-13 21:58:38 +03:00 committed by Evgeny Gavrin
parent 06d0c1806d
commit 44b7b95781
11 changed files with 242 additions and 166 deletions

View File

@ -256,19 +256,24 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p,
}
const opcode_t* opcodes_p;
bool is_syntax_correct;
jsp_status_t parse_status;
is_syntax_correct = parser_parse_new_function ((const jerry_api_char_t **) utf8_string_params_p,
utf8_string_params_size,
params_count,
&opcodes_p);
parse_status = parser_parse_new_function ((const jerry_api_char_t **) utf8_string_params_p,
utf8_string_params_size,
params_count,
&opcodes_p);
if (!is_syntax_correct)
if (parse_status == JSP_STATUS_SYNTAX_ERROR)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_SYNTAX));
}
else if (parse_status == JSP_STATUS_REFERENCE_ERROR)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_REFERENCE));
}
else
{
JERRY_ASSERT (parse_status == JSP_STATUS_OK);
bool is_strict = false;
bool do_instantiate_arguments_object = true;

View File

@ -87,21 +87,27 @@ ecma_op_eval_chars_buffer (const jerry_api_char_t *code_p, /**< code characters
ecma_completion_value_t completion;
const opcode_t *opcodes_p;
bool is_syntax_correct;
jsp_status_t parse_status;
bool is_strict_call = (is_direct && is_called_from_strict_mode_code);
is_syntax_correct = parser_parse_eval (code_p,
code_buffer_size,
is_strict_call,
&opcodes_p);
parse_status = parser_parse_eval (code_p,
code_buffer_size,
is_strict_call,
&opcodes_p);
if (!is_syntax_correct)
if (parse_status == JSP_STATUS_SYNTAX_ERROR)
{
completion = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_SYNTAX));
}
else if (parse_status == JSP_STATUS_REFERENCE_ERROR)
{
completion = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_REFERENCE));
}
else
{
JERRY_ASSERT (parse_status == JSP_STATUS_OK);
opcode_counter_t first_opcode_index = 0u;
bool is_strict_prologue = false;
opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (opcodes_p,

View File

@ -1334,14 +1334,16 @@ jerry_parse (const jerry_api_char_t* source_p, /**< script source */
parser_set_show_opcodes (is_show_opcodes);
const opcode_t *opcodes_p;
bool is_syntax_correct;
jsp_status_t parse_status;
is_syntax_correct = parser_parse_script (source_p,
source_size,
&opcodes_p);
parse_status = parser_parse_script (source_p,
source_size,
&opcodes_p);
if (!is_syntax_correct)
if (parse_status != JSP_STATUS_OK)
{
JERRY_ASSERT (parse_status == JSP_STATUS_SYNTAX_ERROR || parse_status == JSP_STATUS_REFERENCE_ERROR);
return false;
}

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "syntax-errors.h"
#include "jsp-early-error.h"
#include "stack.h"
#include "jrt.h"
#include "parser.h"
@ -22,13 +22,18 @@
#include "lit-magic-strings.h"
/**
* SyntaxError longjmp label, used to finish parse upon a SyntaxError is raised
* Early error longjmp label, used to finish parse upon an early error occurence
*
* See also:
* syntax_get_syntax_error_longjmp_label
* syntax_raise_error
* jsp_early_error_get_early_error_longjmp_label
* jsp_early_error_raise_error
*/
static jmp_buf jsp_syntax_error_label;
static jmp_buf jsp_early_error_label;
/**
* Type of early error occured, or JSP_EARLY_ERROR__NO_ERROR
*/
jsp_early_error_t jsp_early_error_type;
typedef struct
{
@ -54,19 +59,42 @@ STATIC_STACK (size_t_stack, size_t)
* @return pointer to jmp_buf
*/
jmp_buf *
syntax_get_syntax_error_longjmp_label (void)
jsp_early_error_get_early_error_longjmp_label (void)
{
return &jsp_syntax_error_label;
} /* syntax_get_syntax_error_longjmp_label */
return &jsp_early_error_label;
} /* jsp_early_error_get_early_error_longjmp_label */
/**
* Raise SyntaxError, i.e. perform longjmp to SyntaxError longjmp label
* Raise an early error of specified type
*
* Note:
* Performs longjmp to early error longjmp label
*
* See also:
* parser_parse_program
*/
void __attribute__((noreturn))
syntax_raise_error (void)
jsp_early_error_raise_error (jsp_early_error_t type) /**< type of error to raise */
{
longjmp (jsp_syntax_error_label, 1);
} /* syntax_raise_error */
JERRY_ASSERT (jsp_early_error_type == JSP_EARLY_ERROR__NO_ERROR);
jsp_early_error_type = type;
longjmp (jsp_early_error_label, 1);
} /* jsp_early_error_raise_error */
/**
* Get type of occured early error
*
* @return type
*/
jsp_early_error_t
jsp_early_error_get_type (void)
{
JERRY_ASSERT (jsp_early_error_type != JSP_EARLY_ERROR__NO_ERROR);
return jsp_early_error_type;
} /* jsp_early_error_get_type */
static prop_literal
create_prop_literal (literal_t lit, prop_type type)
@ -80,20 +108,20 @@ create_prop_literal (literal_t lit, prop_type type)
}
void
syntax_start_checking_of_prop_names (void)
jsp_early_error_start_checking_of_prop_names (void)
{
STACK_PUSH (size_t_stack, STACK_SIZE (props));
}
void
syntax_add_prop_name (operand op, prop_type pt)
jsp_early_error_add_prop_name (operand op, prop_type pt)
{
JERRY_ASSERT (op.type == OPERAND_LITERAL);
STACK_PUSH (props, create_prop_literal (lit_get_literal_by_cp (op.data.lit_id), pt));
}
void
syntax_check_for_duplication_of_prop_names (bool is_strict, locus loc __attr_unused___)
jsp_early_error_check_for_duplication_of_prop_names (bool is_strict, locus loc __attr_unused___)
{
if (STACK_SIZE (props) - STACK_TOP (size_t_stack) < 2)
{
@ -129,28 +157,32 @@ syntax_check_for_duplication_of_prop_names (bool is_strict, locus loc __attr_unu
/*a*/
if (is_strict && previous.type == PROP_DATA && current.type == PROP_DATA)
{
PARSE_ERROR_VARG ("Duplication of parameter name '%s' in ObjectDeclaration is not allowed in strict mode",
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Duplication of parameter name '%s' in ObjectDeclaration is not allowed in strict mode",
loc, lit_literal_to_str_internal_buf (current.lit));
}
/*b*/
if (previous.type == PROP_DATA
&& (current.type == PROP_SET || current.type == PROP_GET))
{
PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
loc, lit_literal_to_str_internal_buf (current.lit));
}
/*c*/
if (current.type == PROP_DATA
&& (previous.type == PROP_SET || previous.type == PROP_GET))
{
PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
loc, lit_literal_to_str_internal_buf (current.lit));
}
/*d*/
if ((previous.type == PROP_SET && current.type == PROP_SET)
|| (previous.type == PROP_GET && current.type == PROP_GET))
{
PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be accessor of same type",
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Parameter name '%s' in ObjectDeclaration may not be accessor of same type",
loc, lit_literal_to_str_internal_buf (current.lit));
}
}
@ -162,12 +194,12 @@ syntax_check_for_duplication_of_prop_names (bool is_strict, locus loc __attr_unu
}
void
syntax_start_checking_of_vargs (void)
jsp_early_error_start_checking_of_vargs (void)
{
STACK_PUSH (size_t_stack, STACK_SIZE (props));
}
void syntax_add_varg (operand op)
void jsp_early_error_add_varg (operand op)
{
JERRY_ASSERT (op.type == OPERAND_LITERAL);
STACK_PUSH (props, create_prop_literal (lit_get_literal_by_cp (op.data.lit_id), VARG));
@ -185,13 +217,13 @@ emit_error_on_eval_and_arguments (operand op, locus loc __attr_unused___)
lit_get_magic_string_utf8 (LIT_MAGIC_STRING_EVAL),
lit_get_magic_string_size (LIT_MAGIC_STRING_EVAL)))
{
PARSE_ERROR ("'eval' and 'arguments' are not allowed here in strict mode", loc);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "'eval' and 'arguments' are not allowed here in strict mode", loc);
}
}
}
void
syntax_check_for_eval_and_arguments_in_strict_mode (operand op, bool is_strict, locus loc)
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (operand op, bool is_strict, locus loc)
{
if (is_strict)
{
@ -201,7 +233,7 @@ syntax_check_for_eval_and_arguments_in_strict_mode (operand op, bool is_strict,
/* 13.1, 15.3.2 */
void
syntax_check_for_syntax_errors_in_formal_param_list (bool is_strict, locus loc __attr_unused___)
jsp_early_error_check_for_syntax_errors_in_formal_param_list (bool is_strict, locus loc __attr_unused___)
{
if (STACK_SIZE (props) - STACK_TOP (size_t_stack) < 2 || !is_strict)
{
@ -224,7 +256,8 @@ syntax_check_for_syntax_errors_in_formal_param_list (bool is_strict, locus loc _
|| current->get_type () == LIT_MAGIC_STR_EX_T);
if (lit_literal_equal_type (previous, current))
{
PARSE_ERROR_VARG ("Duplication of literal '%s' in FormalParameterList is not allowed in strict mode",
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Duplication of literal '%s' in FormalParameterList is not allowed in strict mode",
loc, lit_literal_to_str_internal_buf (previous));
}
}
@ -235,23 +268,25 @@ syntax_check_for_syntax_errors_in_formal_param_list (bool is_strict, locus loc _
}
void
syntax_check_delete (bool is_strict, locus loc __attr_unused___)
jsp_early_error_check_delete (bool is_strict, locus loc __attr_unused___)
{
if (is_strict)
{
PARSE_ERROR ("'delete' operator shall not apply on identifier in strict mode.", loc);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "'delete' operator shall not apply on identifier in strict mode.", loc);
}
}
void
syntax_init (void)
jsp_early_error_init (void)
{
jsp_early_error_type = JSP_EARLY_ERROR__NO_ERROR;
STACK_INIT (props);
STACK_INIT (size_t_stack);
}
void
syntax_free (void)
jsp_early_error_free (void)
{
STACK_FREE (size_t_stack);
STACK_FREE (props);

View File

@ -13,12 +13,12 @@
* limitations under the License.
*/
#ifndef SYNTAX_ERRORS_H
#define SYNTAX_ERRORS_H
#ifndef JSP_EARLY_ERROR_H
#define JSP_EARLY_ERROR_H
#include "jrt-libc-includes.h"
#include "opcodes-dumper.h"
#include "lexer.h"
#include "opcodes-dumper.h"
#ifndef JERRY_NDEBUG
#define PARSE_ERROR_PRINT_PLACE(TYPE, LOCUS) do { \
@ -32,33 +32,25 @@
printf ("^\n"); \
printf ("%s: Ln %lu, Col %lu: ", TYPE, (unsigned long) (line + 1), (unsigned long) (column + 1)); \
} while (0)
#define PARSE_ERROR(MESSAGE, LOCUS) do { \
#define PARSE_ERROR(type, MESSAGE, LOCUS) do { \
locus __loc = LOCUS; \
PARSE_ERROR_PRINT_PLACE ("ERROR", __loc); \
printf ("%s\n", MESSAGE); \
syntax_raise_error (); \
jsp_early_error_raise_error (type); \
} while (0)
#define PARSE_ERROR_VARG(MESSAGE, LOCUS, ...) do { \
#define PARSE_ERROR_VARG(type, MESSAGE, LOCUS, ...) do { \
locus __loc = LOCUS; \
PARSE_ERROR_PRINT_PLACE ("ERROR", __loc); \
printf (MESSAGE, __VA_ARGS__); \
printf ("\n"); \
syntax_raise_error (); \
} while (0)
#define PARSE_SORRY(MESSAGE, LOCUS) do { \
PARSE_ERROR_PRINT_PLACE ("SORRY, Unimplemented", LOCUS); \
printf ("%s\n", MESSAGE); \
JERRY_UNIMPLEMENTED ("Unimplemented parser feature."); \
jsp_early_error_raise_error (type); \
} while (0)
#else /* JERRY_NDEBUG */
#define PARSE_ERROR(MESSAGE, LOCUS) do { \
syntax_raise_error (); \
#define PARSE_ERROR(type, MESSAGE, LOCUS) do { \
jsp_early_error_raise_error (type); \
} while (0)
#define PARSE_ERROR_VARG(MESSAGE, LOCUS, ...) do { \
syntax_raise_error (); \
} while (0)
#define PARSE_SORRY(MESSAGE, LOCUS) do { \
JERRY_UNIMPLEMENTED ("Unimplemented parser feature."); \
#define PARSE_ERROR_VARG(type, MESSAGE, LOCUS, ...) do { \
jsp_early_error_raise_error (type); \
} while (0)
#endif /* JERRY_NDEBUG */
@ -70,21 +62,32 @@ typedef enum __attr_packed___
VARG
} prop_type;
void syntax_init (void);
void syntax_free (void);
/**
* Early error types (ECMA-262 v5, 16)
*/
typedef enum
{
JSP_EARLY_ERROR__NO_ERROR, /** initializer value (indicates that no error occured) */
JSP_EARLY_ERROR_SYNTAX, /**< SyntaxError */
JSP_EARLY_ERROR_REFERENCE /**< ReferenceError */
} jsp_early_error_t;
void syntax_start_checking_of_prop_names (void);
void syntax_add_prop_name (operand, prop_type);
void syntax_check_for_duplication_of_prop_names (bool, locus);
void jsp_early_error_init (void);
void jsp_early_error_free (void);
void syntax_start_checking_of_vargs (void);
void syntax_add_varg (operand);
void syntax_check_for_eval_and_arguments_in_strict_mode (operand, bool, locus);
void syntax_check_for_syntax_errors_in_formal_param_list (bool, locus);
void jsp_early_error_start_checking_of_prop_names (void);
void jsp_early_error_add_prop_name (operand, prop_type);
void jsp_early_error_check_for_duplication_of_prop_names (bool, locus);
void syntax_check_delete (bool, locus);
void jsp_early_error_start_checking_of_vargs (void);
void jsp_early_error_add_varg (operand);
void jsp_early_error_check_for_eval_and_arguments_in_strict_mode (operand, bool, locus);
void jsp_early_error_check_for_syntax_errors_in_formal_param_list (bool, locus);
jmp_buf * syntax_get_syntax_error_longjmp_label (void);
void __attribute__((noreturn)) syntax_raise_error (void);
void jsp_early_error_check_delete (bool, locus);
#endif /* SYNTAX_ERRORS_H */
jmp_buf * jsp_early_error_get_early_error_longjmp_label (void);
void __attribute__((noreturn)) jsp_early_error_raise_error (jsp_early_error_t type);
jsp_early_error_t jsp_early_error_get_type (void);
#endif /* JSP_EARLY_ERROR_H */

View File

@ -21,7 +21,7 @@
#include "lit-char-helpers.h"
#include "lit-magic-strings.h"
#include "lit-strings.h"
#include "syntax-errors.h"
#include "jsp-early-error.h"
static token saved_token, prev_token, sent_token, empty_token;
@ -460,7 +460,7 @@ lexer_transform_escape_sequences (const jerry_api_char_t *source_str_p, /**< str
}
else
{
PARSE_ERROR ("Illegal escape sequence", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Illegal escape sequence", token_start_pos);
}
} /* lexer_transform_escape_sequences */
@ -774,7 +774,7 @@ lexer_parse_identifier_or_keyword (void)
if (!is_correct_identifier_name)
{
PARSE_ERROR ("Illegal identifier name", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Illegal identifier name", lit_utf8_iterator_get_pos (&src_iter));
}
const lit_utf8_size_t charset_size = TOK_SIZE ();
@ -853,7 +853,7 @@ lexer_parse_number (void)
c = LA (0);
if (!lit_char_is_hex_digit (c))
{
PARSE_ERROR ("Invalid HexIntegerLiteral", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Invalid HexIntegerLiteral", lit_utf8_iterator_get_pos (&src_iter));
}
do
@ -865,7 +865,9 @@ lexer_parse_number (void)
if (lexer_is_char_can_be_identifier_start (c))
{
PARSE_ERROR ("Identifier just after integer literal", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX,
"Identifier just after integer literal",
lit_utf8_iterator_get_pos (&src_iter));
}
tok_length = (size_t) (TOK_SIZE ());
@ -913,7 +915,8 @@ lexer_parse_number (void)
{
if (is_exp)
{
PARSE_ERROR ("Numeric literal shall not contain more than exponential marker ('e' or 'E')",
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX,
"Numeric literal shall not contain more than exponential marker ('e' or 'E')",
lit_utf8_iterator_get_pos (&src_iter));
}
else
@ -934,7 +937,8 @@ lexer_parse_number (void)
{
if (lexer_is_char_can_be_identifier_start (c))
{
PARSE_ERROR ("Numeric literal shall not contain non-numeric characters",
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX,
"Numeric literal shall not contain non-numeric characters",
lit_utf8_iterator_get_pos (&src_iter));
}
@ -963,7 +967,7 @@ lexer_parse_number (void)
/* Octal integer literals */
if (strict_mode)
{
PARSE_ERROR ("Octal integer literals are not allowed in strict mode", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Octal integer literals are not allowed in strict mode", token_start_pos);
}
else
{
@ -1029,11 +1033,11 @@ lexer_parse_string (void)
if (c == LIT_CHAR_NULL)
{
PARSE_ERROR ("Unclosed string", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unclosed string", token_start_pos);
}
else if (lit_char_is_line_terminator (c))
{
PARSE_ERROR ("String literal shall not contain newline character", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "String literal shall not contain newline character", token_start_pos);
}
else if (c == LIT_CHAR_BACKSLASH)
{
@ -1097,18 +1101,20 @@ lexer_parse_regexp (void)
if (c == LIT_CHAR_NULL)
{
PARSE_ERROR ("Unclosed string", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unclosed string", token_start_pos);
}
else if (lit_char_is_line_terminator (c))
{
PARSE_ERROR ("RegExp literal shall not contain newline character", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "RegExp literal shall not contain newline character", token_start_pos);
}
else if (c == LIT_CHAR_BACKSLASH)
{
consume_char ();
if (lit_char_is_line_terminator (LA (0)))
{
PARSE_ERROR ("RegExp literal backslash sequence should not contain newline character", token_start_pos);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX,
"RegExp literal backslash sequence should not contain newline character",
token_start_pos);
}
}
else if (c == LIT_CHAR_LEFT_SQUARE)
@ -1195,7 +1201,7 @@ lexer_parse_comment (void)
}
else if (c == LIT_CHAR_NULL)
{
PARSE_ERROR ("Unclosed multiline comment", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unclosed multiline comment", lit_utf8_iterator_get_pos (&src_iter));
}
}
@ -1461,7 +1467,7 @@ lexer_parse_token (void)
}
}
PARSE_ERROR ("Illegal character", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Illegal character", lit_utf8_iterator_get_pos (&src_iter));
} /* lexer_parse_token */
token
@ -1490,7 +1496,7 @@ lexer_next_token (void)
if (prev_token.type == TOK_EOF
&& sent_token.type == TOK_EOF)
{
PARSE_ERROR ("Unexpected EOF", lit_utf8_iterator_get_pos (&src_iter));
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unexpected EOF", lit_utf8_iterator_get_pos (&src_iter));
}
prev_token = sent_token;
@ -1829,7 +1835,7 @@ lexer_init (const jerry_api_char_t *source, /**< script source */
if (!lit_is_utf8_string_valid (source, (lit_utf8_size_t) source_size))
{
PARSE_ERROR ("Invalid source encoding", LIT_ITERATOR_POS_ZERO);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Invalid source encoding", LIT_ITERATOR_POS_ZERO);
}
src_iter = lit_utf8_iterator_create (source, (lit_utf8_size_t) source_size);

View File

@ -17,7 +17,7 @@
#include "serializer.h"
#include "stack.h"
#include "syntax-errors.h"
#include "jsp-early-error.h"
#include "opcodes-native-call.h"
static idx_t temp_name, max_temp_name;
@ -140,7 +140,7 @@ next_temp_name (void)
* FIXME:
* Implement mechanism, allowing reusage of register variables
*/
PARSE_ERROR ("Not enough register variables", LIT_ITERATOR_POS_ZERO);
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Not enough register variables", LIT_ITERATOR_POS_ZERO);
}
if (max_temp_name < next_reg)
@ -1463,7 +1463,7 @@ dump_delete (operand res, operand op, bool is_strict, locus loc)
|| lit->get_type () == LIT_MAGIC_STR_T
|| lit->get_type () == LIT_MAGIC_STR_EX_T)
{
syntax_check_delete (is_strict, loc);
jsp_early_error_check_delete (is_strict, loc);
switch (res.type)
{
case OPERAND_LITERAL:

View File

@ -27,7 +27,7 @@
#include "scopes-tree.h"
#include "serializer.h"
#include "stack.h"
#include "syntax-errors.h"
#include "jsp-early-error.h"
#include "vm.h"
/**
@ -58,9 +58,8 @@ enum
};
STATIC_STACK (scopes, scopes_tree)
#define EMIT_ERROR(MESSAGE) PARSE_ERROR(MESSAGE, tok.loc)
#define EMIT_SORRY(MESSAGE) PARSE_SORRY(MESSAGE, tok.loc)
#define EMIT_ERROR_VARG(MESSAGE, ...) PARSE_ERROR_VARG(MESSAGE, tok.loc, __VA_ARGS__)
#define EMIT_ERROR(type, MESSAGE) PARSE_ERROR(type, MESSAGE, tok.loc)
#define EMIT_ERROR_VARG(type, MESSAGE, ...) PARSE_ERROR_VARG(type, MESSAGE, tok.loc, __VA_ARGS__)
#define OPCODE_IS(OP, ID) (OP.op_idx == __op__idx_##ID)
@ -107,7 +106,7 @@ assert_keyword (keyword kw)
{
if (!token_is (TOK_KEYWORD) || token_data () != kw)
{
EMIT_ERROR_VARG ("Expected keyword '%s'", lexer_keyword_to_string (kw));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Expected keyword '%s'", lexer_keyword_to_string (kw));
JERRY_UNREACHABLE ();
}
}
@ -123,7 +122,7 @@ current_token_must_be (token_type tt)
{
if (!token_is (tt))
{
EMIT_ERROR_VARG ("Expected '%s' token", lexer_token_type_to_string (tt));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Expected '%s' token", lexer_token_type_to_string (tt));
}
}
@ -143,7 +142,7 @@ next_token_must_be (token_type tt)
skip_token ();
if (!token_is (tt))
{
EMIT_ERROR_VARG ("Expected '%s' token", lexer_token_type_to_string (tt));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Expected '%s' token", lexer_token_type_to_string (tt));
}
}
@ -153,7 +152,7 @@ token_after_newlines_must_be (token_type tt)
skip_newlines ();
if (!token_is (tt))
{
EMIT_ERROR_VARG ("Expected '%s' token", lexer_token_type_to_string (tt));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Expected '%s' token", lexer_token_type_to_string (tt));
}
}
@ -163,7 +162,7 @@ token_after_newlines_must_be_keyword (keyword kw)
skip_newlines ();
if (!is_keyword (kw))
{
EMIT_ERROR_VARG ("Expected keyword '%s'", lexer_keyword_to_string (kw));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Expected keyword '%s'", lexer_keyword_to_string (kw));
}
}
@ -263,7 +262,7 @@ jsp_find_next_token_before_the_locus (token_type token_to_find, /**< token to se
}
else if (token_is (TOK_CLOSE_BRACE))
{
EMIT_ERROR ("Unmatched } brace");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unmatched } brace");
}
}
@ -325,7 +324,7 @@ parse_property_name (void)
}
default:
{
EMIT_ERROR_VARG ("Wrong property name type: %s", lexer_token_type_to_string (tok.type));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Wrong property name type: %s", lexer_token_type_to_string (tok.type));
}
}
}
@ -341,7 +340,7 @@ parse_property_name_and_value (void)
skip_newlines ();
const operand value = parse_assignment_expression (true);
dump_prop_name_and_value (name, value);
syntax_add_prop_name (name, PROP_DATA);
jsp_early_error_add_prop_name (name, PROP_DATA);
}
/* property_assignment
@ -386,7 +385,7 @@ parse_property_assignment (void)
bool is_outer_scope_strict = is_strict_mode ();
const operand name = parse_property_name ();
syntax_add_prop_name (name, is_setter ? PROP_SET : PROP_GET);
jsp_early_error_add_prop_name (name, is_setter ? PROP_SET : PROP_GET);
skip_newlines ();
const operand func = parse_argument_list (VARG_FUNC_EXPR, empty_operand (), NULL, NULL);
@ -533,7 +532,7 @@ parse_argument_list (varg_list_type vlt, operand obj, uint8_t *args_count, opera
current_token_must_be (TOK_OPEN_BRACE);
close_tt = TOK_CLOSE_BRACE;
dump_varg_header_for_rewrite (vlt, obj);
syntax_start_checking_of_prop_names ();
jsp_early_error_start_checking_of_prop_names ();
break;
}
}
@ -550,8 +549,8 @@ parse_argument_list (varg_list_type vlt, operand obj, uint8_t *args_count, opera
{
current_token_must_be (TOK_NAME);
op = literal_operand (token_data_as_lit_cp ());
syntax_add_varg (op);
syntax_check_for_eval_and_arguments_in_strict_mode (op, is_strict_mode (), tok.loc);
jsp_early_error_add_varg (op);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (op, is_strict_mode (), tok.loc);
dump_varg (op);
skip_newlines ();
}
@ -622,7 +621,7 @@ parse_argument_list (varg_list_type vlt, operand obj, uint8_t *args_count, opera
}
case VARG_OBJ_DECL:
{
syntax_check_for_duplication_of_prop_names (is_strict_mode (), tok.loc);
jsp_early_error_check_for_duplication_of_prop_names (is_strict_mode (), tok.loc);
res = rewrite_varg_header_set_args_count (args_num);
break;
}
@ -649,7 +648,7 @@ parse_function_declaration (void)
token_after_newlines_must_be (TOK_NAME);
const operand name = literal_operand (token_data_as_lit_cp ());
syntax_check_for_eval_and_arguments_in_strict_mode (name, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (name, is_strict_mode (), tok.loc);
skip_newlines ();
STACK_PUSH (scopes, scopes_tree_init (STACK_TOP (scopes)));
@ -657,7 +656,7 @@ parse_function_declaration (void)
scopes_tree_set_strict_mode (STACK_TOP (scopes), scopes_tree_strict_mode (STACK_HEAD (scopes, 2)));
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));
syntax_start_checking_of_vargs ();
jsp_early_error_start_checking_of_vargs ();
parse_argument_list (VARG_FUNC_DECL, name, NULL, NULL);
dump_function_end_for_rewrite ();
@ -677,7 +676,7 @@ parse_function_declaration (void)
inside_function = was_in_function;
syntax_check_for_syntax_errors_in_formal_param_list (is_strict_mode (), tok.loc);
jsp_early_error_check_for_syntax_errors_in_formal_param_list (is_strict_mode (), tok.loc);
STACK_DROP (scopes, 1);
serializer_set_scope (STACK_TOP (scopes));
@ -700,13 +699,13 @@ parse_function_expression (void)
bool is_outer_scope_strict = is_strict_mode ();
syntax_start_checking_of_vargs ();
jsp_early_error_start_checking_of_vargs ();
skip_newlines ();
if (token_is (TOK_NAME))
{
const operand name = literal_operand (token_data_as_lit_cp ());
syntax_check_for_eval_and_arguments_in_strict_mode (name, is_outer_scope_strict, tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (name, is_outer_scope_strict, tok.loc);
skip_newlines ();
res = parse_argument_list (VARG_FUNC_EXPR, name, NULL, NULL);
@ -740,7 +739,7 @@ parse_function_expression (void)
inside_function = was_in_function;
syntax_check_for_syntax_errors_in_formal_param_list (is_strict_mode (), tok.loc);
jsp_early_error_check_for_syntax_errors_in_formal_param_list (is_strict_mode (), tok.loc);
scopes_tree_set_strict_mode (STACK_TOP (scopes), is_outer_scope_strict);
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));
@ -787,7 +786,7 @@ parse_literal (void)
case TOK_SMALL_INT: return dump_smallint_assignment_res ((idx_t) token_data ());
default:
{
EMIT_ERROR ("Expected literal");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Expected literal");
}
}
}
@ -833,7 +832,7 @@ parse_primary_expression (void)
}
default:
{
EMIT_ERROR_VARG ("Unknown token %s", lexer_token_type_to_string (tok.type));
EMIT_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX, "Unknown token %s", lexer_token_type_to_string (tok.type));
}
}
}
@ -914,7 +913,7 @@ parse_member_expression (operand *this_arg, operand *prop_gl)
(lit_utf8_size_t) strlen (s));
if (lit == NULL)
{
EMIT_ERROR ("Expected identifier");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Expected identifier");
}
prop = dump_string_assignment_res (lit_cpointer_t::compress (lit));
}
@ -929,7 +928,7 @@ parse_member_expression (operand *this_arg, operand *prop_gl)
}
else
{
EMIT_ERROR ("Expected identifier");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Expected identifier");
}
}
skip_newlines ();
@ -1054,7 +1053,7 @@ parse_postfix_expression (operand *out_this_arg_gl_p, /**< out: if expression ev
skip_token ();
if (token_is (TOK_DOUBLE_PLUS))
{
syntax_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
const operand res = dump_post_increment_res (expr);
if (!operand_is_empty (this_arg) && !operand_is_empty (prop))
@ -1065,7 +1064,7 @@ parse_postfix_expression (operand *out_this_arg_gl_p, /**< out: if expression ev
}
else if (token_is (TOK_DOUBLE_MINUS))
{
syntax_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
const operand res = dump_post_decrement_res (expr);
if (!operand_is_empty (this_arg) && !operand_is_empty (prop))
@ -1106,7 +1105,7 @@ parse_unary_expression (operand *this_arg_gl, operand *prop_gl)
{
skip_newlines ();
expr = parse_unary_expression (&this_arg, &prop);
syntax_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
expr = dump_pre_increment_res (expr);
if (!operand_is_empty (this_arg) && !operand_is_empty (prop))
{
@ -1118,7 +1117,7 @@ parse_unary_expression (operand *this_arg_gl, operand *prop_gl)
{
skip_newlines ();
expr = parse_unary_expression (&this_arg, &prop);
syntax_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
expr = dump_pre_decrement_res (expr);
if (!operand_is_empty (this_arg) && !operand_is_empty (prop))
{
@ -1682,7 +1681,7 @@ parse_assignment_expression (bool in_allowed)
|| tt == TOK_XOR_EQ
|| tt == TOK_OR_EQ)
{
syntax_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (expr, is_strict_mode (), tok.loc);
skip_newlines ();
start_dumping_assignment_expression ();
const operand assign_expr = parse_assignment_expression (in_allowed);
@ -1903,7 +1902,7 @@ jsp_parse_for_statement (jsp_label_t *outermost_stmt_label_p, /**< outermost (fi
for_body_statement_loc,
true))
{
EMIT_ERROR ("Invalid for statement");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Invalid for statement");
}
current_token_must_be (TOK_SEMICOLON);
@ -2071,7 +2070,7 @@ jsp_parse_for_in_statement (jsp_label_t *outermost_stmt_label_p, /**< outermost
}
else
{
EMIT_ERROR ("Invalid for statement");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Invalid for statement");
}
}
@ -2315,7 +2314,7 @@ parse_with_statement (void)
assert_keyword (KW_WITH);
if (is_strict_mode ())
{
EMIT_ERROR ("'with' expression is not allowed in strict mode.");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "'with' expression is not allowed in strict mode.");
}
const operand expr = parse_expression_inside_parens ();
@ -2386,7 +2385,7 @@ parse_switch_statement (void)
{
if (was_default)
{
EMIT_ERROR ("Duplication of 'default' clause");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Duplication of 'default' clause");
}
was_default = true;
token_after_newlines_must_be (TOK_COLON);
@ -2461,7 +2460,7 @@ parse_catch_clause (void)
token_after_newlines_must_be (TOK_OPEN_PAREN);
token_after_newlines_must_be (TOK_NAME);
const operand exception = literal_operand (token_data_as_lit_cp ());
syntax_check_for_eval_and_arguments_in_strict_mode (exception, is_strict_mode (), tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (exception, is_strict_mode (), tok.loc);
token_after_newlines_must_be (TOK_CLOSE_PAREN);
dump_catch_for_rewrite (exception);
@ -2532,7 +2531,7 @@ parse_try_statement (void)
}
else
{
EMIT_ERROR ("Expected either 'catch' or 'finally' token");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Expected either 'catch' or 'finally' token");
}
dump_end_try_catch_finally ();
@ -2558,7 +2557,7 @@ insert_semicolon (void)
}
else if (!token_is (TOK_SEMICOLON) && !token_is (TOK_EOF))
{
EMIT_ERROR ("Expected either ';' or newline token");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Expected either ';' or newline token");
}
}
@ -2736,7 +2735,7 @@ parse_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) lab
if (label_p == NULL)
{
EMIT_ERROR ("Label not found");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Label not found");
}
}
else if (is_break)
@ -2747,7 +2746,7 @@ parse_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) lab
if (label_p == NULL)
{
EMIT_ERROR ("No corresponding statement for the break");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "No corresponding statement for the break");
}
}
else
@ -2760,7 +2759,7 @@ parse_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) lab
if (label_p == NULL)
{
EMIT_ERROR ("No corresponding statement for the continue");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "No corresponding statement for the continue");
}
}
@ -2774,7 +2773,7 @@ parse_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) lab
{
if (!inside_function)
{
EMIT_ERROR ("Return is illegal");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Return is illegal");
}
skip_token ();
@ -2825,7 +2824,7 @@ parse_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) lab
jsp_label_t *label_p = jsp_label_find (JSP_LABEL_TYPE_NAMED, temp, NULL);
if (label_p != NULL)
{
EMIT_ERROR ("Label is duplicated");
EMIT_ERROR (JSP_EARLY_ERROR_SYNTAX, "Label is duplicated");
}
jsp_label_t label;
@ -2981,9 +2980,9 @@ preparse_scope (bool is_global)
{
if (!var_declared (token_data_as_lit_cp ()))
{
syntax_check_for_eval_and_arguments_in_strict_mode (literal_operand (token_data_as_lit_cp ()),
is_strict_mode (),
tok.loc);
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (literal_operand (token_data_as_lit_cp ()),
is_strict_mode (),
tok.loc);
dump_variable_declaration (token_data_as_lit_cp ());
}
}
@ -3142,7 +3141,7 @@ parse_source_element_list (bool is_global) /**< flag indicating if we are parsin
* @return true - if parse finished successfully (no SyntaxError was raised);
* false - otherwise.
*/
static bool
static jsp_status_t
parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer */
size_t source_size, /**< source code size in bytes */
bool in_function, /**< flag indicating if we are parsing body of a function */
@ -3161,22 +3160,22 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
volatile bool is_parse_finished = false;
#endif /* !JERRY_NDEBUG */
bool is_syntax_correct;
jsp_status_t status;
jsp_mm_init ();
jsp_label_init ();
serializer_set_show_opcodes (parser_show_opcodes);
dumper_init ();
syntax_init ();
jsp_early_error_init ();
STACK_INIT (scopes);
STACK_PUSH (scopes, scopes_tree_init (NULL));
serializer_set_scope (STACK_TOP (scopes));
scopes_tree_set_strict_mode (STACK_TOP (scopes), is_strict);
jmp_buf *syntax_error_label_p = syntax_get_syntax_error_longjmp_label ();
int r = setjmp (*syntax_error_label_p);
jmp_buf *jsp_early_error_label_p = jsp_early_error_get_early_error_longjmp_label ();
int r = setjmp (*jsp_early_error_label_p);
if (r == 0)
{
@ -3211,7 +3210,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
is_parse_finished = true;
#endif /* !JERRY_NDEBUG */
syntax_free ();
jsp_early_error_free ();
*out_opcodes_p = serializer_merge_scopes_into_bytecode ();
@ -3222,7 +3221,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
STACK_DROP (scopes, 1);
STACK_FREE (scopes);
is_syntax_correct = true;
status = JSP_STATUS_OK;
}
else
{
@ -3237,13 +3236,24 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
jsp_label_remove_all_labels ();
jsp_mm_free_all ();
is_syntax_correct = false;
jsp_early_error_t type = jsp_early_error_get_type ();
if (type == JSP_EARLY_ERROR_SYNTAX)
{
status = JSP_STATUS_SYNTAX_ERROR;
}
else
{
JERRY_ASSERT (type == JSP_EARLY_ERROR_REFERENCE);
status = JSP_STATUS_REFERENCE_ERROR;
}
}
jsp_label_finalize ();
jsp_mm_finalize ();
return is_syntax_correct;
return status;
} /* parser_parse_program */
/**
@ -3252,7 +3262,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
* @return true - if parse finished successfully (no SyntaxError were raised);
* false - otherwise.
*/
bool
jsp_status_t
parser_parse_script (const jerry_api_char_t *source, /**< source script */
size_t source_size, /**< source script size it bytes */
const opcode_t **opcodes_p) /**< out: generated byte-code array
@ -3267,7 +3277,7 @@ parser_parse_script (const jerry_api_char_t *source, /**< source script */
* @return true - if parse finished successfully (no SyntaxError were raised);
* false - otherwise.
*/
bool
jsp_status_t
parser_parse_eval (const jerry_api_char_t *source, /**< string passed to eval() */
size_t source_size, /**< string size in bytes */
bool is_strict, /**< flag, indicating whether eval is called
@ -3288,7 +3298,7 @@ parser_parse_eval (const jerry_api_char_t *source, /**< string passed to eval()
* @return true - if parse finished successfully (no SyntaxError were raised);
* false - otherwise.
*/
bool
jsp_status_t
parser_parse_new_function (const jerry_api_char_t **params, /**< array of arguments of new Function (p1, p2, ..., pn,
* body) call */
const size_t *params_size, /**< sizes of arguments strings */

View File

@ -18,9 +18,19 @@
#include "jrt.h"
/**
* Parser completion status
*/
typedef enum
{
JSP_STATUS_OK, /**< parse finished successfully, no early errors occured */
JSP_STATUS_SYNTAX_ERROR, /**< SyntaxError early error occured */
JSP_STATUS_REFERENCE_ERROR /**< ReferenceError early error occured */
} jsp_status_t;
void parser_set_show_opcodes (bool);
bool parser_parse_script (const jerry_api_char_t *, size_t, const opcode_t **);
bool parser_parse_eval (const jerry_api_char_t *, size_t, bool, const opcode_t **);
bool parser_parse_new_function (const jerry_api_char_t **, const size_t *, size_t, const opcode_t **);
jsp_status_t parser_parse_script (const jerry_api_char_t *, size_t, const opcode_t **);
jsp_status_t parser_parse_eval (const jerry_api_char_t *, size_t, bool, const opcode_t **);
jsp_status_t parser_parse_new_function (const jerry_api_char_t **, const size_t *, size_t, const opcode_t **);
#endif /* PARSER_H */

View File

@ -20,7 +20,6 @@
#include "jrt-libc-includes.h"
#include "lit-char-helpers.h"
#include "re-parser.h"
#include "syntax-errors.h"
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN

View File

@ -72,7 +72,7 @@ main (int __attr_unused___ argc,
TEST_INIT ();
const opcode_t *opcodes_p;
bool is_syntax_correct;
jsp_status_t parse_status;
mem_init ();
@ -81,9 +81,9 @@ main (int __attr_unused___ argc,
serializer_init ();
parser_set_show_opcodes (true);
is_syntax_correct = parser_parse_script ((jerry_api_char_t *) program1, strlen (program1), &opcodes_p);
parse_status = parser_parse_script ((jerry_api_char_t *) program1, strlen (program1), &opcodes_p);
JERRY_ASSERT (is_syntax_correct && opcodes_p != NULL);
JERRY_ASSERT (parse_status == JSP_STATUS_OK && opcodes_p != NULL);
opcode_t opcodes[] =
{
@ -107,9 +107,9 @@ main (int __attr_unused___ argc,
serializer_init ();
parser_set_show_opcodes (true);
is_syntax_correct = parser_parse_script ((jerry_api_char_t *) program2, strlen (program2), &opcodes_p);
parse_status = parser_parse_script ((jerry_api_char_t *) program2, strlen (program2), &opcodes_p);
JERRY_ASSERT (!is_syntax_correct && opcodes_p == NULL);
JERRY_ASSERT (parse_status == JSP_STATUS_SYNTAX_ERROR && opcodes_p == NULL);
serializer_free ();