Skip to content

Commit 67c708f

Browse files
Introduce jsp_skip_braces function for skiping blocks, surrounded with braces.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 47ceaad commit 67c708f

File tree

1 file changed

+55
-69
lines changed

1 file changed

+55
-69
lines changed

jerry-core/parser/js/parser.cpp

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ static void parse_statement (jsp_label_t *outermost_stmt_label_p);
6767
static operand parse_assignment_expression (bool);
6868
static void parse_source_element_list (bool);
6969
static operand parse_argument_list (varg_list_type, operand, uint8_t *, operand *);
70-
static void skip_braces (void);
71-
static void skip_parens (void);
7270

7371
static bool
7472
token_is (token_type tt)
@@ -173,6 +171,54 @@ is_strict_mode (void)
173171
return scopes_tree_strict_mode (STACK_TOP (scopes));
174172
}
175173

174+
/**
175+
* Skip block, defined with braces of specified type
176+
*
177+
* Note:
178+
* Missing corresponding brace is considered a syntax error
179+
*
180+
* Note:
181+
* current token should at the beginning of the scope when the routine is called
182+
*/
183+
static void
184+
jsp_skip_braces (token_type brace_type) /**< type of the opening brace */
185+
{
186+
current_token_must_be (brace_type);
187+
188+
token_type closing_bracket_type;
189+
190+
if (brace_type == TOK_OPEN_PAREN)
191+
{
192+
closing_bracket_type = TOK_CLOSE_PAREN;
193+
}
194+
else if (brace_type == TOK_OPEN_BRACE)
195+
{
196+
closing_bracket_type = TOK_CLOSE_BRACE;
197+
}
198+
else
199+
{
200+
JERRY_ASSERT (brace_type == TOK_OPEN_SQUARE);
201+
closing_bracket_type = TOK_CLOSE_SQUARE;
202+
}
203+
204+
skip_newlines ();
205+
206+
while (!token_is (closing_bracket_type)
207+
&& !token_is (TOK_EOF))
208+
{
209+
if (token_is (TOK_OPEN_PAREN)
210+
|| token_is (TOK_OPEN_BRACE)
211+
|| token_is (TOK_OPEN_SQUARE))
212+
{
213+
jsp_skip_braces (tok.type);
214+
}
215+
216+
skip_newlines ();
217+
}
218+
219+
current_token_must_be (closing_bracket_type);
220+
} /* jsp_skip_braces */
221+
176222
/* property_name
177223
: Identifier
178224
| Keyword
@@ -1964,7 +2010,7 @@ parse_while_statement (jsp_label_t *outermost_stmt_label_p) /**< outermost (firs
19642010

19652011
token_after_newlines_must_be (TOK_OPEN_PAREN);
19662012
const locus cond_loc = tok.loc;
1967-
skip_parens ();
2013+
jsp_skip_braces (TOK_OPEN_PAREN);
19682014

19692015
dump_jump_to_end_for_rewrite ();
19702016

@@ -2020,7 +2066,7 @@ skip_case_clause_body (void)
20202066
{
20212067
if (token_is (TOK_OPEN_BRACE))
20222068
{
2023-
skip_braces ();
2069+
jsp_skip_braces (TOK_OPEN_BRACE);
20242070
}
20252071
skip_newlines ();
20262072
}
@@ -2565,73 +2611,13 @@ skip_optional_name_and_parens (void)
25652611
}
25662612
}
25672613

2568-
static void
2569-
skip_braces (void)
2570-
{
2571-
current_token_must_be (TOK_OPEN_BRACE);
2572-
2573-
uint8_t nesting_level = 1;
2574-
while (nesting_level > 0)
2575-
{
2576-
skip_newlines ();
2577-
if (token_is (TOK_OPEN_BRACE))
2578-
{
2579-
nesting_level++;
2580-
}
2581-
else if (token_is (TOK_CLOSE_BRACE))
2582-
{
2583-
nesting_level--;
2584-
}
2585-
}
2586-
}
2587-
25882614
static void
25892615
skip_function (void)
25902616
{
25912617
skip_newlines ();
25922618
skip_optional_name_and_parens ();
25932619
skip_newlines ();
2594-
skip_braces ();
2595-
}
2596-
2597-
static void
2598-
skip_squares (void)
2599-
{
2600-
current_token_must_be (TOK_OPEN_SQUARE);
2601-
2602-
uint8_t nesting_level = 1;
2603-
while (nesting_level > 0)
2604-
{
2605-
skip_newlines ();
2606-
if (token_is (TOK_OPEN_SQUARE))
2607-
{
2608-
nesting_level++;
2609-
}
2610-
else if (token_is (TOK_CLOSE_SQUARE))
2611-
{
2612-
nesting_level--;
2613-
}
2614-
}
2615-
}
2616-
2617-
static void
2618-
skip_parens (void)
2619-
{
2620-
current_token_must_be (TOK_OPEN_PAREN);
2621-
2622-
uint8_t nesting_level = 1;
2623-
while (nesting_level > 0)
2624-
{
2625-
skip_newlines ();
2626-
if (token_is (TOK_OPEN_PAREN))
2627-
{
2628-
nesting_level++;
2629-
}
2630-
else if (token_is (TOK_CLOSE_PAREN))
2631-
{
2632-
nesting_level--;
2633-
}
2634-
}
2620+
jsp_skip_braces (TOK_OPEN_BRACE);
26352621
}
26362622

26372623
static bool
@@ -2670,15 +2656,15 @@ preparse_var_decls (void)
26702656
}
26712657
else if (token_is (TOK_OPEN_BRACE))
26722658
{
2673-
skip_braces ();
2659+
jsp_skip_braces (TOK_OPEN_BRACE);
26742660
}
26752661
else if (token_is (TOK_OPEN_SQUARE))
26762662
{
2677-
skip_squares ();
2663+
jsp_skip_braces (TOK_OPEN_SQUARE);
26782664
}
26792665
else if (token_is (TOK_OPEN_PAREN))
26802666
{
2681-
skip_parens ();
2667+
jsp_skip_braces (TOK_OPEN_PAREN);
26822668
}
26832669
skip_token ();
26842670
}
@@ -2739,7 +2725,7 @@ preparse_scope (bool is_global)
27392725
}
27402726
else if (token_is (TOK_OPEN_BRACE))
27412727
{
2742-
skip_braces ();
2728+
jsp_skip_braces (TOK_OPEN_BRACE);
27432729
}
27442730
else
27452731
{

0 commit comments

Comments
 (0)