1/*
2 * Copyright (c) 1987, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char sccsid[] = "@(#)yacc.c	8.3 (Berkeley) 4/2/94";
33#endif
34#endif
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include <ctype.h>
40#include <limits.h>
41#include <stdio.h>
42
43#include "ctags.h"
44
45/*
46 * y_entries:
47 *	find the yacc tags and put them in.
48 */
49void
50y_entries(void)
51{
52	int	c;
53	char	*sp;
54	bool	in_rule;
55	char	tok[MAXTOKEN];
56
57	in_rule = NO;
58
59	while (GETC(!=, EOF))
60		switch (c) {
61		case '\n':
62			SETLINE;
63			/* FALLTHROUGH */
64		case ' ':
65		case '\f':
66		case '\r':
67		case '\t':
68			break;
69		case '{':
70			if (skip_key('}'))
71				in_rule = NO;
72			break;
73		case '\'':
74		case '"':
75			if (skip_key(c))
76				in_rule = NO;
77			break;
78		case '%':
79			if (GETC(==, '%'))
80				return;
81			(void)ungetc(c, inf);
82			break;
83		case '/':
84			if (GETC(==, '*') || c == '/')
85				skip_comment(c);
86			else
87				(void)ungetc(c, inf);
88			break;
89		case '|':
90		case ';':
91			in_rule = NO;
92			break;
93		default:
94			if (in_rule || (!isalpha(c) && c != '.' && c != '_'))
95				break;
96			sp = tok;
97			*sp++ = c;
98			while (GETC(!=, EOF) && (intoken(c) || c == '.'))
99				*sp++ = c;
100			*sp = EOS;
101			get_line();		/* may change before ':' */
102			while (iswhite(c)) {
103				if (c == '\n')
104					SETLINE;
105				if (GETC(==, EOF))
106					return;
107			}
108			if (c == ':') {
109				pfnote(tok, lineno);
110				in_rule = YES;
111			}
112			else
113				(void)ungetc(c, inf);
114		}
115}
116
117/*
118 * toss_yysec --
119 *	throw away lines up to the next "\n%%\n"
120 */
121void
122toss_yysec(void)
123{
124	int	c;			/* read character */
125	int	state;
126
127	/*
128	 * state == 0 : waiting
129	 * state == 1 : received a newline
130	 * state == 2 : received first %
131	 * state == 3 : received second %
132	 */
133	lineftell = ftell(inf);
134	for (state = 0; GETC(!=, EOF);)
135		switch (c) {
136		case '\n':
137			++lineno;
138			lineftell = ftell(inf);
139			if (state == 3)		/* done! */
140				return;
141			state = 1;		/* start over */
142			break;
143		case '%':
144			if (state)		/* if 1 or 2 */
145				++state;	/* goto 3 */
146			break;
147		default:
148			state = 0;		/* reset */
149			break;
150		}
151}
152