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