Reactos
at master 405 lines 8.6 kB view raw
1/* 2 * Copyright 2014 Jacek Caban for CodeWeavers 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19#pragma once 20 21typedef struct _source_elements_t source_elements_t; 22typedef struct _expression_t expression_t; 23typedef struct _statement_t statement_t; 24 25typedef struct { 26 BOOL is_num; 27 union { 28 BOOL b; 29 double n; 30 } u; 31} ccval_t; 32 33typedef struct _parser_ctx_t { 34 const WCHAR *begin; 35 const WCHAR *end; 36 const WCHAR *ptr; 37 38 script_ctx_t *script; 39 struct _compiler_ctx_t *compiler; 40 source_elements_t *source; 41 BOOL nl; 42 BOOL implicit_nl_semicolon; 43 BOOL is_html; 44 BOOL lexer_error; 45 HRESULT hres; 46 47 ccval_t ccval; 48 unsigned cc_if_depth; 49 50 heap_pool_t heap; 51} parser_ctx_t; 52 53HRESULT script_parse(script_ctx_t*,struct _compiler_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN; 54void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN; 55 56int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN; 57 58static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size) 59{ 60 return heap_pool_alloc(&ctx->heap, size); 61} 62 63static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size) 64{ 65 return heap_pool_alloc(&ctx->script->tmp_heap, size); 66} 67 68BOOL is_identifier_char(WCHAR) DECLSPEC_HIDDEN; 69BOOL unescape(WCHAR*,size_t*) DECLSPEC_HIDDEN; 70HRESULT parse_decimal(const WCHAR**,const WCHAR*,double*) DECLSPEC_HIDDEN; 71 72typedef enum { 73 LT_DOUBLE, 74 LT_STRING, 75 LT_BOOL, 76 LT_NULL, 77 LT_REGEXP 78}literal_type_t; 79 80typedef struct { 81 literal_type_t type; 82 union { 83 double dval; 84 jsstr_t *str; 85 BOOL bval; 86 struct { 87 jsstr_t *str; 88 DWORD flags; 89 } regexp; 90 } u; 91} literal_t; 92 93literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN; 94literal_t *new_boolean_literal(parser_ctx_t*,BOOL) DECLSPEC_HIDDEN; 95 96typedef struct _variable_declaration_t { 97 const WCHAR *identifier; 98 expression_t *expr; 99 100 struct _variable_declaration_t *next; 101 struct _variable_declaration_t *global_next; /* for compiler */ 102} variable_declaration_t; 103 104typedef enum { 105 STAT_BLOCK, 106 STAT_BREAK, 107 STAT_CONTINUE, 108 STAT_EMPTY, 109 STAT_EXPR, 110 STAT_FOR, 111 STAT_FORIN, 112 STAT_IF, 113 STAT_LABEL, 114 STAT_RETURN, 115 STAT_SWITCH, 116 STAT_THROW, 117 STAT_TRY, 118 STAT_VAR, 119 STAT_WHILE, 120 STAT_WITH 121} statement_type_t; 122 123struct _statement_t { 124 statement_type_t type; 125 statement_t *next; 126}; 127 128typedef struct { 129 statement_t stat; 130 statement_t *stat_list; 131} block_statement_t; 132 133typedef struct { 134 statement_t stat; 135 variable_declaration_t *variable_list; 136} var_statement_t; 137 138typedef struct { 139 statement_t stat; 140 expression_t *expr; 141} expression_statement_t; 142 143typedef struct { 144 statement_t stat; 145 expression_t *expr; 146 statement_t *if_stat; 147 statement_t *else_stat; 148} if_statement_t; 149 150typedef struct { 151 statement_t stat; 152 BOOL do_while; 153 expression_t *expr; 154 statement_t *statement; 155} while_statement_t; 156 157typedef struct { 158 statement_t stat; 159 variable_declaration_t *variable_list; 160 expression_t *begin_expr; 161 expression_t *expr; 162 expression_t *end_expr; 163 statement_t *statement; 164} for_statement_t; 165 166typedef struct { 167 statement_t stat; 168 variable_declaration_t *variable; 169 expression_t *expr; 170 expression_t *in_expr; 171 statement_t *statement; 172} forin_statement_t; 173 174typedef struct { 175 statement_t stat; 176 const WCHAR *identifier; 177} branch_statement_t; 178 179typedef struct { 180 statement_t stat; 181 expression_t *expr; 182 statement_t *statement; 183} with_statement_t; 184 185typedef struct { 186 statement_t stat; 187 const WCHAR *identifier; 188 statement_t *statement; 189} labelled_statement_t; 190 191typedef struct _case_clausule_t { 192 expression_t *expr; 193 statement_t *stat; 194 195 struct _case_clausule_t *next; 196} case_clausule_t; 197 198typedef struct { 199 statement_t stat; 200 expression_t *expr; 201 case_clausule_t *case_list; 202} switch_statement_t; 203 204typedef struct { 205 const WCHAR *identifier; 206 statement_t *statement; 207} catch_block_t; 208 209typedef struct { 210 statement_t stat; 211 statement_t *try_statement; 212 catch_block_t *catch_block; 213 statement_t *finally_statement; 214} try_statement_t; 215 216typedef enum { 217 EXPR_COMMA, 218 EXPR_OR, 219 EXPR_AND, 220 EXPR_BOR, 221 EXPR_BXOR, 222 EXPR_BAND, 223 EXPR_INSTANCEOF, 224 EXPR_IN, 225 EXPR_ADD, 226 EXPR_SUB, 227 EXPR_MUL, 228 EXPR_DIV, 229 EXPR_MOD, 230 EXPR_DELETE, 231 EXPR_VOID, 232 EXPR_TYPEOF, 233 EXPR_MINUS, 234 EXPR_PLUS, 235 EXPR_POSTINC, 236 EXPR_POSTDEC, 237 EXPR_PREINC, 238 EXPR_PREDEC, 239 EXPR_EQ, 240 EXPR_EQEQ, 241 EXPR_NOTEQ, 242 EXPR_NOTEQEQ, 243 EXPR_LESS, 244 EXPR_LESSEQ, 245 EXPR_GREATER, 246 EXPR_GREATEREQ, 247 EXPR_BITNEG, 248 EXPR_LOGNEG, 249 EXPR_LSHIFT, 250 EXPR_RSHIFT, 251 EXPR_RRSHIFT, 252 EXPR_ASSIGN, 253 EXPR_ASSIGNLSHIFT, 254 EXPR_ASSIGNRSHIFT, 255 EXPR_ASSIGNRRSHIFT, 256 EXPR_ASSIGNADD, 257 EXPR_ASSIGNSUB, 258 EXPR_ASSIGNMUL, 259 EXPR_ASSIGNDIV, 260 EXPR_ASSIGNMOD, 261 EXPR_ASSIGNAND, 262 EXPR_ASSIGNOR, 263 EXPR_ASSIGNXOR, 264 EXPR_COND, 265 EXPR_ARRAY, 266 EXPR_MEMBER, 267 EXPR_NEW, 268 EXPR_CALL, 269 EXPR_THIS, 270 EXPR_FUNC, 271 EXPR_IDENT, 272 EXPR_ARRAYLIT, 273 EXPR_PROPVAL, 274 EXPR_LITERAL 275} expression_type_t; 276 277struct _expression_t { 278 expression_type_t type; 279}; 280 281typedef struct _parameter_t { 282 const WCHAR *identifier; 283 struct _parameter_t *next; 284} parameter_t; 285 286struct _source_elements_t { 287 statement_t *statement; 288 statement_t *statement_tail; 289}; 290 291typedef struct _function_expression_t { 292 expression_t expr; 293 const WCHAR *identifier; 294 const WCHAR *event_target; 295 parameter_t *parameter_list; 296 source_elements_t *source_elements; 297 const WCHAR *src_str; 298 DWORD src_len; 299 unsigned func_id; 300 301 struct _function_expression_t *next; /* for compiler */ 302} function_expression_t; 303 304typedef struct { 305 expression_t expr; 306 expression_t *expression1; 307 expression_t *expression2; 308} binary_expression_t; 309 310typedef struct { 311 expression_t expr; 312 expression_t *expression; 313} unary_expression_t; 314 315typedef struct { 316 expression_t expr; 317 expression_t *expression; 318 expression_t *true_expression; 319 expression_t *false_expression; 320} conditional_expression_t; 321 322typedef struct { 323 expression_t expr; 324 expression_t *expression; 325 const WCHAR *identifier; 326} member_expression_t; 327 328typedef struct _argument_t { 329 expression_t *expr; 330 331 struct _argument_t *next; 332} argument_t; 333 334typedef struct { 335 expression_t expr; 336 expression_t *expression; 337 argument_t *argument_list; 338} call_expression_t; 339 340typedef struct { 341 expression_t expr; 342 const WCHAR *identifier; 343} identifier_expression_t; 344 345typedef struct { 346 expression_t expr; 347 literal_t *literal; 348} literal_expression_t; 349 350typedef struct _array_element_t { 351 int elision; 352 expression_t *expr; 353 354 struct _array_element_t *next; 355} array_element_t; 356 357typedef struct { 358 expression_t expr; 359 array_element_t *element_list; 360 int length; 361} array_literal_expression_t; 362 363typedef struct _property_definition_t { 364 unsigned type; 365 literal_t *name; 366 expression_t *value; 367 368 struct _property_definition_t *next; 369} property_definition_t; 370 371typedef struct { 372 expression_t expr; 373 property_definition_t *property_list; 374} property_value_expression_t; 375 376BOOL try_parse_ccval(parser_ctx_t*,ccval_t*) DECLSPEC_HIDDEN; 377BOOL parse_cc_expr(parser_ctx_t*) DECLSPEC_HIDDEN; 378 379static inline ccval_t ccval_num(double n) 380{ 381 ccval_t r; 382 r.is_num = TRUE; 383 r.u.n = n; 384 return r; 385} 386 387static inline ccval_t ccval_bool(BOOL b) 388{ 389 ccval_t r; 390 r.is_num = FALSE; 391 r.u.b = b; 392 return r; 393} 394 395static inline BOOL get_ccbool(ccval_t v) 396{ 397 return v.is_num ? v.u.n != 0 : v.u.b; 398} 399 400static inline double get_ccnum(ccval_t v) 401{ 402 return v.is_num ? v.u.n : v.u.b; 403} 404 405jsstr_t *compiler_alloc_string_len(struct _compiler_ctx_t*,const WCHAR *,unsigned) DECLSPEC_HIDDEN;