1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/* ------------------------------------------------------------------------
@NAME : input.c
@DESCRIPTION: Routines for input of BibTeX data.
@GLOBALS : InputFilename
StringOptions
@CALLS :
@CREATED : 1997/10/14, Greg Ward (from code in bibparse.c)
@MODIFIED :
@VERSION : $Id: input.c,v 1.18 1999/11/29 01:13:10 greg Rel $
@COPYRIGHT : Copyright (c) 1996-99 by Gregory P. Ward. All rights reserved.
This file is part of the btparse library. This library is
free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
-------------------------------------------------------------------------- */
/*#include "bt_config.h"*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include "stdpccts.h"
#include "lex_auxiliary.h"
#include "prototypes.h"
#include "error.h"
/*#include "my_dmalloc.h"*/
char * InputFilename;
ushort StringOptions[NUM_METATYPES] =
{
0, /* BTE_UNKNOWN */
BTO_FULL, /* BTE_REGULAR */
BTO_MINIMAL, /* BTE_COMMENT */
BTO_MINIMAL, /* BTE_PREAMBLE */
BTO_MACRO /* BTE_MACRODEF */
};
/* ------------------------------------------------------------------------
@NAME : bt_set_filename
@INPUT : filename
@OUTPUT :
@RETURNS :
@DESCRIPTION: Sets the current input filename -- used for generating
error and warning messages.
@GLOBALS : InputFilename
@CALLS :
@CREATED : Feb 1997, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
#if 0
void bt_set_filename (char *filename)
{
InputFilename = filename;
}
#endif
/* ------------------------------------------------------------------------
@NAME : bt_set_stringopts
@INPUT : metatype
options
@OUTPUT :
@RETURNS :
@DESCRIPTION: Sets the string-processing options for a particular
entry metatype. Used later on by bt_parse_* to determine
just how to post-process each particular entry.
@GLOBALS : StringOptions
@CREATED : 1997/08/24, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
void bt_set_stringopts (bt_metatype metatype, ushort options)
{
if (metatype < BTE_REGULAR || metatype > BTE_MACRODEF)
usage_error ("bt_set_stringopts: illegal metatype");
if (options & ~BTO_STRINGMASK)
usage_error ("bt_set_stringopts: illegal options "
"(must only set string option bits");
StringOptions[metatype] = options;
}
/* ------------------------------------------------------------------------
@NAME : start_parse
@INPUT : infile input stream we'll read from (or NULL if reading
from string)
instring input string we'll read from (or NULL if reading
from stream)
line line number of the start of the string (just
use 1 if the string is standalone and independent;
if it comes from a file, you should supply the
line number where it starts for better error
messages) (ignored if infile != NULL)
@OUTPUT :
@RETURNS :
@DESCRIPTION: Prepares things for parsing, in particular initializes the
lexical state and lexical buffer, prepares DLG for
reading (either from a stream or a string), and reads
the first token.
@GLOBALS :
@CALLS : initialize_lexer_state()
alloc_lex_buffer()
zzrdstream() or zzrdstr()
zzgettok()
@CALLERS :
@CREATED : 1997/06/21, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static void
start_parse (FILE *infile, char *instring, int line)
{
if ( (infile == NULL) == (instring == NULL) )
{
internal_error ("start_parse(): exactly one of infile and "
"instring may be non-NULL");
}
initialize_lexer_state ();
alloc_lex_buffer (ZZLEXBUFSIZE);
if (infile)
{
zzrdstream (infile);
}
else
{
zzrdstr (instring);
zzline = line;
}
zzendcol = zzbegcol = 0;
zzgettok ();
}
/* ------------------------------------------------------------------------
@NAME : finish_parse()
@INPUT : err_counts - pointer to error count list (which is local to
the parsing functions, hence has to be passed in)
@OUTPUT :
@RETURNS :
@DESCRIPTION: Frees up what was needed to parse a whole file or a sequence
of strings: the lexical buffer and the error count list.
@GLOBALS :
@CALLS : free_lex_buffer()
@CALLERS :
@CREATED : 1997/06/21, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static void
finish_parse (int **err_counts)
{
free_lex_buffer ();
free (*err_counts);
*err_counts = NULL;
}
/* ------------------------------------------------------------------------
@NAME : parse_status()
@INPUT : saved_counts
@OUTPUT :
@RETURNS : false if there were serious errors in the recently-parsed input
true otherwise (no errors or just warnings)
@DESCRIPTION: Gets the "error status" bitmap relative to a saved set of
error counts and masks of non-serious errors.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/06/21, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static boolean
parse_status (int *saved_counts)
{
ushort ignore_etqmask;
/*
* This bit-twiddling fetches the error status (which has a bit
* for each error class), masks off the bits for trivial errors
* to get "true" if there were any serious errors, and then
* returns the opposite of that.
*/
ignore_etqmask =
(1<<BTERR_NOTIFY) | (1<<BTERR_CONTENT) | (1<<BTERR_LEXWARN);
return !(bt_error_status (saved_counts) & ~ignore_etqmask);
}
/* ------------------------------------------------------------------------
@NAME : bt_parse_entry_s()
@INPUT : entry_text - string containing the entire entry to parse,
or NULL meaning we're done, please cleanup
options - standard btparse options bitmap
line - current line number (if that makes any sense)
-- passed to the parser to set zzline, so that
lexical and syntax errors are properly localized
@OUTPUT : *top - newly-allocated AST for the entry
(or NULL if entry_text was NULL, ie. at EOF)
@RETURNS : 1 with *top set to AST for entry on successful read/parse
1 with *top==NULL if entry_text was NULL, ie. at EOF
0 if any serious errors seen in input (*top is still
set to the AST, but only for as much of the input as we
were able to parse)
(A "serious" error is a lexical or syntax error; "trivial"
errors such as warnings and notifications count as "success"
for the purposes of this function's return value.)
@DESCRIPTION: Parses a BibTeX entry contained in a string.
@GLOBALS :
@CALLS : ANTLR
@CREATED : 1997/01/18, GPW (from code in bt_parse_entry())
@MODIFIED :
-------------------------------------------------------------------------- */
AST * bt_parse_entry_s (char * entry_text,
char * filename,
int line,
ushort options,
boolean * status)
{
AST * entry_ast = NULL;
static int * err_counts = NULL;
if (options & BTO_STRINGMASK) /* any string options set? */
{
usage_error ("bt_parse_entry_s: illegal options "
"(string options not allowed");
}
InputFilename = filename;
err_counts = bt_get_error_counts (err_counts);
if (entry_text == NULL) /* signal to clean up */
{
finish_parse (&err_counts);
if (status) *status = TRUE;
return NULL;
}
zzast_sp = ZZAST_STACKSIZE; /* workaround aptqparent pccts bug */
start_parse (NULL, entry_text, line);
entry (&entry_ast); /* enter the parser */
++zzasp; /* why is this done? */
if (entry_ast == NULL) /* can happen with very bad input */
{
if (status) *status = FALSE;
return entry_ast;
}
#if DEBUG
dump_ast ("bt_parse_entry_s: single entry, after parsing:\n",
entry_ast);
#endif
bt_postprocess_entry (entry_ast,
StringOptions[entry_ast->metatype] | options);
#if DEBUG
dump_ast ("bt_parse_entry_s: single entry, after post-processing:\n",
entry_ast);
#endif
if (status) *status = parse_status (err_counts);
return entry_ast;
} /* bt_parse_entry_s () */
/* ------------------------------------------------------------------------
@NAME : bt_parse_entry()
@INPUT : infile - file to read next entry from
options - standard btparse options bitmap
@OUTPUT : *top - AST for the entry, or NULL if no entries left in file
@RETURNS : same as bt_parse_entry_s()
@DESCRIPTION: Starts (or continues) parsing from a file.
@GLOBALS :
@CALLS :
@CREATED : Jan 1997, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
AST * bt_parse_entry (FILE * infile,
char * filename,
ushort options,
boolean * status)
{
AST * entry_ast = NULL;
static int * err_counts = NULL;
static FILE * prev_file = NULL;
if (prev_file != NULL && infile != prev_file)
{
usage_error ("bt_parse_entry: you can't interleave calls "
"across different files");
}
if (options & BTO_STRINGMASK) /* any string options set? */
{
usage_error ("bt_parse_entry: illegal options "
"(string options not allowed)");
}
InputFilename = filename;
err_counts = bt_get_error_counts (err_counts);
if (feof (infile))
{
if (prev_file != NULL) /* haven't already done the cleanup */
{
prev_file = NULL;
finish_parse (&err_counts);
}
else
{
usage_warning ("bt_parse_entry: second attempt to read past eof");
}
if (status) *status = TRUE;
return NULL;
}
/*
* Here we do some nasty poking about the innards of PCCTS in order to
* enter the parser multiple times on the same input stream. This code
* comes from expanding the macro invokation:
*
* ANTLR (entry (top), infile);
*
* When LL_K, ZZINF_LOOK, and DEMAND_LOOK are all undefined, this
* ultimately expands to
*
* zzbufsize = ZZLEXBUFSIZE;
* {
* static char zztoktext[ZZLEXBUFSIZE];
* zzlextext = zztoktext;
* zzrdstream (f);
* zzgettok();
* }
* entry (top);
* ++zzasp;
*
* (I'm expanding hte zzenterANTLR, zzleaveANTLR, and zzPrimateLookAhead
* macros, but leaving ZZLEXBUFSIZE -- a simple constant -- alone.)
*
* There are two problems with this: 1) zztoktext is a statically
* allocated buffer, and when it overflows we just ignore further
* characters that should belong to that lexeme; and 2) zzrdstream() and
* zzgettok() are called every time we enter the parser, which means the
* token left over from the previous entry will be discarded when we
* parse entries 2 .. N.
*
* I handle the static buffer problem with alloc_lex_buffer() and
* realloc_lex_buffer() (in lex_auxiliary.c), and by rewriting the ZZCOPY
* macro to call realloc_lex_buffer() when overflow is detected.
*
* I handle the extra token-read by hanging on to a static file
* pointer, prev_file, between calls to bt_parse_entry() -- when
* the program starts it is NULL, and we reset it to NULL on
* finishing a file. Thus, any call that is the first on a given
* file will allocate the lexical buffer and read the first token;
* thereafter, we skip those steps, and free the buffer on reaching
* end-of-file. Currently, this method precludes interleaving
* calls to bt_parse_entry() on different files -- perhaps I could
* fix this with the zz{save,restore}_{antlr,dlg}_state()
* functions?
*/
zzast_sp = ZZAST_STACKSIZE; /* workaround aptqparent pccts bug */
#if defined(LL_K) || defined(ZZINF_LOOK) || defined(DEMAND_LOOK)
# error One of LL_K, ZZINF_LOOK, or DEMAND_LOOK was defined
#endif
if (prev_file == NULL) /* only read from input stream if */
{ /* starting afresh with a file */
start_parse (infile, NULL, 0);
prev_file = infile;
}
assert (prev_file == infile);
entry (&entry_ast); /* enter the parser */
++zzasp; /* why is this done? */
if (entry_ast == NULL) /* can happen with very bad input */
{
if (status) *status = FALSE;
return entry_ast;
}
#if DEBUG
dump_ast ("bt_parse_entry(): single entry, after parsing:\n",
entry_ast);
#endif
bt_postprocess_entry (entry_ast,
StringOptions[entry_ast->metatype] | options);
#if DEBUG
dump_ast ("bt_parse_entry(): single entry, after post-processing:\n",
entry_ast);
#endif
if (status) *status = parse_status (err_counts);
return entry_ast;
} /* bt_parse_entry() */
/* ------------------------------------------------------------------------
@NAME : bt_parse_file ()
@INPUT : filename - name of file to open. If NULL or "-", we read
from stdin rather than opening a new file.
options
@OUTPUT : top
@RETURNS : 0 if any entries in the file had serious errors
1 if all entries were OK
@DESCRIPTION: Parses an entire BibTeX file, and returns a linked list
of ASTs (or, if you like, a forest) for the entries in it.
(Any entries with serious errors are omitted from the list.)
@GLOBALS :
@CALLS : bt_parse_entry()
@CREATED : 1997/01/18, from process_file() in bibparse.c
@MODIFIED :
@COMMENTS : This function bears a *striking* resemblance to bibparse.c's
process_file(). Eventually, I plan to replace this with
a generalized process_file() that takes a function pointer
to call for each entry. Until I decide on the right interface
for that, though, I'm sticking with this simpler (but possibly
memory-intensive) approach.
-------------------------------------------------------------------------- */
AST * bt_parse_file (char * filename,
ushort options,
boolean * status)
{
FILE * infile;
AST * entries,
* cur_entry,
* last;
boolean entry_status,
overall_status;
if (options & BTO_STRINGMASK) /* any string options set? */
{
usage_error ("bt_parse_file: illegal options "
"(string options not allowed");
}
/*
* If a string was given, and it's *not* "-", then open that filename.
* Otherwise just use stdin.
*/
if (filename != NULL && strcmp (filename, "-") != 0)
{
InputFilename = filename;
infile = fopen (filename, "r");
if (infile == NULL)
{
perror (filename);
return 0;
}
}
else
{
InputFilename = "(stdin)";
infile = stdin;
}
entries = NULL;
last = NULL;
#if 1
/* explicit loop over entries, with junk cleaned out by read_entry () */
overall_status = TRUE; /* assume success */
while ((cur_entry = bt_parse_entry
(infile, InputFilename, options, &entry_status)))
{
overall_status &= entry_status;
if (!entry_status) continue; /* bad entry -- try next one */
if (!cur_entry) break; /* at eof -- we're done */
if (last == NULL) /* this is the first entry */
entries = cur_entry;
else /* have already seen one */
last->right = cur_entry;
last = cur_entry;
}
#else
/* let the PCCTS lexer/parser handle everything */
initialize_lexer_state ();
ANTLR (bibfile (top), infile);
#endif
fclose (infile);
InputFilename = NULL;
if (status) *status = overall_status;
return entries;
} /* bt_parse_file() */
|