yacc.c revision 87750
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1987, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3487628Sdwmalone#if 0
3587628Sdwmalone#ifndef lint
3687628Sdwmalonestatic char sccsid[] = "@(#)yacc.c	8.3 (Berkeley) 4/2/94";
3787628Sdwmalone#endif
3887628Sdwmalone#endif
3987628Sdwmalone
4087249Smarkm#include <sys/cdefs.h>
4187249Smarkm__FBSDID("$FreeBSD: head/usr.bin/ctags/yacc.c 87750 2001-12-12 18:24:42Z charnier $");
4287249Smarkm
431590Srgrimes#include <ctype.h>
441590Srgrimes#include <limits.h>
451590Srgrimes#include <stdio.h>
461590Srgrimes
471590Srgrimes#include "ctags.h"
481590Srgrimes
491590Srgrimes/*
501590Srgrimes * y_entries:
511590Srgrimes *	find the yacc tags and put them in.
521590Srgrimes */
531590Srgrimesvoid
541590Srgrimesy_entries()
551590Srgrimes{
561590Srgrimes	int	c;
571590Srgrimes	char	*sp;
581590Srgrimes	bool	in_rule;
591590Srgrimes	char	tok[MAXTOKEN];
601590Srgrimes
611590Srgrimes	in_rule = NO;
621590Srgrimes
631590Srgrimes	while (GETC(!=, EOF))
641590Srgrimes		switch (c) {
651590Srgrimes		case '\n':
661590Srgrimes			SETLINE;
671590Srgrimes			/* FALLTHROUGH */
681590Srgrimes		case ' ':
691590Srgrimes		case '\f':
701590Srgrimes		case '\r':
711590Srgrimes		case '\t':
721590Srgrimes			break;
731590Srgrimes		case '{':
741590Srgrimes			if (skip_key('}'))
751590Srgrimes				in_rule = NO;
761590Srgrimes			break;
771590Srgrimes		case '\'':
781590Srgrimes		case '"':
791590Srgrimes			if (skip_key(c))
801590Srgrimes				in_rule = NO;
811590Srgrimes			break;
821590Srgrimes		case '%':
831590Srgrimes			if (GETC(==, '%'))
841590Srgrimes				return;
851590Srgrimes			(void)ungetc(c, inf);
861590Srgrimes			break;
871590Srgrimes		case '/':
881590Srgrimes			if (GETC(==, '*'))
891590Srgrimes				skip_comment();
901590Srgrimes			else
911590Srgrimes				(void)ungetc(c, inf);
921590Srgrimes			break;
931590Srgrimes		case '|':
941590Srgrimes		case ';':
951590Srgrimes			in_rule = NO;
961590Srgrimes			break;
971590Srgrimes		default:
9832069Salex			if (in_rule || (!isalpha(c) && c != '.' && c != '_'))
991590Srgrimes				break;
1001590Srgrimes			sp = tok;
1011590Srgrimes			*sp++ = c;
1021590Srgrimes			while (GETC(!=, EOF) && (intoken(c) || c == '.'))
1031590Srgrimes				*sp++ = c;
1041590Srgrimes			*sp = EOS;
1051590Srgrimes			getline();		/* may change before ':' */
1061590Srgrimes			while (iswhite(c)) {
1071590Srgrimes				if (c == '\n')
1081590Srgrimes					SETLINE;
1091590Srgrimes				if (GETC(==, EOF))
1101590Srgrimes					return;
1111590Srgrimes			}
1121590Srgrimes			if (c == ':') {
1131590Srgrimes				pfnote(tok, lineno);
1141590Srgrimes				in_rule = YES;
1151590Srgrimes			}
1161590Srgrimes			else
1171590Srgrimes				(void)ungetc(c, inf);
1181590Srgrimes		}
1191590Srgrimes}
1201590Srgrimes
1211590Srgrimes/*
1221590Srgrimes * toss_yysec --
1231590Srgrimes *	throw away lines up to the next "\n%%\n"
1241590Srgrimes */
1251590Srgrimesvoid
1261590Srgrimestoss_yysec()
1271590Srgrimes{
1281590Srgrimes	int	c;			/* read character */
1291590Srgrimes	int	state;
1301590Srgrimes
1311590Srgrimes	/*
1321590Srgrimes	 * state == 0 : waiting
1331590Srgrimes	 * state == 1 : received a newline
1341590Srgrimes	 * state == 2 : received first %
13587750Scharnier	 * state == 3 : received second %
1361590Srgrimes	 */
1371590Srgrimes	lineftell = ftell(inf);
1381590Srgrimes	for (state = 0; GETC(!=, EOF);)
1391590Srgrimes		switch (c) {
1401590Srgrimes		case '\n':
1411590Srgrimes			++lineno;
1421590Srgrimes			lineftell = ftell(inf);
1431590Srgrimes			if (state == 3)		/* done! */
1441590Srgrimes				return;
1451590Srgrimes			state = 1;		/* start over */
1461590Srgrimes			break;
1471590Srgrimes		case '%':
1481590Srgrimes			if (state)		/* if 1 or 2 */
1491590Srgrimes				++state;	/* goto 3 */
1501590Srgrimes			break;
1511590Srgrimes		default:
1521590Srgrimes			state = 0;		/* reset */
1531590Srgrimes			break;
1541590Srgrimes		}
1551590Srgrimes}
156