* keyboard.c (parse_modifiers_uncached, parse_modifiers):
[emacs.git] / lib-src / ebrowse.c
blob7871a804997bc97267f9756376d3b1053f4bd13a
1 /* ebrowse.c --- parsing files for the ebrowse C++ browser
3 Copyright (C) 1992-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
28 #include <string.h>
29 #include <ctype.h>
30 #include <assert.h>
31 #include <getopt.h>
33 /* The SunOS compiler doesn't have SEEK_END. */
34 #ifndef SEEK_END
35 #define SEEK_END 2
36 #endif
38 /* Conditionalize function prototypes. */
40 /* Value is non-zero if strings X and Y compare equal. */
42 #define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)
44 #include <min-max.h>
46 /* Files are read in chunks of this number of bytes. */
48 #define READ_CHUNK_SIZE (100 * 1024)
50 /* The character used as a separator in path lists (like $PATH). */
52 #if defined(__MSDOS__)
53 #define PATH_LIST_SEPARATOR ';'
54 #define FILENAME_EQ(X,Y) (strcasecmp(X,Y) == 0)
55 #else
56 #if defined(WINDOWSNT)
57 #define PATH_LIST_SEPARATOR ';'
58 #define FILENAME_EQ(X,Y) (stricmp(X,Y) == 0)
59 #else
60 #define PATH_LIST_SEPARATOR ':'
61 #define FILENAME_EQ(X,Y) (streq(X,Y))
62 #endif
63 #endif
64 /* The default output file name. */
66 #define DEFAULT_OUTFILE "BROWSE"
68 /* A version string written to the output file. Change this whenever
69 the structure of the output file changes. */
71 #define EBROWSE_FILE_VERSION "ebrowse 5.0"
73 /* The output file consists of a tree of Lisp objects, with major
74 nodes built out of Lisp structures. These are the heads of the
75 Lisp structs with symbols identifying their type. */
77 #define TREE_HEADER_STRUCT "[ebrowse-hs "
78 #define TREE_STRUCT "[ebrowse-ts "
79 #define MEMBER_STRUCT "[ebrowse-ms "
80 #define CLASS_STRUCT "[ebrowse-cs "
82 /* The name of the symbol table entry for global functions, variables,
83 defines etc. This name also appears in the browser display. */
85 #define GLOBALS_NAME "*Globals*"
87 /* Token definitions. */
89 enum token
91 YYEOF = 0, /* end of file */
92 CSTRING = 256, /* string constant */
93 CCHAR, /* character constant */
94 CINT, /* integral constant */
95 CFLOAT, /* real constant */
97 ELLIPSIS, /* ... */
98 LSHIFTASGN, /* <<= */
99 RSHIFTASGN, /* >>= */
100 ARROWSTAR, /* ->* */
101 IDENT, /* identifier */
102 DIVASGN, /* /= */
103 INC, /* ++ */
104 ADDASGN, /* += */
105 DEC, /* -- */
106 ARROW, /* -> */
107 SUBASGN, /* -= */
108 MULASGN, /* *= */
109 MODASGN, /* %= */
110 LOR, /* || */
111 ORASGN, /* |= */
112 LAND, /* && */
113 ANDASGN, /* &= */
114 XORASGN, /* ^= */
115 POINTSTAR, /* .* */
116 DCOLON, /* :: */
117 EQ, /* == */
118 NE, /* != */
119 LE, /* <= */
120 LSHIFT, /* << */
121 GE, /* >= */
122 RSHIFT, /* >> */
124 /* Keywords. The undef's are there because these
125 three symbols are very likely to be defined somewhere. */
126 #undef BOOL
127 #undef TRUE
128 #undef FALSE
130 ASM, /* asm */
131 AUTO, /* auto */
132 BREAK, /* break */
133 CASE, /* case */
134 CATCH, /* catch */
135 CHAR, /* char */
136 CLASS, /* class */
137 CONST, /* const */
138 CONTINUE, /* continue */
139 DEFAULT, /* default */
140 DELETE, /* delete */
141 DO, /* do */
142 DOUBLE, /* double */
143 ELSE, /* else */
144 ENUM, /* enum */
145 EXTERN, /* extern */
146 FLOAT, /* float */
147 FOR, /* for */
148 FRIEND, /* friend */
149 GOTO, /* goto */
150 IF, /* if */
151 T_INLINE, /* inline */
152 INT, /* int */
153 LONG, /* long */
154 NEW, /* new */
155 OPERATOR, /* operator */
156 PRIVATE, /* private */
157 PROTECTED, /* protected */
158 PUBLIC, /* public */
159 REGISTER, /* register */
160 RETURN, /* return */
161 SHORT, /* short */
162 SIGNED, /* signed */
163 SIZEOF, /* sizeof */
164 STATIC, /* static */
165 STRUCT, /* struct */
166 SWITCH, /* switch */
167 TEMPLATE, /* template */
168 THIS, /* this */
169 THROW, /* throw */
170 TRY, /* try */
171 TYPEDEF, /* typedef */
172 UNION, /* union */
173 UNSIGNED, /* unsigned */
174 VIRTUAL, /* virtual */
175 VOID, /* void */
176 VOLATILE, /* volatile */
177 WHILE, /* while */
178 MUTABLE, /* mutable */
179 BOOL, /* bool */
180 TRUE, /* true */
181 FALSE, /* false */
182 SIGNATURE, /* signature (GNU extension) */
183 NAMESPACE, /* namespace */
184 EXPLICIT, /* explicit */
185 TYPENAME, /* typename */
186 CONST_CAST, /* const_cast */
187 DYNAMIC_CAST, /* dynamic_cast */
188 REINTERPRET_CAST, /* reinterpret_cast */
189 STATIC_CAST, /* static_cast */
190 TYPEID, /* typeid */
191 USING, /* using */
192 WCHAR /* wchar_t */
195 /* Storage classes, in a wider sense. */
197 enum sc
199 SC_UNKNOWN,
200 SC_MEMBER, /* Is an instance member. */
201 SC_STATIC, /* Is static member. */
202 SC_FRIEND, /* Is friend function. */
203 SC_TYPE /* Is a type definition. */
206 /* Member visibility. */
208 enum visibility
210 V_PUBLIC,
211 V_PROTECTED,
212 V_PRIVATE
215 /* Member flags. */
217 #define F_VIRTUAL 1 /* Is virtual function. */
218 #define F_INLINE 2 /* Is inline function. */
219 #define F_CONST 4 /* Is const. */
220 #define F_PURE 8 /* Is pure virtual function. */
221 #define F_MUTABLE 16 /* Is mutable. */
222 #define F_TEMPLATE 32 /* Is a template. */
223 #define F_EXPLICIT 64 /* Is explicit constructor. */
224 #define F_THROW 128 /* Has a throw specification. */
225 #define F_EXTERNC 256 /* Is declared extern "C". */
226 #define F_DEFINE 512 /* Is a #define. */
228 /* Two macros to set and test a bit in an int. */
230 #define SET_FLAG(F, FLAG) ((F) |= (FLAG))
231 #define HAS_FLAG(F, FLAG) (((F) & (FLAG)) != 0)
233 /* Structure describing a class member. */
235 struct member
237 struct member *next; /* Next in list of members. */
238 struct member *anext; /* Collision chain in member_table. */
239 struct member **list; /* Pointer to list in class. */
240 unsigned param_hash; /* Hash value for parameter types. */
241 int vis; /* Visibility (public, ...). */
242 int flags; /* See F_* above. */
243 char *regexp; /* Matching regular expression. */
244 const char *filename; /* Don't free this shared string. */
245 int pos; /* Buffer position of occurrence. */
246 char *def_regexp; /* Regular expression matching definition. */
247 const char *def_filename; /* File name of definition. */
248 int def_pos; /* Buffer position of definition. */
249 char name[1]; /* Member name. */
252 /* Structures of this type are used to connect class structures with
253 their super and subclasses. */
255 struct link
257 struct sym *sym; /* The super or subclass. */
258 struct link *next; /* Next in list or NULL. */
261 /* Structure used to record namespace aliases. */
263 struct alias
265 struct alias *next; /* Next in list. */
266 struct sym *namesp; /* Namespace in which defined. */
267 struct link *aliasee; /* List of aliased namespaces (A::B::C...). */
268 char name[1]; /* Alias name. */
271 /* The structure used to describe a class in the symbol table,
272 or a namespace in all_namespaces. */
274 struct sym
276 int flags; /* Is class a template class?. */
277 unsigned char visited; /* Used to find circles. */
278 struct sym *next; /* Hash collision list. */
279 struct link *subs; /* List of subclasses. */
280 struct link *supers; /* List of superclasses. */
281 struct member *vars; /* List of instance variables. */
282 struct member *fns; /* List of instance functions. */
283 struct member *static_vars; /* List of static variables. */
284 struct member *static_fns; /* List of static functions. */
285 struct member *friends; /* List of friend functions. */
286 struct member *types; /* List of local types. */
287 char *regexp; /* Matching regular expression. */
288 int pos; /* Buffer position. */
289 const char *filename; /* File in which it can be found. */
290 const char *sfilename; /* File in which members can be found. */
291 struct sym *namesp; /* Namespace in which defined. . */
292 char name[1]; /* Name of the class. */
295 /* Experimental: Print info for `--position-info'. We print
296 '(CLASS-NAME SCOPE MEMBER-NAME). */
298 #define P_DEFN 1
299 #define P_DECL 2
301 int info_where;
302 struct sym *info_cls = NULL;
303 struct member *info_member = NULL;
305 /* Experimental. For option `--position-info', the buffer position we
306 are interested in. When this position is reached, print out
307 information about what we know about that point. */
309 int info_position = -1;
311 /* Command line options structure for getopt_long. */
313 struct option options[] =
315 {"append", no_argument, NULL, 'a'},
316 {"files", required_argument, NULL, 'f'},
317 {"help", no_argument, NULL, -2},
318 {"min-regexp-length", required_argument, NULL, 'm'},
319 {"max-regexp-length", required_argument, NULL, 'M'},
320 {"no-nested-classes", no_argument, NULL, 'n'},
321 {"no-regexps", no_argument, NULL, 'x'},
322 {"no-structs-or-unions", no_argument, NULL, 's'},
323 {"output-file", required_argument, NULL, 'o'},
324 {"position-info", required_argument, NULL, 'p'},
325 {"search-path", required_argument, NULL, 'I'},
326 {"verbose", no_argument, NULL, 'v'},
327 {"version", no_argument, NULL, -3},
328 {"very-verbose", no_argument, NULL, 'V'},
329 {NULL, 0, NULL, 0}
332 /* Semantic values of tokens. Set by yylex.. */
334 unsigned yyival; /* Set for token CINT. */
335 char *yytext; /* Set for token IDENT. */
336 char *yytext_end;
338 /* Output file. */
340 FILE *yyout;
342 /* Current line number. */
344 int yyline;
346 /* The name of the current input file. */
348 const char *filename;
350 /* Three character class vectors, and macros to test membership
351 of characters. */
353 char is_ident[255];
354 char is_digit[255];
355 char is_white[255];
357 #define IDENTP(C) is_ident[(unsigned char) (C)]
358 #define DIGITP(C) is_digit[(unsigned char) (C)]
359 #define WHITEP(C) is_white[(unsigned char) (C)]
361 /* Command line flags. */
363 int f_append;
364 int f_verbose;
365 int f_very_verbose;
366 int f_structs = 1;
367 int f_regexps = 1;
368 int f_nested_classes = 1;
370 /* Maximum and minimum lengths of regular expressions matching a
371 member, class etc., for writing them to the output file. These are
372 overridable from the command line. */
374 int min_regexp = 5;
375 int max_regexp = 50;
377 /* Input buffer. */
379 char *inbuffer;
380 char *in;
381 size_t inbuffer_size;
383 /* Return the current buffer position in the input file. */
385 #define BUFFER_POS() (in - inbuffer)
387 /* If current lookahead is CSTRING, the following points to the
388 first character in the string constant. Used for recognizing
389 extern "C". */
391 char *string_start;
393 /* The size of the hash tables for classes.and members. Should be
394 prime. */
396 #define TABLE_SIZE 1001
398 /* The hash table for class symbols. */
400 struct sym *class_table[TABLE_SIZE];
402 /* Hash table containing all member structures. This is generally
403 faster for member lookup than traversing the member lists of a
404 `struct sym'. */
406 struct member *member_table[TABLE_SIZE];
408 /* Hash table for namespace aliases */
410 struct alias *namespace_alias_table[TABLE_SIZE];
412 /* The special class symbol used to hold global functions,
413 variables etc. */
415 struct sym *global_symbols;
417 /* The current namespace. */
419 struct sym *current_namespace;
421 /* The list of all known namespaces. */
423 struct sym *all_namespaces;
425 /* Stack of namespaces we're currently nested in, during the parse. */
427 struct sym **namespace_stack;
428 int namespace_stack_size;
429 int namespace_sp;
431 /* The current lookahead token. */
433 int tk = -1;
435 /* Structure describing a keyword. */
437 struct kw
439 const char *name; /* Spelling. */
440 int tk; /* Token value. */
441 struct kw *next; /* Next in collision chain. */
444 /* Keywords are lookup up in a hash table of their own. */
446 #define KEYWORD_TABLE_SIZE 1001
447 struct kw *keyword_table[KEYWORD_TABLE_SIZE];
449 /* Search path. */
451 struct search_path
453 char *path;
454 struct search_path *next;
457 struct search_path *search_path;
458 struct search_path *search_path_tail;
460 /* Function prototypes. */
462 static char *matching_regexp (void);
463 static struct sym *add_sym (const char *, struct sym *);
464 static void add_global_defn (char *, char *, int, unsigned, int, int, int);
465 static void add_global_decl (char *, char *, int, unsigned, int, int, int);
466 static struct member *add_member (struct sym *, char *, int, int, unsigned);
467 static void class_definition (struct sym *, int, int, int);
468 static char *operator_name (int *);
469 static void parse_qualified_param_ident_or_type (char **);
470 static void usage (int) NO_RETURN;
471 static void version (void) NO_RETURN;
475 /***********************************************************************
476 Utilities
477 ***********************************************************************/
479 /* Print an error in a printf-like style with the current input file
480 name and line number. */
482 static void
483 yyerror (const char *format, const char *s)
485 fprintf (stderr, "%s:%d: ", filename, yyline);
486 fprintf (stderr, format, s);
487 putc ('\n', stderr);
491 /* Like malloc but print an error and exit if not enough memory is
492 available. */
494 static void *
495 xmalloc (size_t nbytes)
497 void *p = malloc (nbytes);
498 if (p == NULL)
500 yyerror ("out of memory", NULL);
501 exit (EXIT_FAILURE);
503 return p;
507 /* Like realloc but print an error and exit if out of memory. */
509 static void *
510 xrealloc (void *p, size_t sz)
512 p = realloc (p, sz);
513 if (p == NULL)
515 yyerror ("out of memory", NULL);
516 exit (EXIT_FAILURE);
518 return p;
522 /* Like strdup, but print an error and exit if not enough memory is
523 available.. If S is null, return null. */
525 static char *
526 xstrdup (char *s)
528 if (s)
529 s = strcpy (xmalloc (strlen (s) + 1), s);
530 return s;
535 /***********************************************************************
536 Symbols
537 ***********************************************************************/
539 /* Initialize the symbol table. This currently only sets up the
540 special symbol for globals (`*Globals*'). */
542 static void
543 init_sym (void)
545 global_symbols = add_sym (GLOBALS_NAME, NULL);
549 /* Add a symbol for class NAME to the symbol table. NESTED_IN_CLASS
550 is the class in which class NAME was found. If it is null,
551 this means the scope of NAME is the current namespace.
553 If a symbol for NAME already exists, return that. Otherwise
554 create a new symbol and set it to default values. */
556 static struct sym *
557 add_sym (const char *name, struct sym *nested_in_class)
559 struct sym *sym;
560 unsigned h;
561 const char *s;
562 struct sym *scope = nested_in_class ? nested_in_class : current_namespace;
564 for (s = name, h = 0; *s; ++s)
565 h = (h << 1) ^ *s;
566 h %= TABLE_SIZE;
568 for (sym = class_table[h]; sym; sym = sym->next)
569 if (streq (name, sym->name)
570 && ((!sym->namesp && !scope)
571 || (sym->namesp && scope
572 && streq (sym->namesp->name, scope->name))))
573 break;
575 if (sym == NULL)
577 if (f_very_verbose)
579 putchar ('\t');
580 puts (name);
583 sym = (struct sym *) xmalloc (sizeof *sym + strlen (name));
584 memset (sym, 0, sizeof *sym);
585 strcpy (sym->name, name);
586 sym->namesp = scope;
587 sym->next = class_table[h];
588 class_table[h] = sym;
591 return sym;
595 /* Add links between superclass SUPER and subclass SUB. */
597 static void
598 add_link (struct sym *super, struct sym *sub)
600 struct link *lnk, *lnk2, *p, *prev;
602 /* See if a link already exists. */
603 for (p = super->subs, prev = NULL;
604 p && strcmp (sub->name, p->sym->name) > 0;
605 prev = p, p = p->next)
608 /* Avoid duplicates. */
609 if (p == NULL || p->sym != sub)
611 lnk = (struct link *) xmalloc (sizeof *lnk);
612 lnk2 = (struct link *) xmalloc (sizeof *lnk2);
614 lnk->sym = sub;
615 lnk->next = p;
617 if (prev)
618 prev->next = lnk;
619 else
620 super->subs = lnk;
622 lnk2->sym = super;
623 lnk2->next = sub->supers;
624 sub->supers = lnk2;
629 /* Find in class CLS member NAME.
631 VAR non-zero means look for a member variable; otherwise a function
632 is searched. SC specifies what kind of member is searched---a
633 static, or per-instance member etc. HASH is a hash code for the
634 parameter types of functions. Value is a pointer to the member
635 found or null if not found. */
637 static struct member *
638 find_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
640 struct member **list;
641 struct member *p;
642 unsigned name_hash = 0;
643 char *s;
644 int i;
646 switch (sc)
648 case SC_FRIEND:
649 list = &cls->friends;
650 break;
652 case SC_TYPE:
653 list = &cls->types;
654 break;
656 case SC_STATIC:
657 list = var ? &cls->static_vars : &cls->static_fns;
658 break;
660 default:
661 list = var ? &cls->vars : &cls->fns;
662 break;
665 for (s = name; *s; ++s)
666 name_hash = (name_hash << 1) ^ *s;
667 i = name_hash % TABLE_SIZE;
669 for (p = member_table[i]; p; p = p->anext)
670 if (p->list == list && p->param_hash == hash && streq (name, p->name))
671 break;
673 return p;
677 /* Add to class CLS information for the declaration of member NAME.
678 REGEXP is a regexp matching the declaration, if non-null. POS is
679 the position in the source where the declaration is found. HASH is
680 a hash code for the parameter list of the member, if it's a
681 function. VAR non-zero means member is a variable or type. SC
682 specifies the type of member (instance member, static, ...). VIS
683 is the member's visibility (public, protected, private). FLAGS is
684 a bit set giving additional information about the member (see the
685 F_* defines). */
687 static void
688 add_member_decl (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int vis, int flags)
690 struct member *m;
692 m = find_member (cls, name, var, sc, hash);
693 if (m == NULL)
694 m = add_member (cls, name, var, sc, hash);
696 /* Have we seen a new filename? If so record that. */
697 if (!cls->filename || !FILENAME_EQ (cls->filename, filename))
698 m->filename = filename;
700 m->regexp = regexp;
701 m->pos = pos;
702 m->flags = flags;
704 switch (vis)
706 case PRIVATE:
707 m->vis = V_PRIVATE;
708 break;
710 case PROTECTED:
711 m->vis = V_PROTECTED;
712 break;
714 case PUBLIC:
715 m->vis = V_PUBLIC;
716 break;
719 info_where = P_DECL;
720 info_cls = cls;
721 info_member = m;
725 /* Add to class CLS information for the definition of member NAME.
726 REGEXP is a regexp matching the declaration, if non-null. POS is
727 the position in the source where the declaration is found. HASH is
728 a hash code for the parameter list of the member, if it's a
729 function. VAR non-zero means member is a variable or type. SC
730 specifies the type of member (instance member, static, ...). VIS
731 is the member's visibility (public, protected, private). FLAGS is
732 a bit set giving additional information about the member (see the
733 F_* defines). */
735 static void
736 add_member_defn (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
738 struct member *m;
740 if (sc == SC_UNKNOWN)
742 m = find_member (cls, name, var, SC_MEMBER, hash);
743 if (m == NULL)
745 m = find_member (cls, name, var, SC_STATIC, hash);
746 if (m == NULL)
747 m = add_member (cls, name, var, sc, hash);
750 else
752 m = find_member (cls, name, var, sc, hash);
753 if (m == NULL)
754 m = add_member (cls, name, var, sc, hash);
757 if (!cls->sfilename)
758 cls->sfilename = filename;
760 if (!FILENAME_EQ (cls->sfilename, filename))
761 m->def_filename = filename;
763 m->def_regexp = regexp;
764 m->def_pos = pos;
765 m->flags |= flags;
767 info_where = P_DEFN;
768 info_cls = cls;
769 info_member = m;
773 /* Add a symbol for a define named NAME to the symbol table.
774 REGEXP is a regular expression matching the define in the source,
775 if it is non-null. POS is the position in the file. */
777 static void
778 add_define (char *name, char *regexp, int pos)
780 add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
781 add_global_decl (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
785 /* Add information for the global definition of NAME.
786 REGEXP is a regexp matching the declaration, if non-null. POS is
787 the position in the source where the declaration is found. HASH is
788 a hash code for the parameter list of the member, if it's a
789 function. VAR non-zero means member is a variable or type. SC
790 specifies the type of member (instance member, static, ...). VIS
791 is the member's visibility (public, protected, private). FLAGS is
792 a bit set giving additional information about the member (see the
793 F_* defines). */
795 static void
796 add_global_defn (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
798 int i;
799 struct sym *sym;
801 /* Try to find out for which classes a function is a friend, and add
802 what we know about it to them. */
803 if (!var)
804 for (i = 0; i < TABLE_SIZE; ++i)
805 for (sym = class_table[i]; sym; sym = sym->next)
806 if (sym != global_symbols && sym->friends)
807 if (find_member (sym, name, 0, SC_FRIEND, hash))
808 add_member_defn (sym, name, regexp, pos, hash, 0,
809 SC_FRIEND, flags);
811 /* Add to global symbols. */
812 add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags);
816 /* Add information for the global declaration of NAME.
817 REGEXP is a regexp matching the declaration, if non-null. POS is
818 the position in the source where the declaration is found. HASH is
819 a hash code for the parameter list of the member, if it's a
820 function. VAR non-zero means member is a variable or type. SC
821 specifies the type of member (instance member, static, ...). VIS
822 is the member's visibility (public, protected, private). FLAGS is
823 a bit set giving additional information about the member (see the
824 F_* defines). */
826 static void
827 add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
829 /* Add declaration only if not already declared. Header files must
830 be processed before source files for this to have the right effect.
831 I do not want to handle implicit declarations at the moment. */
832 struct member *m;
833 struct member *found;
835 m = found = find_member (global_symbols, name, var, sc, hash);
836 if (m == NULL)
837 m = add_member (global_symbols, name, var, sc, hash);
839 /* Definition already seen => probably last declaration implicit.
840 Override. This means that declarations must always be added to
841 the symbol table before definitions. */
842 if (!found)
844 if (!global_symbols->filename
845 || !FILENAME_EQ (global_symbols->filename, filename))
846 m->filename = filename;
848 m->regexp = regexp;
849 m->pos = pos;
850 m->vis = V_PUBLIC;
851 m->flags = flags;
853 info_where = P_DECL;
854 info_cls = global_symbols;
855 info_member = m;
860 /* Add a symbol for member NAME to class CLS.
861 VAR non-zero means it's a variable. SC specifies the kind of
862 member. HASH is a hash code for the parameter types of a function.
863 Value is a pointer to the member's structure. */
865 static struct member *
866 add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
868 struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name));
869 struct member **list;
870 struct member *p;
871 struct member *prev;
872 unsigned name_hash = 0;
873 int i;
874 char *s;
876 strcpy (m->name, name);
877 m->param_hash = hash;
879 m->vis = 0;
880 m->flags = 0;
881 m->regexp = NULL;
882 m->filename = NULL;
883 m->pos = 0;
884 m->def_regexp = NULL;
885 m->def_filename = NULL;
886 m->def_pos = 0;
888 assert (cls != NULL);
890 switch (sc)
892 case SC_FRIEND:
893 list = &cls->friends;
894 break;
896 case SC_TYPE:
897 list = &cls->types;
898 break;
900 case SC_STATIC:
901 list = var ? &cls->static_vars : &cls->static_fns;
902 break;
904 default:
905 list = var ? &cls->vars : &cls->fns;
906 break;
909 for (s = name; *s; ++s)
910 name_hash = (name_hash << 1) ^ *s;
911 i = name_hash % TABLE_SIZE;
912 m->anext = member_table[i];
913 member_table[i] = m;
914 m->list = list;
916 /* Keep the member list sorted. It's cheaper to do it here than to
917 sort them in Lisp. */
918 for (prev = NULL, p = *list;
919 p && strcmp (name, p->name) > 0;
920 prev = p, p = p->next)
923 m->next = p;
924 if (prev)
925 prev->next = m;
926 else
927 *list = m;
928 return m;
932 /* Given the root R of a class tree, step through all subclasses
933 recursively, marking functions as virtual that are declared virtual
934 in base classes. */
936 static void
937 mark_virtual (struct sym *r)
939 struct link *p;
940 struct member *m, *m2;
942 for (p = r->subs; p; p = p->next)
944 for (m = r->fns; m; m = m->next)
945 if (HAS_FLAG (m->flags, F_VIRTUAL))
947 for (m2 = p->sym->fns; m2; m2 = m2->next)
948 if (m->param_hash == m2->param_hash && streq (m->name, m2->name))
949 SET_FLAG (m2->flags, F_VIRTUAL);
952 mark_virtual (p->sym);
957 /* For all roots of the class tree, mark functions as virtual that
958 are virtual because of a virtual declaration in a base class. */
960 static void
961 mark_inherited_virtual (void)
963 struct sym *r;
964 int i;
966 for (i = 0; i < TABLE_SIZE; ++i)
967 for (r = class_table[i]; r; r = r->next)
968 if (r->supers == NULL)
969 mark_virtual (r);
973 /* Create and return a symbol for a namespace with name NAME. */
975 static struct sym *
976 make_namespace (char *name, struct sym *context)
978 struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name));
979 memset (s, 0, sizeof *s);
980 strcpy (s->name, name);
981 s->next = all_namespaces;
982 s->namesp = context;
983 all_namespaces = s;
984 return s;
988 /* Find the symbol for namespace NAME. If not found, retrun NULL */
990 static struct sym *
991 check_namespace (char *name, struct sym *context)
993 struct sym *p = NULL;
995 for (p = all_namespaces; p; p = p->next)
997 if (streq (p->name, name) && (p->namesp == context))
998 break;
1001 return p;
1004 /* Find the symbol for namespace NAME. If not found, add a new symbol
1005 for NAME to all_namespaces. */
1007 static struct sym *
1008 find_namespace (char *name, struct sym *context)
1010 struct sym *p = check_namespace (name, context);
1012 if (p == NULL)
1013 p = make_namespace (name, context);
1015 return p;
1019 /* Find namespace alias with name NAME. If not found return NULL. */
1021 static struct link *
1022 check_namespace_alias (char *name)
1024 struct link *p = NULL;
1025 struct alias *al;
1026 unsigned h;
1027 char *s;
1029 for (s = name, h = 0; *s; ++s)
1030 h = (h << 1) ^ *s;
1031 h %= TABLE_SIZE;
1033 for (al = namespace_alias_table[h]; al; al = al->next)
1034 if (streq (name, al->name) && (al->namesp == current_namespace))
1036 p = al->aliasee;
1037 break;
1040 return p;
1043 /* Register the name NEW_NAME as an alias for namespace list OLD_NAME. */
1045 static void
1046 register_namespace_alias (char *new_name, struct link *old_name)
1048 unsigned h;
1049 char *s;
1050 struct alias *al;
1052 for (s = new_name, h = 0; *s; ++s)
1053 h = (h << 1) ^ *s;
1054 h %= TABLE_SIZE;
1057 /* Is it already in the table of aliases? */
1058 for (al = namespace_alias_table[h]; al; al = al->next)
1059 if (streq (new_name, al->name) && (al->namesp == current_namespace))
1060 return;
1062 al = (struct alias *) xmalloc (sizeof *al + strlen (new_name));
1063 strcpy (al->name, new_name);
1064 al->next = namespace_alias_table[h];
1065 al->namesp = current_namespace;
1066 al->aliasee = old_name;
1067 namespace_alias_table[h] = al;
1071 /* Enter namespace with name NAME. */
1073 static void
1074 enter_namespace (char *name)
1076 struct sym *p = find_namespace (name, current_namespace);
1078 if (namespace_sp == namespace_stack_size)
1080 int size = max (10, 2 * namespace_stack_size);
1081 namespace_stack
1082 = (struct sym **) xrealloc ((void *)namespace_stack,
1083 size * sizeof *namespace_stack);
1084 namespace_stack_size = size;
1087 namespace_stack[namespace_sp++] = current_namespace;
1088 current_namespace = p;
1092 /* Leave the current namespace. */
1094 static void
1095 leave_namespace (void)
1097 assert (namespace_sp > 0);
1098 current_namespace = namespace_stack[--namespace_sp];
1103 /***********************************************************************
1104 Writing the Output File
1105 ***********************************************************************/
1107 /* Write string S to the output file FP in a Lisp-readable form.
1108 If S is null, write out `()'. */
1110 static inline void
1111 putstr (const char *s, FILE *fp)
1113 if (!s)
1115 putc ('(', fp);
1116 putc (')', fp);
1117 putc (' ', fp);
1119 else
1121 putc ('"', fp);
1122 fputs (s, fp);
1123 putc ('"', fp);
1124 putc (' ', fp);
1128 /* A dynamically allocated buffer for constructing a scope name. */
1130 char *scope_buffer;
1131 int scope_buffer_size;
1132 int scope_buffer_len;
1135 /* Make sure scope_buffer has enough room to add LEN chars to it. */
1137 static void
1138 ensure_scope_buffer_room (int len)
1140 if (scope_buffer_len + len >= scope_buffer_size)
1142 int new_size = max (2 * scope_buffer_size, scope_buffer_len + len);
1143 scope_buffer = (char *) xrealloc (scope_buffer, new_size);
1144 scope_buffer_size = new_size;
1149 /* Recursively add the scope names of symbol P and the scopes of its
1150 namespaces to scope_buffer. Value is a pointer to the complete
1151 scope name constructed. */
1153 static char *
1154 sym_scope_1 (struct sym *p)
1156 int len;
1158 if (p->namesp)
1159 sym_scope_1 (p->namesp);
1161 if (*scope_buffer)
1163 ensure_scope_buffer_room (3);
1164 strcat (scope_buffer, "::");
1165 scope_buffer_len += 2;
1168 len = strlen (p->name);
1169 ensure_scope_buffer_room (len + 1);
1170 strcat (scope_buffer, p->name);
1171 scope_buffer_len += len;
1173 if (HAS_FLAG (p->flags, F_TEMPLATE))
1175 ensure_scope_buffer_room (3);
1176 strcat (scope_buffer, "<>");
1177 scope_buffer_len += 2;
1180 return scope_buffer;
1184 /* Return the scope of symbol P in printed representation, i.e.
1185 as it would appear in a C*+ source file. */
1187 static char *
1188 sym_scope (struct sym *p)
1190 if (!scope_buffer)
1192 scope_buffer_size = 1024;
1193 scope_buffer = (char *) xmalloc (scope_buffer_size);
1196 *scope_buffer = '\0';
1197 scope_buffer_len = 0;
1199 if (p->namesp)
1200 sym_scope_1 (p->namesp);
1202 return scope_buffer;
1206 /* Dump the list of members M to file FP. Value is the length of the
1207 list. */
1209 static int
1210 dump_members (FILE *fp, struct member *m)
1212 int n;
1214 putc ('(', fp);
1216 for (n = 0; m; m = m->next, ++n)
1218 fputs (MEMBER_STRUCT, fp);
1219 putstr (m->name, fp);
1220 putstr (NULL, fp); /* FIXME? scope for globals */
1221 fprintf (fp, "%u ", (unsigned) m->flags);
1222 putstr (m->filename, fp);
1223 putstr (m->regexp, fp);
1224 fprintf (fp, "%u ", (unsigned) m->pos);
1225 fprintf (fp, "%u ", (unsigned) m->vis);
1226 putc (' ', fp);
1227 putstr (m->def_filename, fp);
1228 putstr (m->def_regexp, fp);
1229 fprintf (fp, "%u", (unsigned) m->def_pos);
1230 putc (']', fp);
1231 putc ('\n', fp);
1234 putc (')', fp);
1235 putc ('\n', fp);
1236 return n;
1240 /* Dump class ROOT to stream FP. */
1242 static void
1243 dump_sym (FILE *fp, struct sym *root)
1245 fputs (CLASS_STRUCT, fp);
1246 putstr (root->name, fp);
1248 /* Print scope, if any. */
1249 if (root->namesp)
1250 putstr (sym_scope (root), fp);
1251 else
1252 putstr (NULL, fp);
1254 /* Print flags. */
1255 fprintf (fp, "%u", root->flags);
1256 putstr (root->filename, fp);
1257 putstr (root->regexp, fp);
1258 fprintf (fp, "%u", (unsigned) root->pos);
1259 putstr (root->sfilename, fp);
1260 putc (']', fp);
1261 putc ('\n', fp);
1265 /* Dump class ROOT and its subclasses to file FP. Value is the
1266 number of classes written. */
1268 static int
1269 dump_tree (FILE *fp, struct sym *root)
1271 struct link *lk;
1272 unsigned n = 0;
1274 dump_sym (fp, root);
1276 if (f_verbose)
1278 putchar ('+');
1279 fflush (stdout);
1282 putc ('(', fp);
1284 for (lk = root->subs; lk; lk = lk->next)
1286 fputs (TREE_STRUCT, fp);
1287 n += dump_tree (fp, lk->sym);
1288 putc (']', fp);
1291 putc (')', fp);
1293 dump_members (fp, root->vars);
1294 n += dump_members (fp, root->fns);
1295 dump_members (fp, root->static_vars);
1296 n += dump_members (fp, root->static_fns);
1297 n += dump_members (fp, root->friends);
1298 dump_members (fp, root->types);
1300 /* Superclasses. */
1301 putc ('(', fp);
1302 putc (')', fp);
1304 /* Mark slot. */
1305 putc ('(', fp);
1306 putc (')', fp);
1308 putc ('\n', fp);
1309 return n;
1313 /* Dump the entire class tree to file FP. */
1315 static void
1316 dump_roots (FILE *fp)
1318 int i, n = 0;
1319 struct sym *r;
1321 /* Output file header containing version string, command line
1322 options etc. */
1323 if (!f_append)
1325 fputs (TREE_HEADER_STRUCT, fp);
1326 putstr (EBROWSE_FILE_VERSION, fp);
1328 putc ('\"', fp);
1329 if (!f_structs)
1330 fputs (" -s", fp);
1331 if (f_regexps)
1332 fputs (" -x", fp);
1333 putc ('\"', fp);
1334 fputs (" ()", fp);
1335 fputs (" ()", fp);
1336 putc (']', fp);
1339 /* Mark functions as virtual that are so because of functions
1340 declared virtual in base classes. */
1341 mark_inherited_virtual ();
1343 /* Dump the roots of the graph. */
1344 for (i = 0; i < TABLE_SIZE; ++i)
1345 for (r = class_table[i]; r; r = r->next)
1346 if (!r->supers)
1348 fputs (TREE_STRUCT, fp);
1349 n += dump_tree (fp, r);
1350 putc (']', fp);
1353 if (f_verbose)
1354 putchar ('\n');
1359 /***********************************************************************
1360 Scanner
1361 ***********************************************************************/
1363 #ifdef DEBUG
1364 #define INCREMENT_LINENO \
1365 do { \
1366 if (f_very_verbose) \
1368 ++yyline; \
1369 printf ("%d:\n", yyline); \
1371 else \
1372 ++yyline; \
1373 } while (0)
1374 #else
1375 #define INCREMENT_LINENO ++yyline
1376 #endif
1378 /* Define two macros for accessing the input buffer (current input
1379 file). GET(C) sets C to the next input character and advances the
1380 input pointer. UNGET retracts the input pointer. */
1382 #define GET(C) ((C) = *in++)
1383 #define UNGET() (--in)
1386 /* Process a preprocessor line. Value is the next character from the
1387 input buffer not consumed. */
1389 static int
1390 process_pp_line (void)
1392 int in_comment = 0, in_string = 0;
1393 int c;
1394 char *p = yytext;
1396 /* Skip over white space. The `#' has been consumed already. */
1397 while (WHITEP (GET (c)))
1400 /* Read the preprocessor command (if any). */
1401 while (IDENTP (c))
1403 *p++ = c;
1404 GET (c);
1407 /* Is it a `define'? */
1408 *p = '\0';
1410 if (*yytext && streq (yytext, "define"))
1412 p = yytext;
1413 while (WHITEP (c))
1414 GET (c);
1415 while (IDENTP (c))
1417 *p++ = c;
1418 GET (c);
1421 *p = '\0';
1423 if (*yytext)
1425 char *regexp = matching_regexp ();
1426 int pos = BUFFER_POS ();
1427 add_define (yytext, regexp, pos);
1431 while (c && (c != '\n' || in_comment || in_string))
1433 if (c == '\\')
1434 GET (c);
1435 else if (c == '/' && !in_comment)
1437 if (GET (c) == '*')
1438 in_comment = 1;
1440 else if (c == '*' && in_comment)
1442 if (GET (c) == '/')
1443 in_comment = 0;
1445 else if (c == '"')
1446 in_string = !in_string;
1448 if (c == '\n')
1449 INCREMENT_LINENO;
1451 GET (c);
1454 return c;
1458 /* Value is the next token from the input buffer. */
1460 static int
1461 yylex (void)
1463 int c;
1464 char end_char;
1465 char *p;
1467 for (;;)
1469 while (WHITEP (GET (c)))
1472 switch (c)
1474 case '\n':
1475 INCREMENT_LINENO;
1476 break;
1478 case '\r':
1479 break;
1481 case 0:
1482 /* End of file. */
1483 return YYEOF;
1485 case '\\':
1486 GET (c);
1487 break;
1489 case '"':
1490 case '\'':
1491 /* String and character constants. */
1492 end_char = c;
1493 string_start = in;
1494 while (GET (c) && c != end_char)
1496 switch (c)
1498 case '\\':
1499 /* Escape sequences. */
1500 if (!GET (c))
1502 if (end_char == '\'')
1503 yyerror ("EOF in character constant", NULL);
1504 else
1505 yyerror ("EOF in string constant", NULL);
1506 goto end_string;
1508 else switch (c)
1510 case '\n':
1511 INCREMENT_LINENO;
1512 case 'a':
1513 case 'b':
1514 case 'f':
1515 case 'n':
1516 case 'r':
1517 case 't':
1518 case 'v':
1519 break;
1521 case 'x':
1523 /* Hexadecimal escape sequence. */
1524 int i;
1525 for (i = 0; i < 2; ++i)
1527 GET (c);
1529 if (c >= '0' && c <= '7')
1531 else if (c >= 'a' && c <= 'f')
1533 else if (c >= 'A' && c <= 'F')
1535 else
1537 UNGET ();
1538 break;
1542 break;
1544 case '0':
1546 /* Octal escape sequence. */
1547 int i;
1548 for (i = 0; i < 3; ++i)
1550 GET (c);
1552 if (c >= '0' && c <= '7')
1554 else
1556 UNGET ();
1557 break;
1561 break;
1563 default:
1564 break;
1566 break;
1568 case '\n':
1569 if (end_char == '\'')
1570 yyerror ("newline in character constant", NULL);
1571 else
1572 yyerror ("newline in string constant", NULL);
1573 INCREMENT_LINENO;
1574 break;
1576 default:
1577 break;
1581 end_string:
1582 return end_char == '\'' ? CCHAR : CSTRING;
1584 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1585 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1586 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1587 case 'v': case 'w': case 'x': case 'y': case 'z':
1588 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1589 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1590 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1591 case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
1593 /* Identifier and keywords. */
1594 unsigned hash;
1595 struct kw *k;
1597 p = yytext;
1598 *p++ = hash = c;
1600 while (IDENTP (GET (*p)))
1602 hash = (hash << 1) ^ *p++;
1603 if (p == yytext_end - 1)
1605 int size = yytext_end - yytext;
1606 yytext = (char *) xrealloc (yytext, 2 * size);
1607 yytext_end = yytext + 2 * size;
1608 p = yytext + size - 1;
1612 UNGET ();
1613 *p = 0;
1615 for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next)
1616 if (streq (k->name, yytext))
1617 return k->tk;
1619 return IDENT;
1622 case '/':
1623 /* C and C++ comments, '/' and '/='. */
1624 switch (GET (c))
1626 case '*':
1627 while (GET (c))
1629 switch (c)
1631 case '*':
1632 if (GET (c) == '/')
1633 goto comment_end;
1634 UNGET ();
1635 break;
1636 case '\\':
1637 GET (c);
1638 break;
1639 case '\n':
1640 INCREMENT_LINENO;
1641 break;
1644 comment_end:;
1645 break;
1647 case '=':
1648 return DIVASGN;
1650 case '/':
1651 while (GET (c) && c != '\n')
1653 /* Don't try to read past the end of the input buffer if
1654 the file ends in a C++ comment without a newline. */
1655 if (c == 0)
1656 return YYEOF;
1658 INCREMENT_LINENO;
1659 break;
1661 default:
1662 UNGET ();
1663 return '/';
1665 break;
1667 case '+':
1668 if (GET (c) == '+')
1669 return INC;
1670 else if (c == '=')
1671 return ADDASGN;
1672 UNGET ();
1673 return '+';
1675 case '-':
1676 switch (GET (c))
1678 case '-':
1679 return DEC;
1680 case '>':
1681 if (GET (c) == '*')
1682 return ARROWSTAR;
1683 UNGET ();
1684 return ARROW;
1685 case '=':
1686 return SUBASGN;
1688 UNGET ();
1689 return '-';
1691 case '*':
1692 if (GET (c) == '=')
1693 return MULASGN;
1694 UNGET ();
1695 return '*';
1697 case '%':
1698 if (GET (c) == '=')
1699 return MODASGN;
1700 UNGET ();
1701 return '%';
1703 case '|':
1704 if (GET (c) == '|')
1705 return LOR;
1706 else if (c == '=')
1707 return ORASGN;
1708 UNGET ();
1709 return '|';
1711 case '&':
1712 if (GET (c) == '&')
1713 return LAND;
1714 else if (c == '=')
1715 return ANDASGN;
1716 UNGET ();
1717 return '&';
1719 case '^':
1720 if (GET (c) == '=')
1721 return XORASGN;
1722 UNGET ();
1723 return '^';
1725 case '.':
1726 if (GET (c) == '*')
1727 return POINTSTAR;
1728 else if (c == '.')
1730 if (GET (c) != '.')
1731 yyerror ("invalid token '..' ('...' assumed)", NULL);
1732 UNGET ();
1733 return ELLIPSIS;
1735 else if (!DIGITP (c))
1737 UNGET ();
1738 return '.';
1740 goto mantissa;
1742 case ':':
1743 if (GET (c) == ':')
1744 return DCOLON;
1745 UNGET ();
1746 return ':';
1748 case '=':
1749 if (GET (c) == '=')
1750 return EQ;
1751 UNGET ();
1752 return '=';
1754 case '!':
1755 if (GET (c) == '=')
1756 return NE;
1757 UNGET ();
1758 return '!';
1760 case '<':
1761 switch (GET (c))
1763 case '=':
1764 return LE;
1765 case '<':
1766 if (GET (c) == '=')
1767 return LSHIFTASGN;
1768 UNGET ();
1769 return LSHIFT;
1771 UNGET ();
1772 return '<';
1774 case '>':
1775 switch (GET (c))
1777 case '=':
1778 return GE;
1779 case '>':
1780 if (GET (c) == '=')
1781 return RSHIFTASGN;
1782 UNGET ();
1783 return RSHIFT;
1785 UNGET ();
1786 return '>';
1788 case '#':
1789 c = process_pp_line ();
1790 if (c == 0)
1791 return YYEOF;
1792 break;
1794 case '(': case ')': case '[': case ']': case '{': case '}':
1795 case ';': case ',': case '?': case '~':
1796 return c;
1798 case '0':
1799 yyival = 0;
1801 if (GET (c) == 'x' || c == 'X')
1803 while (GET (c))
1805 if (DIGITP (c))
1806 yyival = yyival * 16 + c - '0';
1807 else if (c >= 'a' && c <= 'f')
1808 yyival = yyival * 16 + c - 'a' + 10;
1809 else if (c >= 'A' && c <= 'F')
1810 yyival = yyival * 16 + c - 'A' + 10;
1811 else
1812 break;
1815 goto int_suffixes;
1817 else if (c == '.')
1818 goto mantissa;
1820 while (c >= '0' && c <= '7')
1822 yyival = (yyival << 3) + c - '0';
1823 GET (c);
1826 int_suffixes:
1827 /* Integer suffixes. */
1828 while (isalpha (c))
1829 GET (c);
1830 UNGET ();
1831 return CINT;
1833 case '1': case '2': case '3': case '4': case '5': case '6':
1834 case '7': case '8': case '9':
1835 /* Integer or floating constant, part before '.'. */
1836 yyival = c - '0';
1838 while (GET (c) && DIGITP (c))
1839 yyival = 10 * yyival + c - '0';
1841 if (c != '.')
1842 goto int_suffixes;
1844 mantissa:
1845 /* Digits following '.'. */
1846 while (DIGITP (c))
1847 GET (c);
1849 /* Optional exponent. */
1850 if (c == 'E' || c == 'e')
1852 if (GET (c) == '-' || c == '+')
1853 GET (c);
1855 while (DIGITP (c))
1856 GET (c);
1859 /* Optional type suffixes. */
1860 while (isalpha (c))
1861 GET (c);
1862 UNGET ();
1863 return CFLOAT;
1865 default:
1866 break;
1872 /* Actually local to matching_regexp. These variables must be in
1873 global scope for the case that `static' get's defined away. */
1875 static char *matching_regexp_buffer, *matching_regexp_end_buf;
1878 /* Value is the string from the start of the line to the current
1879 position in the input buffer, or maybe a bit more if that string is
1880 shorter than min_regexp. */
1882 static char *
1883 matching_regexp (void)
1885 char *p;
1886 char *s;
1887 char *t;
1889 if (!f_regexps)
1890 return NULL;
1892 if (matching_regexp_buffer == NULL)
1894 matching_regexp_buffer = (char *) xmalloc (max_regexp);
1895 matching_regexp_end_buf = &matching_regexp_buffer[max_regexp] - 1;
1898 /* Scan back to previous newline of buffer start. */
1899 for (p = in - 1; p > inbuffer && *p != '\n'; --p)
1902 if (*p == '\n')
1904 while (in - p < min_regexp && p > inbuffer)
1906 /* Line probably not significant enough */
1907 for (--p; p > inbuffer && *p != '\n'; --p)
1910 if (*p == '\n')
1911 ++p;
1914 /* Copy from end to make sure significant portions are included.
1915 This implies that in the browser a regular expressing of the form
1916 `^.*{regexp}' has to be used. */
1917 for (s = matching_regexp_end_buf - 1, t = in;
1918 s > matching_regexp_buffer && t > p;)
1920 *--s = *--t;
1922 if (*s == '"' || *s == '\\')
1923 *--s = '\\';
1926 *(matching_regexp_end_buf - 1) = '\0';
1927 return xstrdup (s);
1931 /* Return a printable representation of token T. */
1933 static const char *
1934 token_string (int t)
1936 static char b[3];
1938 switch (t)
1940 case CSTRING: return "string constant";
1941 case CCHAR: return "char constant";
1942 case CINT: return "int constant";
1943 case CFLOAT: return "floating constant";
1944 case ELLIPSIS: return "...";
1945 case LSHIFTASGN: return "<<=";
1946 case RSHIFTASGN: return ">>=";
1947 case ARROWSTAR: return "->*";
1948 case IDENT: return "identifier";
1949 case DIVASGN: return "/=";
1950 case INC: return "++";
1951 case ADDASGN: return "+=";
1952 case DEC: return "--";
1953 case ARROW: return "->";
1954 case SUBASGN: return "-=";
1955 case MULASGN: return "*=";
1956 case MODASGN: return "%=";
1957 case LOR: return "||";
1958 case ORASGN: return "|=";
1959 case LAND: return "&&";
1960 case ANDASGN: return "&=";
1961 case XORASGN: return "^=";
1962 case POINTSTAR: return ".*";
1963 case DCOLON: return "::";
1964 case EQ: return "==";
1965 case NE: return "!=";
1966 case LE: return "<=";
1967 case LSHIFT: return "<<";
1968 case GE: return ">=";
1969 case RSHIFT: return ">>";
1970 case ASM: return "asm";
1971 case AUTO: return "auto";
1972 case BREAK: return "break";
1973 case CASE: return "case";
1974 case CATCH: return "catch";
1975 case CHAR: return "char";
1976 case CLASS: return "class";
1977 case CONST: return "const";
1978 case CONTINUE: return "continue";
1979 case DEFAULT: return "default";
1980 case DELETE: return "delete";
1981 case DO: return "do";
1982 case DOUBLE: return "double";
1983 case ELSE: return "else";
1984 case ENUM: return "enum";
1985 case EXTERN: return "extern";
1986 case FLOAT: return "float";
1987 case FOR: return "for";
1988 case FRIEND: return "friend";
1989 case GOTO: return "goto";
1990 case IF: return "if";
1991 case T_INLINE: return "inline";
1992 case INT: return "int";
1993 case LONG: return "long";
1994 case NEW: return "new";
1995 case OPERATOR: return "operator";
1996 case PRIVATE: return "private";
1997 case PROTECTED: return "protected";
1998 case PUBLIC: return "public";
1999 case REGISTER: return "register";
2000 case RETURN: return "return";
2001 case SHORT: return "short";
2002 case SIGNED: return "signed";
2003 case SIZEOF: return "sizeof";
2004 case STATIC: return "static";
2005 case STRUCT: return "struct";
2006 case SWITCH: return "switch";
2007 case TEMPLATE: return "template";
2008 case THIS: return "this";
2009 case THROW: return "throw";
2010 case TRY: return "try";
2011 case TYPEDEF: return "typedef";
2012 case UNION: return "union";
2013 case UNSIGNED: return "unsigned";
2014 case VIRTUAL: return "virtual";
2015 case VOID: return "void";
2016 case VOLATILE: return "volatile";
2017 case WHILE: return "while";
2018 case MUTABLE: return "mutable";
2019 case BOOL: return "bool";
2020 case TRUE: return "true";
2021 case FALSE: return "false";
2022 case SIGNATURE: return "signature";
2023 case NAMESPACE: return "namespace";
2024 case EXPLICIT: return "explicit";
2025 case TYPENAME: return "typename";
2026 case CONST_CAST: return "const_cast";
2027 case DYNAMIC_CAST: return "dynamic_cast";
2028 case REINTERPRET_CAST: return "reinterpret_cast";
2029 case STATIC_CAST: return "static_cast";
2030 case TYPEID: return "typeid";
2031 case USING: return "using";
2032 case WCHAR: return "wchar_t";
2033 case YYEOF: return "EOF";
2035 default:
2036 if (t < 255)
2038 b[0] = t;
2039 b[1] = '\0';
2040 return b;
2042 else
2043 return "???";
2048 /* Reinitialize the scanner for a new input file. */
2050 static void
2051 re_init_scanner (void)
2053 in = inbuffer;
2054 yyline = 1;
2056 if (yytext == NULL)
2058 int size = 256;
2059 yytext = (char *) xmalloc (size * sizeof *yytext);
2060 yytext_end = yytext + size;
2065 /* Insert a keyword NAME with token value TKV into the keyword hash
2066 table. */
2068 static void
2069 insert_keyword (const char *name, int tkv)
2071 const char *s;
2072 unsigned h = 0;
2073 struct kw *k = (struct kw *) xmalloc (sizeof *k);
2075 for (s = name; *s; ++s)
2076 h = (h << 1) ^ *s;
2078 h %= KEYWORD_TABLE_SIZE;
2079 k->name = name;
2080 k->tk = tkv;
2081 k->next = keyword_table[h];
2082 keyword_table[h] = k;
2086 /* Initialize the scanner for the first file. This sets up the
2087 character class vectors and fills the keyword hash table. */
2089 static void
2090 init_scanner (void)
2092 int i;
2094 /* Allocate the input buffer */
2095 inbuffer_size = READ_CHUNK_SIZE + 1;
2096 inbuffer = in = (char *) xmalloc (inbuffer_size);
2097 yyline = 1;
2099 /* Set up character class vectors. */
2100 for (i = 0; i < sizeof is_ident; ++i)
2102 if (i == '_' || isalnum (i))
2103 is_ident[i] = 1;
2105 if (i >= '0' && i <= '9')
2106 is_digit[i] = 1;
2108 if (i == ' ' || i == '\t' || i == '\f' || i == '\v')
2109 is_white[i] = 1;
2112 /* Fill keyword hash table. */
2113 insert_keyword ("and", LAND);
2114 insert_keyword ("and_eq", ANDASGN);
2115 insert_keyword ("asm", ASM);
2116 insert_keyword ("auto", AUTO);
2117 insert_keyword ("bitand", '&');
2118 insert_keyword ("bitor", '|');
2119 insert_keyword ("bool", BOOL);
2120 insert_keyword ("break", BREAK);
2121 insert_keyword ("case", CASE);
2122 insert_keyword ("catch", CATCH);
2123 insert_keyword ("char", CHAR);
2124 insert_keyword ("class", CLASS);
2125 insert_keyword ("compl", '~');
2126 insert_keyword ("const", CONST);
2127 insert_keyword ("const_cast", CONST_CAST);
2128 insert_keyword ("continue", CONTINUE);
2129 insert_keyword ("default", DEFAULT);
2130 insert_keyword ("delete", DELETE);
2131 insert_keyword ("do", DO);
2132 insert_keyword ("double", DOUBLE);
2133 insert_keyword ("dynamic_cast", DYNAMIC_CAST);
2134 insert_keyword ("else", ELSE);
2135 insert_keyword ("enum", ENUM);
2136 insert_keyword ("explicit", EXPLICIT);
2137 insert_keyword ("extern", EXTERN);
2138 insert_keyword ("false", FALSE);
2139 insert_keyword ("float", FLOAT);
2140 insert_keyword ("for", FOR);
2141 insert_keyword ("friend", FRIEND);
2142 insert_keyword ("goto", GOTO);
2143 insert_keyword ("if", IF);
2144 insert_keyword ("inline", T_INLINE);
2145 insert_keyword ("int", INT);
2146 insert_keyword ("long", LONG);
2147 insert_keyword ("mutable", MUTABLE);
2148 insert_keyword ("namespace", NAMESPACE);
2149 insert_keyword ("new", NEW);
2150 insert_keyword ("not", '!');
2151 insert_keyword ("not_eq", NE);
2152 insert_keyword ("operator", OPERATOR);
2153 insert_keyword ("or", LOR);
2154 insert_keyword ("or_eq", ORASGN);
2155 insert_keyword ("private", PRIVATE);
2156 insert_keyword ("protected", PROTECTED);
2157 insert_keyword ("public", PUBLIC);
2158 insert_keyword ("register", REGISTER);
2159 insert_keyword ("reinterpret_cast", REINTERPRET_CAST);
2160 insert_keyword ("return", RETURN);
2161 insert_keyword ("short", SHORT);
2162 insert_keyword ("signed", SIGNED);
2163 insert_keyword ("sizeof", SIZEOF);
2164 insert_keyword ("static", STATIC);
2165 insert_keyword ("static_cast", STATIC_CAST);
2166 insert_keyword ("struct", STRUCT);
2167 insert_keyword ("switch", SWITCH);
2168 insert_keyword ("template", TEMPLATE);
2169 insert_keyword ("this", THIS);
2170 insert_keyword ("throw", THROW);
2171 insert_keyword ("true", TRUE);
2172 insert_keyword ("try", TRY);
2173 insert_keyword ("typedef", TYPEDEF);
2174 insert_keyword ("typeid", TYPEID);
2175 insert_keyword ("typename", TYPENAME);
2176 insert_keyword ("union", UNION);
2177 insert_keyword ("unsigned", UNSIGNED);
2178 insert_keyword ("using", USING);
2179 insert_keyword ("virtual", VIRTUAL);
2180 insert_keyword ("void", VOID);
2181 insert_keyword ("volatile", VOLATILE);
2182 insert_keyword ("wchar_t", WCHAR);
2183 insert_keyword ("while", WHILE);
2184 insert_keyword ("xor", '^');
2185 insert_keyword ("xor_eq", XORASGN);
2190 /***********************************************************************
2191 Parser
2192 ***********************************************************************/
2194 /* Match the current lookahead token and set it to the next token. */
2196 #define MATCH() (tk = yylex ())
2198 /* Return the lookahead token. If current lookahead token is cleared,
2199 read a new token. */
2201 #define LA1 (tk == -1 ? (tk = yylex ()) : tk)
2203 /* Is the current lookahead equal to the token T? */
2205 #define LOOKING_AT(T) (tk == (T))
2207 /* Is the current lookahead one of T1 or T2? */
2209 #define LOOKING_AT2(T1, T2) (tk == (T1) || tk == (T2))
2211 /* Is the current lookahead one of T1, T2 or T3? */
2213 #define LOOKING_AT3(T1, T2, T3) (tk == (T1) || tk == (T2) || tk == (T3))
2215 /* Is the current lookahead one of T1...T4? */
2217 #define LOOKING_AT4(T1, T2, T3, T4) \
2218 (tk == (T1) || tk == (T2) || tk == (T3) || tk == (T4))
2220 /* Match token T if current lookahead is T. */
2222 #define MATCH_IF(T) if (LOOKING_AT (T)) MATCH (); else ((void) 0)
2224 /* Skip to matching token if current token is T. */
2226 #define SKIP_MATCHING_IF(T) \
2227 if (LOOKING_AT (T)) skip_matching (); else ((void) 0)
2230 /* Skip forward until a given token TOKEN or YYEOF is seen and return
2231 the current lookahead token after skipping. */
2233 static int
2234 skip_to (int token)
2236 while (!LOOKING_AT2 (YYEOF, token))
2237 MATCH ();
2238 return tk;
2241 /* Skip over pairs of tokens (parentheses, square brackets,
2242 angle brackets, curly brackets) matching the current lookahead. */
2244 static void
2245 skip_matching (void)
2247 int open, close, n;
2249 switch (open = LA1)
2251 case '{':
2252 close = '}';
2253 break;
2255 case '(':
2256 close = ')';
2257 break;
2259 case '<':
2260 close = '>';
2261 break;
2263 case '[':
2264 close = ']';
2265 break;
2267 default:
2268 abort ();
2271 for (n = 0;;)
2273 if (LOOKING_AT (open))
2274 ++n;
2275 else if (LOOKING_AT (close))
2276 --n;
2277 else if (LOOKING_AT (YYEOF))
2278 break;
2280 MATCH ();
2282 if (n == 0)
2283 break;
2287 static void
2288 skip_initializer (void)
2290 for (;;)
2292 switch (LA1)
2294 case ';':
2295 case ',':
2296 case YYEOF:
2297 return;
2299 case '{':
2300 case '[':
2301 case '(':
2302 skip_matching ();
2303 break;
2305 default:
2306 MATCH ();
2307 break;
2312 /* Build qualified namespace alias (A::B::c) and return it. */
2314 static struct link *
2315 match_qualified_namespace_alias (void)
2317 struct link *head = NULL;
2318 struct link *cur = NULL;
2319 struct link *tmp = NULL;
2321 for (;;)
2323 MATCH ();
2324 switch (LA1)
2326 case IDENT:
2327 tmp = (struct link *) xmalloc (sizeof *cur);
2328 tmp->sym = find_namespace (yytext, cur ? cur->sym : NULL);
2329 tmp->next = NULL;
2330 if (head)
2332 cur = cur->next = tmp;
2334 else
2336 head = cur = tmp;
2338 break;
2339 case DCOLON:
2340 /* Just skip */
2341 break;
2342 default:
2343 return head;
2344 break;
2349 /* Re-initialize the parser by resetting the lookahead token. */
2351 static void
2352 re_init_parser (void)
2354 tk = -1;
2358 /* Parse a parameter list, including the const-specifier,
2359 pure-specifier, and throw-list that may follow a parameter list.
2360 Return in FLAGS what was seen following the parameter list.
2361 Returns a hash code for the parameter types. This value is used to
2362 distinguish between overloaded functions. */
2364 static unsigned
2365 parm_list (int *flags)
2367 unsigned hash = 0;
2368 int type_seen = 0;
2370 while (!LOOKING_AT2 (YYEOF, ')'))
2372 switch (LA1)
2374 /* Skip over grouping parens or parameter lists in parameter
2375 declarations. */
2376 case '(':
2377 skip_matching ();
2378 break;
2380 /* Next parameter. */
2381 case ',':
2382 MATCH ();
2383 type_seen = 0;
2384 break;
2386 /* Ignore the scope part of types, if any. This is because
2387 some types need scopes when defined outside of a class body,
2388 and don't need them inside the class body. This means that
2389 we have to look for the last IDENT in a sequence of
2390 IDENT::IDENT::... */
2391 case IDENT:
2392 if (!type_seen)
2394 char *last_id;
2395 unsigned ident_type_hash = 0;
2397 parse_qualified_param_ident_or_type (&last_id);
2398 if (last_id)
2400 /* LAST_ID null means something like `X::*'. */
2401 for (; *last_id; ++last_id)
2402 ident_type_hash = (ident_type_hash << 1) ^ *last_id;
2403 hash = (hash << 1) ^ ident_type_hash;
2404 type_seen = 1;
2407 else
2408 MATCH ();
2409 break;
2411 case VOID:
2412 /* This distinction is made to make `func (void)' equivalent
2413 to `func ()'. */
2414 type_seen = 1;
2415 MATCH ();
2416 if (!LOOKING_AT (')'))
2417 hash = (hash << 1) ^ VOID;
2418 break;
2420 case BOOL: case CHAR: case CLASS: case CONST:
2421 case DOUBLE: case ENUM: case FLOAT: case INT:
2422 case LONG: case SHORT: case SIGNED: case STRUCT:
2423 case UNION: case UNSIGNED: case VOLATILE: case WCHAR:
2424 case ELLIPSIS:
2425 type_seen = 1;
2426 hash = (hash << 1) ^ LA1;
2427 MATCH ();
2428 break;
2430 case '*': case '&': case '[': case ']':
2431 hash = (hash << 1) ^ LA1;
2432 MATCH ();
2433 break;
2435 default:
2436 MATCH ();
2437 break;
2441 if (LOOKING_AT (')'))
2443 MATCH ();
2445 if (LOOKING_AT (CONST))
2447 /* We can overload the same function on `const' */
2448 hash = (hash << 1) ^ CONST;
2449 SET_FLAG (*flags, F_CONST);
2450 MATCH ();
2453 if (LOOKING_AT (THROW))
2455 MATCH ();
2456 SKIP_MATCHING_IF ('(');
2457 SET_FLAG (*flags, F_THROW);
2460 if (LOOKING_AT ('='))
2462 MATCH ();
2463 if (LOOKING_AT (CINT) && yyival == 0)
2465 MATCH ();
2466 SET_FLAG (*flags, F_PURE);
2471 return hash;
2475 /* Print position info to stdout. */
2477 static void
2478 print_info (void)
2480 if (info_position >= 0 && BUFFER_POS () <= info_position)
2481 if (info_cls)
2482 printf ("(\"%s\" \"%s\" \"%s\" %d)\n",
2483 info_cls->name, sym_scope (info_cls),
2484 info_member->name, info_where);
2488 /* Parse a member declaration within the class body of CLS. VIS is
2489 the access specifier for the member (private, protected,
2490 public). */
2492 static void
2493 member (struct sym *cls, int vis)
2495 char *id = NULL;
2496 int sc = SC_MEMBER;
2497 char *regexp = NULL;
2498 int pos;
2499 int is_constructor;
2500 int anonymous = 0;
2501 int flags = 0;
2502 int class_tag;
2503 int type_seen = 0;
2504 int paren_seen = 0;
2505 unsigned hash = 0;
2506 int tilde = 0;
2508 while (!LOOKING_AT4 (';', '{', '}', YYEOF))
2510 switch (LA1)
2512 default:
2513 MATCH ();
2514 break;
2516 /* A function or class may follow. */
2517 case TEMPLATE:
2518 MATCH();
2519 SET_FLAG (flags, F_TEMPLATE);
2520 /* Skip over template argument list */
2521 SKIP_MATCHING_IF ('<');
2522 break;
2524 case EXPLICIT:
2525 SET_FLAG (flags, F_EXPLICIT);
2526 goto typeseen;
2528 case MUTABLE:
2529 SET_FLAG (flags, F_MUTABLE);
2530 goto typeseen;
2532 case T_INLINE:
2533 SET_FLAG (flags, F_INLINE);
2534 goto typeseen;
2536 case VIRTUAL:
2537 SET_FLAG (flags, F_VIRTUAL);
2538 goto typeseen;
2540 case '[':
2541 skip_matching ();
2542 break;
2544 case ENUM:
2545 sc = SC_TYPE;
2546 goto typeseen;
2548 case TYPEDEF:
2549 sc = SC_TYPE;
2550 goto typeseen;
2552 case FRIEND:
2553 sc = SC_FRIEND;
2554 goto typeseen;
2556 case STATIC:
2557 sc = SC_STATIC;
2558 goto typeseen;
2560 case '~':
2561 tilde = 1;
2562 MATCH ();
2563 break;
2565 case IDENT:
2566 /* Remember IDENTS seen so far. Among these will be the member
2567 name. */
2568 id = (char *) xrealloc (id, strlen (yytext) + 2);
2569 if (tilde)
2571 *id = '~';
2572 strcpy (id + 1, yytext);
2574 else
2575 strcpy (id, yytext);
2576 MATCH ();
2577 break;
2579 case OPERATOR:
2581 char *s = operator_name (&sc);
2582 id = (char *) xrealloc (id, strlen (s) + 1);
2583 strcpy (id, s);
2585 break;
2587 case '(':
2588 /* Most probably the beginning of a parameter list. */
2589 MATCH ();
2590 paren_seen = 1;
2592 if (id && cls)
2594 if (!(is_constructor = streq (id, cls->name)))
2595 regexp = matching_regexp ();
2597 else
2598 is_constructor = 0;
2600 pos = BUFFER_POS ();
2601 hash = parm_list (&flags);
2603 if (is_constructor)
2604 regexp = matching_regexp ();
2606 if (id && cls != NULL)
2607 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, flags);
2609 while (!LOOKING_AT3 (';', '{', YYEOF))
2610 MATCH ();
2612 if (LOOKING_AT ('{') && id && cls)
2613 add_member_defn (cls, id, regexp, pos, hash, 0, sc, flags);
2615 free (id);
2616 id = NULL;
2617 sc = SC_MEMBER;
2618 break;
2620 case STRUCT: case UNION: case CLASS:
2621 /* Nested class */
2622 class_tag = LA1;
2623 type_seen = 1;
2624 MATCH ();
2625 anonymous = 1;
2627 /* More than one ident here to allow for MS-DOS specialties
2628 like `_export class' etc. The last IDENT seen counts
2629 as the class name. */
2630 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
2632 if (LOOKING_AT (IDENT))
2633 anonymous = 0;
2634 MATCH ();
2637 if (LOOKING_AT2 (':', '{'))
2638 class_definition (anonymous ? NULL : cls, class_tag, flags, 1);
2639 else
2640 skip_to (';');
2641 break;
2643 case INT: case CHAR: case LONG: case UNSIGNED:
2644 case SIGNED: case CONST: case DOUBLE: case VOID:
2645 case SHORT: case VOLATILE: case BOOL: case WCHAR:
2646 case TYPENAME:
2647 typeseen:
2648 type_seen = 1;
2649 MATCH ();
2650 break;
2654 if (LOOKING_AT (';'))
2656 /* The end of a member variable, a friend declaration or an access
2657 declaration. We don't want to add friend classes as members. */
2658 if (id && sc != SC_FRIEND && cls)
2660 regexp = matching_regexp ();
2661 pos = BUFFER_POS ();
2663 if (cls != NULL)
2665 if (type_seen || !paren_seen)
2666 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2667 else
2668 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, 0);
2672 MATCH ();
2673 print_info ();
2675 else if (LOOKING_AT ('{'))
2677 /* A named enum. */
2678 if (sc == SC_TYPE && id && cls)
2680 regexp = matching_regexp ();
2681 pos = BUFFER_POS ();
2683 if (cls != NULL)
2685 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2686 add_member_defn (cls, id, regexp, pos, 0, 1, sc, 0);
2690 skip_matching ();
2691 print_info ();
2694 free (id);
2698 /* Parse the body of class CLS. TAG is the tag of the class (struct,
2699 union, class). */
2701 static void
2702 class_body (struct sym *cls, int tag)
2704 int vis = tag == CLASS ? PRIVATE : PUBLIC;
2705 int temp;
2707 while (!LOOKING_AT2 (YYEOF, '}'))
2709 switch (LA1)
2711 case PRIVATE: case PROTECTED: case PUBLIC:
2712 temp = LA1;
2713 MATCH ();
2715 if (LOOKING_AT (':'))
2717 vis = temp;
2718 MATCH ();
2720 else
2722 /* Probably conditional compilation for inheritance list.
2723 We don't known whether there comes more of this.
2724 This is only a crude fix that works most of the time. */
2727 MATCH ();
2729 while (LOOKING_AT2 (IDENT, ',')
2730 || LOOKING_AT3 (PUBLIC, PROTECTED, PRIVATE));
2732 break;
2734 case TYPENAME:
2735 case USING:
2736 skip_to (';');
2737 break;
2739 /* Try to synchronize */
2740 case CHAR: case CLASS: case CONST:
2741 case DOUBLE: case ENUM: case FLOAT: case INT:
2742 case LONG: case SHORT: case SIGNED: case STRUCT:
2743 case UNION: case UNSIGNED: case VOID: case VOLATILE:
2744 case TYPEDEF: case STATIC: case T_INLINE: case FRIEND:
2745 case VIRTUAL: case TEMPLATE: case IDENT: case '~':
2746 case BOOL: case WCHAR: case EXPLICIT: case MUTABLE:
2747 member (cls, vis);
2748 break;
2750 default:
2751 MATCH ();
2752 break;
2758 /* Parse a qualified identifier. Current lookahead is IDENT. A
2759 qualified ident has the form `X<..>::Y<...>::T<...>. Returns a
2760 symbol for that class. */
2762 static struct sym *
2763 parse_classname (void)
2765 struct sym *last_class = NULL;
2767 while (LOOKING_AT (IDENT))
2769 last_class = add_sym (yytext, last_class);
2770 MATCH ();
2772 if (LOOKING_AT ('<'))
2774 skip_matching ();
2775 SET_FLAG (last_class->flags, F_TEMPLATE);
2778 if (!LOOKING_AT (DCOLON))
2779 break;
2781 MATCH ();
2784 return last_class;
2788 /* Parse an operator name. Add the `static' flag to *SC if an
2789 implicitly static operator has been parsed. Value is a pointer to
2790 a static buffer holding the constructed operator name string. */
2792 static char *
2793 operator_name (int *sc)
2795 static size_t id_size = 0;
2796 static char *id = NULL;
2797 const char *s;
2798 size_t len;
2800 MATCH ();
2802 if (LOOKING_AT2 (NEW, DELETE))
2804 /* `new' and `delete' are implicitly static. */
2805 if (*sc != SC_FRIEND)
2806 *sc = SC_STATIC;
2808 s = token_string (LA1);
2809 MATCH ();
2811 len = strlen (s) + 10;
2812 if (len > id_size)
2814 size_t new_size = max (len, 2 * id_size);
2815 id = (char *) xrealloc (id, new_size);
2816 id_size = new_size;
2818 strcpy (id, s);
2820 /* Vector new or delete? */
2821 if (LOOKING_AT ('['))
2823 strcat (id, "[");
2824 MATCH ();
2826 if (LOOKING_AT (']'))
2828 strcat (id, "]");
2829 MATCH ();
2833 else
2835 size_t tokens_matched = 0;
2837 len = 20;
2838 if (len > id_size)
2840 int new_size = max (len, 2 * id_size);
2841 id = (char *) xrealloc (id, new_size);
2842 id_size = new_size;
2844 strcpy (id, "operator");
2846 /* Beware access declarations of the form "X::f;" Beware of
2847 `operator () ()'. Yet another difficulty is found in
2848 GCC 2.95's STL: `operator == __STL_NULL_TMPL_ARGS (...'. */
2849 while (!(LOOKING_AT ('(') && tokens_matched)
2850 && !LOOKING_AT2 (';', YYEOF))
2852 s = token_string (LA1);
2853 len += strlen (s) + 2;
2854 if (len > id_size)
2856 size_t new_size = max (len, 2 * id_size);
2857 id = (char *) xrealloc (id, new_size);
2858 id_size = new_size;
2861 if (*s != ')' && *s != ']')
2862 strcat (id, " ");
2863 strcat (id, s);
2864 MATCH ();
2866 /* If this is a simple operator like `+', stop now. */
2867 if (!isalpha ((unsigned char) *s) && *s != '(' && *s != '[')
2868 break;
2870 ++tokens_matched;
2874 return id;
2878 /* This one consumes the last IDENT of a qualified member name like
2879 `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
2880 symbol structure for the ident. */
2882 static struct sym *
2883 parse_qualified_ident_or_type (char **last_id)
2885 struct sym *cls = NULL;
2886 char *id = NULL;
2887 size_t id_size = 0;
2888 int enter = 0;
2890 while (LOOKING_AT (IDENT))
2892 int len = strlen (yytext) + 1;
2893 if (len > id_size)
2895 id = (char *) xrealloc (id, len);
2896 id_size = len;
2898 strcpy (id, yytext);
2899 *last_id = id;
2900 MATCH ();
2902 SKIP_MATCHING_IF ('<');
2904 if (LOOKING_AT (DCOLON))
2906 struct sym *pcn = NULL;
2907 struct link *pna = check_namespace_alias (id);
2908 if (pna)
2912 enter_namespace (pna->sym->name);
2913 enter++;
2914 pna = pna->next;
2916 while (pna);
2918 else if ((pcn = check_namespace (id, current_namespace)))
2920 enter_namespace (pcn->name);
2921 enter++;
2923 else
2924 cls = add_sym (id, cls);
2926 *last_id = NULL;
2927 free (id);
2928 id = NULL;
2929 id_size = 0;
2930 MATCH ();
2932 else
2933 break;
2936 while (enter--)
2937 leave_namespace();
2939 return cls;
2943 /* This one consumes the last IDENT of a qualified member name like
2944 `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
2945 symbol structure for the ident. */
2947 static void
2948 parse_qualified_param_ident_or_type (char **last_id)
2950 struct sym *cls = NULL;
2951 static char *id = NULL;
2952 static int id_size = 0;
2954 assert (LOOKING_AT (IDENT));
2958 int len = strlen (yytext) + 1;
2959 if (len > id_size)
2961 id = (char *) xrealloc (id, len);
2962 id_size = len;
2964 strcpy (id, yytext);
2965 *last_id = id;
2966 MATCH ();
2968 SKIP_MATCHING_IF ('<');
2970 if (LOOKING_AT (DCOLON))
2972 cls = add_sym (id, cls);
2973 *last_id = NULL;
2974 MATCH ();
2976 else
2977 break;
2979 while (LOOKING_AT (IDENT));
2983 /* Parse a class definition.
2985 CONTAINING is the class containing the class being parsed or null.
2986 This may also be null if NESTED != 0 if the containing class is
2987 anonymous. TAG is the tag of the class (struct, union, class).
2988 NESTED is non-zero if we are parsing a nested class.
2990 Current lookahead is the class name. */
2992 static void
2993 class_definition (struct sym *containing, int tag, int flags, int nested)
2995 struct sym *current;
2996 struct sym *base_class;
2998 /* Set CURRENT to null if no entry has to be made for the class
2999 parsed. This is the case for certain command line flag
3000 settings. */
3001 if ((tag != CLASS && !f_structs) || (nested && !f_nested_classes))
3002 current = NULL;
3003 else
3005 current = add_sym (yytext, containing);
3006 current->pos = BUFFER_POS ();
3007 current->regexp = matching_regexp ();
3008 current->filename = filename;
3009 current->flags = flags;
3012 /* If at ':', base class list follows. */
3013 if (LOOKING_AT (':'))
3015 int done = 0;
3016 MATCH ();
3018 while (!done)
3020 switch (LA1)
3022 case VIRTUAL: case PUBLIC: case PROTECTED: case PRIVATE:
3023 MATCH ();
3024 break;
3026 case IDENT:
3027 base_class = parse_classname ();
3028 if (base_class && current && base_class != current)
3029 add_link (base_class, current);
3030 break;
3032 /* The `,' between base classes or the end of the base
3033 class list. Add the previously found base class.
3034 It's done this way to skip over sequences of
3035 `A::B::C' until we reach the end.
3037 FIXME: it is now possible to handle `class X : public B::X'
3038 because we have enough information. */
3039 case ',':
3040 MATCH ();
3041 break;
3043 default:
3044 /* A syntax error, possibly due to preprocessor constructs
3045 like
3047 #ifdef SOMETHING
3048 class A : public B
3049 #else
3050 class A : private B.
3052 MATCH until we see something like `;' or `{'. */
3053 while (!LOOKING_AT3 (';', YYEOF, '{'))
3054 MATCH ();
3055 done = 1;
3057 case '{':
3058 done = 1;
3059 break;
3064 /* Parse the class body if there is one. */
3065 if (LOOKING_AT ('{'))
3067 if (tag != CLASS && !f_structs)
3068 skip_matching ();
3069 else
3071 MATCH ();
3072 class_body (current, tag);
3074 if (LOOKING_AT ('}'))
3076 MATCH ();
3077 if (LOOKING_AT (';') && !nested)
3078 MATCH ();
3084 /* Add to class *CLS information for the declaration of variable or
3085 type *ID. If *CLS is null, this means a global declaration. SC is
3086 the storage class of *ID. FLAGS is a bit set giving additional
3087 information about the member (see the F_* defines). */
3089 static void
3090 add_declarator (struct sym **cls, char **id, int flags, int sc)
3092 if (LOOKING_AT2 (';', ','))
3094 /* The end of a member variable or of an access declaration
3095 `X::f'. To distinguish between them we have to know whether
3096 type information has been seen. */
3097 if (*id)
3099 char *regexp = matching_regexp ();
3100 int pos = BUFFER_POS ();
3102 if (*cls)
3103 add_member_defn (*cls, *id, regexp, pos, 0, 1, SC_UNKNOWN, flags);
3104 else
3105 add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
3108 MATCH ();
3109 print_info ();
3111 else if (LOOKING_AT ('{'))
3113 if (sc == SC_TYPE && *id)
3115 /* A named enumeration. */
3116 char *regexp = matching_regexp ();
3117 int pos = BUFFER_POS ();
3118 add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
3121 skip_matching ();
3122 print_info ();
3125 free (*id);
3126 *id = NULL;
3127 *cls = NULL;
3130 /* Parse a declaration. */
3132 static void
3133 declaration (int flags)
3135 char *id = NULL;
3136 struct sym *cls = NULL;
3137 char *regexp = NULL;
3138 int pos = 0;
3139 unsigned hash = 0;
3140 int is_constructor;
3141 int sc = 0;
3143 while (!LOOKING_AT3 (';', '{', YYEOF))
3145 switch (LA1)
3147 default:
3148 MATCH ();
3149 break;
3151 case '[':
3152 skip_matching ();
3153 break;
3155 case ENUM:
3156 case TYPEDEF:
3157 sc = SC_TYPE;
3158 MATCH ();
3159 break;
3161 case STATIC:
3162 sc = SC_STATIC;
3163 MATCH ();
3164 break;
3166 case INT: case CHAR: case LONG: case UNSIGNED:
3167 case SIGNED: case CONST: case DOUBLE: case VOID:
3168 case SHORT: case VOLATILE: case BOOL: case WCHAR:
3169 MATCH ();
3170 break;
3172 case CLASS: case STRUCT: case UNION:
3173 /* This is for the case `STARTWRAP class X : ...' or
3174 `declare (X, Y)\n class A : ...'. */
3175 if (id)
3177 free (id);
3178 return;
3181 case '=':
3182 /* Assumed to be the start of an initialization in this
3183 context. */
3184 skip_initializer ();
3185 break;
3187 case ',':
3188 add_declarator (&cls, &id, flags, sc);
3189 break;
3191 case OPERATOR:
3193 char *s = operator_name (&sc);
3194 id = (char *) xrealloc (id, strlen (s) + 1);
3195 strcpy (id, s);
3197 break;
3199 case T_INLINE:
3200 SET_FLAG (flags, F_INLINE);
3201 MATCH ();
3202 break;
3204 case '~':
3205 MATCH ();
3206 if (LOOKING_AT (IDENT))
3208 id = (char *) xrealloc (id, strlen (yytext) + 2);
3209 *id = '~';
3210 strcpy (id + 1, yytext);
3211 MATCH ();
3213 break;
3215 case IDENT:
3216 cls = parse_qualified_ident_or_type (&id);
3217 break;
3219 case '(':
3220 /* Most probably the beginning of a parameter list. */
3221 if (cls)
3223 MATCH ();
3225 if (id && cls)
3227 if (!(is_constructor = streq (id, cls->name)))
3228 regexp = matching_regexp ();
3230 else
3231 is_constructor = 0;
3233 pos = BUFFER_POS ();
3234 hash = parm_list (&flags);
3236 if (is_constructor)
3237 regexp = matching_regexp ();
3239 if (id && cls)
3240 add_member_defn (cls, id, regexp, pos, hash, 0,
3241 SC_UNKNOWN, flags);
3243 else
3245 /* This may be a C functions, but also a macro
3246 call of the form `declare (A, B)' --- such macros
3247 can be found in some class libraries. */
3248 MATCH ();
3250 if (id)
3252 regexp = matching_regexp ();
3253 pos = BUFFER_POS ();
3254 hash = parm_list (&flags);
3255 add_global_decl (id, regexp, pos, hash, 0, sc, flags);
3258 /* This is for the case that the function really is
3259 a macro with no `;' following it. If a CLASS directly
3260 follows, we would miss it otherwise. */
3261 if (LOOKING_AT3 (CLASS, STRUCT, UNION))
3262 return;
3265 while (!LOOKING_AT3 (';', '{', YYEOF))
3266 MATCH ();
3268 if (!cls && id && LOOKING_AT ('{'))
3269 add_global_defn (id, regexp, pos, hash, 0, sc, flags);
3271 free (id);
3272 id = NULL;
3273 break;
3277 add_declarator (&cls, &id, flags, sc);
3281 /* Parse a list of top-level declarations/definitions. START_FLAGS
3282 says in which context we are parsing. If it is F_EXTERNC, we are
3283 parsing in an `extern "C"' block. Value is 1 if EOF is reached, 0
3284 otherwise. */
3286 static int
3287 globals (int start_flags)
3289 int anonymous;
3290 int class_tk;
3291 int flags = start_flags;
3293 for (;;)
3295 char *prev_in = in;
3297 switch (LA1)
3299 case NAMESPACE:
3301 MATCH ();
3303 if (LOOKING_AT (IDENT))
3305 char *namespace_name = xstrdup (yytext);
3306 MATCH ();
3308 if (LOOKING_AT ('='))
3310 struct link *qna = match_qualified_namespace_alias ();
3311 if (qna)
3312 register_namespace_alias (namespace_name, qna);
3314 if (skip_to (';') == ';')
3315 MATCH ();
3317 else if (LOOKING_AT ('{'))
3319 MATCH ();
3320 enter_namespace (namespace_name);
3321 globals (0);
3322 leave_namespace ();
3323 MATCH_IF ('}');
3326 free (namespace_name);
3329 break;
3331 case EXTERN:
3332 MATCH ();
3333 if (LOOKING_AT (CSTRING) && *string_start == 'C'
3334 && *(string_start + 1) == '"')
3336 /* This is `extern "C"'. */
3337 MATCH ();
3339 if (LOOKING_AT ('{'))
3341 MATCH ();
3342 globals (F_EXTERNC);
3343 MATCH_IF ('}');
3345 else
3346 SET_FLAG (flags, F_EXTERNC);
3348 break;
3350 case TEMPLATE:
3351 MATCH ();
3352 SKIP_MATCHING_IF ('<');
3353 SET_FLAG (flags, F_TEMPLATE);
3354 break;
3356 case CLASS: case STRUCT: case UNION:
3357 class_tk = LA1;
3358 MATCH ();
3359 anonymous = 1;
3361 /* More than one ident here to allow for MS-DOS and OS/2
3362 specialties like `far', `_Export' etc. Some C++ libs
3363 have constructs like `_OS_DLLIMPORT(_OS_CLIENT)' in front
3364 of the class name. */
3365 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
3367 if (LOOKING_AT (IDENT))
3368 anonymous = 0;
3369 MATCH ();
3372 /* Don't add anonymous unions. */
3373 if (LOOKING_AT2 (':', '{') && !anonymous)
3374 class_definition (NULL, class_tk, flags, 0);
3375 else
3377 if (skip_to (';') == ';')
3378 MATCH ();
3381 flags = start_flags;
3382 break;
3384 case YYEOF:
3385 return 1;
3387 case '}':
3388 return 0;
3390 default:
3391 declaration (flags);
3392 flags = start_flags;
3393 break;
3396 if (prev_in == in)
3397 yyerror ("parse error", NULL);
3402 /* Parse the current input file. */
3404 static void
3405 yyparse (void)
3407 while (globals (0) == 0)
3408 MATCH_IF ('}');
3413 /***********************************************************************
3414 Main Program
3415 ***********************************************************************/
3417 /* Add the list of paths PATH_LIST to the current search path for
3418 input files. */
3420 static void
3421 add_search_path (char *path_list)
3423 while (*path_list)
3425 char *start = path_list;
3426 struct search_path *p;
3428 while (*path_list && *path_list != PATH_LIST_SEPARATOR)
3429 ++path_list;
3431 p = (struct search_path *) xmalloc (sizeof *p);
3432 p->path = (char *) xmalloc (path_list - start + 1);
3433 memcpy (p->path, start, path_list - start);
3434 p->path[path_list - start] = '\0';
3435 p->next = NULL;
3437 if (search_path_tail)
3439 search_path_tail->next = p;
3440 search_path_tail = p;
3442 else
3443 search_path = search_path_tail = p;
3445 while (*path_list == PATH_LIST_SEPARATOR)
3446 ++path_list;
3451 /* Open FILE and return a file handle for it, or -1 if FILE cannot be
3452 opened. Try to find FILE in search_path first, then try the
3453 unchanged file name. */
3455 static FILE *
3456 open_file (char *file)
3458 FILE *fp = NULL;
3459 static char *buffer;
3460 static int buffer_size;
3461 struct search_path *path;
3462 int flen = strlen (file) + 1; /* +1 for the slash */
3464 filename = xstrdup (file);
3466 for (path = search_path; path && fp == NULL; path = path->next)
3468 int len = strlen (path->path) + flen;
3470 if (len + 1 >= buffer_size)
3472 buffer_size = max (len + 1, 2 * buffer_size);
3473 buffer = (char *) xrealloc (buffer, buffer_size);
3476 strcpy (buffer, path->path);
3477 strcat (buffer, "/");
3478 strcat (buffer, file);
3479 fp = fopen (buffer, "r");
3482 /* Try the original file name. */
3483 if (fp == NULL)
3484 fp = fopen (file, "r");
3486 if (fp == NULL)
3487 yyerror ("cannot open", NULL);
3489 return fp;
3493 /* Display usage information and exit program. */
3495 #define USAGE "\
3496 Usage: ebrowse [options] {files}\n\
3498 -a, --append append output to existing file\n\
3499 -f, --files=FILES read input file names from FILE\n\
3500 -I, --search-path=LIST set search path for input files\n\
3501 -m, --min-regexp-length=N set minimum regexp length to N\n\
3502 -M, --max-regexp-length=N set maximum regexp length to N\n\
3503 -n, --no-nested-classes exclude nested classes\n\
3504 -o, --output-file=FILE set output file name to FILE\n\
3505 -p, --position-info print info about position in file\n\
3506 -s, --no-structs-or-unions don't record structs or unions\n\
3507 -v, --verbose be verbose\n\
3508 -V, --very-verbose be very verbose\n\
3509 -x, --no-regexps don't record regular expressions\n\
3510 --help display this help\n\
3511 --version display version info\n\
3514 static void
3515 usage (int error)
3517 puts (USAGE);
3518 exit (error ? EXIT_FAILURE : EXIT_SUCCESS);
3522 /* Display version and copyright info. The VERSION macro is set
3523 from config.h and contains the Emacs version. */
3525 #ifndef VERSION
3526 # define VERSION "21"
3527 #endif
3529 static void
3530 version (void)
3532 /* Makes it easier to update automatically. */
3533 char emacs_copyright[] = "Copyright (C) 2011 Free Software Foundation, Inc.";
3535 printf ("ebrowse %s\n", VERSION);
3536 puts (emacs_copyright);
3537 puts ("This program is distributed under the same terms as Emacs.");
3538 exit (EXIT_SUCCESS);
3542 /* Parse one input file FILE, adding classes and members to the symbol
3543 table. */
3545 static void
3546 process_file (char *file)
3548 FILE *fp;
3550 fp = open_file (file);
3551 if (fp)
3553 size_t nread, nbytes;
3555 /* Give a progress indication if needed. */
3556 if (f_very_verbose)
3558 puts (filename);
3559 fflush (stdout);
3561 else if (f_verbose)
3563 putchar ('.');
3564 fflush (stdout);
3567 /* Read file to inbuffer. */
3568 for (nread = 0;;)
3570 if (nread + READ_CHUNK_SIZE >= inbuffer_size)
3572 inbuffer_size = nread + READ_CHUNK_SIZE + 1;
3573 inbuffer = (char *) xrealloc (inbuffer, inbuffer_size);
3576 nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp);
3577 if (nbytes == 0)
3578 break;
3579 nread += nbytes;
3581 inbuffer[nread] = '\0';
3583 /* Reinitialize scanner and parser for the new input file. */
3584 re_init_scanner ();
3585 re_init_parser ();
3587 /* Parse it and close the file. */
3588 yyparse ();
3589 fclose (fp);
3594 /* Read a line from stream FP and return a pointer to a static buffer
3595 containing its contents without the terminating newline. Value
3596 is null when EOF is reached. */
3598 static char *
3599 read_line (FILE *fp)
3601 static char *buffer;
3602 static int buffer_size;
3603 int i = 0, c;
3605 while ((c = getc (fp)) != EOF && c != '\n')
3607 if (i >= buffer_size)
3609 buffer_size = max (100, buffer_size * 2);
3610 buffer = (char *) xrealloc (buffer, buffer_size);
3613 buffer[i++] = c;
3616 if (c == EOF && i == 0)
3617 return NULL;
3619 if (i == buffer_size)
3621 buffer_size = max (100, buffer_size * 2);
3622 buffer = (char *) xrealloc (buffer, buffer_size);
3625 buffer[i] = '\0';
3626 if (i > 0 && buffer[i - 1] == '\r')
3627 buffer[i - 1] = '\0';
3628 return buffer;
3632 /* Main entry point. */
3635 main (int argc, char **argv)
3637 int i;
3638 int any_inputfiles = 0;
3639 static const char *out_filename = DEFAULT_OUTFILE;
3640 static char **input_filenames = NULL;
3641 static int input_filenames_size = 0;
3642 static int n_input_files;
3644 filename = "command line";
3645 yyout = stdout;
3647 while ((i = getopt_long (argc, argv, "af:I:m:M:no:p:svVx",
3648 options, NULL)) != EOF)
3650 switch (i)
3652 /* Experimental. */
3653 case 'p':
3654 info_position = atoi (optarg);
3655 break;
3657 case 'n':
3658 f_nested_classes = 0;
3659 break;
3661 case 'x':
3662 f_regexps = 0;
3663 break;
3665 /* Add the name of a file containing more input files. */
3666 case 'f':
3667 if (n_input_files == input_filenames_size)
3669 input_filenames_size = max (10, 2 * input_filenames_size);
3670 input_filenames = (char **) xrealloc ((void *)input_filenames,
3671 input_filenames_size);
3673 input_filenames[n_input_files++] = xstrdup (optarg);
3674 break;
3676 /* Append new output to output file instead of truncating it. */
3677 case 'a':
3678 f_append = 1;
3679 break;
3681 /* Include structs in the output */
3682 case 's':
3683 f_structs = 0;
3684 break;
3686 /* Be verbose (give a progress indication). */
3687 case 'v':
3688 f_verbose = 1;
3689 break;
3691 /* Be very verbose (print file names as they are processed). */
3692 case 'V':
3693 f_verbose = 1;
3694 f_very_verbose = 1;
3695 break;
3697 /* Change the name of the output file. */
3698 case 'o':
3699 out_filename = optarg;
3700 break;
3702 /* Set minimum length for regular expression strings
3703 when recorded in the output file. */
3704 case 'm':
3705 min_regexp = atoi (optarg);
3706 break;
3708 /* Set maximum length for regular expression strings
3709 when recorded in the output file. */
3710 case 'M':
3711 max_regexp = atoi (optarg);
3712 break;
3714 /* Add to search path. */
3715 case 'I':
3716 add_search_path (optarg);
3717 break;
3719 /* Display help */
3720 case -2:
3721 usage (0);
3722 break;
3724 case -3:
3725 version ();
3726 break;
3730 /* Call init_scanner after command line flags have been processed to be
3731 able to add keywords depending on command line (not yet
3732 implemented). */
3733 init_scanner ();
3734 init_sym ();
3736 /* Open output file */
3737 if (*out_filename)
3739 if (f_append)
3741 /* Check that the file to append to exists, and is not
3742 empty. More specifically, it should be a valid file
3743 produced by a previous run of ebrowse, but that's too
3744 difficult to check. */
3745 FILE *fp;
3746 int rc;
3748 fp = fopen (out_filename, "r");
3749 if (fp == NULL)
3751 yyerror ("file `%s' must exist for --append", out_filename);
3752 exit (EXIT_FAILURE);
3755 rc = fseek (fp, 0, SEEK_END);
3756 if (rc == -1)
3758 yyerror ("error seeking in file `%s'", out_filename);
3759 exit (EXIT_FAILURE);
3762 rc = ftell (fp);
3763 if (rc == -1)
3765 yyerror ("error getting size of file `%s'", out_filename);
3766 exit (EXIT_FAILURE);
3769 else if (rc == 0)
3771 yyerror ("file `%s' is empty", out_filename);
3772 /* It may be ok to use an empty file for appending.
3773 exit (EXIT_FAILURE); */
3776 fclose (fp);
3779 yyout = fopen (out_filename, f_append ? "a" : "w");
3780 if (yyout == NULL)
3782 yyerror ("cannot open output file `%s'", out_filename);
3783 exit (EXIT_FAILURE);
3787 /* Process input files specified on the command line. */
3788 while (optind < argc)
3790 process_file (argv[optind++]);
3791 any_inputfiles = 1;
3794 /* Process files given on stdin if no files specified. */
3795 if (!any_inputfiles && n_input_files == 0)
3797 char *file;
3798 while ((file = read_line (stdin)) != NULL)
3799 process_file (file);
3801 else
3803 /* Process files from `--files=FILE'. Every line in FILE names
3804 one input file to process. */
3805 for (i = 0; i < n_input_files; ++i)
3807 FILE *fp = fopen (input_filenames[i], "r");
3809 if (fp == NULL)
3810 yyerror ("cannot open input file `%s'", input_filenames[i]);
3811 else
3813 char *file;
3814 while ((file = read_line (fp)) != NULL)
3815 process_file (file);
3816 fclose (fp);
3821 /* Write output file. */
3822 dump_roots (yyout);
3824 /* Close output file. */
3825 if (yyout != stdout)
3826 fclose (yyout);
3828 return EXIT_SUCCESS;
3831 /* ebrowse.c ends here */