Skip to content

Commit 48d012f

Browse files
Introducing 'try' and 'with' nestings in parser.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 5c012a1 commit 48d012f

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

jerry-core/parser/js/parser.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,33 @@
2929
#include "opcodes-dumper.h"
3030
#include "serializer.h"
3131

32-
#define NESTING_ITERATIONAL 1
33-
#define NESTING_SWITCH 2
34-
#define NESTING_FUNCTION 3
32+
/**
33+
* Nesting types
34+
*
35+
* Note:
36+
* Nesting is an element, describing classes of syntax blocks, handled by parser.
37+
*
38+
* Nestings are pushed to the nestings stack upon entering them, and popped upon leaving.
39+
*
40+
* The top-most nesting, if any, describes the inner-most syntax block of specified type,
41+
* currently reached by parser.
42+
*/
43+
typedef enum
44+
{
45+
NESTING_ITERATIONAL, /**< an iterational (for, for-in, while, do-while) statement */
46+
NESTING_SWITCH, /**< switch-case block */
47+
NESTING_FUNCTION, /**< function */
48+
NESTING_TRY, /**< try-catch-finally block */
49+
NESTING_WITH /**< with block */
50+
} nesting_t;
3551

3652
static token tok;
3753

3854
enum
3955
{
4056
nestings_global_size
4157
};
42-
STATIC_STACK (nestings, uint8_t)
58+
STATIC_STACK (nestings, nesting_t)
4359

4460
enum
4561
{
@@ -56,7 +72,12 @@ STATIC_STACK (scopes, scopes_tree)
5672
: I == NESTING_ITERATIONAL \
5773
? "iterational" \
5874
: I == NESTING_SWITCH \
59-
? "switch" : "unknown")
75+
? "switch" \
76+
: I == NESTING_TRY \
77+
? "try" \
78+
: I == NESTING_WITH \
79+
? "with" \
80+
: "unknown")
6081

6182
#define OPCODE_IS(OP, ID) (OP.op_idx == __op__idx_##ID)
6283

@@ -69,18 +90,24 @@ static void process_keyword_names (void);
6990
static void skip_braces (void);
7091
static void skip_parens (void);
7192

93+
/**
94+
* Push a nesting to the nesting stack, so setting new current nesting
95+
*/
7296
static void
73-
push_nesting (uint8_t nesting_type)
97+
push_nesting (nesting_t nesting_type) /**< type of new nesting */
7498
{
7599
STACK_PUSH (nestings, nesting_type);
76-
}
100+
} /* push_nesting */
77101

102+
/**
103+
* Restore nesting from nestings stack
104+
*/
78105
static void
79-
pop_nesting (uint8_t nesting_type)
106+
pop_nesting (nesting_t nesting_type) /**< type of current nesting */
80107
{
81108
JERRY_ASSERT (STACK_HEAD (nestings, 1) == nesting_type);
82109
STACK_DROP (nestings, 1);
83-
}
110+
} /* pop_nesting */
84111

85112
static void
86113
must_be_inside_but_not_in (uint8_t not_in, uint8_t insides_count, ...)
@@ -1992,10 +2019,15 @@ parse_with_statement (void)
19922019
EMIT_ERROR ("'with' expression is not allowed in strict mode.");
19932020
}
19942021
const operand expr = parse_expression_inside_parens ();
2022+
2023+
push_nesting (NESTING_WITH);
2024+
19952025
dump_with (expr);
19962026
skip_newlines ();
19972027
parse_statement ();
19982028
dump_with_end ();
2029+
2030+
pop_nesting (NESTING_WITH);
19992031
}
20002032

20012033
static void
@@ -2165,6 +2197,8 @@ parse_try_statement (void)
21652197
{
21662198
assert_keyword (KW_TRY);
21672199

2200+
push_nesting (NESTING_TRY);
2201+
21682202
dump_try_for_rewrite ();
21692203

21702204
token_after_newlines_must_be (TOK_OPEN_BRACE);
@@ -2199,6 +2233,8 @@ parse_try_statement (void)
21992233
}
22002234

22012235
dump_end_try_catch_finally ();
2236+
2237+
pop_nesting (NESTING_TRY);
22022238
}
22032239

22042240
static void
@@ -2346,13 +2382,24 @@ parse_statement (void)
23462382
}
23472383
if (is_keyword (KW_CONTINUE))
23482384
{
2385+
must_be_inside_but_not_in (NESTING_FUNCTION,
2386+
4,
2387+
NESTING_ITERATIONAL,
2388+
NESTING_TRY,
2389+
NESTING_WITH);
2390+
23492391
must_be_inside_but_not_in (NESTING_FUNCTION, 1, NESTING_ITERATIONAL);
23502392
dump_continue_for_rewrite ();
23512393
return;
23522394
}
23532395
if (is_keyword (KW_BREAK))
23542396
{
2355-
must_be_inside_but_not_in (NESTING_FUNCTION, 2, NESTING_ITERATIONAL, NESTING_SWITCH);
2397+
must_be_inside_but_not_in (NESTING_FUNCTION,
2398+
4,
2399+
NESTING_ITERATIONAL,
2400+
NESTING_SWITCH,
2401+
NESTING_TRY,
2402+
NESTING_WITH);
23562403
dump_break_for_rewrite ();
23572404
return;
23582405
}

0 commit comments

Comments
 (0)