xref: /openbsd/usr.bin/lex/yylex.c (revision db3296cf)
1 /*	$OpenBSD: yylex.c,v 1.5 2003/06/04 17:34:44 millert Exp $	*/
2 
3 /* yylex - scanner front-end for flex */
4 
5 /*-
6  * Copyright (c) 1990 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Vern Paxson.
11  *
12  * The United States Government has rights in this work pursuant
13  * to contract no. DE-AC03-76SF00098 between the United States
14  * Department of Energy and the University of California.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * Neither the name of the University nor the names of its contributors
27  * may be used to endorse or promote products derived from this software
28  * without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE.
34  */
35 
36 /* $Header: /home/cvs/src/usr.bin/lex/yylex.c,v 1.5 2003/06/04 17:34:44 millert Exp $ */
37 
38 #include <ctype.h>
39 #include "flexdef.h"
40 #include "parse.h"
41 
42 
43 /* yylex - scan for a regular expression token */
44 
45 int yylex()
46 	{
47 	int toktype;
48 	static int beglin = false;
49 	extern char *yytext;
50 
51 	if ( eofseen )
52 		toktype = EOF;
53 	else
54 		toktype = flexscan();
55 
56 	if ( toktype == EOF || toktype == 0 )
57 		{
58 		eofseen = 1;
59 
60 		if ( sectnum == 1 )
61 			{
62 			synerr( _( "premature EOF" ) );
63 			sectnum = 2;
64 			toktype = SECTEND;
65 			}
66 
67 		else
68 			toktype = 0;
69 		}
70 
71 	if ( trace )
72 		{
73 		if ( beglin )
74 			{
75 			fprintf( stderr, "%d\t", num_rules + 1 );
76 			beglin = 0;
77 			}
78 
79 		switch ( toktype )
80 			{
81 			case '<':
82 			case '>':
83 			case '^':
84 			case '$':
85 			case '"':
86 			case '[':
87 			case ']':
88 			case '{':
89 			case '}':
90 			case '|':
91 			case '(':
92 			case ')':
93 			case '-':
94 			case '/':
95 			case '\\':
96 			case '?':
97 			case '.':
98 			case '*':
99 			case '+':
100 			case ',':
101 				(void) putc( toktype, stderr );
102 				break;
103 
104 			case '\n':
105 				(void) putc( '\n', stderr );
106 
107 				if ( sectnum == 2 )
108 				beglin = 1;
109 
110 				break;
111 
112 			case SCDECL:
113 				fputs( "%s", stderr );
114 				break;
115 
116 			case XSCDECL:
117 				fputs( "%x", stderr );
118 				break;
119 
120 			case SECTEND:
121 				fputs( "%%\n", stderr );
122 
123 				/* We set beglin to be true so we'll start
124 				 * writing out numbers as we echo rules.
125 				 * flexscan() has already assigned sectnum.
126 				 */
127 				if ( sectnum == 2 )
128 					beglin = 1;
129 
130 				break;
131 
132 			case NAME:
133 				fprintf( stderr, "'%s'", nmstr );
134 				break;
135 
136 			case CHAR:
137 				switch ( yylval )
138 					{
139 					case '<':
140 					case '>':
141 					case '^':
142 					case '$':
143 					case '"':
144 					case '[':
145 					case ']':
146 					case '{':
147 					case '}':
148 					case '|':
149 					case '(':
150 					case ')':
151 					case '-':
152 					case '/':
153 					case '\\':
154 					case '?':
155 					case '.':
156 					case '*':
157 					case '+':
158 					case ',':
159 						fprintf( stderr, "\\%c",
160 							yylval );
161 						break;
162 
163 					default:
164 						if ( ! isascii( yylval ) ||
165 						     ! isprint( yylval ) )
166 							fprintf( stderr,
167 								"\\%.3o",
168 							(unsigned int) yylval );
169 						else
170 							(void) putc( yylval,
171 								stderr );
172 					break;
173 					}
174 
175 				break;
176 
177 			case NUMBER:
178 				fprintf( stderr, "%d", yylval );
179 				break;
180 
181 			case PREVCCL:
182 				fprintf( stderr, "[%d]", yylval );
183 				break;
184 
185 			case EOF_OP:
186 				fprintf( stderr, "<<EOF>>" );
187 				break;
188 
189 			case OPTION_OP:
190 				fprintf( stderr, "%s ", yytext );
191 				break;
192 
193 			case OPT_OUTFILE:
194 			case OPT_PREFIX:
195 			case CCE_ALNUM:
196 			case CCE_ALPHA:
197 			case CCE_BLANK:
198 			case CCE_CNTRL:
199 			case CCE_DIGIT:
200 			case CCE_GRAPH:
201 			case CCE_LOWER:
202 			case CCE_PRINT:
203 			case CCE_PUNCT:
204 			case CCE_SPACE:
205 			case CCE_UPPER:
206 			case CCE_XDIGIT:
207 				fprintf( stderr, "%s", yytext );
208 				break;
209 
210 			case 0:
211 				fprintf( stderr, _( "End Marker\n" ) );
212 				break;
213 
214 			default:
215 				fprintf( stderr,
216 				_( "*Something Weird* - tok: %d val: %d\n" ),
217 					toktype, yylval );
218 				break;
219 			}
220 		}
221 
222 	return toktype;
223 	}
224