winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / jscript / parser.h
blobab5744a49e77f7c565045838094dcdbe4b46c4a9
1 /*
2 * Copyright 2014 Jacek Caban for CodeWeavers
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.
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.
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
19 typedef struct _source_elements_t source_elements_t;
20 typedef struct _expression_t expression_t;
21 typedef struct _statement_t statement_t;
23 typedef struct {
24 BOOL is_num;
25 union {
26 BOOL b;
27 double n;
28 } u;
29 } ccval_t;
31 typedef struct _parser_ctx_t {
32 const WCHAR *begin;
33 const WCHAR *end;
34 const WCHAR *ptr;
36 script_ctx_t *script;
37 source_elements_t *source;
38 BOOL nl;
39 BOOL implicit_nl_semicolon;
40 BOOL is_html;
41 BOOL lexer_error;
42 HRESULT hres;
44 ccval_t ccval;
45 unsigned cc_if_depth;
47 heap_pool_t heap;
48 } parser_ctx_t;
50 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
51 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
53 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
55 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
57 return heap_pool_alloc(&ctx->heap, size);
60 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
62 return heap_pool_alloc(&ctx->script->tmp_heap, size);
65 typedef enum {
66 LT_DOUBLE,
67 LT_STRING,
68 LT_BOOL,
69 LT_NULL,
70 LT_REGEXP
71 }literal_type_t;
73 typedef struct {
74 literal_type_t type;
75 union {
76 double dval;
77 const WCHAR *wstr;
78 BOOL bval;
79 struct {
80 const WCHAR *str;
81 DWORD str_len;
82 DWORD flags;
83 } regexp;
84 } u;
85 } literal_t;
87 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
88 literal_t *new_boolean_literal(parser_ctx_t*,BOOL) DECLSPEC_HIDDEN;
90 typedef struct _variable_declaration_t {
91 const WCHAR *identifier;
92 expression_t *expr;
94 struct _variable_declaration_t *next;
95 struct _variable_declaration_t *global_next; /* for compiler */
96 } variable_declaration_t;
98 typedef enum {
99 STAT_BLOCK,
100 STAT_BREAK,
101 STAT_CONTINUE,
102 STAT_EMPTY,
103 STAT_EXPR,
104 STAT_FOR,
105 STAT_FORIN,
106 STAT_IF,
107 STAT_LABEL,
108 STAT_RETURN,
109 STAT_SWITCH,
110 STAT_THROW,
111 STAT_TRY,
112 STAT_VAR,
113 STAT_WHILE,
114 STAT_WITH
115 } statement_type_t;
117 struct _statement_t {
118 statement_type_t type;
119 statement_t *next;
122 typedef struct {
123 statement_t stat;
124 statement_t *stat_list;
125 } block_statement_t;
127 typedef struct {
128 statement_t stat;
129 variable_declaration_t *variable_list;
130 } var_statement_t;
132 typedef struct {
133 statement_t stat;
134 expression_t *expr;
135 } expression_statement_t;
137 typedef struct {
138 statement_t stat;
139 expression_t *expr;
140 statement_t *if_stat;
141 statement_t *else_stat;
142 } if_statement_t;
144 typedef struct {
145 statement_t stat;
146 BOOL do_while;
147 expression_t *expr;
148 statement_t *statement;
149 } while_statement_t;
151 typedef struct {
152 statement_t stat;
153 variable_declaration_t *variable_list;
154 expression_t *begin_expr;
155 expression_t *expr;
156 expression_t *end_expr;
157 statement_t *statement;
158 } for_statement_t;
160 typedef struct {
161 statement_t stat;
162 variable_declaration_t *variable;
163 expression_t *expr;
164 expression_t *in_expr;
165 statement_t *statement;
166 } forin_statement_t;
168 typedef struct {
169 statement_t stat;
170 const WCHAR *identifier;
171 } branch_statement_t;
173 typedef struct {
174 statement_t stat;
175 expression_t *expr;
176 statement_t *statement;
177 } with_statement_t;
179 typedef struct {
180 statement_t stat;
181 const WCHAR *identifier;
182 statement_t *statement;
183 } labelled_statement_t;
185 typedef struct _case_clausule_t {
186 expression_t *expr;
187 statement_t *stat;
189 struct _case_clausule_t *next;
190 } case_clausule_t;
192 typedef struct {
193 statement_t stat;
194 expression_t *expr;
195 case_clausule_t *case_list;
196 } switch_statement_t;
198 typedef struct {
199 const WCHAR *identifier;
200 statement_t *statement;
201 } catch_block_t;
203 typedef struct {
204 statement_t stat;
205 statement_t *try_statement;
206 catch_block_t *catch_block;
207 statement_t *finally_statement;
208 } try_statement_t;
210 typedef enum {
211 EXPR_COMMA,
212 EXPR_OR,
213 EXPR_AND,
214 EXPR_BOR,
215 EXPR_BXOR,
216 EXPR_BAND,
217 EXPR_INSTANCEOF,
218 EXPR_IN,
219 EXPR_ADD,
220 EXPR_SUB,
221 EXPR_MUL,
222 EXPR_DIV,
223 EXPR_MOD,
224 EXPR_DELETE,
225 EXPR_VOID,
226 EXPR_TYPEOF,
227 EXPR_MINUS,
228 EXPR_PLUS,
229 EXPR_POSTINC,
230 EXPR_POSTDEC,
231 EXPR_PREINC,
232 EXPR_PREDEC,
233 EXPR_EQ,
234 EXPR_EQEQ,
235 EXPR_NOTEQ,
236 EXPR_NOTEQEQ,
237 EXPR_LESS,
238 EXPR_LESSEQ,
239 EXPR_GREATER,
240 EXPR_GREATEREQ,
241 EXPR_BITNEG,
242 EXPR_LOGNEG,
243 EXPR_LSHIFT,
244 EXPR_RSHIFT,
245 EXPR_RRSHIFT,
246 EXPR_ASSIGN,
247 EXPR_ASSIGNLSHIFT,
248 EXPR_ASSIGNRSHIFT,
249 EXPR_ASSIGNRRSHIFT,
250 EXPR_ASSIGNADD,
251 EXPR_ASSIGNSUB,
252 EXPR_ASSIGNMUL,
253 EXPR_ASSIGNDIV,
254 EXPR_ASSIGNMOD,
255 EXPR_ASSIGNAND,
256 EXPR_ASSIGNOR,
257 EXPR_ASSIGNXOR,
258 EXPR_COND,
259 EXPR_ARRAY,
260 EXPR_MEMBER,
261 EXPR_NEW,
262 EXPR_CALL,
263 EXPR_THIS,
264 EXPR_FUNC,
265 EXPR_IDENT,
266 EXPR_ARRAYLIT,
267 EXPR_PROPVAL,
268 EXPR_LITERAL
269 } expression_type_t;
271 struct _expression_t {
272 expression_type_t type;
275 typedef struct _parameter_t {
276 const WCHAR *identifier;
277 struct _parameter_t *next;
278 } parameter_t;
280 struct _source_elements_t {
281 statement_t *statement;
282 statement_t *statement_tail;
285 typedef struct _function_expression_t {
286 expression_t expr;
287 const WCHAR *identifier;
288 parameter_t *parameter_list;
289 source_elements_t *source_elements;
290 const WCHAR *src_str;
291 DWORD src_len;
293 struct _function_expression_t *next; /* for compiler */
294 } function_expression_t;
296 typedef struct {
297 expression_t expr;
298 expression_t *expression1;
299 expression_t *expression2;
300 } binary_expression_t;
302 typedef struct {
303 expression_t expr;
304 expression_t *expression;
305 } unary_expression_t;
307 typedef struct {
308 expression_t expr;
309 expression_t *expression;
310 expression_t *true_expression;
311 expression_t *false_expression;
312 } conditional_expression_t;
314 typedef struct {
315 expression_t expr;
316 expression_t *expression;
317 const WCHAR *identifier;
318 } member_expression_t;
320 typedef struct _argument_t {
321 expression_t *expr;
323 struct _argument_t *next;
324 } argument_t;
326 typedef struct {
327 expression_t expr;
328 expression_t *expression;
329 argument_t *argument_list;
330 } call_expression_t;
332 typedef struct {
333 expression_t expr;
334 const WCHAR *identifier;
335 } identifier_expression_t;
337 typedef struct {
338 expression_t expr;
339 literal_t *literal;
340 } literal_expression_t;
342 typedef struct _array_element_t {
343 int elision;
344 expression_t *expr;
346 struct _array_element_t *next;
347 } array_element_t;
349 typedef struct {
350 expression_t expr;
351 array_element_t *element_list;
352 int length;
353 } array_literal_expression_t;
355 typedef struct _prop_val_t {
356 literal_t *name;
357 expression_t *value;
359 struct _prop_val_t *next;
360 } prop_val_t;
362 typedef struct {
363 expression_t expr;
364 prop_val_t *property_list;
365 } property_value_expression_t;
367 BOOL try_parse_ccval(parser_ctx_t*,ccval_t*) DECLSPEC_HIDDEN;
368 BOOL parse_cc_expr(parser_ctx_t*) DECLSPEC_HIDDEN;
370 static inline ccval_t ccval_num(double n)
372 ccval_t r;
373 r.is_num = TRUE;
374 r.u.n = n;
375 return r;
378 static inline ccval_t ccval_bool(BOOL b)
380 ccval_t r;
381 r.is_num = FALSE;
382 r.u.b = b;
383 return r;
386 static inline BOOL get_ccbool(ccval_t v)
388 return v.is_num ? v.u.n != 0 : v.u.b;
391 static inline double get_ccnum(ccval_t v)
393 return v.is_num ? v.u.n : v.u.b;