diff --git a/jerry-core/parser/js/parser.cpp b/jerry-core/parser/js/parser.cpp index 3babf7d87e..2a9cf0f533 100644 --- a/jerry-core/parser/js/parser.cpp +++ b/jerry-core/parser/js/parser.cpp @@ -65,6 +65,7 @@ static void parse_statement (void); static operand parse_assignment_expression (bool); static void parse_source_element_list (bool); static operand parse_argument_list (varg_list_type, operand, uint8_t *, operand *); +static void process_keyword_names (void); static void skip_braces (void); static void skip_parens (void); @@ -2463,62 +2464,70 @@ skip_optional_name_and_parens (void) } } -static void -skip_braces (void) +static void process_keyword_names () { - current_token_must_be (TOK_OPEN_BRACE); - - uint8_t nesting_level = 1; - while (nesting_level > 0) + if (token_is (TOK_KEYWORD)) { + keyword kw = (keyword) token_data (); skip_newlines (); - if (token_is (TOK_OPEN_BRACE)) + if (token_is (TOK_COLON)) { - nesting_level++; + lexer_add_keyword_or_numeric_literal_if_not_present ( + create_literal_from_str_compute_len (lexer_keyword_to_string (kw))); } - else if (token_is (TOK_CLOSE_BRACE)) + else { - nesting_level--; + lexer_save_token (tok); } - else if (token_is (TOK_KEYWORD)) + } + else if (token_is (TOK_NAME)) + { + if (literal_equal_type_s (lexer_get_literal_by_id (token_data ()), "get") + || literal_equal_type_s (lexer_get_literal_by_id (token_data ()), "set")) { - keyword kw = (keyword) token_data (); skip_newlines (); - if (token_is (TOK_COLON)) - { - lexer_add_keyword_or_numeric_literal_if_not_present ( - create_literal_from_str_compute_len (lexer_keyword_to_string (kw))); - } - else - { - lexer_save_token (tok); - } - } - else if (token_is (TOK_NAME)) - { - if (literal_equal_type_s (lexer_get_literal_by_id (token_data ()), "get") - || literal_equal_type_s (lexer_get_literal_by_id (token_data ()), "set")) + if (token_is (TOK_KEYWORD)) { + keyword kw = (keyword) token_data (); skip_newlines (); - if (token_is (TOK_KEYWORD)) + if (token_is (TOK_OPEN_PAREN)) { - keyword kw = (keyword) token_data (); - skip_newlines (); - if (token_is (TOK_OPEN_PAREN)) - { - lexer_add_keyword_or_numeric_literal_if_not_present ( - create_literal_from_str_compute_len (lexer_keyword_to_string (kw))); - } - else - { - lexer_save_token (tok); - } + lexer_add_keyword_or_numeric_literal_if_not_present ( + create_literal_from_str_compute_len (lexer_keyword_to_string (kw))); } else { lexer_save_token (tok); } } + else + { + lexer_save_token (tok); + } + } + } +} + +static void +skip_braces (void) +{ + current_token_must_be (TOK_OPEN_BRACE); + + uint8_t nesting_level = 1; + while (nesting_level > 0) + { + skip_newlines (); + if (token_is (TOK_OPEN_BRACE)) + { + nesting_level++; + } + else if (token_is (TOK_CLOSE_BRACE)) + { + nesting_level--; + } + else + { + process_keyword_names (); } } } @@ -2649,9 +2658,18 @@ preparse_scope (bool is_global) dump_reg_var_decl_for_rewrite (); - while (!token_is (end_tt)) + size_t nesting_level = 0; + while (nesting_level > 0 || !token_is (end_tt)) { - if (is_keyword (KW_VAR)) + if (token_is (TOK_OPEN_BRACE)) + { + nesting_level++; + } + else if (token_is (TOK_CLOSE_BRACE)) + { + nesting_level--; + } + else if (is_keyword (KW_VAR)) { preparse_var_decls (); } @@ -2663,6 +2681,10 @@ preparse_scope (bool is_global) { skip_braces (); } + else + { + process_keyword_names (); + } skip_newlines (); } diff --git a/tests/jerry/var_decl.js b/tests/jerry/var_decl.js new file mode 100644 index 0000000000..021f24b0e4 --- /dev/null +++ b/tests/jerry/var_decl.js @@ -0,0 +1,19 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +{ + var y; +} +var x = y; +assert (x === undefined);