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 JS_SCANNER_H 17425bb815Sopenharmony_ci#define JS_SCANNER_H 18425bb815Sopenharmony_ci 19425bb815Sopenharmony_ci/** \addtogroup parser Parser 20425bb815Sopenharmony_ci * @{ 21425bb815Sopenharmony_ci * 22425bb815Sopenharmony_ci * \addtogroup jsparser JavaScript 23425bb815Sopenharmony_ci * @{ 24425bb815Sopenharmony_ci * 25425bb815Sopenharmony_ci * \addtogroup jsparser_scanner Scanner 26425bb815Sopenharmony_ci * @{ 27425bb815Sopenharmony_ci */ 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_ci/** 30425bb815Sopenharmony_ci * Allowed types for scanner_info_t structures. 31425bb815Sopenharmony_ci */ 32425bb815Sopenharmony_citypedef enum 33425bb815Sopenharmony_ci{ 34425bb815Sopenharmony_ci SCANNER_TYPE_END, /**< mark the last info block */ 35425bb815Sopenharmony_ci SCANNER_TYPE_END_ARGUMENTS, /**< mark the end of function arguments 36425bb815Sopenharmony_ci * (only present if a function script is parsed) */ 37425bb815Sopenharmony_ci SCANNER_TYPE_FUNCTION, /**< declarations in a function */ 38425bb815Sopenharmony_ci SCANNER_TYPE_BLOCK, /**< declarations in a code block (usually enclosed in {}) */ 39425bb815Sopenharmony_ci SCANNER_TYPE_WHILE, /**< while statement */ 40425bb815Sopenharmony_ci SCANNER_TYPE_FOR, /**< for statement */ 41425bb815Sopenharmony_ci SCANNER_TYPE_FOR_IN, /**< for-in statement */ 42425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 43425bb815Sopenharmony_ci SCANNER_TYPE_FOR_OF, /**< for-of statement */ 44425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 45425bb815Sopenharmony_ci SCANNER_TYPE_SWITCH, /**< switch statement */ 46425bb815Sopenharmony_ci SCANNER_TYPE_CASE, /**< case statement */ 47425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 48425bb815Sopenharmony_ci SCANNER_TYPE_INITIALIZER, /**< destructuring binding or assignment pattern with initializer */ 49425bb815Sopenharmony_ci SCANNER_TYPE_CLASS_CONSTRUCTOR, /**< class constructor */ 50425bb815Sopenharmony_ci SCANNER_TYPE_LET_EXPRESSION, /**< let expression */ 51425bb815Sopenharmony_ci SCANNER_TYPE_ERR_REDECLARED, /**< syntax error: a variable is redeclared */ 52425bb815Sopenharmony_ci SCANNER_TYPE_ERR_ASYNC_FUNCTION, /**< an invalid async function follows */ 53425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 54425bb815Sopenharmony_ci} scanner_info_type_t; 55425bb815Sopenharmony_ci 56425bb815Sopenharmony_ci/** 57425bb815Sopenharmony_ci * Source code location which can be used to change the position of parsing. 58425bb815Sopenharmony_ci */ 59425bb815Sopenharmony_citypedef struct 60425bb815Sopenharmony_ci{ 61425bb815Sopenharmony_ci const uint8_t *source_p; /**< next source byte */ 62425bb815Sopenharmony_ci parser_line_counter_t line; /**< token start line */ 63425bb815Sopenharmony_ci parser_line_counter_t column; /**< token start column */ 64425bb815Sopenharmony_ci} scanner_location_t; 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_ci/** 67425bb815Sopenharmony_ci * Scanner info blocks which provides information for the parser. 68425bb815Sopenharmony_ci */ 69425bb815Sopenharmony_citypedef struct scanner_info_t 70425bb815Sopenharmony_ci{ 71425bb815Sopenharmony_ci struct scanner_info_t *next_p; /**< next info structure */ 72425bb815Sopenharmony_ci const uint8_t *source_p; /**< triggering position of this scanner info */ 73425bb815Sopenharmony_ci uint8_t type; /**< type of the scanner info */ 74425bb815Sopenharmony_ci uint8_t u8_arg; /**< custom 8-bit value */ 75425bb815Sopenharmony_ci uint16_t u16_arg; /**< custom 16-bit value */ 76425bb815Sopenharmony_ci} scanner_info_t; 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci/** 79425bb815Sopenharmony_ci * Scanner info extended with a location. 80425bb815Sopenharmony_ci */ 81425bb815Sopenharmony_citypedef struct 82425bb815Sopenharmony_ci{ 83425bb815Sopenharmony_ci scanner_info_t info; /**< header */ 84425bb815Sopenharmony_ci scanner_location_t location; /**< location */ 85425bb815Sopenharmony_ci} scanner_location_info_t; 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci/** 88425bb815Sopenharmony_ci * Scanner info for "for" statements. 89425bb815Sopenharmony_ci */ 90425bb815Sopenharmony_citypedef struct 91425bb815Sopenharmony_ci{ 92425bb815Sopenharmony_ci scanner_info_t info; /**< header */ 93425bb815Sopenharmony_ci scanner_location_t expression_location; /**< location of expression start */ 94425bb815Sopenharmony_ci scanner_location_t end_location; /**< location of expression end */ 95425bb815Sopenharmony_ci} scanner_for_info_t; 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ci/** 98425bb815Sopenharmony_ci * Case statement list for scanner_switch_info_t structure. 99425bb815Sopenharmony_ci */ 100425bb815Sopenharmony_citypedef struct scanner_case_info_t 101425bb815Sopenharmony_ci{ 102425bb815Sopenharmony_ci struct scanner_case_info_t *next_p; /**< next case statement info */ 103425bb815Sopenharmony_ci scanner_location_t location; /**< location of case statement */ 104425bb815Sopenharmony_ci} scanner_case_info_t; 105425bb815Sopenharmony_ci 106425bb815Sopenharmony_ci/** 107425bb815Sopenharmony_ci * Scanner info for "switch" statements. 108425bb815Sopenharmony_ci */ 109425bb815Sopenharmony_citypedef struct 110425bb815Sopenharmony_ci{ 111425bb815Sopenharmony_ci scanner_info_t info; /**< header */ 112425bb815Sopenharmony_ci scanner_case_info_t *case_p; /**< list of switch cases */ 113425bb815Sopenharmony_ci} scanner_switch_info_t; 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci/* 116425bb815Sopenharmony_ci * Description of compressed streams. 117425bb815Sopenharmony_ci * 118425bb815Sopenharmony_ci * The stream is a sequence of commands which encoded as bytes. The first byte 119425bb815Sopenharmony_ci * contains the type of the command (see scanner_function_compressed_stream_types_t). 120425bb815Sopenharmony_ci * 121425bb815Sopenharmony_ci * The variable declaration commands has two arguments: 122425bb815Sopenharmony_ci * - The first represents the length of the declared identifier 123425bb815Sopenharmony_ci * - The second contains the relative distance from the end of the previous declaration 124425bb815Sopenharmony_ci * Usually the distance is between 1 and 255, and represented as a single byte 125425bb815Sopenharmony_ci * Distances between -256 and 65535 are encoded as two bytes 126425bb815Sopenharmony_ci * Larger distances are encoded as pointers 127425bb815Sopenharmony_ci */ 128425bb815Sopenharmony_ci 129425bb815Sopenharmony_ci/** 130425bb815Sopenharmony_ci * Constants for compressed streams. 131425bb815Sopenharmony_ci */ 132425bb815Sopenharmony_citypedef enum 133425bb815Sopenharmony_ci{ 134425bb815Sopenharmony_ci SCANNER_STREAM_UINT16_DIFF = (1 << 7), /**< relative distance is between -256 and 65535 */ 135425bb815Sopenharmony_ci SCANNER_STREAM_HAS_ESCAPE = (1 << 6), /**< binding has escape */ 136425bb815Sopenharmony_ci SCANNER_STREAM_NO_REG = (1 << 5), /**< binding cannot be stored in register */ 137425bb815Sopenharmony_ci SCANNER_STREAM_EARLY_CREATE = (1 << 4), /**< binding must be created with ECMA_VALUE_UNINITIALIZED */ 138425bb815Sopenharmony_ci /* Update SCANNER_STREAM_TYPE_MASK macro if more bits are added. */ 139425bb815Sopenharmony_ci} scanner_compressed_stream_flags_t; 140425bb815Sopenharmony_ci 141425bb815Sopenharmony_ci/** 142425bb815Sopenharmony_ci * Types for compressed streams. 143425bb815Sopenharmony_ci */ 144425bb815Sopenharmony_citypedef enum 145425bb815Sopenharmony_ci{ 146425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_END, /**< end of scanner data */ 147425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_HOLE, /**< no name is assigned to this argument */ 148425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_VAR, /**< var declaration */ 149425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 150425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_LET, /**< let declaration */ 151425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_CONST, /**< const declaration */ 152425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_LOCAL, /**< local declaration (e.g. catch block) */ 153425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 154425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015_MODULE_SYSTEM) 155425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_IMPORT, /**< module import */ 156425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ 157425bb815Sopenharmony_ci /* The next four types must be in this order (see SCANNER_STREAM_TYPE_IS_ARG). */ 158425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_ARG, /**< argument declaration */ 159425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 160425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_ARG_VAR, /**< argument declaration which is later copied 161425bb815Sopenharmony_ci * into a variable declared by var statement */ 162425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_DESTRUCTURED_ARG, /**< destructuring argument declaration */ 163425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_VAR, /**< destructuring argument declaration which is later 164425bb815Sopenharmony_ci * copied into a variable declared by var statement */ 165425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 166425bb815Sopenharmony_ci /* Function types should be at the end. See the SCANNER_STREAM_TYPE_IS_FUNCTION macro. */ 167425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_ARG_FUNC, /**< argument declaration which 168425bb815Sopenharmony_ci * is later initialized with a function */ 169425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 170425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_FUNC, /**< destructuring argument declaration which 171425bb815Sopenharmony_ci * is later initialized with a function */ 172425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 173425bb815Sopenharmony_ci SCANNER_STREAM_TYPE_FUNC, /**< function declaration */ 174425bb815Sopenharmony_ci} scanner_compressed_stream_types_t; 175425bb815Sopenharmony_ci 176425bb815Sopenharmony_ci/** 177425bb815Sopenharmony_ci * Mask for decoding the type from the compressed stream. 178425bb815Sopenharmony_ci */ 179425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_MASK 0xf 180425bb815Sopenharmony_ci 181425bb815Sopenharmony_ci/** 182425bb815Sopenharmony_ci * Checks whether the decoded type represents a function declaration. 183425bb815Sopenharmony_ci */ 184425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_IS_FUNCTION(type) ((type) >= SCANNER_STREAM_TYPE_ARG_FUNC) 185425bb815Sopenharmony_ci 186425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 187425bb815Sopenharmony_ci 188425bb815Sopenharmony_ci/** 189425bb815Sopenharmony_ci * Checks whether the decoded type represents a function argument. 190425bb815Sopenharmony_ci */ 191425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_IS_ARG(type) \ 192425bb815Sopenharmony_ci ((type) >= SCANNER_STREAM_TYPE_ARG && (type) <= SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_VAR) 193425bb815Sopenharmony_ci 194425bb815Sopenharmony_ci/** 195425bb815Sopenharmony_ci * Checks whether the decoded type represents both a function argument and a function declaration. 196425bb815Sopenharmony_ci */ 197425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_IS_ARG_FUNC(type) \ 198425bb815Sopenharmony_ci ((type) == SCANNER_STREAM_TYPE_ARG_FUNC || (type) == SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_FUNC) 199425bb815Sopenharmony_ci 200425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_ES2015) */ 201425bb815Sopenharmony_ci 202425bb815Sopenharmony_ci/** 203425bb815Sopenharmony_ci * Checks whether the decoded type represents a function argument. 204425bb815Sopenharmony_ci */ 205425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_IS_ARG(type) ((type) == SCANNER_STREAM_TYPE_ARG) 206425bb815Sopenharmony_ci 207425bb815Sopenharmony_ci/** 208425bb815Sopenharmony_ci * Checks whether the decoded type represents both a function argument and a function declaration. 209425bb815Sopenharmony_ci */ 210425bb815Sopenharmony_ci#define SCANNER_STREAM_TYPE_IS_ARG_FUNC(type) ((type) == SCANNER_STREAM_TYPE_ARG_FUNC) 211425bb815Sopenharmony_ci 212425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 213425bb815Sopenharmony_ci 214425bb815Sopenharmony_ci/** 215425bb815Sopenharmony_ci * Constants for u8_arg flags in scanner_function_info_t. 216425bb815Sopenharmony_ci */ 217425bb815Sopenharmony_citypedef enum 218425bb815Sopenharmony_ci{ 219425bb815Sopenharmony_ci SCANNER_FUNCTION_ARGUMENTS_NEEDED = (1 << 0), /**< arguments object needs to be created */ 220425bb815Sopenharmony_ci SCANNER_FUNCTION_MAPPED_ARGUMENTS = (1 << 1), /**< arguments object should be mapped */ 221425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 222425bb815Sopenharmony_ci SCANNER_FUNCTION_LEXICAL_ENV_NEEDED = (1 << 2), /**< lexical environment is needed for the function body */ 223425bb815Sopenharmony_ci SCANNER_FUNCTION_STATEMENT = (1 << 3), /**< function is function statement (not arrow expression) 224425bb815Sopenharmony_ci * this flag must be combined with the type of function (e.g. async) */ 225425bb815Sopenharmony_ci SCANNER_FUNCTION_ASYNC = (1 << 4), /**< function is async function */ 226425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 227425bb815Sopenharmony_ci} scanner_function_flags_t; 228425bb815Sopenharmony_ci 229425bb815Sopenharmony_ci/** 230425bb815Sopenharmony_ci * Option bits for scanner_create_variables function. 231425bb815Sopenharmony_ci */ 232425bb815Sopenharmony_citypedef enum 233425bb815Sopenharmony_ci{ 234425bb815Sopenharmony_ci SCANNER_CREATE_VARS_NO_OPTS = 0, /**< no options */ 235425bb815Sopenharmony_ci SCANNER_CREATE_VARS_IS_SCRIPT = (1 << 0), /**< create variables for script or direct eval */ 236425bb815Sopenharmony_ci SCANNER_CREATE_VARS_IS_FUNCTION_ARGS = (1 << 1), /**< create variables for function arguments */ 237425bb815Sopenharmony_ci SCANNER_CREATE_VARS_IS_FUNCTION_BODY = (1 << 2), /**< create variables for function body */ 238425bb815Sopenharmony_ci} scanner_create_variables_flags_t; 239425bb815Sopenharmony_ci 240425bb815Sopenharmony_ci/** 241425bb815Sopenharmony_ci * @} 242425bb815Sopenharmony_ci * @} 243425bb815Sopenharmony_ci * @} 244425bb815Sopenharmony_ci */ 245425bb815Sopenharmony_ci 246425bb815Sopenharmony_ci#endif /* !JS_SCANNER_H */ 247