1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef COMMON_H
17425bb815Sopenharmony_ci#define COMMON_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#include <stdio.h>
20425bb815Sopenharmony_ci#include <stdlib.h>
21425bb815Sopenharmony_ci#include <string.h>
22425bb815Sopenharmony_ci#include <inttypes.h>
23425bb815Sopenharmony_ci#include <setjmp.h>
24425bb815Sopenharmony_ci
25425bb815Sopenharmony_ci/** \addtogroup parser Parser
26425bb815Sopenharmony_ci * @{
27425bb815Sopenharmony_ci *
28425bb815Sopenharmony_ci * \addtogroup jsparser JavaScript
29425bb815Sopenharmony_ci * @{
30425bb815Sopenharmony_ci *
31425bb815Sopenharmony_ci * \addtogroup jsparser_utils Utility
32425bb815Sopenharmony_ci * @{
33425bb815Sopenharmony_ci */
34425bb815Sopenharmony_ci
35425bb815Sopenharmony_ci#include "config.h"
36425bb815Sopenharmony_ci#include "ecma-globals.h"
37425bb815Sopenharmony_ci#include "ecma-regexp-object.h"
38425bb815Sopenharmony_ci#include "jmem.h"
39425bb815Sopenharmony_ci
40425bb815Sopenharmony_ci/* Immediate management. */
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_ci/**
43425bb815Sopenharmony_ci * Literal types.
44425bb815Sopenharmony_ci *
45425bb815Sopenharmony_ci * The LEXER_UNUSED_LITERAL type is internal and
46425bb815Sopenharmony_ci * used for various purposes.
47425bb815Sopenharmony_ci */
48425bb815Sopenharmony_citypedef enum
49425bb815Sopenharmony_ci{
50425bb815Sopenharmony_ci  /* The LEXER_IS_IDENT_OR_STRING macro must be updated if the order is changed. */
51425bb815Sopenharmony_ci  LEXER_IDENT_LITERAL = 0,          /**< identifier literal */
52425bb815Sopenharmony_ci  LEXER_STRING_LITERAL = 1,         /**< string literal */
53425bb815Sopenharmony_ci  LEXER_NUMBER_LITERAL = 2,         /**< number literal */
54425bb815Sopenharmony_ci  LEXER_FUNCTION_LITERAL = 3,       /**< function literal */
55425bb815Sopenharmony_ci  LEXER_REGEXP_LITERAL = 4,         /**< regexp literal */
56425bb815Sopenharmony_ci  LEXER_UNUSED_LITERAL = 5,         /**< unused literal, can only be
57425bb815Sopenharmony_ci                                         used by the byte code generator. */
58425bb815Sopenharmony_ci  LEXER_NEW_IDENT_LITERAL = 6,      /**< new local variable, can only be
59425bb815Sopenharmony_ci                                         used by the byte code generator. */
60425bb815Sopenharmony_ci} lexer_literal_type_t;
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci/**
63425bb815Sopenharmony_ci * Checks whether the literal type is identifier or string.
64425bb815Sopenharmony_ci */
65425bb815Sopenharmony_ci#define LEXER_IS_IDENT_OR_STRING(literal_type) ((literal_type) <= LEXER_STRING_LITERAL)
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci/**
68425bb815Sopenharmony_ci * Flag bits for status_flags member of lexer_literal_t.
69425bb815Sopenharmony_ci */
70425bb815Sopenharmony_citypedef enum
71425bb815Sopenharmony_ci{
72425bb815Sopenharmony_ci  LEXER_FLAG_USED = (1 << 0), /**< this local identifier needs to be stored in the constant pool */
73425bb815Sopenharmony_ci  LEXER_FLAG_FUNCTION_ARGUMENT = (1 << 1), /**< this local identifier is a function argument */
74425bb815Sopenharmony_ci  LEXER_FLAG_SOURCE_PTR = (1 << 2), /**< the literal is directly referenced in the source code
75425bb815Sopenharmony_ci                                     *   (no need to allocate memory) */
76425bb815Sopenharmony_ci  LEXER_FLAG_LATE_INIT = (1 << 3), /**< initialize this variable after the byte code is freed */
77425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015)
78425bb815Sopenharmony_ci  LEXER_FLAG_GLOBAL = (1 << 4), /**< this local identifier is not a let or const declaration */
79425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */
80425bb815Sopenharmony_ci} lexer_literal_status_flags_t;
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci/**
83425bb815Sopenharmony_ci * Type of property length.
84425bb815Sopenharmony_ci */
85425bb815Sopenharmony_ci#if ENABLED (JERRY_CPOINTER_32_BIT)
86425bb815Sopenharmony_citypedef uint32_t prop_length_t;
87425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_CPOINTER_32_BIT) */
88425bb815Sopenharmony_citypedef uint16_t prop_length_t;
89425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_CPOINTER_32_BIT) */
90425bb815Sopenharmony_ci
91425bb815Sopenharmony_ci/**
92425bb815Sopenharmony_ci * Literal data.
93425bb815Sopenharmony_ci */
94425bb815Sopenharmony_citypedef struct
95425bb815Sopenharmony_ci{
96425bb815Sopenharmony_ci  union
97425bb815Sopenharmony_ci  {
98425bb815Sopenharmony_ci    ecma_value_t value;                  /**< literal value (not processed by the parser) */
99425bb815Sopenharmony_ci    const uint8_t *char_p;               /**< character value */
100425bb815Sopenharmony_ci    ecma_compiled_code_t *bytecode_p;    /**< compiled function or regexp pointer */
101425bb815Sopenharmony_ci    uint32_t source_data;                /**< encoded source literal */
102425bb815Sopenharmony_ci  } u;
103425bb815Sopenharmony_ci
104425bb815Sopenharmony_ci#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE)
105425bb815Sopenharmony_ci  struct
106425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
107425bb815Sopenharmony_ci  union
108425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
109425bb815Sopenharmony_ci  {
110425bb815Sopenharmony_ci    prop_length_t length;                /**< length of ident / string literal */
111425bb815Sopenharmony_ci    uint16_t index;                      /**< real index during post processing */
112425bb815Sopenharmony_ci  } prop;
113425bb815Sopenharmony_ci
114425bb815Sopenharmony_ci  uint8_t type;                          /**< type of the literal */
115425bb815Sopenharmony_ci  uint8_t status_flags;                  /**< status flags */
116425bb815Sopenharmony_ci} lexer_literal_t;
117425bb815Sopenharmony_ci
118425bb815Sopenharmony_civoid util_free_literal (lexer_literal_t *literal_p);
119425bb815Sopenharmony_ci
120425bb815Sopenharmony_ci#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE)
121425bb815Sopenharmony_civoid util_print_literal (lexer_literal_t *);
122425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
123425bb815Sopenharmony_ci
124425bb815Sopenharmony_ci/* TRY/CATCH block */
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci#define PARSER_TRY_CONTEXT(context_name) \
127425bb815Sopenharmony_ci  jmp_buf context_name
128425bb815Sopenharmony_ci
129425bb815Sopenharmony_ci#define PARSER_THROW(context_name) \
130425bb815Sopenharmony_ci  longjmp (context_name, 1);
131425bb815Sopenharmony_ci
132425bb815Sopenharmony_ci#define PARSER_TRY(context_name) \
133425bb815Sopenharmony_ci  { \
134425bb815Sopenharmony_ci    if (!setjmp (context_name)) \
135425bb815Sopenharmony_ci    { \
136425bb815Sopenharmony_ci
137425bb815Sopenharmony_ci#define PARSER_CATCH \
138425bb815Sopenharmony_ci    } \
139425bb815Sopenharmony_ci    else \
140425bb815Sopenharmony_ci    {
141425bb815Sopenharmony_ci
142425bb815Sopenharmony_ci#define PARSER_TRY_END \
143425bb815Sopenharmony_ci    } \
144425bb815Sopenharmony_ci  }
145425bb815Sopenharmony_ci
146425bb815Sopenharmony_ci/**
147425bb815Sopenharmony_ci * @}
148425bb815Sopenharmony_ci * @}
149425bb815Sopenharmony_ci * @}
150425bb815Sopenharmony_ci */
151425bb815Sopenharmony_ci
152425bb815Sopenharmony_ci#endif /* !COMMON_H */
153