summaryrefslogtreecommitdiffstats
path: root/usr.bin/lex/scan.l
blob: 74b589c771ce9540c42ca3e35770de24585e8574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* scan.l - scanner for flex input */

%{
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Vern Paxson.
 * 
 * The United States Government has rights in this work pursuant
 * to contract no. DE-AC03-76SF00098 between the United States
 * Department of Energy and the University of California.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement:  ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/* $Header: scan.l,v 1.2 94/01/04 14:33:09 vern Exp $ */

#include "flexdef.h"
#include "parse.h"

#define ACTION_ECHO add_action( yytext )
#define MARK_END_OF_PROLOG mark_prolog();

#define YY_DECL \
	int flexscan()

#define RETURNCHAR \
	yylval = (unsigned char) yytext[0]; \
	return CHAR;

#define RETURNNAME \
	strcpy( nmstr, yytext ); \
	return NAME;

#define PUT_BACK_STRING(str, start) \
	for ( i = strlen( str ) - 1; i >= start; --i ) \
		unput((str)[i])

#define CHECK_REJECT(str) \
	if ( all_upper( str ) ) \
		reject = true;

#define CHECK_YYMORE(str) \
	if ( all_lower( str ) ) \
		yymore_used = true;
%}

%x SECT2 SECT2PROLOG SECT3 CODEBLOCK PICKUPDEF SC CARETISBOL NUM QUOTE
%x FIRSTCCL CCL ACTION RECOVER BRACEERROR C_COMMENT ACTION_COMMENT
%x ACTION_STRING PERCENT_BRACE_ACTION USED_LIST CODEBLOCK_2

WS		[ \t]+
OPTWS		[ \t]*
NOT_WS		[^ \t\n]

NL		(\n|\r\n|\n\r)

NAME		([a-z_][a-z_0-9-]*)
NOT_NAME	[^a-z_*\n]+

SCNAME		{NAME}

ESCSEQ		(\\([^\n]|[0-9]{1,3}|x[0-9a-f]{1,2}))

FIRST_CCL_CHAR	([^\\\n]|{ESCSEQ})
CCL_CHAR	([^\\\n\]]|{ESCSEQ})

%%
	static int bracelevel, didadef, indented_code, checking_used;

	int doing_codeblock = false;
	int i;
	Char nmdef[MAXLINE], myesc();


^{WS}			indented_code = true; BEGIN(CODEBLOCK);
^"/*"			ACTION_ECHO; BEGIN(C_COMMENT);
^"%s"{NAME}?		return SCDECL;
^"%x"{NAME}?		return XSCDECL;
^"%{".*{NL}		{
			++linenum;
			line_directive_out( (FILE *) 0 );
			indented_code = false;
			BEGIN(CODEBLOCK);
			}

{WS}			return WHITESPACE;

^"%%".*			{
			sectnum = 2;
			bracelevel = 0;
			mark_defs1();
			line_directive_out( (FILE *) 0 );
			BEGIN(SECT2PROLOG);
			return SECTEND;
			}

^"%pointer".*{NL}	{
			if ( lex_compat )
				warn( "%pointer incompatible with -l option" );
			else
				yytext_is_array = false;
			++linenum;
			}
^"%array".*{NL}		{
			if ( C_plus_plus )
				warn( "%array incompatible with -+ option" );
			else
				yytext_is_array = true;
			++linenum;
			}

^"%used"		{
			warn( "%used/%unused have been deprecated" );
			checking_used = REALLY_USED; BEGIN(USED_LIST);
			}
^"%unused"		{
			warn( "%used/%unused have been deprecated" );
			checking_used = REALLY_NOT_USED; BEGIN(USED_LIST);
			}


^"%"[aceknopr]{OPTWS}[0-9]*{OPTWS}{NL}	++linenum;	/* ignore */

^"%"[^sxanpekotcru{}].*	synerr( "unrecognized '%' directive" );

^{NAME}			{
			strcpy( nmstr, yytext );
			didadef = false;
			BEGIN(PICKUPDEF);
			}

{SCNAME}		RETURNNAME;
^{OPTWS}{NL}		++linenum; /* allows blank lines in section 1 */
{OPTWS}{NL}		++linenum; return '\n';


<C_COMMENT>"*/"		ACTION_ECHO; BEGIN(INITIAL);
<C_COMMENT>"*/".*{NL}	++linenum; ACTION_ECHO; BEGIN(INITIAL);
<C_COMMENT>[^*\n]+	ACTION_ECHO;
<C_COMMENT>"*"		ACTION_ECHO;
<C_COMMENT>{NL}		++linenum; ACTION_ECHO;


<CODEBLOCK>^"%}".*{NL}	++linenum; BEGIN(INITIAL);
<CODEBLOCK>"reject"	ACTION_ECHO; CHECK_REJECT(yytext);
<CODEBLOCK>"yymore"	ACTION_ECHO; CHECK_YYMORE(yytext);
<CODEBLOCK>{NAME}|{NOT_NAME}|.	ACTION_ECHO;
<CODEBLOCK>{NL}		{
			++linenum;
			ACTION_ECHO;
			if ( indented_code )
				BEGIN(INITIAL);
			}


<PICKUPDEF>{WS}		/* separates name and definition */

<PICKUPDEF>{NOT_WS}.*	{
			strcpy( (char *) nmdef, yytext );

			/* Skip trailing whitespace. */
			for ( i = strlen( (char *) nmdef ) - 1;
			      i >= 0 && (nmdef[i] == ' ' || nmdef[i] == '\t');
			      --i )
				;

			nmdef[i + 1] = '\0';

			ndinstal( nmstr, nmdef );
			didadef = true;
			}

<PICKUPDEF>{NL}		{
			if ( ! didadef )
				synerr( "incomplete name definition" );
			BEGIN(INITIAL);
			++linenum;
			}

<RECOVER>.*{NL}		++linenum; BEGIN(INITIAL); RETURNNAME;


<USED_LIST>{NL}		++linenum; BEGIN(INITIAL);
<USED_LIST>{WS}
<USED_LIST>"reject"	{
			if ( all_upper( yytext ) )
				reject_really_used = checking_used;
			else
				synerr(
				"unrecognized %used/%unused construct" );
			}
<USED_LIST>"yymore"	{
			if ( all_lower( yytext ) )
				yymore_really_used = checking_used;
			else
				synerr(
				"unrecognized %used/%unused construct" );
			}
<USED_LIST>{NOT_WS}+	synerr( "unrecognized %used/%unused construct" );


<SECT2PROLOG>^"%{".*	++bracelevel; yyless( 2 );	/* eat only %{ */
<SECT2PROLOG>^"%}".*	--bracelevel; yyless( 2 );	/* eat only %} */

<SECT2PROLOG>^{WS}.*	ACTION_ECHO;	/* indented code in prolog */

<SECT2PROLOG>^{NOT_WS}.*	{	/* non-indented code */
			if ( bracelevel <= 0 )
				{ /* not in %{ ... %} */
				yyless( 0 );	/* put it all back */
				mark_prolog();
				BEGIN(SECT2);
				}
			else
				ACTION_ECHO;
			}

<SECT2PROLOG>.*		ACTION_ECHO;
<SECT2PROLOG>{NL}	++linenum; ACTION_ECHO;

<SECT2PROLOG><<EOF>>	{
			mark_prolog();
			sectnum = 0;
			yyterminate(); /* to stop the parser */
			}

<SECT2>^{OPTWS}{NL}	++linenum; /* allow blank lines in section 2 */

<SECT2>^({WS}|"%{")	{
			indented_code = (yytext[0] != '%');
			doing_codeblock = true;
			bracelevel = 1;

			if ( indented_code )
				ACTION_ECHO;

			BEGIN(CODEBLOCK_2);
			}

<SECT2>^"<"		BEGIN(SC); return '<';
<SECT2>^"^"		return '^';
<SECT2>\"		BEGIN(QUOTE); return '"';
<SECT2>"{"/[0-9]		BEGIN(NUM); return '{';
<SECT2>"{"[^0-9\n][^}\n]*	BEGIN(BRACEERROR);
<SECT2>"$"/([ \t]|{NL})	return '$';

<SECT2>{WS}"%{"		{
			bracelevel = 1;
			BEGIN(PERCENT_BRACE_ACTION);
			return '\n';
			}
<SECT2>{WS}"|".*{NL}	continued_action = true; ++linenum; return '\n';

<SECT2>{WS}		{
			/* This rule is separate from the one below because
			 * otherwise we get variable trailing context, so
			 * we can't build the scanner using -{f,F}.
			 */
			bracelevel = 0;
			continued_action = false;
			BEGIN(ACTION);
			return '\n';
			}

<SECT2>{OPTWS}{NL}	{
			bracelevel = 0;
			continued_action = false;
			BEGIN(ACTION);
			unput( '\n' );	/* so <ACTION> sees it */
			return '\n';
			}

<SECT2>"<<EOF>>"	return EOF_OP;

<SECT2>^"%%".*		{
			sectnum = 3;
			BEGIN(SECT3);
			yyterminate(); /* to stop the parser */
			}

<SECT2>"["{FIRST_CCL_CHAR}{CCL_CHAR}*	{
			int cclval;

			strcpy( nmstr, yytext );

			/* Check to see if we've already encountered this
			 * ccl.
			 */
			if ( (cclval = ccllookup( (Char *) nmstr )) )
				{
				if ( input() != ']' )
					synerr( "bad character class" );

				yylval = cclval;
				++cclreuse;
				return PREVCCL;
				}
			else
				{
				/* We fudge a bit.  We know that this ccl will
				 * soon be numbered as lastccl + 1 by cclinit.
				 */
				cclinstal( (Char *) nmstr, lastccl + 1 );

				/* Push back everything but the leading bracket
				 * so the ccl can be rescanned.
				 */
				yyless( 1 );

				BEGIN(FIRSTCCL);
				return '[';
				}
			}

<SECT2>"{"{NAME}"}"	{
			register Char *nmdefptr;
			Char *ndlookup();

			strcpy( nmstr, yytext + 1 );
			nmstr[yyleng - 2] = '\0';  /* chop trailing brace */

			if ( ! (nmdefptr = ndlookup( nmstr )) )
				format_synerr( "undefined definition {%s}",
						nmstr );

			else
				{ /* push back name surrounded by ()'s */
				int len = strlen( (char *) nmdefptr );

				if ( lex_compat || nmdefptr[0] == '^' ||
				     (len > 0 && nmdefptr[len - 1] == '$') )
					{ /* don't use ()'s after all */
					PUT_BACK_STRING((char *) nmdefptr, 0);

					if ( nmdefptr[0] == '^' )
						BEGIN(CARETISBOL);
					}

				else
					{
					unput(')');
					PUT_BACK_STRING((char *) nmdefptr, 0);
					unput('(');
					}
				}
			}

<SECT2>[/|*+?.()]	return (unsigned char) yytext[0];
<SECT2>.		RETURNCHAR;


<SC>[,*]		return (unsigned char) yytext[0];
<SC>">"			BEGIN(SECT2); return '>';
<SC>">"/^		BEGIN(CARETISBOL); return '>';
<SC>{SCNAME}		RETURNNAME;
<SC>.			{
			format_synerr( "bad <start condition>: %s", yytext );
			}

<CARETISBOL>"^"		BEGIN(SECT2); return '^';


<QUOTE>[^"\n]		RETURNCHAR;
<QUOTE>\"		BEGIN(SECT2); return '"';

<QUOTE>{NL}		{
			synerr( "missing quote" );
			BEGIN(SECT2);
			++linenum;
			return '"';
			}


<FIRSTCCL>"^"/[^-\]\n]	BEGIN(CCL); return '^';
<FIRSTCCL>"^"/("-"|"]")	return '^';
<FIRSTCCL>.		BEGIN(CCL); RETURNCHAR;

<CCL>-/[^\]\n]		return '-';
<CCL>[^\]\n]		RETURNCHAR;
<CCL>"]"		BEGIN(SECT2); return ']';
<CCL>.|{NL}		{
			synerr( "bad character class" );
			BEGIN(SECT2);
			return ']';
			}


<NUM>[0-9]+		{
			yylval = myctoi( yytext );
			return NUMBER;
			}

<NUM>","		return ',';
<NUM>"}"		BEGIN(SECT2); return '}';

<NUM>.			{
			synerr( "bad character inside {}'s" );
			BEGIN(SECT2);
			return '}';
			}

<NUM>{NL}		{
			synerr( "missing }" );
			BEGIN(SECT2);
			++linenum;
			return '}';
			}


<BRACEERROR>"}"		synerr( "bad name in {}'s" ); BEGIN(SECT2);
<BRACEERROR>{NL}	synerr( "missing }" ); ++linenum; BEGIN(SECT2);


<CODEBLOCK_2>"/*"	ACTION_ECHO; BEGIN(ACTION_COMMENT);
<PERCENT_BRACE_ACTION,CODEBLOCK_2>{OPTWS}"%}".*		bracelevel = 0;
<PERCENT_BRACE_ACTION,CODEBLOCK_2,ACTION>"reject"	{
			ACTION_ECHO;
			CHECK_REJECT(yytext);
			}
<PERCENT_BRACE_ACTION,CODEBLOCK_2,ACTION>"yymore"	{
			ACTION_ECHO;
			CHECK_YYMORE(yytext);
			}
<PERCENT_BRACE_ACTION,CODEBLOCK_2>{NAME}|{NOT_NAME}|.	ACTION_ECHO;
<PERCENT_BRACE_ACTION,CODEBLOCK_2>{NL}			{
			++linenum;
			ACTION_ECHO;
			if ( bracelevel == 0 ||
			     (doing_codeblock && indented_code) )
				{
				if ( ! doing_codeblock )
					add_action( "\tYY_BREAK\n" );
				
				doing_codeblock = false;
				BEGIN(SECT2);
				}
			}


	/* Reject and YYmore() are checked for above, in PERCENT_BRACE_ACTION */
<ACTION>"{"		ACTION_ECHO; ++bracelevel;
<ACTION>"}"		ACTION_ECHO; --bracelevel;
<ACTION>[^a-z_{}"'/\n]+	ACTION_ECHO;
<ACTION>{NAME}		ACTION_ECHO;
<ACTION>"/*"		ACTION_ECHO; BEGIN(ACTION_COMMENT);
<ACTION>"'"([^'\\\n]|\\.)*"'"	ACTION_ECHO; /* character constant */
<ACTION>\"		ACTION_ECHO; BEGIN(ACTION_STRING);
<ACTION>{NL}		{
			++linenum;
			ACTION_ECHO;
			if ( bracelevel == 0 )
				{
				add_action( "\tYY_BREAK\n" );
				BEGIN(SECT2);
				}
			}
<ACTION>.		ACTION_ECHO;

<ACTION_COMMENT>"*/"	{
			ACTION_ECHO;
			if ( doing_codeblock )
				BEGIN(CODEBLOCK_2);
			else
				BEGIN(ACTION);
			}

<ACTION_COMMENT>"*"	ACTION_ECHO;
<ACTION_COMMENT>[^*\n]+	ACTION_ECHO;
<ACTION_COMMENT>[^*\n]*{NL}	++linenum; ACTION_ECHO;

<ACTION_STRING>[^"\\\n]+	ACTION_ECHO;
<ACTION_STRING>\\.	ACTION_ECHO;
<ACTION_STRING>{NL}	++linenum; ACTION_ECHO;
<ACTION_STRING>\"	ACTION_ECHO; BEGIN(ACTION);
<ACTION_STRING>.	ACTION_ECHO;

<ACTION,ACTION_COMMENT,ACTION_STRING><<EOF>>	{
			synerr( "EOF encountered inside an action" );
			yyterminate();
			}


<SECT2,QUOTE,CCL>{ESCSEQ}	{
			yylval = myesc( (Char *) yytext );
			return CHAR;
			}

<FIRSTCCL>{ESCSEQ}	{
			yylval = myesc( (Char *) yytext );
			BEGIN(CCL);
			return CHAR;
			}


<SECT3>.*(\n?)		ECHO;
<SECT3><<EOF>>		sectnum = 0; yyterminate();

<*>.|\n			format_synerr( "bad character: %s", yytext );

%%


int yywrap()
	{
	if ( --num_input_files > 0 )
		{
		set_input_file( *++input_files );
		return 0;
		}

	else
		return 1;
	}


/* set_input_file - open the given file (if NULL, stdin) for scanning */

void set_input_file( file )
char *file;
	{
	if ( file )
		{
		infilename = file;
		yyin = fopen( infilename, "r" );

		if ( yyin == NULL )
			lerrsf( "can't open %s", file );
		}

	else
		{
		yyin = stdin;
		infilename = "<stdin>";
		}
	}


/* Wrapper routines for accessing the scanner's malloc routines. */

void *flex_alloc( size )
unsigned int size;
	{
	return yy_flex_alloc( size );
	}

void *flex_realloc( ptr, size )
void *ptr;
unsigned int size;
	{
	return yy_flex_realloc( ptr, size );
	}

void flex_free( ptr )
void *ptr;
	{
	yy_flex_free( ptr );
	}
OpenPOWER on IntegriCloud