jscript: Added parser support for regular expressions.
[wine] / dlls / jscript / engine.h
1 /*
2  * Copyright 2008 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 typedef struct _source_elements_t source_elements_t;
20
21 typedef struct _obj_literal_t {
22     DispatchEx *obj;
23     struct _obj_literal_t *next;
24 } obj_literal_t;
25
26 typedef struct _parser_ctx_t {
27     LONG ref;
28
29     const WCHAR *ptr;
30     const WCHAR *begin;
31     const WCHAR *end;
32
33     script_ctx_t *script;
34     source_elements_t *source;
35     BOOL nl;
36     HRESULT hres;
37
38     jsheap_t tmp_heap;
39     jsheap_t heap;
40
41     obj_literal_t *obj_literals;
42
43     struct _parser_ctx_t *next;
44 } parser_ctx_t;
45
46 HRESULT script_parse(script_ctx_t*,const WCHAR*,parser_ctx_t**);
47 void parser_release(parser_ctx_t*);
48
49 int parser_lex(void*,parser_ctx_t*);
50
51 static inline void parser_addref(parser_ctx_t *ctx)
52 {
53     ctx->ref++;
54 }
55
56 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
57 {
58     return jsheap_alloc(&ctx->heap, size);
59 }
60
61 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
62 {
63     return jsheap_alloc(&ctx->tmp_heap, size);
64 }
65
66 typedef struct _scope_chain_t {
67     LONG ref;
68     DispatchEx *obj;
69     struct _scope_chain_t *next;
70 } scope_chain_t;
71
72 HRESULT scope_push(scope_chain_t*,DispatchEx*,scope_chain_t**);
73 void scope_release(scope_chain_t*);
74
75 static inline void scope_addref(scope_chain_t *scope)
76 {
77     scope->ref++;
78 }
79
80 struct _exec_ctx_t {
81     LONG ref;
82
83     parser_ctx_t *parser;
84     scope_chain_t *scope_chain;
85     DispatchEx *var_disp;
86     IDispatch *this_obj;
87 };
88
89 static inline void exec_addref(exec_ctx_t *ctx)
90 {
91     ctx->ref++;
92 }
93
94 void exec_release(exec_ctx_t*);
95 HRESULT create_exec_ctx(IDispatch*,DispatchEx*,scope_chain_t*,exec_ctx_t**);
96 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,jsexcept_t*,VARIANT*);
97
98 typedef struct _statement_t statement_t;
99 typedef struct _expression_t expression_t;
100 typedef struct _parameter_t parameter_t;
101
102 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,DispatchEx**);
103
104 typedef struct {
105     VARTYPE vt;
106     union {
107         LONG lval;
108         double dval;
109         const WCHAR *wstr;
110         VARIANT_BOOL bval;
111         IDispatch *disp;
112     } u;
113 } literal_t;
114
115 literal_t *parse_regexp(parser_ctx_t*);
116
117 typedef struct _variable_declaration_t {
118     const WCHAR *identifier;
119     expression_t *expr;
120
121     struct _variable_declaration_t *next;
122 } variable_declaration_t;
123
124 typedef struct {
125     enum{
126         RT_NORMAL,
127         RT_RETURN,
128         RT_BREAK,
129         RT_CONTINUE
130     } type;
131     jsexcept_t ei;
132 } return_type_t;
133
134 typedef HRESULT (*statement_eval_t)(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
135
136 struct _statement_t {
137     statement_eval_t eval;
138     statement_t *next;
139 };
140
141 typedef struct {
142     statement_t stat;
143     statement_t *stat_list;
144 } block_statement_t;
145
146 typedef struct {
147     statement_t stat;
148     variable_declaration_t *variable_list;
149 } var_statement_t;
150
151 typedef struct {
152     statement_t stat;
153     expression_t *expr;
154 } expression_statement_t;
155
156 typedef struct {
157     statement_t stat;
158     expression_t *expr;
159     statement_t *if_stat;
160     statement_t *else_stat;
161 } if_statement_t;
162
163 typedef struct {
164     statement_t stat;
165     expression_t *expr;
166     statement_t *statement;
167 } while_statement_t;
168
169 typedef struct {
170     statement_t stat;
171     variable_declaration_t *variable_list;
172     expression_t *begin_expr;
173     expression_t *expr;
174     expression_t *end_expr;
175     statement_t *statement;
176 } for_statement_t;
177
178 typedef struct {
179     statement_t stat;
180     variable_declaration_t *variable;
181     expression_t *expr;
182     expression_t *in_expr;
183     statement_t *statement;
184 } forin_statement_t;
185
186 typedef struct {
187     statement_t stat;
188     const WCHAR *identifier;
189 } branch_statement_t;
190
191 typedef struct {
192     statement_t stat;
193     expression_t *expr;
194     statement_t *statement;
195 } with_statement_t;
196
197 typedef struct {
198     statement_t stat;
199     const WCHAR *identifier;
200     statement_t *statement;
201 } labelled_statement_t;
202
203 typedef struct _case_clausule_t {
204     expression_t *expr;
205     statement_t *stat;
206
207     struct _case_clausule_t *next;
208 } case_clausule_t;
209
210 typedef struct {
211     statement_t stat;
212     expression_t *expr;
213     case_clausule_t *case_list;
214 } switch_statement_t;
215
216 typedef struct {
217     const WCHAR *identifier;
218     statement_t *statement;
219 } catch_block_t;
220
221 typedef struct {
222     statement_t stat;
223     statement_t *try_statement;
224     catch_block_t *catch_block;
225     statement_t *finally_statement;
226 } try_statement_t;
227
228 HRESULT block_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
229 HRESULT var_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
230 HRESULT empty_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
231 HRESULT expression_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
232 HRESULT if_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
233 HRESULT dowhile_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
234 HRESULT while_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
235 HRESULT for_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
236 HRESULT forin_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
237 HRESULT continue_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
238 HRESULT break_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
239 HRESULT return_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
240 HRESULT with_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
241 HRESULT labelled_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
242 HRESULT switch_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
243 HRESULT throw_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
244 HRESULT try_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
245
246 typedef struct {
247     enum {
248         EXPRVAL_VARIANT,
249         EXPRVAL_IDREF,
250         EXPRVAL_NAMEREF
251     } type;
252     union {
253         VARIANT var;
254         struct {
255             IDispatch *disp;
256             DISPID id;
257         } idref;
258         struct {
259             IDispatch *disp;
260             BSTR name;
261         } nameref;
262     } u;
263 } exprval_t;
264
265 typedef HRESULT (*expression_eval_t)(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
266
267 struct _expression_t {
268     expression_eval_t eval;
269 };
270
271 struct _parameter_t {
272     const WCHAR *identifier;
273
274     struct _parameter_t *next;
275 };
276
277 typedef struct _function_declaration_t {
278     const WCHAR *identifier;
279     parameter_t *parameter_list;
280     source_elements_t *source_elements;
281
282     struct _function_declaration_t *next;
283 } function_declaration_t;
284
285 struct _source_elements_t {
286     statement_t *statement;
287     statement_t *statement_tail;
288     function_declaration_t *functions;
289     function_declaration_t *functions_tail;
290 };
291
292 typedef struct {
293     expression_t expr;
294     const WCHAR *identifier;
295     parameter_t *parameter_list;
296     source_elements_t *source_elements;
297 } function_expression_t;
298
299 typedef struct {
300     expression_t expr;
301     expression_t *expression1;
302     expression_t *expression2;
303 } binary_expression_t;
304
305 typedef struct {
306     expression_t expr;
307     expression_t *expression;
308 } unary_expression_t;
309
310 typedef struct {
311     expression_t expr;
312     expression_t *expression;
313     expression_t *true_expression;
314     expression_t *false_expression;
315 } conditional_expression_t;
316
317 typedef struct {
318     expression_t expr;
319     expression_t *member_expr;
320     expression_t *expression;
321 } array_expression_t;
322
323 typedef struct {
324     expression_t expr;
325     expression_t *expression;
326     const WCHAR *identifier;
327 } member_expression_t;
328
329 typedef struct _argument_t {
330     expression_t *expr;
331
332     struct _argument_t *next;
333 } argument_t;
334
335 typedef struct {
336     expression_t expr;
337     expression_t *expression;
338     argument_t *argument_list;
339 } call_expression_t;
340
341 typedef struct {
342     expression_t expr;
343     const WCHAR *identifier;
344 } identifier_expression_t;
345
346 typedef struct {
347     expression_t expr;
348     literal_t *literal;
349 } literal_expression_t;
350
351 typedef struct _array_element_t {
352     int elision;
353     expression_t *expr;
354
355     struct _array_element_t *next;
356 } array_element_t;
357
358 typedef struct {
359     expression_t expr;
360     array_element_t *element_list;
361     int length;
362 } array_literal_expression_t;
363
364 typedef struct _prop_val_t {
365     literal_t *name;
366     expression_t *value;
367
368     struct _prop_val_t *next;
369 } prop_val_t;
370
371 typedef struct {
372     expression_t expr;
373     prop_val_t *property_list;
374 } property_value_expression_t;
375
376 typedef enum {
377      EXPR_COMMA,
378      EXPR_OR,
379      EXPR_AND,
380      EXPR_BOR,
381      EXPR_BXOR,
382      EXPR_BAND,
383      EXPR_INSTANCEOF,
384      EXPR_IN,
385      EXPR_ADD,
386      EXPR_SUB,
387      EXPR_MUL,
388      EXPR_DIV,
389      EXPR_MOD,
390      EXPR_DELETE,
391      EXPR_VOID,
392      EXPR_TYPEOF,
393      EXPR_MINUS,
394      EXPR_PLUS,
395      EXPR_POSTINC,
396      EXPR_POSTDEC,
397      EXPR_PREINC,
398      EXPR_PREDEC,
399      EXPR_NEW,
400      EXPR_EQ,
401      EXPR_EQEQ,
402      EXPR_NOTEQ,
403      EXPR_NOTEQEQ,
404      EXPR_LESS,
405      EXPR_LESSEQ,
406      EXPR_GREATER,
407      EXPR_GREATEREQ,
408      EXPR_BITNEG,
409      EXPR_LOGNEG,
410      EXPR_LSHIFT,
411      EXPR_RSHIFT,
412      EXPR_RRSHIFT,
413      EXPR_ASSIGN,
414      EXPR_ASSIGNLSHIFT,
415      EXPR_ASSIGNRSHIFT,
416      EXPR_ASSIGNRRSHIFT,
417      EXPR_ASSIGNADD,
418      EXPR_ASSIGNSUB,
419      EXPR_ASSIGNMUL,
420      EXPR_ASSIGNDIV,
421      EXPR_ASSIGNMOD,
422      EXPR_ASSIGNAND,
423      EXPR_ASSIGNOR,
424      EXPR_ASSIGNXOR
425 } expression_type_t;
426
427 HRESULT function_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
428 HRESULT conditional_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
429 HRESULT array_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
430 HRESULT member_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
431 HRESULT member_new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
432 HRESULT call_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
433 HRESULT this_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
434 HRESULT identifier_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
435 HRESULT literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
436 HRESULT array_literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
437 HRESULT property_value_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
438
439 HRESULT comma_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
440 HRESULT logical_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
441 HRESULT logical_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
442 HRESULT binary_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
443 HRESULT binary_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
444 HRESULT binary_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
445 HRESULT instanceof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
446 HRESULT in_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
447 HRESULT add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
448 HRESULT sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
449 HRESULT mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
450 HRESULT div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
451 HRESULT mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
452 HRESULT delete_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
453 HRESULT void_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
454 HRESULT typeof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
455 HRESULT minus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
456 HRESULT plus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
457 HRESULT post_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
458 HRESULT post_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
459 HRESULT pre_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
460 HRESULT pre_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
461 HRESULT new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
462 HRESULT equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
463 HRESULT equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
464 HRESULT not_equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
465 HRESULT not_equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
466 HRESULT less_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
467 HRESULT lesseq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
468 HRESULT greater_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
469 HRESULT greatereq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
470 HRESULT binary_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
471 HRESULT logical_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
472 HRESULT left_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
473 HRESULT right_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
474 HRESULT right2_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
475 HRESULT assign_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
476 HRESULT assign_lshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
477 HRESULT assign_rshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
478 HRESULT assign_rrshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
479 HRESULT assign_add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
480 HRESULT assign_sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
481 HRESULT assign_mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
482 HRESULT assign_div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
483 HRESULT assign_mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
484 HRESULT assign_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
485 HRESULT assign_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
486 HRESULT assign_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);