src/parser.c

filter: Statementstmt 84.5% 560 / 662 Branchbran 69.8% 299 / 428 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.8% 859 / 1090 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Text/BibTeX.xs ↑ Summary src/postprocess.c »
1 /* ------------------------------------------------------------------------
2 @NAME : parser.c
3 @DESCRIPTION: A reentrant lexer and recursive-descent parser for the BibTeX
4 data language. This replaces the PCCTS-generated parser of
5 the original btparse; it accepts the same language, builds
6 the same AST and produces the same error messages, but keeps
7 all of its state in a bt_parser object and reads its input
8 through a callback (or from memory).
9  
10 The lexer has three modes, as before:
11  
12 START between entries: looks for '@', skips whitespace
13 and %-comments, and counts any other "junk"
14 ENTRY inside an entry: names, numbers and punctuation
15 STRING inside a quoted ("...") or braced ({...}) string
16  
17 The grammar is:
18  
19 entry : '@' NAME body
20 body : STRING (@comment only)
21 | ENTRY_OPEN contents ENTRY_CLOSE
22 | (empty) (@comment not followed by a STRING:
23 ignored, like BibTeX does)
24 contents : (NAME | NUMBER) [ ',' fields ] (regular entry)
25 | fields (@string)
26 | value (@preamble)
27 fields : field { ',' fields } | (empty)
28 field : NAME '=' value
29 value : simple_value ( '#' simple_value )*
30 simple_value : STRING | NUMBER | NAME
31  
32 Error recovery follows the original parser: an error in a
33 field skips to the next ',' or end of entry; an error in a
34 simple value skips to the next '#', ',' or end of entry;
35 anything worse skips to the next '@' at top level. In
36 addition, a name token that starts with '@' inside an entry
37 almost always means a missing closing brace, so during error
38 recovery (and where a field name is expected) the parser
39 backs up and restarts at that '@' rather than losing the
40 following entry.
41 @COPYRIGHT : Copyright (c) 1996-99 by Gregory P. Ward. All rights reserved.
42  
43 This file is part of the btparse library. This library is
44 free software; you can redistribute it and/or modify it under
45 the terms of the GNU Library General Public License as
46 published by the Free Software Foundation; either version 2
47 of the License, or (at your option) any later version.
48 -------------------------------------------------------------------------- */
49  
50 #include <stdlib.h>
51 #include <string.h>
52 #include "bt_internal.h"
53  
54 #define READ_CHUNK 65536
55 #define MAX_BAD_TEXT 200 /* bytes of a token quoted in errors */
56  
57 typedef enum { M_START, M_ENTRY, M_STRING } lex_mode;
58  
59 typedef enum
60 {
61 S_TOPLEVEL, /* between entries */
62 S_AFTER_AT, /* seen '@' */
63 S_AFTER_TYPE, /* seen '@name' (not "comment") */
64 S_IN_COMMENT, /* seen '@comment' */
65 S_IN_ENTRY /* seen '@name{' or '@name(' */
66 } entry_state;
67  
68 typedef enum
69 {
70 T_EOF, T_AT, T_NAME, T_NUMBER, T_LBRACE, T_RBRACE, T_ENTRY_OPEN,
71 T_ENTRY_CLOSE, T_EQUALS, T_HASH, T_COMMA, T_STRING, T_QUOTE
72 } token;
73  
74 #define TOKSET(t) (1u << (t))
75  
76 /* Is c (a byte, or -1 at end of input) one of the characters in set?
77 * Unlike plain strchr() this never matches NUL. */
78 #define ISIN(set, c) ((c) > 0 && strchr (set, (c)) != NULL)
79  
80 struct bt_parser
81 {
82 /* input */
83 bt_read_fn read;
84 void * userdata;
85 char * buf;
86 size_t cap;
87 size_t keep; /* start of bytes we must not discard */
88 size_t pos;
89 size_t end;
90 long base; /* input offset of buf[0] */
91 boolean eof;
92 boolean read_error;
93 unsigned flags;
94 char * filename;
95 int line;
96 bt_macros * macros;
97  
98 /* lexical state */
99 lex_mode mode;
100 entry_state state;
101 char entry_opener;
102 bt_metatype metatype;
103 long junk_count;
104 char string_opener;
105 int brace_depth;
106 int paren_depth;
107 int string_start;
108 boolean apparent_runaway;
109 int bad_utf8_line;
110  
111 /* the lookahead token */
112 boolean have_tok;
113 token tok;
114 bt_sbuf text;
115 int tok_line; /* line at end of token */
116 int tok_start_line;
117 size_t tok_pos; /* buffer position of token start */
118  
119 /* parsing */
120 bt_errors * errs;
121 boolean bail; /* abandon current entry (restarted at '@') */
122 int entries; /* entries returned so far */
123 int attempts; /* entries attempted so far */
124 };
125  
126  
127 /* ----------------------------------------------------------------------
128 * Construction
129 */
130  
131 static bt_parser *
132 73 new_parser (const char *filename, unsigned flags)
133 {
134 73 bt_parser * p = bt_xcalloc (1, sizeof (bt_parser));
135  
136 73 p->filename = bt_xstrdup (filename);
137 73 p->flags = flags;
138 73 p->line = 1;
139 73 p->mode = M_START;
140 73 p->state = S_TOPLEVEL;
141 73 p->metatype = BTE_UNKNOWN;
142 73 p->string_start = -1;
143 73 bt_sbuf_init (&p->text);
144 73 return p;
145 }
146  
147 bt_parser *
148 35 bt_parser_new_reader (bt_read_fn read, void *userdata,
149 const char *filename, unsigned flags)
150 {
151 35 bt_parser * p = new_parser (filename, flags);
152  
153 35 p->read = read;
154 35 p->userdata = userdata;
155 35 p->cap = READ_CHUNK;
156 35 p->buf = bt_xmalloc (p->cap);
157 35 return p;
158 }
159  
160 bt_parser *
161 38 bt_parser_new_string (const char *text, size_t len, const char *filename,
162 int line, unsigned flags)
163 {
164 38 bt_parser * p = new_parser (filename, flags);
165  
166 38 p->buf = bt_xstrndup (text, len);
167 38 p->cap = len + 1;
168 38 p->end = len;
169 38 p->eof = TRUE;
170 38 p->line = line > 0 ? line : 1;
171 38 return p;
172 }
173  
174 void
175 73 bt_parser_free (bt_parser *p)
176 {
177 73 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 73 73
178 0 return;
179 73 free (p->buf);
180 73 free (p->filename);
181 73 bt_sbuf_free (&p->text);
182 73 free (p);
183 }
184  
185 void
186 73 bt_parser_set_macros (bt_parser *p, bt_macros *macros)
187 {
188 73 p->macros = macros;
189 73 }
190  
191 bt_macros *
192 167 bt_parser_macros (bt_parser *p)
193 {
194 167 return p->macros;
195 }
196  
197 const char *
198 0 bt_parser_filename (bt_parser *p)
199 {
200 0 return p->filename;
201 }
202  
203 boolean
204 0 bt_parser_read_error (bt_parser *p)
205 {
206 0 return p->read_error;
207 }
208  
209  
210 boolean
211 3 bt_parser_eof (bt_parser *p)
212 {
213 3 ▶ if (p->have_tok && p->tok != T_EOF)
Branch: 2 branches
BranchTrueFalseTotal
if (p->have_tok && p->tok != T_EOF) 0 3 3
if (p->have_tok && p->tok != T_EOF) 0 0 0
214 0 return FALSE;
215 3 ▶ return p->eof && p->pos >= p->end;
Branch: 2 branches
BranchTrueFalseTotal
return p->eof && p->pos >= p->end; 0 3 3
return p->eof && p->pos >= p->end; 0 0 0
216 }
217  
218  
219 /* ----------------------------------------------------------------------
220 * Input buffer
221 */
222  
223 static void
224 89 fill (bt_parser *p)
225 {
226 long n;
227  
228 89 ▶ if (p->eof)
Branch: 1 branch
BranchTrueFalseTotal
if (p->eof) 0 89 89
229 0 return;
230  
231 /* discard what we no longer need, then make room */
232 89 ▶ if (p->keep > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->keep > 0) 48 41 89
233 {
234 48 memmove (p->buf, p->buf + p->keep, p->end - p->keep);
235 48 p->pos -= p->keep;
236 48 p->end -= p->keep;
237 48 p->tok_pos -= p->keep < p->tok_pos ? p->keep : p->tok_pos;
238 48 p->base += (long) p->keep;
239 48 p->keep = 0;
240 }
241 89 ▶ if (p->cap - p->end < READ_CHUNK / 4)
Branch: 1 branch
BranchTrueFalseTotal
if (p->cap - p->end < READ_CHUNK / 4) 0 89 89
242 {
243 0 p->cap *= 2;
244 0 p->buf = bt_xrealloc (p->buf, p->cap);
245 }
246  
247 89 n = p->read (p->userdata, p->buf + p->end, p->cap - p->end);
248 89 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 1 88 89
249 {
250 1 p->read_error = TRUE;
251 1 p->eof = TRUE;
252 }
253 88 ▶ else if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (n == 0) 29 59 88
254 29 p->eof = TRUE;
255 else
256 59 p->end += (size_t) n;
257 }
258  
259 /* The byte k positions ahead, or -1 at end of input */
260 static int
261 190868 peek (bt_parser *p, size_t k)
262 {
263 190957 ▶ while (p->pos + k >= p->end && !p->eof)
Branch: 2 branches
BranchTrueFalseTotal
while (p->pos + k >= p->end && !p->eof) 130 190827 190957
while (p->pos + k >= p->end && !p->eof) 89 41 130
264 89 fill (p);
265 190868 ▶ if (p->pos + k >= p->end)
Branch: 1 branch
BranchTrueFalseTotal
if (p->pos + k >= p->end) 41 190827 190868
266 41 return -1;
267 190827 return (unsigned char) p->buf[p->pos + k];
268 }
269  
270 #define CUR(p) peek (p, 0)
271  
272  
273 /* ----------------------------------------------------------------------
274 * Lexical errors
275 */
276  
277 static void
278 lex_report (bt_parser *p, bt_errclass errclass, const char *fmt, ...)
279 #ifdef __GNUC__
280 __attribute__ ((format (printf, 3, 4)))
281 #endif
282 ;
283  
284 static void
285 6 lex_report (bt_parser *p, bt_errclass errclass, const char *fmt, ...)
286 {
287 va_list ap;
288  
289 6 va_start (ap, fmt);
290 6 bt_vreport (p->errs, errclass, p->filename, p->line, NULL, -1, fmt, ap);
291 6 va_end (ap);
292 6 }
293  
294 #define lexical_warning(p, ...) lex_report (p, BTERR_LEXWARN, __VA_ARGS__)
295 #define lexical_error(p, ...) lex_report (p, BTERR_LEXERR, __VA_ARGS__)
296  
297  
298 /* Copy one character from the input to out (if out is non-NULL),
299 * validating UTF-8 if asked to; returns the number of bytes consumed. */
300 static size_t
301 84942 take_char (bt_parser *p, bt_sbuf *out)
302 {
303 84942 int c = CUR (p);
304 size_t avail, len;
305 long cp;
306  
307 84942 ▶ if (c < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) 0 84942 84942
308 0 return 0;
309 84942 ▶ if (c < 0x80 && c != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x80 && c != 0) 84794 148 84942
if (c < 0x80 && c != 0) 84794 0 84794
310 {
311 84794 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 84690 104 84794
312 84690 bt_sbuf_putc (out, (char) c);
313 84794 p->pos++;
314 84794 return 1;
315 }
316  
317 148 peek (p, 3); /* make sure a whole char is here */
318 148 avail = p->end - p->pos;
319 148 len = bt_utf8_decode (p->buf + p->pos, avail, &cp);
320 148 ▶ if (cp < 0 || c == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (cp < 0 || c == 0) 132 16 148
if (cp < 0 || c == 0) 0 132 132
321 16 len = 1;
322  
323 148 ▶ if ((cp < 0 || c == 0) && (p->flags & BTP_UTF8))
Branch: 3 branches
BranchTrueFalseTotal
if ((cp < 0 || c == 0) && (p->flags & BTP_UTF8)) 132 16 148
if ((cp < 0 || c == 0) && (p->flags & BTP_UTF8)) 0 132 132
if ((cp < 0 || c == 0) && (p->flags & BTP_UTF8)) 9 7 16
324 {
325 9 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 9 0 9
326 9 bt_sbuf_puts (out, BT_REPLACEMENT_CHAR);
327 9 ▶ if (p->bad_utf8_line != p->line)
Branch: 1 branch
BranchTrueFalseTotal
if (p->bad_utf8_line != p->line) 5 4 9
328 {
329 5 lexical_warning (p, "invalid UTF-8 byte sequence "
330 "(replaced with U+FFFD)");
331 5 p->bad_utf8_line = p->line;
332 }
333 }
334 139 ▶ else if (out)
Branch: 1 branch
BranchTrueFalseTotal
else if (out) 139 0 139
335 139 bt_sbuf_putn (out, p->buf + p->pos, len);
336  
337 148 p->pos += len;
338 148 return len;
339 }
340  
341  
342 /* ----------------------------------------------------------------------
343 * Lexer state and actions
344 */
345  
346 static void
347 165 initialize_lexer_state (bt_parser *p)
348 {
349 165 p->mode = M_START;
350 165 p->state = S_TOPLEVEL;
351 165 p->entry_opener = 0;
352 165 p->metatype = BTE_UNKNOWN;
353 165 p->junk_count = 0;
354 165 }
355  
356 /* Characters allowed in names (entry types, keys, field and macro names)
357 * in ENTRY mode. This matches the scanner tables of the original
358 * btparse, which allowed all printable ASCII except the BibTeX
359 * punctuation, plus all non-ASCII bytes (so UTF-8 names work). */
360 static boolean
361 11283 is_name_char (int c)
362 {
363 11283 ▶ if (c >= 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 0x80) 24 11259 11283
364 24 return TRUE;
365 11259 ▶ if (c <= 32 || c == 127)
Branch: 2 branches
BranchTrueFalseTotal
if (c <= 32 || c == 127) 10542 717 11259
if (c <= 32 || c == 127) 0 10542 10542
366 717 return FALSE;
367 10542 return strchr ("\"#%'(),={}\\", c) == NULL;
368 }
369  
370 static boolean
371 0 is_invalid_entry_char (int c)
372 {
373 0 ▶ return (c >= 0 && c < 32 && c != '\t' && c != '\n' && c != '\r')
Branch: 4 branches
BranchTrueFalseTotal
return (c >= 0 && c < 32 && c != '\t' && c != '\n' && c != '\r') 0 0 0
return (c >= 0 && c < 32 && c != '\t' && c != '\n' && c != '\r') 0 0 0
return (c >= 0 && c < 32 && c != '\t' && c != '\n' && c != '\r') 0 0 0
return (c >= 0 && c < 32 && c != '\t' && c != '\n' && c != '\r') 0 0 0
374 0 ▶ || c == '\'' || c == '\\' || c == 127;
Branch: 4 branches
BranchTrueFalseTotal
|| c == '\'' || c == '\\' || c == 127; 0 0 0
|| c == '\'' || c == '\\' || c == 127; 0 0 0
|| c == '\'' || c == '\\' || c == 127; 0 0 0
|| c == '\'' || c == '\\' || c == 127; 0 0 0
375 }
376  
377 /* Set the metatype when the entry type has been seen */
378 static void
379 1049 name_action (bt_parser *p)
380 {
381 1049 ▶ if (p->state == S_AFTER_AT)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_AFTER_AT) 170 879 1049
382 {
383 170 const char * etype = p->text.s;
384  
385 170 p->state = S_AFTER_TYPE;
386 170 ▶ if (bt_ascii_strcasecmp (etype, "comment") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (bt_ascii_strcasecmp (etype, "comment") == 0) 8 162 170
387 {
388 8 p->metatype = BTE_COMMENT;
389 8 p->state = S_IN_COMMENT;
390 }
391 162 ▶ else if (bt_ascii_strcasecmp (etype, "preamble") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (bt_ascii_strcasecmp (etype, "preamble") == 0) 2 160 162
392 2 p->metatype = BTE_PREAMBLE;
393 160 ▶ else if (bt_ascii_strcasecmp (etype, "string") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (bt_ascii_strcasecmp (etype, "string") == 0) 18 142 160
394 18 p->metatype = BTE_MACRODEF;
395 else
396 142 p->metatype = BTE_REGULAR;
397 }
398 1049 }
399  
400 /* Called at each newline in a string: if what follows the newline looks
401 * like the start of a field ("name =") the string is probably a runaway
402 * (eg. a missing closing quote), so warn -- once per string. `chunk' is
403 * the newline plus the text up to the next brace, parenthesis, quote,
404 * backslash or newline, already converted to spaces. */
405 static void
406 8 check_runaway_string (bt_parser *p, const char *chunk, size_t len)
407 {
408 static const char alpha_chars[] = "abcdefghijklmnopqrstuvwxyz";
409 static const char name_chars[] = "abcdefghijklmnopqrstuvwxyz0123456789:+/'.-";
410 8 enum { none, entry, field, giveup } guess = none;
411 8 size_t i = 1;
412  
413 8 ▶ if (p->apparent_runaway)
Branch: 1 branch
BranchTrueFalseTotal
if (p->apparent_runaway) 0 8 8
414 0 return;
415  
416 #define IN(set, c) ((c) != 0 && strchr (set, BT_TOLOWER (c)) != NULL)
417  
418 47 ▶ while (i < len && chunk[i] == ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && chunk[i] == ' ') i++; 44 3 47
while (i < len && chunk[i] == ' ') i++; 39 5 44
419 8 ▶ if (i < len && chunk[i] == '@')
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && chunk[i] == '@') 5 3 8
if (i < len && chunk[i] == '@') 1 4 5
420 {
421 1 i++;
422 1 ▶ while (i < len && chunk[i] == ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && chunk[i] == ' ') i++; 1 0 1
while (i < len && chunk[i] == ' ') i++; 0 1 1
423 1 guess = entry;
424 }
425  
426 8 ▶ if (i < len && IN (alpha_chars, chunk[i]))
Branch: 5 branches
BranchTrueFalseTotal
if (i < len && IN (alpha_chars, chunk[i])) 5 3 8
if (i < len && IN (alpha_chars, chunk[i])) 5 0 5
if (i < len && IN (alpha_chars, chunk[i])) 5 0 5
if (i < len && IN (alpha_chars, chunk[i])) 3 2 5
if (i < len && IN (alpha_chars, chunk[i])) 5 0 5
427 {
428 32 ▶ while (i < len && IN (name_chars, chunk[i]))
Branch: 5 branches
BranchTrueFalseTotal
while (i < len && IN (name_chars, chunk[i])) 30 2 32
while (i < len && IN (name_chars, chunk[i])) 30 0 30
while (i < len && IN (name_chars, chunk[i])) 27 3 30
while (i < len && IN (name_chars, chunk[i])) 8 19 27
while (i < len && IN (name_chars, chunk[i])) 27 3 30
429 27 i++;
430 6 ▶ while (i < len && chunk[i] == ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && chunk[i] == ' ') i++; 4 2 6
while (i < len && chunk[i] == ' ') i++; 1 3 4
431 5 ▶ if (i == len)
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) 2 3 5
432 2 guess = giveup;
433 3 ▶ else if (guess == entry)
Branch: 1 branch
BranchTrueFalseTotal
else if (guess == entry) 0 3 3
434 {
435 0 ▶ if (chunk[i] != '{' && chunk[i] != '(')
Branch: 2 branches
BranchTrueFalseTotal
if (chunk[i] != '{' && chunk[i] != '(') 0 0 0
if (chunk[i] != '{' && chunk[i] != '(') 0 0 0
436 0 guess = giveup;
437 }
438 else
439 3 ▶ guess = (chunk[i] == '=') ? field : giveup;
Branch: 1 branch
BranchTrueFalseTotal
guess = (chunk[i] == '=') ? field : giveup; 1 2 3
440 }
441 else
442 3 guess = giveup;
443 #undef IN
444  
445 8 ▶ if (guess != giveup)
Branch: 1 branch
BranchTrueFalseTotal
if (guess != giveup) 1 7 8
446 {
447 1 lexical_warning (p, "possible runaway string started at line %d",
448 p->string_start);
449 1 p->apparent_runaway = TRUE;
450 }
451 }
452  
453 static void
454 677 end_string (bt_parser *p)
455 {
456 677 ▶ if (p->brace_depth > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->brace_depth > 0) 0 677 677
457 {
458 0 lexical_error (p, "unbalanced braces: too many {'s");
459 0 p->brace_depth = 0;
460 }
461 677 p->string_opener = 0;
462 677 p->string_start = -1;
463 677 p->tok = T_STRING;
464  
465 677 ▶ if (p->state == S_IN_COMMENT)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_IN_COMMENT) 3 674 677
466 {
467 /* convert @comment(...) to the standard {...} delimiters */
468 3 ▶ if (p->text.s[0] == '(')
Branch: 1 branch
BranchTrueFalseTotal
if (p->text.s[0] == '(') 2 1 3
469 {
470 2 p->text.s[0] = '{';
471 2 p->text.s[p->text.len - 1] = '}';
472 }
473 3 p->state = S_TOPLEVEL;
474 3 p->mode = M_START;
475 }
476 else
477 674 p->mode = M_ENTRY;
478 677 }
479  
480 /* Lex the body of a string (the opening delimiter has been consumed and
481 * put in p->text). Returns FALSE if end of input came first. */
482 static boolean
483 679 lex_string (bt_parser *p)
484 {
485 for (;;)
486 894 {
487 1573 int c = CUR (p);
488  
489 1573 switch (c)
490 {
491 2 case -1:
492 2 return FALSE;
493  
494 8 case '\n':
495 {
496 /* newline plus following text up to a delimiter or
497 * backslash, with all whitespace turned into spaces */
498 8 size_t chunk = p->text.len;
499  
500 8 p->pos++;
501 8 p->line++;
502 8 bt_sbuf_putc (&p->text, ' ');
503 121 ▶ while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"\\", c))
Branch: 3 branches
BranchTrueFalseTotal
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"\\", c)) 119 2 121
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"\\", c)) 0 119 119
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"\\", c)) 113 6 119
504 {
505 113 ▶ if (c >= 9 && c <= 13)
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 9 && c <= 13) 113 0 113
if (c >= 9 && c <= 13) 0 113 113
506 {
507 0 bt_sbuf_putc (&p->text, ' ');
508 0 p->pos++;
509 }
510 else
511 113 take_char (p, &p->text);
512 }
513 8 check_runaway_string (p, p->text.s + chunk, p->text.len - chunk);
514 8 break;
515 }
516  
517 47 case '{':
518 47 p->brace_depth++;
519 47 bt_sbuf_putc (&p->text, '{');
520 47 p->pos++;
521 47 break;
522  
523 672 case '}':
524 672 p->brace_depth--;
525 672 bt_sbuf_putc (&p->text, '}');
526 672 p->pos++;
527 672 ▶ if (p->string_opener == '{' && p->brace_depth == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p->string_opener == '{' && p->brace_depth == 0) 668 4 672
if (p->string_opener == '{' && p->brace_depth == 0) 625 43 668
528 {
529 625 end_string (p);
530 625 return TRUE;
531 }
532 47 ▶ if (p->brace_depth < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->brace_depth < 0) 0 47 47
533 {
534 0 lexical_error (p, "unbalanced braces: too many }'s");
535 0 p->brace_depth = 0;
536 }
537 47 break;
538  
539 10 case '(':
540 10 p->paren_depth++;
541 10 bt_sbuf_putc (&p->text, '(');
542 10 p->pos++;
543 10 break;
544  
545 12 case ')':
546 12 p->paren_depth--;
547 12 bt_sbuf_putc (&p->text, ')');
548 12 p->pos++;
549 12 ▶ if (p->string_opener == '(' && p->paren_depth == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p->string_opener == '(' && p->paren_depth == 0) 3 9 12
if (p->string_opener == '(' && p->paren_depth == 0) 2 1 3
550 {
551 2 end_string (p);
552 2 return TRUE;
553 }
554 10 break;
555  
556 58 case '"':
557 58 bt_sbuf_putc (&p->text, '"');
558 58 p->pos++;
559 58 ▶ if (p->string_opener == '"' && p->brace_depth == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p->string_opener == '"' && p->brace_depth == 0) 51 7 58
if (p->string_opener == '"' && p->brace_depth == 0) 50 1 51
560 {
561 50 end_string (p);
562 50 return TRUE;
563 }
564 8 break;
565  
566 0 case '\r':
567 case '\t':
568 {
569 /* A lone tab/CR before a delimiter becomes a space; otherwise
570 * it is just part of the text (as in the original lexer). */
571 0 int next = peek (p, 1);
572 0 ▶ if (next < 0 || ISIN ("\n{}()\"", next))
Branch: 3 branches
BranchTrueFalseTotal
if (next < 0 || ISIN ("\n{}()\"", next)) 0 0 0
if (next < 0 || ISIN ("\n{}()\"", next)) 0 0 0
if (next < 0 || ISIN ("\n{}()\"", next)) 0 0 0
573 {
574 0 bt_sbuf_putc (&p->text, ' ');
575 0 p->pos++;
576 0 break;
577 }
578 }
579 /* FALLTHROUGH */
580 default:
581 78367 ▶ while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"", c))
Branch: 3 branches
BranchTrueFalseTotal
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"", c)) 78367 0 78367
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"", c)) 0 78367 78367
while ((c = CUR (p)) >= 0 && !ISIN ("\n{}()\"", c)) 77603 764 78367
582 77603 take_char (p, &p->text);
583 764 break;
584 }
585 }
586 }
587  
588 /* Start a string at the current character (one of { " ( ). Returns the
589 * token: T_STRING, T_QUOTE (for the @comment"..." error), or T_EOF. */
590 static token
591 679 start_string (bt_parser *p, char opener)
592 {
593 679 p->string_opener = opener;
594 679 p->brace_depth = (opener == '{');
595 679 p->paren_depth = (opener == '(');
596 679 p->string_start = p->line;
597 679 p->apparent_runaway = FALSE;
598 679 bt_sbuf_putc (&p->text, opener);
599 679 p->pos++;
600  
601 679 ▶ if (opener == '"' && p->state == S_IN_COMMENT)
Branch: 2 branches
BranchTrueFalseTotal
if (opener == '"' && p->state == S_IN_COMMENT) 50 629 679
if (opener == '"' && p->state == S_IN_COMMENT) 0 50 50
602 {
603 0 lexical_error (p, "comment entries must be delimited by either "
604 "braces or parentheses");
605 0 p->state = S_TOPLEVEL;
606 0 p->mode = M_START;
607 0 return T_QUOTE;
608 }
609 679 ▶ if (p->state != S_IN_ENTRY && p->state != S_IN_COMMENT)
Branch: 2 branches
BranchTrueFalseTotal
if (p->state != S_IN_ENTRY && p->state != S_IN_COMMENT) 5 674 679
if (p->state != S_IN_ENTRY && p->state != S_IN_COMMENT) 0 5 5
610 0 lexical_warning (p, "start of string seen at weird place");
611  
612 679 p->mode = M_STRING;
613 679 ▶ if (!lex_string (p))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_string (p)) 2 677 679
614 {
615 2 bt_sbuf_clear (&p->text);
616 2 return T_EOF;
617 }
618 677 return T_STRING;
619 }
620  
621 /* Skip a %-comment if there is a newline to end it; returns FALSE (and
622 * consumes nothing) if not. */
623 static boolean
624 14 skip_comment (bt_parser *p)
625 {
626 14 size_t k = 1;
627 int c;
628  
629 660 ▶ while ((c = peek (p, k)) >= 0 && c != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while ((c = peek (p, k)) >= 0 && c != '\n') 660 0 660
while ((c = peek (p, k)) >= 0 && c != '\n') 646 14 660
630 646 k++;
631 14 ▶ if (c < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) 0 14 14
632 0 return FALSE;
633 14 p->pos += k + 1;
634 14 p->line++;
635 14 return TRUE;
636 }
637  
638 /* Lex the next token in START mode */
639 static token
640 200 lex_start (bt_parser *p)
641 {
642 /* skip a byte order mark at the very start of the input */
643 200 ▶ if (p->base + (long) p->pos == 0 && CUR (p) == 0xEF
Branch: 2 branches
BranchTrueFalseTotal
if (p->base + (long) p->pos == 0 && CUR (p) == 0xEF 73 127 200
if (p->base + (long) p->pos == 0 && CUR (p) == 0xEF 1 72 73
644 1 ▶ && peek (p, 1) == 0xBB && peek (p, 2) == 0xBF)
Branch: 2 branches
BranchTrueFalseTotal
&& peek (p, 1) == 0xBB && peek (p, 2) == 0xBF) 1 0 1
&& peek (p, 1) == 0xBB && peek (p, 2) == 0xBF) 1 0 1
645 1 p->pos += 3;
646  
647 for (;;)
648 245 {
649 int c;
650  
651 445 p->tok_pos = p->pos;
652 445 p->keep = p->pos;
653 445 c = CUR (p);
654  
655 445 switch (c)
656 {
657 30 case -1:
658 30 return T_EOF;
659 170 case '@':
660 170 p->pos++;
661 170 bt_sbuf_putc (&p->text, '@');
662 170 ▶ if (p->state == S_TOPLEVEL)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_TOPLEVEL) 170 0 170
663 {
664 170 p->state = S_AFTER_AT;
665 170 p->mode = M_ENTRY;
666 170 ▶ if (p->junk_count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->junk_count > 0) 0 170 170
667 {
668 0 lexical_warning (p, "%ld characters of junk seen at "
669 "toplevel", p->junk_count);
670 0 p->junk_count = 0;
671 }
672 }
673 else
674 0 lexical_warning (p, "\"@\" in strange place -- should get "
675 "syntax error");
676 170 return T_AT;
677 212 case '\n':
678 212 p->pos++;
679 212 p->line++;
680 212 break;
681 11 case ' ':
682 case '\r':
683 case '\t':
684 11 p->pos++;
685 11 break;
686 8 case '%':
687 8 ▶ if (skip_comment (p))
Branch: 1 branch
BranchTrueFalseTotal
if (skip_comment (p)) 8 0 8
688 8 break;
689 /* FALLTHROUGH: unterminated comment at end of input is junk */
690 default:
691 86 ▶ while ((c = CUR (p)) >= 0 && !ISIN ("@\n \r\t", c))
Branch: 3 branches
BranchTrueFalseTotal
while ((c = CUR (p)) >= 0 && !ISIN ("@\n \r\t", c)) 84 2 86
while ((c = CUR (p)) >= 0 && !ISIN ("@\n \r\t", c)) 0 84 84
while ((c = CUR (p)) >= 0 && !ISIN ("@\n \r\t", c)) 72 12 84
692 {
693 72 take_char (p, NULL);
694 72 p->junk_count++;
695 }
696 14 break;
697 }
698 }
699 }
700  
701 /* Lex the next token in ENTRY mode */
702 static token
703 3492 lex_entry (bt_parser *p)
704 {
705 for (;;)
706 12724 {
707 int c;
708  
709 16216 p->tok_pos = p->pos;
710 16216 p->keep = p->pos;
711 16216 p->tok_start_line = p->line;
712 16216 c = CUR (p);
713  
714 16216 ▶ if (c == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) 1 16215 16216
715 1 return T_EOF;
716 16215 ▶ if (c == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') 771 15444 16215
717 {
718 771 p->pos++;
719 771 p->line++;
720 771 continue;
721 }
722 15444 ▶ if (c == ' ' || c == '\r' || c == '\t')
Branch: 3 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\r' || c == '\t') 3497 11947 15444
if (c == ' ' || c == '\r' || c == '\t') 3497 0 3497
if (c == ' ' || c == '\r' || c == '\t') 0 3497 3497
723 {
724 11947 p->pos++;
725 11947 continue;
726 }
727 3497 ▶ if (c == '%')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '%') 6 3491 3497
728 {
729 6 ▶ if (!skip_comment (p)) /* comment runs to end of input */
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment (p)) /* comment runs to end of input */ 0 6 6
730 0 p->pos = p->end;
731 6 continue;
732 }
733  
734 3491 ▶ if (BT_IS_DIGIT (c) || is_name_char (c))
Branch: 3 branches
BranchTrueFalseTotal
if (BT_IS_DIGIT (c) || is_name_char (c)) 2696 795 3491
if (BT_IS_DIGIT (c) || is_name_char (c)) 2672 24 2696
if (BT_IS_DIGIT (c) || is_name_char (c)) 1049 2418 3467
735 {
736 1073 boolean all_digits = TRUE;
737  
738 8195 ▶ while ((c = CUR (p)) >= 0 && (BT_IS_DIGIT (c) || is_name_char (c)))
Branch: 4 branches
BranchTrueFalseTotal
while ((c = CUR (p)) >= 0 && (BT_IS_DIGIT (c) || is_name_char (c))) 8195 0 8195
while ((c = CUR (p)) >= 0 && (BT_IS_DIGIT (c) || is_name_char (c))) 7290 905 8195
while ((c = CUR (p)) >= 0 && (BT_IS_DIGIT (c) || is_name_char (c))) 379 6911 7290
while ((c = CUR (p)) >= 0 && (BT_IS_DIGIT (c) || is_name_char (c))) 6743 1073 7816
739 {
740 7122 ▶ if (!BT_IS_DIGIT (c))
Branch: 2 branches
BranchTrueFalseTotal
if (!BT_IS_DIGIT (c)) 7122 0 7122
if (!BT_IS_DIGIT (c)) 6743 379 7122
741 6743 all_digits = FALSE;
742 7122 take_char (p, &p->text);
743 }
744 1073 ▶ if (all_digits)
Branch: 1 branch
BranchTrueFalseTotal
if (all_digits) 24 1049 1073
745 24 return T_NUMBER;
746 1049 name_action (p);
747 1049 return T_NAME;
748 }
749  
750 2418 switch (c)
751 {
752 786 case '{':
753 786 ▶ if (p->state == S_IN_ENTRY || p->state == S_IN_COMMENT)
Branch: 2 branches
BranchTrueFalseTotal
if (p->state == S_IN_ENTRY || p->state == S_IN_COMMENT) 162 624 786
if (p->state == S_IN_ENTRY || p->state == S_IN_COMMENT) 3 159 162
754 627 return start_string (p, '{');
755 159 p->pos++;
756 159 bt_sbuf_putc (&p->text, '{');
757 159 ▶ if (p->state == S_AFTER_TYPE)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_AFTER_TYPE) 159 0 159
758 {
759 159 p->state = S_IN_ENTRY;
760 159 p->entry_opener = '{';
761 159 return T_ENTRY_OPEN;
762 }
763 0 lexical_warning (p, "\"{\" in strange place -- should get a "
764 "syntax error");
765 0 return T_LBRACE;
766  
767 158 case '}':
768 158 p->pos++;
769 158 bt_sbuf_putc (&p->text, '}');
770 158 ▶ if (p->state == S_IN_ENTRY)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_IN_ENTRY) 158 0 158
771 {
772 158 ▶ if (p->entry_opener == '(')
Branch: 1 branch
BranchTrueFalseTotal
if (p->entry_opener == '(') 0 158 158
773 0 lexical_warning (p, "entry started with \"(\", but ends "
774 "with \"}\"");
775 158 initialize_lexer_state (p);
776 158 return T_ENTRY_CLOSE;
777 }
778 0 lexical_warning (p, "\"}\" in strange place -- should get a "
779 "syntax error");
780 0 return T_RBRACE;
781  
782 5 case '(':
783 5 ▶ if (p->state == S_IN_COMMENT)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_IN_COMMENT) 2 3 5
784 2 return start_string (p, '(');
785 3 p->pos++;
786 3 bt_sbuf_putc (&p->text, '(');
787 3 ▶ if (p->state == S_AFTER_TYPE)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_AFTER_TYPE) 3 0 3
788 {
789 3 p->state = S_IN_ENTRY;
790 3 p->entry_opener = '(';
791 }
792 else
793 0 lexical_warning (p, "\"(\" in strange place -- should get a "
794 "syntax error");
795 3 return T_ENTRY_OPEN;
796  
797 3 case ')':
798 3 p->pos++;
799 3 bt_sbuf_putc (&p->text, ')');
800 3 ▶ if (p->state == S_IN_ENTRY)
Branch: 1 branch
BranchTrueFalseTotal
if (p->state == S_IN_ENTRY) 3 0 3
801 {
802 3 ▶ if (p->entry_opener == '{')
Branch: 1 branch
BranchTrueFalseTotal
if (p->entry_opener == '{') 0 3 3
803 0 lexical_warning (p, "entry started with \"{\", but ends "
804 "with \")\"");
805 3 initialize_lexer_state (p);
806 }
807 else
808 0 lexical_warning (p, "\")\" in strange place -- should get a "
809 "syntax error");
810 3 return T_ENTRY_CLOSE;
811  
812 679 case '=':
813 679 p->pos++;
814 679 bt_sbuf_putc (&p->text, '=');
815 679 return T_EQUALS;
816 51 case '#':
817 51 p->pos++;
818 51 bt_sbuf_putc (&p->text, '#');
819 51 return T_HASH;
820 686 case ',':
821 686 p->pos++;
822 686 bt_sbuf_putc (&p->text, ',');
823 686 return T_COMMA;
824 50 case '"':
825 50 return start_string (p, '"');
826 }
827  
828 /* anything else is not a valid token here: report and skip it */
829 0 ▶ if (is_invalid_entry_char (c))
Branch: 1 branch
BranchTrueFalseTotal
if (is_invalid_entry_char (c)) 0 0 0
830 {
831 0 ▶ if (c < 32 || c == 127)
Branch: 2 branches
BranchTrueFalseTotal
if (c < 32 || c == 127) 0 0 0
if (c < 32 || c == 127) 0 0 0
832 0 lexical_warning (p, "invalid token (character \\x%02X)", c);
833 else
834 0 lexical_warning (p, "invalid token (text was '%c')", c);
835 }
836 0 p->pos++;
837 }
838 }
839  
840 /* Read the next token into the lookahead */
841 static void
842 3692 lex (bt_parser *p)
843 {
844 3692 bt_sbuf_clear (&p->text);
845 3692 p->tok_start_line = p->line;
846 3692 ▶ if (p->mode == M_START)
Branch: 1 branch
BranchTrueFalseTotal
if (p->mode == M_START) 200 3492 3692
847 200 p->tok = lex_start (p);
848 else
849 3492 p->tok = lex_entry (p);
850 3692 p->tok_line = p->line;
851 3692 p->have_tok = TRUE;
852 3692 }
853  
854 #define consume(p) lex (p)
855  
856  
857 /* ----------------------------------------------------------------------
858 * Syntax errors
859 */
860  
861 static const char *
862 20 token_name (token t)
863 {
864 20 switch (t)
865 {
866 4 case T_AT: return "\"@\"";
867 0 case T_NAME: return "name (entry type, key, field, or macro name)";
868 0 case T_NUMBER: return "number";
869 0 case T_LBRACE: return "left brace (\"{\")";
870 0 case T_RBRACE: return "right brace (\"}\")";
871 2 case T_ENTRY_OPEN: return "start of entry (\"{\" or \"(\")";
872 1 case T_ENTRY_CLOSE: return "end of entry (\"}\" or \")\")";
873 10 case T_EQUALS: return "\"=\"";
874 0 case T_HASH: return "\"#\"";
875 1 case T_COMMA: return "\",\"";
876 2 case T_STRING: return "quoted string ({...} or \"...\")";
877 0 default: return "token";
878 }
879 }
880  
881 static void
882 syntax_error (bt_parser *p, const char *fmt, ...)
883 #ifdef __GNUC__
884 __attribute__ ((format (printf, 2, 3)))
885 #endif
886 ;
887  
888 static void
889 18 syntax_error (bt_parser *p, const char *fmt, ...)
890 {
891 va_list ap;
892  
893 18 va_start (ap, fmt);
894 18 bt_vreport (p->errs, BTERR_SYNTAX, p->filename, p->line, NULL, -1, fmt, ap);
895 18 va_end (ap);
896 18 }
897  
898 /* Report "found X, expected Y" for the current token. `expected' lists
899 * the acceptable tokens (in the order they appear in messages). */
900 static void
901 18 expected (bt_parser *p, const token *expect, int nexpect, const char *suffix)
902 {
903 bt_sbuf msg;
904 int i;
905  
906 18 bt_sbuf_init (&msg);
907 18 ▶ if (p->tok == T_EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_EOF) 7 11 18
908 7 bt_sbuf_puts (&msg, "at end of input");
909 else
910 {
911 11 size_t n = p->text.len;
912 11 bt_sbuf_puts (&msg, "found \"");
913 11 ▶ if (n > MAX_BAD_TEXT)
Branch: 1 branch
BranchTrueFalseTotal
if (n > MAX_BAD_TEXT) 1 10 11
914 {
915 1 n = MAX_BAD_TEXT;
916 1 ▶ while (n > 0 && (p->text.s[n] & 0xC0) == 0x80)
Branch: 2 branches
BranchTrueFalseTotal
while (n > 0 && (p->text.s[n] & 0xC0) == 0x80) 1 0 1
while (n > 0 && (p->text.s[n] & 0xC0) == 0x80) 0 1 1
917 0 n--; /* don't split a UTF-8 character */
918 1 bt_sbuf_putn (&msg, p->text.s, n);
919 1 bt_sbuf_puts (&msg, "...");
920 }
921 else
922 10 bt_sbuf_putn (&msg, p->text.s, n);
923 11 bt_sbuf_puts (&msg, "\"");
924 }
925  
926 18 ▶ bt_sbuf_puts (&msg, nexpect > 1 ? ", expected one of: " : ", expected ");
Branch: 1 branch
BranchTrueFalseTotal
bt_sbuf_puts (&msg, nexpect > 1 ? ", expected one of: " : ", expected "); 2 16 18
927 38 ▶ for (i = 0; i < nexpect; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nexpect; i++) 20 18 38
928 {
929 20 bt_sbuf_puts (&msg, token_name (expect[i]));
930 20 ▶ if (i < nexpect - 2)
Branch: 1 branch
BranchTrueFalseTotal
if (i < nexpect - 2) 0 20 20
931 0 bt_sbuf_puts (&msg, ", ");
932 20 ▶ else if (i == nexpect - 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (i == nexpect - 2) 2 18 20
933 2 bt_sbuf_puts (&msg, " or ");
934 }
935 18 ▶ if (suffix)
Branch: 1 branch
BranchTrueFalseTotal
if (suffix) 1 17 18
936 1 bt_sbuf_puts (&msg, suffix);
937  
938 18 syntax_error (p, "%s", msg.s);
939 18 bt_sbuf_free (&msg);
940 18 }
941  
942 #define EXPECT1(p, t, suffix) do { \
943 token _e[1] = { t }; \
944 expected (p, _e, 1, suffix); \
945 } while (0)
946  
947 static const token body_set[] = { T_ENTRY_OPEN, T_STRING };
948 static const token contents_set[] = { T_NUMBER, T_NAME, T_ENTRY_CLOSE, T_STRING };
949 static const token fields_set[] = { T_NAME, T_ENTRY_CLOSE };
950 static const token value_set[] = { T_NUMBER, T_NAME, T_STRING };
951  
952 #define NELEM(a) ((int) (sizeof (a) / sizeof ((a)[0])))
953  
954 static const char skipping[] = " (skipping to next \"@\")";
955  
956  
957 /* ----------------------------------------------------------------------
958 * Error recovery
959 */
960  
961 static boolean
962 717 at_name (bt_parser *p)
963 {
964 717 ▶ return p->tok == T_NAME && p->text.s[0] == '@';
Branch: 2 branches
BranchTrueFalseTotal
return p->tok == T_NAME && p->text.s[0] == '@'; 690 27 717
return p->tok == T_NAME && p->text.s[0] == '@'; 0 690 690
965 }
966  
967 /* Back up to the '@' at the start of the current (name) token and
968 * restart lexing there at top level, abandoning the current entry. */
969 static void
970 0 restart_at (bt_parser *p)
971 {
972 0 p->pos = p->tok_pos;
973 0 p->line = p->tok_start_line;
974 0 initialize_lexer_state (p);
975 0 lex (p); /* the '@' */
976 0 p->bail = TRUE;
977 0 }
978  
979 /* A name starting with '@' where the parser would accept a name almost
980 * certainly means the entry was not closed: report it and restart. */
981 static boolean
982 692 check_at (bt_parser *p)
983 {
984 692 ▶ if (!at_name (p))
Branch: 1 branch
BranchTrueFalseTotal
if (!at_name (p)) 692 0 692
985 692 return FALSE;
986 0 EXPECT1 (p, T_ENTRY_CLOSE, skipping);
987 0 restart_at (p);
988 0 return TRUE;
989 }
990  
991 /* Skip tokens until one in `set' (or end of input) */
992 static void
993 14 resync (bt_parser *p, unsigned set)
994 {
995 for (;;)
996 {
997 25 ▶ if (at_name (p))
Branch: 1 branch
BranchTrueFalseTotal
if (at_name (p)) 0 25 25
998 {
999 0 restart_at (p);
1000 0 return;
1001 }
1002 25 ▶ if ((set & TOKSET (p->tok)) || p->tok == T_EOF)
Branch: 2 branches
BranchTrueFalseTotal
if ((set & TOKSET (p->tok)) || p->tok == T_EOF) 15 10 25
if ((set & TOKSET (p->tok)) || p->tok == T_EOF) 4 11 15
1003 14 return;
1004 11 consume (p);
1005 }
1006 }
1007  
1008  
1009 /* ----------------------------------------------------------------------
1010 * The parser proper
1011 */
1012  
1013 static void
1014 1566 append_child (AST *parent, AST *child)
1015 {
1016 1566 AST ** link = &parent->down;
1017 4044 ▶ while (*link)
Branch: 1 branch
BranchTrueFalseTotal
while (*link) 2478 1566 4044
1018 2478 link = &(*link)->right;
1019 1566 *link = child;
1020 1566 }
1021  
1022 static AST *
1023 1736 token_node (bt_parser *p, bt_nodetype nodetype)
1024 {
1025 1736 ▶ const char * text = p->text.s ? p->text.s : "";
Branch: 1 branch
BranchTrueFalseTotal
const char * text = p->text.s ? p->text.s : ""; 1736 0 1736
1026 1736 size_t len = p->text.len;
1027  
1028 1736 ▶ if (nodetype == BTAST_STRING && len >= 2) /* strip the delimiters */
Branch: 2 branches
BranchTrueFalseTotal
if (nodetype == BTAST_STRING && len >= 2) /* strip the delimiters */ 667 1069 1736
if (nodetype == BTAST_STRING && len >= 2) /* strip the delimiters */ 667 0 667
1029 {
1030 667 text++;
1031 667 len -= 2;
1032 }
1033 3472 return bt_new_ast (nodetype, text, len, p->filename, p->tok_line,
1034 1736 p->base + (long) p->tok_pos);
1035 }
1036  
1037 static void
1038 732 simple_value (bt_parser *p, AST *parent)
1039 {
1040 732 switch (p->tok)
1041 {
1042 664 case T_STRING:
1043 664 append_child (parent, token_node (p, BTAST_STRING));
1044 664 consume (p);
1045 664 break;
1046 24 case T_NUMBER:
1047 24 append_child (parent, token_node (p, BTAST_NUMBER));
1048 24 consume (p);
1049 24 break;
1050 44 case T_NAME:
1051 44 append_child (parent, token_node (p, BTAST_MACRO));
1052 44 consume (p);
1053 44 break;
1054 0 default:
1055 0 expected (p, value_set, NELEM (value_set), NULL);
1056 0 resync (p, TOKSET (T_ENTRY_CLOSE) | TOKSET (T_HASH) | TOKSET (T_COMMA));
1057 }
1058 732 }
1059  
1060 static void
1061 681 value (bt_parser *p, AST *parent)
1062 {
1063 681 simple_value (p, parent);
1064 732 ▶ while (!p->bail && p->tok == T_HASH)
Branch: 2 branches
BranchTrueFalseTotal
while (!p->bail && p->tok == T_HASH) 732 0 732
while (!p->bail && p->tok == T_HASH) 51 681 732
1065 {
1066 51 consume (p);
1067 51 simple_value (p, parent);
1068 }
1069 681 }
1070  
1071 static void
1072 689 field (bt_parser *p, AST *entry)
1073 {
1074 689 AST * f = token_node (p, BTAST_FIELD);
1075  
1076 689 append_child (entry, f);
1077 689 ▶ if (BT_IS_DIGIT (f->text[0]))
Branch: 2 branches
BranchTrueFalseTotal
if (BT_IS_DIGIT (f->text[0])) 688 1 689
if (BT_IS_DIGIT (f->text[0])) 0 688 688
1078 0 syntax_error (p, "invalid field name \"%s\": cannot start with digit",
1079 f->text);
1080 689 consume (p);
1081  
1082 689 ▶ if (p->tok != T_EQUALS)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok != T_EQUALS) 10 679 689
1083 {
1084 10 EXPECT1 (p, T_EQUALS, NULL);
1085 10 resync (p, TOKSET (T_ENTRY_CLOSE) | TOKSET (T_COMMA));
1086 10 return;
1087 }
1088 679 consume (p);
1089 679 value (p, f);
1090 }
1091  
1092 static void
1093 158 fields (bt_parser *p, AST *entry)
1094 {
1095 for (;;)
1096 {
1097 704 ▶ if (p->tok == T_NAME)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_NAME) 689 15 704
1098 {
1099 689 ▶ if (check_at (p))
Branch: 1 branch
BranchTrueFalseTotal
if (check_at (p)) 0 689 689
1100 0 return;
1101 689 field (p, entry);
1102 689 ▶ if (p->bail || p->tok != T_COMMA)
Branch: 2 branches
BranchTrueFalseTotal
if (p->bail || p->tok != T_COMMA) 689 0 689
if (p->bail || p->tok != T_COMMA) 143 546 689
1103 143 return;
1104 546 consume (p);
1105 546 continue;
1106 }
1107 15 ▶ if (p->tok == T_ENTRY_CLOSE)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_ENTRY_CLOSE) 15 0 15
1108 15 return;
1109 0 expected (p, fields_set, NELEM (fields_set), NULL);
1110 0 resync (p, TOKSET (T_ENTRY_CLOSE));
1111 0 return;
1112 }
1113 }
1114  
1115 static void
1116 162 contents (bt_parser *p, AST *entry)
1117 {
1118 162 switch (entry->metatype)
1119 {
1120 142 case BTE_REGULAR:
1121 142 ▶ if (p->tok == T_NAME || p->tok == T_NUMBER)
Branch: 2 branches
BranchTrueFalseTotal
if (p->tok == T_NAME || p->tok == T_NUMBER) 0 142 142
if (p->tok == T_NAME || p->tok == T_NUMBER) 0 0 0
1122 {
1123 142 append_child (entry, token_node (p, BTAST_KEY));
1124 142 consume (p);
1125 142 ▶ if (p->tok == T_ENTRY_CLOSE) /* no fields, eg. @misc{key}, */
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_ENTRY_CLOSE) /* no fields, eg. @misc{key}, */ 1 141 142
1126 1 return; /* which BibTeX accepts */
1127 141 ▶ if (p->tok != T_COMMA)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok != T_COMMA) 1 140 141
1128 {
1129 1 EXPECT1 (p, T_COMMA, NULL);
1130 1 resync (p, TOKSET (T_ENTRY_CLOSE));
1131 1 return;
1132 }
1133 140 consume (p);
1134 140 fields (p, entry);
1135 140 return;
1136 }
1137 0 break;
1138 18 case BTE_MACRODEF:
1139 18 ▶ if (p->tok == T_NAME || p->tok == T_ENTRY_CLOSE)
Branch: 2 branches
BranchTrueFalseTotal
if (p->tok == T_NAME || p->tok == T_ENTRY_CLOSE) 0 18 18
if (p->tok == T_NAME || p->tok == T_ENTRY_CLOSE) 0 0 0
1140 {
1141 18 fields (p, entry);
1142 18 return;
1143 }
1144 0 break;
1145 2 case BTE_PREAMBLE:
1146 2 ▶ if (p->tok == T_NUMBER || p->tok == T_NAME || p->tok == T_STRING)
Branch: 3 branches
BranchTrueFalseTotal
if (p->tok == T_NUMBER || p->tok == T_NAME || p->tok == T_STRING) 2 0 2
if (p->tok == T_NUMBER || p->tok == T_NAME || p->tok == T_STRING) 2 0 2
if (p->tok == T_NUMBER || p->tok == T_NAME || p->tok == T_STRING) 2 0 2
1147 {
1148 2 value (p, entry);
1149 2 return;
1150 }
1151 0 break;
1152 0 default:
1153 0 break;
1154 }
1155 0 expected (p, contents_set, NELEM (contents_set), NULL);
1156 0 resync (p, TOKSET (T_ENTRY_CLOSE));
1157 }
1158  
1159 static void
1160 167 body (bt_parser *p, AST *entry)
1161 {
1162 167 ▶ if (p->tok == T_STRING && entry->metatype == BTE_COMMENT)
Branch: 2 branches
BranchTrueFalseTotal
if (p->tok == T_STRING && entry->metatype == BTE_COMMENT) 3 164 167
if (p->tok == T_STRING && entry->metatype == BTE_COMMENT) 3 0 3
1163 {
1164 3 append_child (entry, token_node (p, BTAST_STRING));
1165 3 p->have_tok = FALSE; /* lexer is back at top level */
1166 3 return;
1167 }
1168  
1169 164 ▶ if (p->tok == T_ENTRY_OPEN && entry->metatype != BTE_COMMENT)
Branch: 2 branches
BranchTrueFalseTotal
if (p->tok == T_ENTRY_OPEN && entry->metatype != BTE_COMMENT) 162 2 164
if (p->tok == T_ENTRY_OPEN && entry->metatype != BTE_COMMENT) 162 0 162
1170 {
1171 162 consume (p);
1172 162 contents (p, entry);
1173 162 ▶ if (p->bail)
Branch: 1 branch
BranchTrueFalseTotal
if (p->bail) 0 162 162
1174 0 return;
1175 162 ▶ if (p->tok == T_ENTRY_CLOSE)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_ENTRY_CLOSE) 161 1 162
1176 {
1177 161 p->have_tok = FALSE; /* lex the next one lazily */
1178 161 return;
1179 }
1180 1 ▶ if (check_at (p))
Branch: 1 branch
BranchTrueFalseTotal
if (check_at (p)) 0 1 1
1181 0 return;
1182 1 EXPECT1 (p, T_ENTRY_CLOSE, skipping);
1183 1 initialize_lexer_state (p); /* skip the rest at top level */
1184 1 resync (p, TOKSET (T_AT));
1185 1 return;
1186 }
1187  
1188 2 ▶ if (p->tok == T_STRING) /* "..." after a regular entry type */
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_STRING) /* "..." after a regular entry type */ 0 2 2
1189 0 EXPECT1 (p, T_ENTRY_OPEN, NULL);
1190 2 ▶ else if (!check_at (p))
Branch: 1 branch
BranchTrueFalseTotal
else if (!check_at (p)) 2 0 2
1191 2 expected (p, body_set, NELEM (body_set), NULL);
1192 2 ▶ if (!p->bail)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->bail) 2 0 2
1193 2 resync (p, TOKSET (T_AT));
1194 }
1195  
1196 typedef enum { PARSE_EOF, PARSE_ENTRY, PARSE_FAILED, PARSE_SKIPPED } parse_result;
1197  
1198 /* BibTeX ignores the word "@comment" itself, so when it is not followed
1199 * by a braced or parenthesised comment, what follows it is ordinary text
1200 * between entries: back up to the current token and skip to the next '@'
1201 * at top level (without the junk warning; BibTeX doesn't warn either). */
1202 static void
1203 3 skip_bare_comment (bt_parser *p)
1204 {
1205 int c;
1206  
1207 3 p->pos = p->tok_pos;
1208 3 p->line = p->tok_start_line;
1209 40 ▶ while ((c = CUR (p)) >= 0 && c != '@')
Branch: 2 branches
BranchTrueFalseTotal
while ((c = CUR (p)) >= 0 && c != '@') 39 1 40
while ((c = CUR (p)) >= 0 && c != '@') 37 2 39
1210 {
1211 37 ▶ if (c == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') 5 32 37
1212 {
1213 5 p->pos++;
1214 5 p->line++;
1215 }
1216 else
1217 32 take_char (p, NULL);
1218 }
1219 3 initialize_lexer_state (p);
1220 3 bt_sbuf_clear (&p->text);
1221 3 p->have_tok = FALSE;
1222 3 }
1223  
1224 static parse_result
1225 201 parse_one (bt_parser *p, AST **result)
1226 {
1227 AST * entry;
1228  
1229 201 *result = NULL;
1230 201 p->bail = FALSE;
1231 201 ▶ if (!p->have_tok)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->have_tok) 200 1 201
1232 200 lex (p);
1233 201 ▶ if (p->tok == T_EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok == T_EOF) 31 170 201
1234 31 return PARSE_EOF;
1235  
1236 170 ▶ if (p->tok != T_AT) /* can't happen: START mode only */
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok != T_AT) /* can't happen: START mode only */ 0 170 170
1237 { /* produces '@' and EOF */
1238 0 EXPECT1 (p, T_AT, NULL);
1239 0 initialize_lexer_state (p);
1240 0 resync (p, TOKSET (T_AT));
1241 0 return PARSE_FAILED;
1242 }
1243  
1244 170 p->attempts++;
1245 170 consume (p);
1246 170 ▶ if (p->tok != T_NAME)
Branch: 1 branch
BranchTrueFalseTotal
if (p->tok != T_NAME) 0 170 170
1247 {
1248 0 EXPECT1 (p, T_NAME, NULL);
1249 0 resync (p, TOKSET (T_AT));
1250 0 return PARSE_FAILED;
1251 }
1252  
1253 170 entry = token_node (p, BTAST_ENTRY);
1254 170 entry->metatype = p->metatype;
1255 170 consume (p);
1256 /* A bare @comment is one whose next token doesn't start a braced,
1257 * parenthesised or quoted comment ("..." is reported by the lexer).
1258 * An unterminated comment comes back as end of input, but starts with
1259 * its delimiter, and is left to body() to report. */
1260 170 ▶ if (entry->metatype == BTE_COMMENT && p->tok != T_STRING
Branch: 2 branches
BranchTrueFalseTotal
if (entry->metatype == BTE_COMMENT && p->tok != T_STRING 8 162 170
if (entry->metatype == BTE_COMMENT && p->tok != T_STRING 5 3 8
1261 5 ▶ && p->tok != T_QUOTE
Branch: 1 branch
BranchTrueFalseTotal
&& p->tok != T_QUOTE 5 0 5
1262 5 ▶ && !(p->tok_pos < p->end && ISIN ("{(\"", p->buf[p->tok_pos])))
Branch: 3 branches
BranchTrueFalseTotal
&& !(p->tok_pos < p->end && ISIN ("{(\"", p->buf[p->tok_pos]))) 5 0 5
&& !(p->tok_pos < p->end && ISIN ("{(\"", p->buf[p->tok_pos]))) 5 0 5
&& !(p->tok_pos < p->end && ISIN ("{(\"", p->buf[p->tok_pos]))) 3 2 5
1263 {
1264 3 bt_free_ast (entry);
1265 3 skip_bare_comment (p);
1266 3 return PARSE_SKIPPED;
1267 }
1268 167 body (p, entry);
1269 167 *result = entry;
1270 167 return PARSE_ENTRY;
1271 }
1272  
1273 static boolean
1274 167 serious_since (const bt_errors *errs, const int *before)
1275 {
1276 int i;
1277  
1278 1464 ▶ for (i = 0; i < BT_NUM_ERRCLASSES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < BT_NUM_ERRCLASSES; i++) 1310 154 1464
1279 {
1280 1310 ▶ if (i == BTERR_NOTIFY || i == BTERR_CONTENT || i == BTERR_LEXWARN)
Branch: 3 branches
BranchTrueFalseTotal
if (i == BTERR_NOTIFY || i == BTERR_CONTENT || i == BTERR_LEXWARN) 1143 167 1310
if (i == BTERR_NOTIFY || i == BTERR_CONTENT || i == BTERR_LEXWARN) 976 167 1143
if (i == BTERR_NOTIFY || i == BTERR_CONTENT || i == BTERR_LEXWARN) 167 809 976
1281 501 continue;
1282 809 ▶ if (errs->counts[i] != before[i])
Branch: 1 branch
BranchTrueFalseTotal
if (errs->counts[i] != before[i]) 13 796 809
1283 13 return TRUE;
1284 }
1285 154 return FALSE;
1286 }
1287  
1288 AST *
1289 198 bt_parse_entry (bt_parser *p, bt_errors *errs, btshort options,
1290 boolean *status)
1291 {
1292 bt_errors local;
1293  
1294 198 ▶ if (errs == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (errs == NULL) 0 198 198
1295 {
1296 0 bt_errors_init (&local);
1297 0 errs = &local;
1298 }
1299 198 p->errs = errs;
1300 198 ▶ if (status) *status = TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = TRUE; 198 0 198
1301  
1302 for (;;)
1303 3 {
1304 int before[BT_NUM_ERRCLASSES];
1305 AST * entry;
1306 parse_result r;
1307  
1308 201 memcpy (before, errs->counts, sizeof (before));
1309 201 r = parse_one (p, &entry);
1310  
1311 201 ▶ if (r == PARSE_ENTRY)
Branch: 1 branch
BranchTrueFalseTotal
if (r == PARSE_ENTRY) 167 34 201
1312 {
1313 167 ▶ if (! (options & BTO_NOPOSTPROCESS))
Branch: 1 branch
BranchTrueFalseTotal
if (! (options & BTO_NOPOSTPROCESS)) 0 167 167
1314 0 bt_postprocess_entry (p->macros, errs, entry, options);
1315 167 ▶ if (status) *status = !serious_since (errs, before);
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = !serious_since (errs, before); 167 0 167
1316 167 p->entries++;
1317 167 p->errs = NULL;
1318 167 ▶ if (errs == &local)
Branch: 1 branch
BranchTrueFalseTotal
if (errs == &local) 0 167 167
1319 0 bt_errors_clear (&local);
1320 198 return entry;
1321 }
1322  
1323 34 ▶ if (r == PARSE_EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (r == PARSE_EOF) 31 3 34
1324 {
1325 31 ▶ if (p->read_error)
Branch: 1 branch
BranchTrueFalseTotal
if (p->read_error) 1 30 31
1326 1 bt_report (errs, BTERR_LEXERR, p->filename, p->line, NULL, -1,
1327 "error reading input");
1328 31 ▶ if (p->entries == 0 && p->attempts == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p->entries == 0 && p->attempts == 0) 4 27 31
if (p->entries == 0 && p->attempts == 0) 4 0 4
1329 {
1330 4 EXPECT1 (p, T_AT, NULL);
1331 4 ▶ if (status) *status = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = FALSE; 4 0 4
1332 4 p->attempts++; /* only complain once */
1333 }
1334 31 p->errs = NULL;
1335 31 ▶ if (errs == &local)
Branch: 1 branch
BranchTrueFalseTotal
if (errs == &local) 0 31 31
1336 0 bt_errors_clear (&local);
1337 31 return NULL;
1338 }
1339  
1340 3 ▶ if (r == PARSE_SKIPPED) /* a bare @comment: not an error */
Branch: 1 branch
BranchTrueFalseTotal
if (r == PARSE_SKIPPED) /* a bare @comment: not an error */ 3 0 3
1341 3 continue;
1342  
1343 /* PARSE_FAILED: no entry was started; try the next one */
1344 0 ▶ if (status) *status = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = FALSE; 0 0 0
1345 }
1346 }
« lib/Text/BibTeX.xs ↑ Summary src/postprocess.c »