xapian-core  1.4.27
header.h
Go to the documentation of this file.
1 #include <stdio.h>
2 
3 #define SNOWBALL_VERSION "2.0.0"
4 
5 typedef unsigned char byte;
6 typedef unsigned short symbol;
7 
8 #define true 1
9 #define false 0
10 
11 #define MALLOC check_malloc
12 #define FREE check_free
13 
14 #define NEW(type, p) struct type * p = (struct type *) MALLOC(sizeof(struct type))
15 #define NEWVEC(type, p, n) struct type * p = (struct type *) MALLOC(sizeof(struct type) * (n))
16 
17 #define SIZE(p) ((int *)(p))[-1]
18 #define CAPACITY(p) ((int *)(p))[-2]
19 
20 extern symbol * create_b(int n);
21 extern void report_b(FILE * out, const symbol * p);
22 extern void lose_b(symbol * p);
23 extern symbol * increase_capacity(symbol * p, int n);
24 extern symbol * move_to_b(symbol * p, int n, const symbol * q);
25 extern symbol * add_to_b(symbol * p, int n, const symbol * q);
26 extern symbol * copy_b(const symbol * p);
27 extern char * b_to_s(const symbol * p);
28 extern symbol * add_s_to_b(symbol * p, const char * s);
29 
30 #define MOVE_TO_B(B, LIT) \
31  move_to_b(B, sizeof(LIT) / sizeof(LIT[0]), LIT)
32 
33 struct str; /* defined in space.c */
34 
35 extern struct str * str_new(void);
36 extern void str_delete(struct str * str);
37 extern void str_append(struct str * str, struct str * add);
38 extern void str_append_ch(struct str * str, char add);
39 extern void str_append_b(struct str * str, symbol * q);
40 extern void str_append_string(struct str * str, const char * s);
41 extern void str_append_int(struct str * str, int i);
42 extern void str_clear(struct str * str);
43 extern void str_assign(struct str * str, char * s);
44 extern struct str * str_copy(struct str * old);
45 extern symbol * str_data(struct str * str);
46 extern int str_len(struct str * str);
47 extern int str_back(struct str *str);
48 extern int get_utf8(const symbol * p, int * slot);
49 extern int put_utf8(int ch, symbol * p);
50 extern void output_str(FILE * outfile, struct str * str);
51 
53 
54 struct m_pair {
55 
56  struct m_pair * next;
59 
60 };
61 
62 /* struct input must be a prefix of struct tokeniser. */
63 struct input {
64 
65  struct input * next;
66  symbol * p;
67  int c;
68  char * file;
71 
72 };
73 
74 struct include {
75 
76  struct include * next;
77  symbol * b;
78 
79 };
80 
82 
83 #include "syswords2.h"
84 
93 
95 };
96 
101 };
103 /* struct input must be a prefix of struct tokeniser. */
104 struct tokeniser {
106  struct input * next;
108  int c;
109  char * file;
114  int number;
115  int m_start;
116  int m_end;
117  struct m_pair * m_pairs;
120  int token;
124 
125  int omission;
126  struct include * includes;
127 
128  /* Mode in which U+ has been used:
129  * UPLUS_NONE - not used yet
130  * UPLUS_DEFINED - stringdef U+xxxx ....
131  * UPLUS_UNICODE - {U+xxxx} used with implicit meaning
132  */
134 
135  char token_disabled[NUM_TOKEN_CODES];
136 };
137 
138 extern symbol * get_input(const char * filename);
139 extern struct tokeniser * create_tokeniser(symbol * b, char * file);
140 extern int read_token(struct tokeniser * t);
141 extern const char * name_of_token(int code);
142 extern void disable_token(struct tokeniser * t, int code);
143 extern void close_tokeniser(struct tokeniser * t);
144 
145 extern int space_count;
146 extern void * check_malloc(int n);
147 extern void check_free(void * p);
148 
149 struct node;
150 
151 struct name {
152 
153  struct name * next;
155  int type; /* t_string etc */
156  int mode; /* )_ for routines, externals */
157  struct node * definition; /* ) */
158  int count; /* 0, 1, 2 for each type */
159  int among_func_count; /* 1, 2, 3 for routines called by among */
160  struct grouping * grouping; /* for grouping names */
162  byte used_in_among; /* Function used in among? */
163  byte value_used; /* (For variables) is its value ever used? */
164  byte initialised; /* (For variables) is it ever initialised? */
165  byte used_in_definition; /* (grouping) used in grouping definition? */
166  struct node * used; /* First use, or NULL if not used */
167  struct name * local_to; /* Local to one routine/external */
168  int declaration_line_number;/* Line number of declaration */
169 
170 };
171 
173 
174  struct literalstring * next;
176 
177 };
178 
179 struct amongvec {
180 
181  symbol * b; /* the string giving the case */
182  int size; /* - and its size */
183  struct node * action; /* the corresponding action */
184  int i; /* the amongvec index of the longest substring of b */
185  int result; /* the numeric result for the case */
186  int line_number; /* for diagnostics and stable sorting */
187  struct name * function;
188 
189 };
190 
191 struct among {
192 
193  struct among * next;
194  struct amongvec * b; /* pointer to the amongvec */
195  int number; /* amongs are numbered 0, 1, 2 ... */
196  int literalstring_count; /* in this among */
197  int command_count; /* in this among */
198  int nocommand_count; /* number of "no command" entries in this among */
199  int function_count; /* in this among */
200  int amongvar_needed; /* do we need to set among_var? */
201  struct node * starter; /* i.e. among( (starter) 'string' ... ) */
202  struct node * substring; /* i.e. substring ... among ( ... ) */
203  struct node ** commands; /* array with command_count entries */
204 };
205 
206 struct grouping {
207 
208  struct grouping * next;
209  symbol * b; /* the characters of this group */
210  int largest_ch; /* character with max code */
211  int smallest_ch; /* character with min code */
212  struct name * name; /* so g->name->grouping == g */
214 };
215 
216 struct node {
217 
218  struct node * next;
219  struct node * left;
220  struct node * aux; /* used in setlimit */
221  struct among * among; /* used in among */
222  struct node * right;
223  int type;
224  int mode;
225  struct node * AE;
226  struct name * name;
228  int number;
230  int amongvar_needed; /* used in routine definitions */
231 };
232 
234 
235  t_size = 6,
236 
239 
240 /* If this list is extended, adjust wvn in generator.c */
241 };
242 
243 /* In name_count[i] below, remember that
244  type is
245  ----+----
246  0 | string
247  1 | boolean
248  2 | integer
249  3 | routine
250  4 | external
251  5 | grouping
252 */
253 
254 struct analyser {
255 
257  struct node * nodes;
258  struct name * names;
260  int mode;
261  byte modifyable; /* false inside reverse(...) */
262  struct node * program;
263  struct node * program_end;
264  int name_count[t_size]; /* name_count[i] counts the number of names of type i */
265  struct among * amongs;
266  struct among * amongs_end;
268  int amongvar_needed; /* used in reading routine definitions */
269  struct grouping * groupings;
271  struct node * substring; /* pending 'substring' in current routine definition */
273  byte int_limits_used; /* are maxint or minint used? */
274 };
275 
277 
278  m_forward = 0, m_backward /*, m_integer */
279 
280 };
281 
282 extern void print_program(struct analyser * a);
283 extern struct analyser * create_analyser(struct tokeniser * t);
284 extern void close_analyser(struct analyser * a);
285 
286 extern void read_program(struct analyser * a);
287 
288 struct generator {
289 
290  struct analyser * analyser;
291  struct options * options;
292  int unreachable; /* 0 if code can be reached, 1 if current code
293  * is unreachable. */
294  int var_number; /* Number of next variable to use. */
295  struct str * outbuf; /* temporary str to store output */
296  struct str * declarations; /* str storing variable declarations */
298 #ifndef DISABLE_PYTHON
300 #endif
301  int margin;
302 
303  /* if > 0, keep_count to restore in case of a failure;
304  * if < 0, the negated keep_count for the limit to restore in case of
305  * failure. */
307 #if !defined(DISABLE_JAVA) && !defined(DISABLE_JS) && !defined(DISABLE_PYTHON) && !defined(DISABLE_CSHARP)
308  struct str * failure_str; /* This is used by some generators instead of failure_keep_count */
309 #endif
310 
311  int label_used; /* Keep track of whether the failure label is used. */
314  int copy_from_count; /* count of calls to copy_from() */
315 
316  const char * S[10]; /* strings */
317  symbol * B[10]; /* blocks */
318  int I[10]; /* integers */
319  struct name * V[5]; /* variables */
320  symbol * L[5]; /* literals, used in formatted write */
321 
322  int line_count; /* counts number of lines output */
323  int line_labelled; /* in ISO C, will need extra ';' if it is a block end */
325  int keep_count; /* used to number keep/restore pairs to avoid compiler warnings
326  about shadowed variables */
327 };
328 
329 /* Special values for failure_label in struct generator. */
331  x_return = -1
332 };
333 
334 struct options {
335 
336  /* for the command line: */
337 
338  const char * output_file;
339  const char * name;
340  FILE * output_src;
341  FILE * output_h;
345  enum { LANG_JAVA, LANG_C, LANG_CPLUSPLUS, LANG_CSHARP, LANG_PASCAL, LANG_PYTHON, LANG_JAVASCRIPT, LANG_RUST, LANG_GO } make_lang;
346  const char * externals_prefix;
347  const char * variables_prefix;
348  const char * runtime_path;
349  const char * parent_class_name;
350  const char * package;
351  const char * go_snowball_runtime;
352  const char * string_class;
353  const char * among_class;
354  struct include * includes;
356 };
357 
358 /* Generator functions common to several backends. */
359 
360 extern struct generator * create_generator(struct analyser * a, struct options * o);
361 extern void close_generator(struct generator * g);
362 
363 extern void write_char(struct generator * g, int ch);
364 extern void write_newline(struct generator * g);
365 extern void write_string(struct generator * g, const char * s);
366 extern void write_int(struct generator * g, int i);
367 extern void write_b(struct generator * g, symbol * b);
368 extern void write_str(struct generator * g, struct str * str);
369 
370 extern void write_comment_content(struct generator * g, struct node * p);
371 extern void write_generated_comment_content(struct generator * g);
372 extern void write_start_comment(struct generator * g,
373  const char * comment_start,
374  const char * comment_end);
375 
376 extern int K_needed(struct generator * g, struct node * p);
377 extern int repeat_restore(struct generator * g, struct node * p);
378 
379 /* Generator for C code. */
380 extern void generate_program_c(struct generator * g);
381 
382 #ifndef DISABLE_JAVA
383 /* Generator for Java code. */
384 extern void generate_program_java(struct generator * g);
385 #endif
386 
387 #ifndef DISABLE_CSHARP
388 /* Generator for C# code. */
389 extern void generate_program_csharp(struct generator * g);
390 #endif
391 
392 #ifndef DISABLE_PASCAL
393 extern void generate_program_pascal(struct generator * g);
394 #endif
395 
396 #ifndef DISABLE_PYTHON
397 /* Generator for Python code. */
398 extern void generate_program_python(struct generator * g);
399 #endif
400 
401 #ifndef DISABLE_JS
402 extern void generate_program_js(struct generator * g);
403 #endif
404 
405 #ifndef DISABLE_RUST
406 extern void generate_program_rust(struct generator * g);
407 #endif
408 
409 #ifndef DISABLE_GO
410 extern void generate_program_go(struct generator * g);
411 #endif
symbol * b
Definition: header.h:175
void print_program(struct analyser *a)
int number
Definition: header.h:228
int type
Definition: header.h:155
int literalstring_count
Definition: header.h:324
analyser_modes
Definition: header.h:276
void generate_program_js(struct generator *g)
int m_end
Definition: header.h:116
int str_len(struct str *str)
struct name * names
Definition: header.h:258
Definition: header.h:216
Definition: header.h:103
Definition: unittest.cc:678
struct str * str_new(void)
struct str * outbuf
Definition: header.h:295
void write_start_comment(struct generator *g, const char *comment_start, const char *comment_end)
void generate_program_pascal(struct generator *g)
struct name * name
Definition: header.h:226
uplus_modes
Definition: header.h:97
symbol * add_s_to_b(symbol *p, const char *s)
struct among * amongs_end
Definition: header.h:266
int margin
Definition: header.h:301
void str_clear(struct str *str)
void output_str(FILE *outfile, struct str *str)
int omission
Definition: header.h:125
void str_delete(struct str *str)
int largest_ch
Definition: header.h:210
byte comments
Definition: header.h:343
int declaration_line_number
Definition: header.h:168
struct node * nodes
Definition: header.h:257
enc
Definition: header.h:52
int among_func_count
Definition: header.h:159
void read_program(struct analyser *a)
symbol * b
Definition: header.h:154
void write_char(struct generator *g, int ch)
struct node * next
Definition: header.h:218
int error_count
Definition: header.h:119
int i
Definition: header.h:184
FILE * output_src
Definition: header.h:340
struct among * amongs
Definition: header.h:265
void str_append_int(struct str *str, int i)
struct str * str_copy(struct str *old)
struct include * includes
Definition: header.h:354
symbol * name
Definition: header.h:57
int line_labelled
Definition: header.h:323
int number
Definition: header.h:195
int amongvar_needed
Definition: header.h:200
struct str * failure_str
Definition: header.h:308
FILE * output_h
Definition: header.h:341
Definition: header.h:63
struct among * among
Definition: header.h:221
symbol * get_input(const char *filename)
int amongvar_needed
Definition: header.h:230
struct node ** commands
Definition: header.h:203
struct grouping * grouping
Definition: header.h:160
struct node * program_end
Definition: header.h:263
const char * name_of_token(int code)
symbol * add_to_b(symbol *p, int n, const symbol *q)
byte token_held
Definition: header.h:122
enc encoding
Definition: header.h:272
int line_number
Definition: header.h:111
struct name * local_to
Definition: header.h:167
struct m_pair * next
Definition: header.h:56
unsigned char byte
Definition: header.h:5
struct node * used
Definition: header.h:166
symbol * literalstring
Definition: header.h:227
struct grouping * next
Definition: header.h:208
const char * name
Definition: header.h:339
symbol * value
Definition: header.h:58
const char * variables_prefix
Definition: header.h:347
int get_depth
Definition: header.h:118
int mode
Definition: header.h:224
struct tokeniser * create_tokeniser(symbol *b, char *file)
struct node * substring
Definition: header.h:271
char * b_to_s(const symbol *p)
Definition: header.h:74
void write_b(struct generator *g, symbol *b)
symbol * str_data(struct str *str)
int keep_count
Definition: header.h:325
int repeat_restore(struct generator *g, struct node *p)
struct among * next
Definition: header.h:193
int uplusmode
Definition: header.h:133
void write_string(struct generator *g, const char *s)
void generate_program_csharp(struct generator *g)
struct node * AE
Definition: header.h:225
enc encoding
Definition: header.h:344
char * file
Definition: header.h:109
int copy_from_count
Definition: header.h:314
struct node * starter
Definition: header.h:201
void generate_program_python(struct generator *g)
struct analyser * analyser
Definition: header.h:290
int literalstring_count
Definition: header.h:196
struct node * definition
Definition: header.h:157
struct m_pair * m_pairs
Definition: header.h:117
void * check_malloc(int n)
void check_free(void *p)
struct name * name
Definition: header.h:212
symbol * b
Definition: header.h:77
int nocommand_count
Definition: header.h:198
void report_b(FILE *out, const symbol *p)
struct node * aux
Definition: header.h:220
struct node * program
Definition: header.h:262
struct tokeniser * tokeniser
Definition: header.h:256
void generate_program_java(struct generator *g)
int file_needs_freeing
Definition: header.h:110
const char * parent_class_name
Definition: header.h:349
void str_append_ch(struct str *str, char add)
symbol * move_to_b(symbol *p, int n, const symbol *q)
int unreachable
Definition: header.h:292
void str_append_string(struct str *str, const char *s)
void generate_program_go(struct generator *g)
int m_start
Definition: header.h:115
int space_count
struct include * next
Definition: header.h:76
void lose_b(symbol *p)
unsigned short symbol
Definition: header.h:6
int line_number
Definition: header.h:229
struct node * left
Definition: header.h:219
void write_int(struct generator *g, int i)
struct options * options
Definition: header.h:291
byte initialised
Definition: header.h:164
struct grouping * groupings
Definition: header.h:269
int previous_token
Definition: header.h:121
string str(int value)
Convert int to std::string.
Definition: str.cc:90
const char * externals_prefix
Definition: header.h:346
struct analyser * create_analyser(struct tokeniser *t)
int type
Definition: header.h:223
byte used_in_among
Definition: header.h:162
int put_utf8(int ch, symbol *p)
struct include * includes
Definition: header.h:126
symbol * b
Definition: header.h:181
int K_needed(struct generator *g, struct node *p)
int result
Definition: header.h:185
void write_generated_comment_content(struct generator *g)
int max_label
Definition: header.h:299
struct name * next
Definition: header.h:153
int number
Definition: header.h:114
symbol * p
Definition: header.h:107
int mode
Definition: header.h:260
Definition: header.h:54
void close_generator(struct generator *g)
int size
Definition: header.h:182
void close_tokeniser(struct tokeniser *t)
struct input * next
Definition: header.h:65
int line_number
Definition: header.h:70
int label_used
Definition: header.h:311
byte int_limits_used
Definition: header.h:273
const char * string_class
Definition: header.h:352
Definition: header.h:191
symbol * create_b(int n)
int failure_keep_count
Definition: header.h:306
const char * package
Definition: header.h:350
int next_label
Definition: header.h:297
token_codes
Definition: header.h:81
int var_number
Definition: header.h:294
int str_back(struct str *str)
char * file
Definition: header.h:68
void str_assign(struct str *str, char *s)
symbol * b
Definition: header.h:112
symbol * copy_b(const symbol *p)
byte used_in_definition
Definition: header.h:165
const char * runtime_path
Definition: header.h:348
int count
Definition: header.h:158
special_labels
Definition: header.h:330
struct grouping * groupings_end
Definition: header.h:270
symbol * b
Definition: header.h:209
symbol * increase_capacity(symbol *p, int n)
struct include * includes_end
Definition: header.h:355
void disable_token(struct tokeniser *t, int code)
int debug_count
Definition: header.h:313
int smallest_ch
Definition: header.h:211
const char * among_class
Definition: header.h:353
int mode
Definition: header.h:156
int command_count
Definition: header.h:197
void generate_program_rust(struct generator *g)
int get_utf8(const symbol *p, int *slot)
byte referenced
Definition: header.h:161
int token
Definition: header.h:120
symbol * p
Definition: header.h:66
int line_count
Definition: header.h:322
void str_append(struct str *str, struct str *add)
byte value_used
Definition: header.h:163
Definition: header.h:100
void write_newline(struct generator *g)
struct node * right
Definition: header.h:222
Definition: header.h:151
int file_needs_freeing
Definition: header.h:69
struct node * action
Definition: header.h:183
symbol * b2
Definition: header.h:113
int among_count
Definition: header.h:267
int line_number
Definition: header.h:186
struct literalstring * literalstrings
Definition: header.h:259
struct generator * create_generator(struct analyser *a, struct options *o)
struct node * substring
Definition: header.h:202
void str_append_b(struct str *str, symbol *q)
int function_count
Definition: header.h:199
int read_token(struct tokeniser *t)
Definition: header.h:104
struct amongvec * b
Definition: header.h:194
struct str * declarations
Definition: header.h:296
void write_comment_content(struct generator *g, struct node *p)
enc encoding
Definition: header.h:123
void close_analyser(struct analyser *a)
void generate_program_c(struct generator *g)
void write_str(struct generator *g, struct str *str)
struct literalstring * next
Definition: header.h:174
const char * output_file
Definition: header.h:338
int c
Definition: header.h:67
byte modifyable
Definition: header.h:261
int failure_label
Definition: header.h:312
byte syntax_tree
Definition: header.h:342
Definition: header.h:235
int amongvar_needed
Definition: header.h:268
int line_number
Definition: header.h:213
const char * go_snowball_runtime
Definition: header.h:351
name_types
Definition: header.h:233