1 /* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef JS_SCANNER_H 17 #define JS_SCANNER_H 18 19 /** \addtogroup parser Parser 20 * @{ 21 * 22 * \addtogroup jsparser JavaScript 23 * @{ 24 * 25 * \addtogroup jsparser_scanner Scanner 26 * @{ 27 */ 28 29 /** 30 * Allowed types for scanner_info_t structures. 31 */ 32 typedef enum 33 { 34 SCANNER_TYPE_END, /**< mark the last info block */ 35 SCANNER_TYPE_END_ARGUMENTS, /**< mark the end of function arguments 36 * (only present if a function script is parsed) */ 37 SCANNER_TYPE_FUNCTION, /**< declarations in a function */ 38 SCANNER_TYPE_BLOCK, /**< declarations in a code block (usually enclosed in {}) */ 39 SCANNER_TYPE_WHILE, /**< while statement */ 40 SCANNER_TYPE_FOR, /**< for statement */ 41 SCANNER_TYPE_FOR_IN, /**< for-in statement */ 42 #if ENABLED (JERRY_ES2015) 43 SCANNER_TYPE_FOR_OF, /**< for-of statement */ 44 #endif /* ENABLED (JERRY_ES2015) */ 45 SCANNER_TYPE_SWITCH, /**< switch statement */ 46 SCANNER_TYPE_CASE, /**< case statement */ 47 #if ENABLED (JERRY_ES2015) 48 SCANNER_TYPE_INITIALIZER, /**< destructuring binding or assignment pattern with initializer */ 49 SCANNER_TYPE_CLASS_CONSTRUCTOR, /**< class constructor */ 50 SCANNER_TYPE_LET_EXPRESSION, /**< let expression */ 51 SCANNER_TYPE_ERR_REDECLARED, /**< syntax error: a variable is redeclared */ 52 SCANNER_TYPE_ERR_ASYNC_FUNCTION, /**< an invalid async function follows */ 53 #endif /* ENABLED (JERRY_ES2015) */ 54 } scanner_info_type_t; 55 56 /** 57 * Source code location which can be used to change the position of parsing. 58 */ 59 typedef struct 60 { 61 const uint8_t *source_p; /**< next source byte */ 62 parser_line_counter_t line; /**< token start line */ 63 parser_line_counter_t column; /**< token start column */ 64 } scanner_location_t; 65 66 /** 67 * Scanner info blocks which provides information for the parser. 68 */ 69 typedef struct scanner_info_t 70 { 71 struct scanner_info_t *next_p; /**< next info structure */ 72 const uint8_t *source_p; /**< triggering position of this scanner info */ 73 uint8_t type; /**< type of the scanner info */ 74 uint8_t u8_arg; /**< custom 8-bit value */ 75 uint16_t u16_arg; /**< custom 16-bit value */ 76 } scanner_info_t; 77 78 /** 79 * Scanner info extended with a location. 80 */ 81 typedef struct 82 { 83 scanner_info_t info; /**< header */ 84 scanner_location_t location; /**< location */ 85 } scanner_location_info_t; 86 87 /** 88 * Scanner info for "for" statements. 89 */ 90 typedef struct 91 { 92 scanner_info_t info; /**< header */ 93 scanner_location_t expression_location; /**< location of expression start */ 94 scanner_location_t end_location; /**< location of expression end */ 95 } scanner_for_info_t; 96 97 /** 98 * Case statement list for scanner_switch_info_t structure. 99 */ 100 typedef struct scanner_case_info_t 101 { 102 struct scanner_case_info_t *next_p; /**< next case statement info */ 103 scanner_location_t location; /**< location of case statement */ 104 } scanner_case_info_t; 105 106 /** 107 * Scanner info for "switch" statements. 108 */ 109 typedef struct 110 { 111 scanner_info_t info; /**< header */ 112 scanner_case_info_t *case_p; /**< list of switch cases */ 113 } scanner_switch_info_t; 114 115 /* 116 * Description of compressed streams. 117 * 118 * The stream is a sequence of commands which encoded as bytes. The first byte 119 * contains the type of the command (see scanner_function_compressed_stream_types_t). 120 * 121 * The variable declaration commands has two arguments: 122 * - The first represents the length of the declared identifier 123 * - The second contains the relative distance from the end of the previous declaration 124 * Usually the distance is between 1 and 255, and represented as a single byte 125 * Distances between -256 and 65535 are encoded as two bytes 126 * Larger distances are encoded as pointers 127 */ 128 129 /** 130 * Constants for compressed streams. 131 */ 132 typedef enum 133 { 134 SCANNER_STREAM_UINT16_DIFF = (1 << 7), /**< relative distance is between -256 and 65535 */ 135 SCANNER_STREAM_HAS_ESCAPE = (1 << 6), /**< binding has escape */ 136 SCANNER_STREAM_NO_REG = (1 << 5), /**< binding cannot be stored in register */ 137 SCANNER_STREAM_EARLY_CREATE = (1 << 4), /**< binding must be created with ECMA_VALUE_UNINITIALIZED */ 138 /* Update SCANNER_STREAM_TYPE_MASK macro if more bits are added. */ 139 } scanner_compressed_stream_flags_t; 140 141 /** 142 * Types for compressed streams. 143 */ 144 typedef enum 145 { 146 SCANNER_STREAM_TYPE_END, /**< end of scanner data */ 147 SCANNER_STREAM_TYPE_HOLE, /**< no name is assigned to this argument */ 148 SCANNER_STREAM_TYPE_VAR, /**< var declaration */ 149 #if ENABLED (JERRY_ES2015) 150 SCANNER_STREAM_TYPE_LET, /**< let declaration */ 151 SCANNER_STREAM_TYPE_CONST, /**< const declaration */ 152 SCANNER_STREAM_TYPE_LOCAL, /**< local declaration (e.g. catch block) */ 153 #endif /* ENABLED (JERRY_ES2015) */ 154 #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) 155 SCANNER_STREAM_TYPE_IMPORT, /**< module import */ 156 #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ 157 /* The next four types must be in this order (see SCANNER_STREAM_TYPE_IS_ARG). */ 158 SCANNER_STREAM_TYPE_ARG, /**< argument declaration */ 159 #if ENABLED (JERRY_ES2015) 160 SCANNER_STREAM_TYPE_ARG_VAR, /**< argument declaration which is later copied 161 * into a variable declared by var statement */ 162 SCANNER_STREAM_TYPE_DESTRUCTURED_ARG, /**< destructuring argument declaration */ 163 SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_VAR, /**< destructuring argument declaration which is later 164 * copied into a variable declared by var statement */ 165 #endif /* ENABLED (JERRY_ES2015) */ 166 /* Function types should be at the end. See the SCANNER_STREAM_TYPE_IS_FUNCTION macro. */ 167 SCANNER_STREAM_TYPE_ARG_FUNC, /**< argument declaration which 168 * is later initialized with a function */ 169 #if ENABLED (JERRY_ES2015) 170 SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_FUNC, /**< destructuring argument declaration which 171 * is later initialized with a function */ 172 #endif /* ENABLED (JERRY_ES2015) */ 173 SCANNER_STREAM_TYPE_FUNC, /**< function declaration */ 174 } scanner_compressed_stream_types_t; 175 176 /** 177 * Mask for decoding the type from the compressed stream. 178 */ 179 #define SCANNER_STREAM_TYPE_MASK 0xf 180 181 /** 182 * Checks whether the decoded type represents a function declaration. 183 */ 184 #define SCANNER_STREAM_TYPE_IS_FUNCTION(type) ((type) >= SCANNER_STREAM_TYPE_ARG_FUNC) 185 186 #if ENABLED (JERRY_ES2015) 187 188 /** 189 * Checks whether the decoded type represents a function argument. 190 */ 191 #define SCANNER_STREAM_TYPE_IS_ARG(type) \ 192 ((type) >= SCANNER_STREAM_TYPE_ARG && (type) <= SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_VAR) 193 194 /** 195 * Checks whether the decoded type represents both a function argument and a function declaration. 196 */ 197 #define SCANNER_STREAM_TYPE_IS_ARG_FUNC(type) \ 198 ((type) == SCANNER_STREAM_TYPE_ARG_FUNC || (type) == SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_FUNC) 199 200 #else /* !ENABLED (JERRY_ES2015) */ 201 202 /** 203 * Checks whether the decoded type represents a function argument. 204 */ 205 #define SCANNER_STREAM_TYPE_IS_ARG(type) ((type) == SCANNER_STREAM_TYPE_ARG) 206 207 /** 208 * Checks whether the decoded type represents both a function argument and a function declaration. 209 */ 210 #define SCANNER_STREAM_TYPE_IS_ARG_FUNC(type) ((type) == SCANNER_STREAM_TYPE_ARG_FUNC) 211 212 #endif /* ENABLED (JERRY_ES2015) */ 213 214 /** 215 * Constants for u8_arg flags in scanner_function_info_t. 216 */ 217 typedef enum 218 { 219 SCANNER_FUNCTION_ARGUMENTS_NEEDED = (1 << 0), /**< arguments object needs to be created */ 220 SCANNER_FUNCTION_MAPPED_ARGUMENTS = (1 << 1), /**< arguments object should be mapped */ 221 #if ENABLED (JERRY_ES2015) 222 SCANNER_FUNCTION_LEXICAL_ENV_NEEDED = (1 << 2), /**< lexical environment is needed for the function body */ 223 SCANNER_FUNCTION_STATEMENT = (1 << 3), /**< function is function statement (not arrow expression) 224 * this flag must be combined with the type of function (e.g. async) */ 225 SCANNER_FUNCTION_ASYNC = (1 << 4), /**< function is async function */ 226 #endif /* ENABLED (JERRY_ES2015) */ 227 } scanner_function_flags_t; 228 229 /** 230 * Option bits for scanner_create_variables function. 231 */ 232 typedef enum 233 { 234 SCANNER_CREATE_VARS_NO_OPTS = 0, /**< no options */ 235 SCANNER_CREATE_VARS_IS_SCRIPT = (1 << 0), /**< create variables for script or direct eval */ 236 SCANNER_CREATE_VARS_IS_FUNCTION_ARGS = (1 << 1), /**< create variables for function arguments */ 237 SCANNER_CREATE_VARS_IS_FUNCTION_BODY = (1 << 2), /**< create variables for function body */ 238 } scanner_create_variables_flags_t; 239 240 /** 241 * @} 242 * @} 243 * @} 244 */ 245 246 #endif /* !JS_SCANNER_H */ 247