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