FIX bytes highlight ?
[wireshark-wip.git] / tools / npl / ast.h
blob57eca668ecd8b5e15d075d227a14f91333148505
1 /*
2 * Copyright 2012-2013, Jakub Zawadzki <darkjames-ws@darkjames.pl>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 typedef enum {
20 OP1_INVALID = 0,
21 OP1_MINUS,
22 OP1_NOT,
23 OP1_NEG
24 } npl_op1_t;
26 typedef enum {
27 OP2_INVALID = 0,
29 OP2_ASSIGN,
30 OP2_ASSIGN_PLUS,
32 OP2_PLUS,
33 OP2_MINUS,
35 OP2_MULTIPLY,
36 OP2_DIV,
37 OP2_MOD,
39 OP2_SHL,
40 OP2_SHR,
42 OP2_EQUAL,
43 OP2_NOTEQUAL,
45 OP2_LESS,
46 OP2_GREATER,
47 OP2_LEQUAL,
48 OP2_GEQUAL,
50 OP2_LOGIC_OR,
51 OP2_LOGIC_AND,
53 OP2_OR,
54 OP2_AND,
55 OP2_XOR,
57 } npl_op2_t;
59 #define NPL_PARAMS_MAX 20
61 typedef struct {
62 char *args[NPL_PARAMS_MAX];
63 int count;
65 } npl_params_t;
67 typedef enum {
68 EXPRESSION_INVALID = 0,
70 EXPRESSION_ID,
71 EXPRESSION_INT,
72 EXPRESSION_STR,
74 EXPRESSION_INDEX,
75 EXPRESSION_MULTI_INDEX,
76 EXPRESSION_FIELD,
77 EXPRESSION_CALL,
79 EXPRESSION_UNARY,
80 EXPRESSION_BINARY,
81 EXPRESSION_COND
83 } npl_expression_type_t;
85 typedef struct _npl_expression_list {
86 struct _npl_expression_list *next;
88 struct _npl_expression *expr;
89 } npl_expression_list_t;
91 typedef struct _npl_expression {
92 union {
93 struct {
94 npl_expression_type_t type;
97 struct {
98 npl_expression_type_t type; /* EXPRESSION_ID */
99 char *id;
100 } id;
102 struct {
103 npl_expression_type_t type; /* EXPRESSION_INT */
104 unsigned int digit;
105 } num;
107 struct {
108 npl_expression_type_t type; /* EXPRESSION_STR */
109 char *str;
110 } str;
112 struct {
113 npl_expression_type_t type; /* EXPRESSION_INDEX */
115 struct _npl_expression *base;
116 struct _npl_expression *index;
117 } arr;
119 struct {
120 npl_expression_type_t type; /* EXPRESSION_MULTI_INDEX */
122 struct _npl_expression *base;
123 npl_expression_list_t *indexes;
124 } aarr;
126 struct {
127 npl_expression_type_t type; /* EXPRESSION_FIELD */
129 struct _npl_expression *base;
130 char *field;
131 } fld;
133 struct {
134 npl_expression_type_t type; /* EXPRESSION_UNARY */
136 npl_op1_t operator;
137 struct _npl_expression *operand;
139 } u;
141 struct {
142 npl_expression_type_t type; /* EXPRESSION_BINARY */
144 struct _npl_expression *operand1;
145 struct _npl_expression *operand2;
146 npl_op2_t operator;
148 } b;
150 struct {
151 npl_expression_type_t type; /* EXPRESSION_CALL */
153 struct _npl_expression *fn;
154 npl_expression_list_t *args;
156 } call;
158 struct {
159 npl_expression_type_t type; /* EXPRESSION_COND */
161 struct _npl_expression *test_expr;
162 struct _npl_expression *true_expr;
163 struct _npl_expression *false_expr;
164 } c;
168 } npl_expression_t;
170 struct _npl_statement;
172 typedef struct {
173 char *id;
174 int private;
175 npl_params_t params;
177 npl_expression_t *format;
178 npl_expression_t *count_expr;
179 struct _npl_statements *sts;
181 /* code generator */
182 char *tmpid;
183 struct ettinfo *ett;
184 struct symbol *sym;
185 int struct_size;
186 } npl_struct_t;
188 typedef struct {
189 char *id;
190 npl_params_t params;
192 npl_expression_t *switch_expr;
194 struct npl_table_case {
195 struct npl_table_case *next;
197 npl_expression_t e;
199 npl_expression_t *return_expr;
201 } *cases;
203 npl_expression_t *default_expr;
205 /* code generator */
206 struct symbol *sym;
207 } npl_table_t;
209 typedef struct {
210 char *id;
211 npl_expression_t expr;
213 /* code generator */
214 struct symbol *sym;
215 } npl_const_t;
217 typedef enum {
218 STATEMENT_INVALID = 0,
220 STATEMENT_WHILE,
221 STATEMENT_TABLE,
222 STATEMENT_STRUCT,
223 STATEMENT_FIELD,
224 STATEMENT_SWITCH,
225 STATEMENT_DYNAMIC_SWITCH,
227 } npl_statement_type_t;
229 typedef struct {
230 npl_expression_t *switch_expr;
232 struct npl_switch_case {
233 struct npl_switch_case *next;
235 npl_expression_t e;
237 struct _npl_statement *st;
239 } *cases;
241 struct _npl_statement *default_st;
243 } npl_switch_t;
245 typedef struct _npl_attribute_list {
246 struct _npl_attribute_list *next;
247 struct _npl_expression *expr;
249 /* code generator */
250 const char *resolved;
251 npl_expression_t *assign_expr;
252 int flags;
253 } npl_attribute_list_t;
255 typedef struct _npl_statement {
256 union {
257 struct {
258 npl_statement_type_t type;
259 npl_attribute_list_t *attr_list;
262 struct {
263 npl_statement_type_t type; /* STATEMENT_WHILE */
264 npl_attribute_list_t *attr_list;
266 char *id;
267 npl_expression_t expr;
269 struct _npl_statements *sts;
270 } w;
272 struct {
273 npl_statement_type_t type; /* STATEMENT_TABLE */
274 npl_attribute_list_t *attr_list;
276 npl_table_t data;
277 } t;
279 struct {
280 npl_statement_type_t type; /* STATEMENT_STRUCT */
281 npl_attribute_list_t *attr_list;
283 npl_struct_t data;
284 } s;
286 struct {
287 npl_statement_type_t type; /* STATEMENT_SWITCH or STATEMENT_DYNAMIC_SWITCH */
288 npl_attribute_list_t *attr_list;
290 npl_switch_t data;
291 } sw;
293 struct _npl_statement_field {
294 npl_statement_type_t type; /* STATEMENT_FIELD */
295 npl_attribute_list_t *attr_list;
297 char *t_id;
298 char *id;
300 unsigned int bits;
301 npl_expression_t *arr;
303 npl_expression_t *format;
304 struct _npl_statements *sts;
305 npl_expression_list_t *params;
307 /* code generator */
308 struct hfinfo *hfi;
309 npl_expression_t *byte_order_attr;
310 int generate_var;
311 int field_size;
312 } f;
315 } npl_statement_t;
317 struct _npl_statements {
318 struct _npl_statements *next;
320 npl_statement_t st;
323 typedef struct {
324 char *id;
325 npl_params_t params;
327 npl_expression_t *format;
328 struct _npl_statements *sts;
330 /* code generator */
331 struct symbol *sym;
332 } npl_protocol_t;
334 typedef enum {
335 FIELD_INVALID = 0,
337 FIELD_DECIMAL,
338 FIELD_NUMBER,
339 FIELD_TIME,
340 FIELD_UNSIGNED_NUMBER
342 } npl_field_type_t;
344 typedef struct {
345 npl_field_type_t type;
347 char *id;
348 npl_params_t params;
350 npl_expression_t *byte_order;
351 npl_expression_t *display_format;
352 npl_expression_t *size;
354 /* code generator */
355 struct symbol *sym;
357 } npl_type_t;
359 typedef enum {
360 DECL_INVALID = 0,
362 DECL_INCLUDE,
363 DECL_STRUCT,
364 DECL_TABLE,
365 DECL_CONST,
366 DECL_PROTOCOL,
367 DECL_TYPE
369 } npl_decl_type_t;
371 typedef struct {
372 union {
373 struct {
374 npl_decl_type_t type;
375 npl_attribute_list_t *attr_list;
378 struct {
379 npl_decl_type_t type; /* DECL_INCLUDE */
380 npl_attribute_list_t *attr_list;
382 char *file;
383 } i;
385 struct {
386 npl_decl_type_t type; /* DECL_STRUCT */
387 npl_attribute_list_t *attr_list;
389 npl_struct_t data;
390 } s;
392 struct {
393 npl_decl_type_t type; /* DECL_TABLE */
394 npl_attribute_list_t *attr_list;
396 npl_table_t data;
397 } t;
399 struct {
400 npl_decl_type_t type; /* DECL_PROTOCOL */
401 npl_attribute_list_t *attr_list;
403 npl_protocol_t data;
404 } p;
406 struct {
407 npl_decl_type_t type; /* DECL_CONST */
408 npl_attribute_list_t *attr_list;
410 npl_const_t data;
411 } c;
413 struct {
414 npl_decl_type_t type; /* DECL_TYPE */
415 npl_attribute_list_t *attr_list;
417 npl_type_t data;
418 } ty;
421 } npl_decl_t;
423 typedef struct {
424 struct _npl_decl_list {
425 struct _npl_decl_list *next;
426 npl_decl_t d;
428 } *decls;
430 } npl_code_t;