Sync changes from latest snowball compiler version
[xapian.git] / xapian-core / languages / compiler / header.h
blobaa1090d040598e5fe192a50eb20a149548e80d56
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 struct node * used; /* First use, or NULL if not used */
161 struct name * local_to; /* Local to one routine/external */
162 int declaration_line_number;/* Line number of declaration */
166 struct literalstring {
168 struct literalstring * next;
169 symbol * b;
173 struct amongvec {
175 symbol * b; /* the string giving the case */
176 int size; /* - and its size */
177 struct node * p; /* the corresponding node for this string */
178 int i; /* the amongvec index of the longest substring of b */
179 int result; /* the numeric result for the case */
180 struct name * function;
184 struct among {
186 struct among * next;
187 struct amongvec * b; /* pointer to the amongvec */
188 int number; /* amongs are numbered 0, 1, 2 ... */
189 int literalstring_count; /* in this among */
190 int command_count; /* in this among */
191 int function_count; /* in this among */
192 struct node * starter; /* i.e. among( (starter) 'string' ... ) */
193 struct node * substring; /* i.e. substring ... among ( ... ) */
196 struct grouping {
198 struct grouping * next;
199 int number; /* groupings are numbered 0, 1, 2 ... */
200 symbol * b; /* the characters of this group */
201 int largest_ch; /* character with max code */
202 int smallest_ch; /* character with min code */
203 struct name * name; /* so g->name->grouping == g */
204 int line_number;
207 struct node {
209 struct node * next;
210 struct node * left;
211 struct node * aux; /* used in setlimit */
212 struct among * among; /* used in among */
213 struct node * right;
214 int type;
215 int mode;
216 struct node * AE;
217 struct name * name;
218 symbol * literalstring;
219 int number;
220 int line_number;
221 int amongvar_needed; /* used in routine definitions */
224 enum name_types {
226 t_size = 6,
228 t_string = 0, t_boolean = 1, t_integer = 2, t_routine = 3, t_external = 4,
229 t_grouping = 5
231 /* If this list is extended, adjust wvn in generator.c */
234 /* In name_count[i] below, remember that
235 type is
236 ----+----
237 0 | string
238 1 | boolean
239 2 | integer
240 3 | routine
241 4 | external
242 5 | grouping
245 struct analyser {
247 struct tokeniser * tokeniser;
248 struct node * nodes;
249 struct name * names;
250 struct literalstring * literalstrings;
251 int mode;
252 byte modifyable; /* false inside reverse(...) */
253 struct node * program;
254 struct node * program_end;
255 int name_count[t_size]; /* name_count[i] counts the number of names of type i */
256 struct among * amongs;
257 struct among * amongs_end;
258 int among_count;
259 int amongvar_needed; /* used in reading routine definitions */
260 struct grouping * groupings;
261 struct grouping * groupings_end;
262 struct node * substring; /* pending 'substring' in current routine definition */
263 enc encoding;
264 byte int_limits_used; /* are maxint or minint used? */
267 enum analyser_modes {
269 m_forward = 0, m_backward /*, m_integer */
273 extern void print_program(struct analyser * a);
274 extern struct analyser * create_analyser(struct tokeniser * t);
275 extern void close_analyser(struct analyser * a);
277 extern void read_program(struct analyser * a);
279 struct generator {
281 struct analyser * analyser;
282 struct options * options;
283 int unreachable; /* 0 if code can be reached, 1 if current code
284 * is unreachable. */
285 int var_number; /* Number of next variable to use. */
286 struct str * outbuf; /* temporary str to store output */
287 struct str * declarations; /* str storing variable declarations */
288 int next_label;
289 #ifndef DISABLE_PYTHON
290 int max_label;
291 #endif
292 int margin;
294 /* if > 0, keep_count to restore in case of a failure;
295 * if < 0, the negated keep_count for the limit to restore in case of
296 * failure. */
297 int failure_keep_count;
298 #if !defined(DISABLE_JAVA) && !defined(DISABLE_JS) && !defined(DISABLE_PYTHON) && !defined(DISABLE_CSHARP)
299 struct str * failure_str; /* This is used by some generators instead of failure_keep_count */
300 #endif
302 int label_used; /* Keep track of whether the failure label is used. */
303 int failure_label;
304 int debug_count;
305 int copy_from_count; /* count of calls to copy_from() */
307 const char * S[10]; /* strings */
308 symbol * B[10]; /* blocks */
309 int I[10]; /* integers */
310 struct name * V[5]; /* variables */
311 symbol * L[5]; /* literals, used in formatted write */
313 int line_count; /* counts number of lines output */
314 int line_labelled; /* in ISO C, will need extra ';' if it is a block end */
315 int literalstring_count;
316 int keep_count; /* used to number keep/restore pairs to avoid compiler warnings
317 about shadowed variables */
320 struct options {
322 /* for the command line: */
324 const char * output_file;
325 const char * name;
326 FILE * output_src;
327 FILE * output_h;
328 byte syntax_tree;
329 enc encoding;
330 enum { LANG_JAVA, LANG_C, LANG_CPLUSPLUS, LANG_CSHARP, LANG_PYTHON, LANG_JAVASCRIPT, LANG_RUST, LANG_GO } make_lang;
331 const char * externals_prefix;
332 const char * variables_prefix;
333 const char * runtime_path;
334 const char * parent_class_name;
335 const char * package;
336 const char * go_package;
337 const char * go_snowball_runtime;
338 const char * string_class;
339 const char * among_class;
340 struct include * includes;
341 struct include * includes_end;
344 /* Generator functions common to several backends. */
346 extern struct generator * create_generator(struct analyser * a, struct options * o);
347 extern void close_generator(struct generator * g);
349 extern void write_char(struct generator * g, int ch);
350 extern void write_newline(struct generator * g);
351 extern void write_string(struct generator * g, const char * s);
352 extern void write_int(struct generator * g, int i);
353 extern void write_b(struct generator * g, symbol * b);
354 extern void write_str(struct generator * g, struct str * str);
356 extern int K_needed(struct generator * g, struct node * p);
357 extern int repeat_restore(struct generator * g, struct node * p);
359 /* Generator for C code. */
360 extern void generate_program_c(struct generator * g);
362 #ifndef DISABLE_JAVA
363 /* Generator for Java code. */
364 extern void generate_program_java(struct generator * g);
365 #endif
367 #ifndef DISABLE_CSHARP
368 /* Generator for C# code. */
369 extern void generate_program_csharp(struct generator * g);
370 #endif
372 #ifndef DISABLE_PYTHON
373 /* Generator for Python code. */
374 extern void generate_program_python(struct generator * g);
375 #endif
377 #ifndef DISABLE_JS
378 extern void generate_program_js(struct generator * g);
379 #endif
381 #ifndef DISABLE_RUST
382 extern void generate_program_rust(struct generator * g);
383 #endif
385 #ifndef DISABLE_GO
386 extern void generate_program_go(struct generator * g);
387 #endif