Sync changes from latest snowball compiler version
[xapian.git] / xapian-core / languages / compiler / header.h
blob2dcdee3682726624fb15f6911c70a5e7d9e41e1b
1 #include <stdio.h>
3 typedef unsigned char byte;
4 typedef unsigned short symbol;
6 #define true 1
7 #define false 0
9 #define MALLOC check_malloc
10 #define FREE check_free
12 #define NEW(type, p) struct type * p = (struct type *) MALLOC(sizeof(struct type))
13 #define NEWVEC(type, p, n) struct type * p = (struct type *) MALLOC(sizeof(struct type) * n)
15 #define STARTSIZE 10
16 #define SIZE(p) ((int *)(p))[-1]
17 #define CAPACITY(p) ((int *)(p))[-2]
19 extern symbol * create_b(int n);
20 extern void report_b(FILE * out, const symbol * p);
21 extern void lose_b(symbol * p);
22 extern symbol * increase_capacity(symbol * p, int n);
23 extern symbol * move_to_b(symbol * p, int n, const symbol * q);
24 extern symbol * add_to_b(symbol * p, int n, const symbol * q);
25 extern symbol * copy_b(const symbol * p);
26 extern char * b_to_s(const symbol * p);
27 extern symbol * add_s_to_b(symbol * p, const char * s);
29 #define MOVE_TO_B(B, LIT) \
30 move_to_b(B, sizeof(LIT) / sizeof(LIT[0]), LIT)
32 struct str; /* defined in space.c */
34 extern struct str * str_new(void);
35 extern void str_delete(struct str * str);
36 extern void str_append(struct str * str, struct str * add);
37 extern void str_append_ch(struct str * str, char add);
38 extern void str_append_b(struct str * str, symbol * q);
39 extern void str_append_string(struct str * str, const char * s);
40 extern void str_append_int(struct str * str, int i);
41 extern void str_clear(struct str * str);
42 extern void str_assign(struct str * str, char * s);
43 extern struct str * str_copy(struct str * old);
44 extern symbol * str_data(struct str * str);
45 extern int str_len(struct str * str);
46 extern int str_back(struct str *str);
47 extern int get_utf8(const symbol * p, int * slot);
48 extern int put_utf8(int ch, symbol * p);
49 extern void output_str(FILE * outfile, struct str * str);
51 typedef enum { ENC_SINGLEBYTE, ENC_UTF8, ENC_WIDECHARS } enc;
53 struct m_pair {
55 struct m_pair * next;
56 symbol * name;
57 symbol * value;
61 /* struct input must be a prefix of struct tokeniser. */
62 struct input {
64 struct input * next;
65 symbol * p;
66 int c;
67 char * file;
68 int line_number;
72 struct include {
74 struct include * next;
75 symbol * b;
79 enum token_codes {
81 #include "syswords2.h"
83 c_mathassign,
84 c_name,
85 c_number,
86 c_literalstring,
87 c_neg,
88 c_call,
89 c_grouping,
90 c_booltest,
92 NUM_TOKEN_CODES
95 enum uplus_modes {
96 UPLUS_NONE,
97 UPLUS_DEFINED,
98 UPLUS_UNICODE
101 /* struct input must be a prefix of struct tokeniser. */
102 struct tokeniser {
104 struct input * next;
105 symbol * p;
106 int c;
107 char * file;
108 int line_number;
109 symbol * b;
110 symbol * b2;
111 int number;
112 int m_start;
113 int m_end;
114 struct m_pair * m_pairs;
115 int get_depth;
116 int error_count;
117 int token;
118 int previous_token;
119 byte token_held;
120 enc encoding;
122 int omission;
123 struct include * includes;
125 /* Mode in which U+ has been used:
126 * UPLUS_NONE - not used yet
127 * UPLUS_DEFINED - stringdef U+xxxx ....
128 * UPLUS_UNICODE - {U+xxxx} used with implicit meaning
130 int uplusmode;
132 char token_disabled[NUM_TOKEN_CODES];
135 extern symbol * get_input(symbol * p, char ** p_file);
136 extern struct tokeniser * create_tokeniser(symbol * b, char * file);
137 extern int read_token(struct tokeniser * t);
138 extern const char * name_of_token(int code);
139 extern void disable_token(struct tokeniser * t, int code);
140 extern void close_tokeniser(struct tokeniser * t);
142 extern int space_count;
143 extern void * check_malloc(int n);
144 extern void check_free(void * p);
146 struct node;
148 struct name {
150 struct name * next;
151 symbol * b;
152 int type; /* t_string etc */
153 int mode; /* )_ for routines, externals */
154 struct node * definition; /* ) */
155 int count; /* 0, 1, 2 for each type */
156 int among_func_count; /* 1, 2, 3 for routines called by among */
157 struct grouping * grouping; /* for grouping names */
158 byte referenced;
159 byte used_in_among; /* Function used in among? */
160 byte value_used; /* (For variables) is its value ever used? */
161 byte initialised; /* (For variables) is it ever initialised? */
162 struct node * used; /* First use, or NULL if not used */
163 struct name * local_to; /* Local to one routine/external */
164 int declaration_line_number;/* Line number of declaration */
168 struct literalstring {
170 struct literalstring * next;
171 symbol * b;
175 struct amongvec {
177 symbol * b; /* the string giving the case */
178 int size; /* - and its size */
179 struct node * p; /* the corresponding node for this string */
180 int i; /* the amongvec index of the longest substring of b */
181 int result; /* the numeric result for the case */
182 struct name * function;
186 struct among {
188 struct among * next;
189 struct amongvec * b; /* pointer to the amongvec */
190 int number; /* amongs are numbered 0, 1, 2 ... */
191 int literalstring_count; /* in this among */
192 int command_count; /* in this among */
193 int function_count; /* in this among */
194 struct node * starter; /* i.e. among( (starter) 'string' ... ) */
195 struct node * substring; /* i.e. substring ... among ( ... ) */
198 struct grouping {
200 struct grouping * next;
201 int number; /* groupings are numbered 0, 1, 2 ... */
202 symbol * b; /* the characters of this group */
203 int largest_ch; /* character with max code */
204 int smallest_ch; /* character with min code */
205 struct name * name; /* so g->name->grouping == g */
206 int line_number;
209 struct node {
211 struct node * next;
212 struct node * left;
213 struct node * aux; /* used in setlimit */
214 struct among * among; /* used in among */
215 struct node * right;
216 int type;
217 int mode;
218 struct node * AE;
219 struct name * name;
220 symbol * literalstring;
221 int number;
222 int line_number;
223 int amongvar_needed; /* used in routine definitions */
226 enum name_types {
228 t_size = 6,
230 t_string = 0, t_boolean = 1, t_integer = 2, t_routine = 3, t_external = 4,
231 t_grouping = 5
233 /* If this list is extended, adjust wvn in generator.c */
236 /* In name_count[i] below, remember that
237 type is
238 ----+----
239 0 | string
240 1 | boolean
241 2 | integer
242 3 | routine
243 4 | external
244 5 | grouping
247 struct analyser {
249 struct tokeniser * tokeniser;
250 struct node * nodes;
251 struct name * names;
252 struct literalstring * literalstrings;
253 int mode;
254 byte modifyable; /* false inside reverse(...) */
255 struct node * program;
256 struct node * program_end;
257 int name_count[t_size]; /* name_count[i] counts the number of names of type i */
258 struct among * amongs;
259 struct among * amongs_end;
260 int among_count;
261 int amongvar_needed; /* used in reading routine definitions */
262 struct grouping * groupings;
263 struct grouping * groupings_end;
264 struct node * substring; /* pending 'substring' in current routine definition */
265 enc encoding;
266 byte int_limits_used; /* are maxint or minint used? */
269 enum analyser_modes {
271 m_forward = 0, m_backward /*, m_integer */
275 extern void print_program(struct analyser * a);
276 extern struct analyser * create_analyser(struct tokeniser * t);
277 extern void close_analyser(struct analyser * a);
279 extern void read_program(struct analyser * a);
281 struct generator {
283 struct analyser * analyser;
284 struct options * options;
285 int unreachable; /* 0 if code can be reached, 1 if current code
286 * is unreachable. */
287 int var_number; /* Number of next variable to use. */
288 struct str * outbuf; /* temporary str to store output */
289 struct str * declarations; /* str storing variable declarations */
290 int next_label;
291 #ifndef DISABLE_PYTHON
292 int max_label;
293 #endif
294 int margin;
296 /* if > 0, keep_count to restore in case of a failure;
297 * if < 0, the negated keep_count for the limit to restore in case of
298 * failure. */
299 int failure_keep_count;
300 #if !defined(DISABLE_JAVA) && !defined(DISABLE_JS) && !defined(DISABLE_PYTHON) && !defined(DISABLE_CSHARP)
301 struct str * failure_str; /* This is used by some generators instead of failure_keep_count */
302 #endif
304 int label_used; /* Keep track of whether the failure label is used. */
305 int failure_label;
306 int debug_count;
307 int copy_from_count; /* count of calls to copy_from() */
309 const char * S[10]; /* strings */
310 symbol * B[10]; /* blocks */
311 int I[10]; /* integers */
312 struct name * V[5]; /* variables */
313 symbol * L[5]; /* literals, used in formatted write */
315 int line_count; /* counts number of lines output */
316 int line_labelled; /* in ISO C, will need extra ';' if it is a block end */
317 int literalstring_count;
318 int keep_count; /* used to number keep/restore pairs to avoid compiler warnings
319 about shadowed variables */
322 struct options {
324 /* for the command line: */
326 const char * output_file;
327 const char * name;
328 FILE * output_src;
329 FILE * output_h;
330 byte syntax_tree;
331 enc encoding;
332 enum { LANG_JAVA, LANG_C, LANG_CPLUSPLUS, LANG_CSHARP, LANG_PYTHON, LANG_JAVASCRIPT, LANG_RUST, LANG_GO } make_lang;
333 const char * externals_prefix;
334 const char * variables_prefix;
335 const char * runtime_path;
336 const char * parent_class_name;
337 const char * package;
338 const char * go_package;
339 const char * go_snowball_runtime;
340 const char * string_class;
341 const char * among_class;
342 struct include * includes;
343 struct include * includes_end;
346 /* Generator functions common to several backends. */
348 extern struct generator * create_generator(struct analyser * a, struct options * o);
349 extern void close_generator(struct generator * g);
351 extern void write_char(struct generator * g, int ch);
352 extern void write_newline(struct generator * g);
353 extern void write_string(struct generator * g, const char * s);
354 extern void write_int(struct generator * g, int i);
355 extern void write_b(struct generator * g, symbol * b);
356 extern void write_str(struct generator * g, struct str * str);
358 extern int K_needed(struct generator * g, struct node * p);
359 extern int repeat_restore(struct generator * g, struct node * p);
361 /* Generator for C code. */
362 extern void generate_program_c(struct generator * g);
364 #ifndef DISABLE_JAVA
365 /* Generator for Java code. */
366 extern void generate_program_java(struct generator * g);
367 #endif
369 #ifndef DISABLE_CSHARP
370 /* Generator for C# code. */
371 extern void generate_program_csharp(struct generator * g);
372 #endif
374 #ifndef DISABLE_PYTHON
375 /* Generator for Python code. */
376 extern void generate_program_python(struct generator * g);
377 #endif
379 #ifndef DISABLE_JS
380 extern void generate_program_js(struct generator * g);
381 #endif
383 #ifndef DISABLE_RUST
384 extern void generate_program_rust(struct generator * g);
385 #endif
387 #ifndef DISABLE_GO
388 extern void generate_program_go(struct generator * g);
389 #endif