xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_lex.l (revision 62a52c15422470f97fc7b311d89c83f910bcc1b1)
1 %{
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 /*
27  * Copyright (c) 2013 by Delphix. All rights reserved.
28  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29  */
30 
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 
38 #include <dt_impl.h>
39 #include <dt_grammar.h>
40 #include <dt_parser.h>
41 #include <dt_string.h>
42 
43 /*
44  * We need to undefine lex's input and unput macros so that references to these
45  * call the functions provided at the end of this source file.
46  */
47 #ifdef illumos
48 #undef input
49 #undef unput
50 #else
51 /*
52  * Define YY_INPUT for flex since input() can't be re-defined.
53  */
54 #define YY_INPUT(buf, result, max_size)			\
55 	if (yypcb->pcb_fileptr != NULL) {		\
56 		if (((result = fread(buf, 1, max_size, yypcb->pcb_fileptr)) == \
57 		    0) && ferror(yypcb->pcb_fileptr))	\
58 			longjmp(yypcb->pcb_jmpbuf, EDT_FIO); \
59 	} else {					\
60 		int n;					\
61 		for (n = 0; n < max_size &&		\
62 		    yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen; \
63 		    n++)				\
64 			buf[n] = *yypcb->pcb_strptr++;	\
65 		result = n; \
66 	}
67 /*
68  * Do not EOF let tokens to be put back. This does not work with flex.
69  * On the other hand, leaving current buffer in same state it was when
70  * last EOF was received guarantees that input() will keep returning EOF
71  * for all subsequent invocations, which is the effect desired.
72  */
73 #undef  unput
74 #define unput(c)					\
75 	do {						\
76 		int _c = c;				\
77 		if (_c != EOF)				\
78 			yyunput(_c, yytext_ptr);	\
79 	} while(0)
80 #endif
81 
82 static int id_or_type(const char *);
83 #ifdef illumos
84 static int input(void);
85 static void unput(int);
86 #endif
87 
88 /*
89  * We first define a set of labeled states for use in the D lexer and then a
90  * set of regular expressions to simplify things below. The lexer states are:
91  *
92  * S0 - D program clause and expression lexing
93  * S1 - D comments (i.e. skip everything until end of comment)
94  * S2 - D program outer scope (probe specifiers and declarations)
95  * S3 - D control line parsing (i.e. after ^# is seen but before \n)
96  * S4 - D control line scan (locate control directives only and invoke S3)
97  */
98 %}
99 
100 %option yylineno
101 
102 %e 1500		/* maximum nodes */
103 %p 4900		/* maximum positions */
104 %n 600		/* maximum states */
105 %a 3000		/* maximum transitions */
106 
107 %s S0 S1 S2 S3 S4
108 
109 RGX_AGG		"@"[a-zA-Z_][0-9a-zA-Z_]*
110 RGX_PSPEC	[-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
111 RGX_ALTIDENT	[a-zA-Z_][0-9a-zA-Z_]*
112 RGX_LMID	LM[0-9a-fA-F]+`
113 RGX_MOD_IDENT	[a-zA-Z_`][0-9a-z.A-Z_`]*`
114 RGX_IDENT	[a-zA-Z_`][0-9a-zA-Z_`]*
115 RGX_INT		([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
116 RGX_FP		([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
117 RGX_WS		[\f\n\r\t\v ]
118 RGX_STR		([^"\\\n]|\\[^"\n]|\\\")*
119 RGX_CHR		([^'\\\n]|\\[^'\n]|\\')*
120 RGX_INTERP	^[\f\t\v ]*#!.*
121 RGX_CTL		^[\f\t\v ]*#
122 
123 %%
124 
125 %{
126 
127 /*
128  * We insert a special prologue into yylex() itself: if the pcb contains a
129  * context token, we return that prior to running the normal lexer.  This
130  * allows libdtrace to force yacc into one of our three parsing contexts: D
131  * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
132  * Once the token is returned, we clear it so this only happens once.
133  */
134 if (yypcb->pcb_token != 0) {
135 	int tok = yypcb->pcb_token;
136 	yypcb->pcb_token = 0;
137 	return (tok);
138 }
139 
140 %}
141 
142 <S0>auto	return (DT_KEY_AUTO);
143 <S0>break	return (DT_KEY_BREAK);
144 <S0>case	return (DT_KEY_CASE);
145 <S0>char	return (DT_KEY_CHAR);
146 <S0>const	return (DT_KEY_CONST);
147 <S0>continue	return (DT_KEY_CONTINUE);
148 <S0>counter	return (DT_KEY_COUNTER);
149 <S0>default	return (DT_KEY_DEFAULT);
150 <S0>do		return (DT_KEY_DO);
151 <S0>double	return (DT_KEY_DOUBLE);
152 <S0>else	return (DT_KEY_ELSE);
153 <S0>enum	return (DT_KEY_ENUM);
154 <S0>extern	return (DT_KEY_EXTERN);
155 <S0>float	return (DT_KEY_FLOAT);
156 <S0>for		return (DT_KEY_FOR);
157 <S0>goto	return (DT_KEY_GOTO);
158 <S0>if		return (DT_KEY_IF);
159 <S0>import	return (DT_KEY_IMPORT);
160 <S0>inline	return (DT_KEY_INLINE);
161 <S0>int		return (DT_KEY_INT);
162 <S0>long	return (DT_KEY_LONG);
163 <S0>offsetof	return (DT_TOK_OFFSETOF);
164 <S0>probe	return (DT_KEY_PROBE);
165 <S0>provider	return (DT_KEY_PROVIDER);
166 <S0>register	return (DT_KEY_REGISTER);
167 <S0>restrict	return (DT_KEY_RESTRICT);
168 <S0>return	return (DT_KEY_RETURN);
169 <S0>self	return (DT_KEY_SELF);
170 <S0>short	return (DT_KEY_SHORT);
171 <S0>signed	return (DT_KEY_SIGNED);
172 <S0>sizeof	return (DT_TOK_SIZEOF);
173 <S0>static	return (DT_KEY_STATIC);
174 <S0>string	return (DT_KEY_STRING);
175 <S0>stringof	return (DT_TOK_STRINGOF);
176 <S0>struct	return (DT_KEY_STRUCT);
177 <S0>switch	return (DT_KEY_SWITCH);
178 <S0>this	return (DT_KEY_THIS);
179 <S0>translator	return (DT_KEY_XLATOR);
180 <S0>typedef	return (DT_KEY_TYPEDEF);
181 <S0>union	return (DT_KEY_UNION);
182 <S0>unsigned	return (DT_KEY_UNSIGNED);
183 <S0>userland	return (DT_KEY_USERLAND);
184 <S0>void	return (DT_KEY_VOID);
185 <S0>volatile	return (DT_KEY_VOLATILE);
186 <S0>while	return (DT_KEY_WHILE);
187 <S0>xlate	return (DT_TOK_XLATE);
188 
189 <S2>auto	{ yybegin(YYS_EXPR);	return (DT_KEY_AUTO); }
190 <S2>char	{ yybegin(YYS_EXPR);	return (DT_KEY_CHAR); }
191 <S2>const	{ yybegin(YYS_EXPR);	return (DT_KEY_CONST); }
192 <S2>counter	{ yybegin(YYS_DEFINE);	return (DT_KEY_COUNTER); }
193 <S2>double	{ yybegin(YYS_EXPR);	return (DT_KEY_DOUBLE); }
194 <S2>enum	{ yybegin(YYS_EXPR);	return (DT_KEY_ENUM); }
195 <S2>extern	{ yybegin(YYS_EXPR);	return (DT_KEY_EXTERN); }
196 <S2>float	{ yybegin(YYS_EXPR);	return (DT_KEY_FLOAT); }
197 <S2>import	{ yybegin(YYS_EXPR);	return (DT_KEY_IMPORT); }
198 <S2>inline	{ yybegin(YYS_DEFINE);	return (DT_KEY_INLINE); }
199 <S2>int		{ yybegin(YYS_EXPR);	return (DT_KEY_INT); }
200 <S2>long	{ yybegin(YYS_EXPR);	return (DT_KEY_LONG); }
201 <S2>provider	{ yybegin(YYS_DEFINE);	return (DT_KEY_PROVIDER); }
202 <S2>register	{ yybegin(YYS_EXPR);	return (DT_KEY_REGISTER); }
203 <S2>restrict	{ yybegin(YYS_EXPR);	return (DT_KEY_RESTRICT); }
204 <S2>self	{ yybegin(YYS_EXPR);	return (DT_KEY_SELF); }
205 <S2>short	{ yybegin(YYS_EXPR);	return (DT_KEY_SHORT); }
206 <S2>signed	{ yybegin(YYS_EXPR);	return (DT_KEY_SIGNED); }
207 <S2>static	{ yybegin(YYS_EXPR);	return (DT_KEY_STATIC); }
208 <S2>string	{ yybegin(YYS_EXPR);	return (DT_KEY_STRING); }
209 <S2>struct	{ yybegin(YYS_EXPR);	return (DT_KEY_STRUCT); }
210 <S2>this	{ yybegin(YYS_EXPR);	return (DT_KEY_THIS); }
211 <S2>translator	{ yybegin(YYS_DEFINE);	return (DT_KEY_XLATOR); }
212 <S2>typedef	{ yybegin(YYS_EXPR);	return (DT_KEY_TYPEDEF); }
213 <S2>union	{ yybegin(YYS_EXPR);	return (DT_KEY_UNION); }
214 <S2>unsigned	{ yybegin(YYS_EXPR);	return (DT_KEY_UNSIGNED); }
215 <S2>void	{ yybegin(YYS_EXPR);	return (DT_KEY_VOID); }
216 <S2>volatile	{ yybegin(YYS_EXPR);	return (DT_KEY_VOLATILE); }
217 
218 <S0>"$$"[0-9]+	{
219 			int i = atoi(yytext + 2);
220 			char *v = "";
221 
222 			/*
223 			 * A macro argument reference substitutes the text of
224 			 * an argument in place of the current token.  When we
225 			 * see $$<d> we fetch the saved string from pcb_sargv
226 			 * (or use the default argument if the option has been
227 			 * set and the argument hasn't been specified) and
228 			 * return a token corresponding to this string.
229 			 */
230 			if (i < 0 || (i >= yypcb->pcb_sargc &&
231 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
232 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
233 				    "not defined\n", yytext);
234 			}
235 
236 			if (i < yypcb->pcb_sargc) {
237 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
238 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
239 			}
240 
241 			if ((yylval.l_str = strdup(v)) == NULL)
242 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
243 
244 			(void) stresc2chr(yylval.l_str);
245 			return (DT_TOK_STRING);
246 		}
247 
248 <S0>"$"[0-9]+	{
249 			int i = atoi(yytext + 1);
250 			char *p, *v = "0";
251 
252 			/*
253 			 * A macro argument reference substitutes the text of
254 			 * one identifier or integer pattern for another.  When
255 			 * we see $<d> we fetch the saved string from pcb_sargv
256 			 * (or use the default argument if the option has been
257 			 * set and the argument hasn't been specified) and
258 			 * return a token corresponding to this string.
259 			 */
260 			if (i < 0 || (i >= yypcb->pcb_sargc &&
261 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
262 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
263 				    "not defined\n", yytext);
264 			}
265 
266 			if (i < yypcb->pcb_sargc) {
267 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
268 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
269 			}
270 
271 			/*
272 			 * If the macro text is not a valid integer or ident,
273 			 * then we treat it as a string.  The string may be
274 			 * optionally enclosed in quotes, which we strip.
275 			 */
276 			if (strbadidnum(v)) {
277 				size_t len = strlen(v);
278 
279 				if (len != 1 && *v == '"' && v[len - 1] == '"')
280 					yylval.l_str = strndup(v + 1, len - 2);
281 				else
282 					yylval.l_str = strndup(v, len);
283 
284 				if (yylval.l_str == NULL)
285 					longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
286 
287 				(void) stresc2chr(yylval.l_str);
288 				return (DT_TOK_STRING);
289 			}
290 
291 			/*
292 			 * If the macro text is not a string an begins with a
293 			 * digit or a +/- sign, process it as an integer token.
294 			 */
295 			if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
296 				if (isdigit(v[0]))
297 					yyintprefix = 0;
298 				else
299 					yyintprefix = *v++;
300 
301 				errno = 0;
302 				yylval.l_int = strtoull(v, &p, 0);
303 				(void) strncpy(yyintsuffix, p,
304 				    sizeof (yyintsuffix));
305 				yyintdecimal = *v != '0';
306 
307 				if (errno == ERANGE) {
308 					xyerror(D_MACRO_OFLOW, "macro argument"
309 					    " %s constant %s results in integer"
310 					    " overflow\n", yytext, v);
311 				}
312 
313 				return (DT_TOK_INT);
314 			}
315 
316 			return (id_or_type(v));
317 		}
318 
319 <S0>"$$"{RGX_IDENT} {
320 			dt_ident_t *idp = dt_idhash_lookup(
321 			    yypcb->pcb_hdl->dt_macros, yytext + 2);
322 
323 			char s[16]; /* enough for UINT_MAX + \0 */
324 
325 			if (idp == NULL) {
326 				xyerror(D_MACRO_UNDEF, "macro variable %s "
327 				    "is not defined\n", yytext);
328 			}
329 
330 			/*
331 			 * For the moment, all current macro variables are of
332 			 * type id_t (refer to dtrace_update() for details).
333 			 */
334 			(void) snprintf(s, sizeof (s), "%u", idp->di_id);
335 			if ((yylval.l_str = strdup(s)) == NULL)
336 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
337 
338 			return (DT_TOK_STRING);
339 		}
340 
341 <S0>"$"{RGX_IDENT} {
342 			dt_ident_t *idp = dt_idhash_lookup(
343 			    yypcb->pcb_hdl->dt_macros, yytext + 1);
344 
345 			if (idp == NULL) {
346 				xyerror(D_MACRO_UNDEF, "macro variable %s "
347 				    "is not defined\n", yytext);
348 			}
349 
350 			/*
351 			 * For the moment, all current macro variables are of
352 			 * type id_t (refer to dtrace_update() for details).
353 			 */
354 			yylval.l_int = (intmax_t)(int)idp->di_id;
355 			yyintprefix = 0;
356 			yyintsuffix[0] = '\0';
357 			yyintdecimal = 1;
358 
359 			return (DT_TOK_INT);
360 		}
361 
362 <S0>{RGX_IDENT} |
363 <S0>{RGX_MOD_IDENT}{RGX_IDENT} |
364 <S0>{RGX_MOD_IDENT} {
365 			return (id_or_type(yytext));
366 		}
367 
368 <S0>{RGX_AGG}	{
369 			if ((yylval.l_str = strdup(yytext)) == NULL)
370 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
371 			return (DT_TOK_AGG);
372 		}
373 
374 <S0>"@"		{
375 			if ((yylval.l_str = strdup("@_")) == NULL)
376 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
377 			return (DT_TOK_AGG);
378 		}
379 
380 <S0>{RGX_INT}	|
381 <S2>{RGX_INT}	|
382 <S3>{RGX_INT}	{
383 			char *p;
384 
385 			errno = 0;
386 			yylval.l_int = strtoull(yytext, &p, 0);
387 			yyintprefix = 0;
388 			(void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
389 			yyintdecimal = yytext[0] != '0';
390 
391 			if (errno == ERANGE) {
392 				xyerror(D_INT_OFLOW, "constant %s results in "
393 				    "integer overflow\n", yytext);
394 			}
395 
396 			if (*p != '\0' && strchr("uUlL", *p) == NULL) {
397 				xyerror(D_INT_DIGIT, "constant %s contains "
398 				    "invalid digit %c\n", yytext, *p);
399 			}
400 
401 			if ((YYSTATE) != S3)
402 				return (DT_TOK_INT);
403 
404 			yypragma = dt_node_link(yypragma,
405 			    dt_node_int(yylval.l_int));
406 		}
407 
408 <S0>{RGX_FP}	yyerror("floating-point constants are not permitted\n");
409 
410 <S0>\"{RGX_STR}$ |
411 <S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
412 
413 <S0>\"{RGX_STR}\" |
414 <S3>\"{RGX_STR}\" {
415 			/*
416 			 * Quoted string -- convert C escape sequences and
417 			 * return the string as a token.
418 			 */
419 			yylval.l_str = strndup(yytext + 1, yyleng - 2);
420 
421 			if (yylval.l_str == NULL)
422 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
423 
424 			(void) stresc2chr(yylval.l_str);
425 			if ((YYSTATE) != S3)
426 				return (DT_TOK_STRING);
427 
428 			yypragma = dt_node_link(yypragma,
429 			    dt_node_string(yylval.l_str));
430 		}
431 
432 <S0>'{RGX_CHR}$	xyerror(D_CHR_NL, "newline encountered in character constant");
433 
434 <S0>'{RGX_CHR}'	{
435 			char *s, *p, *q;
436 			size_t nbytes;
437 
438 			/*
439 			 * Character constant -- convert C escape sequences and
440 			 * return the character as an integer immediate value.
441 			 */
442 			if (yyleng == 2)
443 				xyerror(D_CHR_NULL, "empty character constant");
444 
445 			s = yytext + 1;
446 			yytext[yyleng - 1] = '\0';
447 			nbytes = stresc2chr(s);
448 			yylval.l_int = 0;
449 			yyintprefix = 0;
450 			yyintsuffix[0] = '\0';
451 			yyintdecimal = 1;
452 
453 			if (nbytes > sizeof (yylval.l_int)) {
454 				xyerror(D_CHR_OFLOW, "character constant is "
455 				    "too long");
456 			}
457 #if BYTE_ORDER == _LITTLE_ENDIAN
458 			p = ((char *)&yylval.l_int) + nbytes - 1;
459 			for (q = s; nbytes != 0; nbytes--)
460 				*p-- = *q++;
461 #else
462 			bcopy(s, ((char *)&yylval.l_int) +
463 			    sizeof (yylval.l_int) - nbytes, nbytes);
464 #endif
465 			return (DT_TOK_INT);
466 		}
467 
468 <S0>"/*"	|
469 <S2>"/*"	{
470 			yypcb->pcb_cstate = (YYSTATE);
471 			BEGIN(S1);
472 		}
473 
474 <S0>{RGX_INTERP} |
475 <S2>{RGX_INTERP} ;	/* discard any #! lines */
476 
477 <S0>{RGX_CTL}	|
478 <S2>{RGX_CTL}	|
479 <S4>{RGX_CTL}	{
480 			assert(yypragma == NULL);
481 			yypcb->pcb_cstate = (YYSTATE);
482 			BEGIN(S3);
483 		}
484 
485 <S4>.		;	/* discard */
486 <S4>"\n"	;	/* discard */
487 
488 <S0>"/"		{
489 			int c, tok;
490 
491 			/*
492 			 * The use of "/" as the predicate delimiter and as the
493 			 * integer division symbol requires special lookahead
494 			 * to avoid a shift/reduce conflict in the D grammar.
495 			 * We look ahead to the next non-whitespace character.
496 			 * If we encounter EOF, ";", "{", or "/", then this "/"
497 			 * closes the predicate and we return DT_TOK_EPRED.
498 			 * If we encounter anything else, it's DT_TOK_DIV.
499 			 */
500 			while ((c = input()) != 0) {
501 				if (strchr("\f\n\r\t\v ", c) == NULL)
502 					break;
503 			}
504 
505 			if (c == 0 || c == ';' || c == '{' || c == '/') {
506 				if (yypcb->pcb_parens != 0) {
507 					yyerror("closing ) expected in "
508 					    "predicate before /\n");
509 				}
510 				if (yypcb->pcb_brackets != 0) {
511 					yyerror("closing ] expected in "
512 					    "predicate before /\n");
513 				}
514 				tok = DT_TOK_EPRED;
515 			} else
516 				tok = DT_TOK_DIV;
517 
518 			unput(c);
519 			return (tok);
520 		}
521 
522 <S0>"("		{
523 			yypcb->pcb_parens++;
524 			return (DT_TOK_LPAR);
525 		}
526 
527 <S0>")"		{
528 			if (--yypcb->pcb_parens < 0)
529 				yyerror("extra ) in input stream\n");
530 			return (DT_TOK_RPAR);
531 		}
532 
533 <S0>"["		{
534 			yypcb->pcb_brackets++;
535 			return (DT_TOK_LBRAC);
536 		}
537 
538 <S0>"]"		{
539 			if (--yypcb->pcb_brackets < 0)
540 				yyerror("extra ] in input stream\n");
541 			return (DT_TOK_RBRAC);
542 		}
543 
544 <S0>"{"		|
545 <S2>"{"		{
546 			yypcb->pcb_braces++;
547 			return ('{');
548 		}
549 
550 <S0>"}"		{
551 			if (--yypcb->pcb_braces < 0)
552 				yyerror("extra } in input stream\n");
553 			return ('}');
554 		}
555 
556 <S0>"|"		return (DT_TOK_BOR);
557 <S0>"^"		return (DT_TOK_XOR);
558 <S0>"&"		return (DT_TOK_BAND);
559 <S0>"&&"	return (DT_TOK_LAND);
560 <S0>"^^"	return (DT_TOK_LXOR);
561 <S0>"||"	return (DT_TOK_LOR);
562 <S0>"=="	return (DT_TOK_EQU);
563 <S0>"!="	return (DT_TOK_NEQ);
564 <S0>"<"		return (DT_TOK_LT);
565 <S0>"<="	return (DT_TOK_LE);
566 <S0>">"		return (DT_TOK_GT);
567 <S0>">="	return (DT_TOK_GE);
568 <S0>"<<"	return (DT_TOK_LSH);
569 <S0>">>"	return (DT_TOK_RSH);
570 <S0>"+"		return (DT_TOK_ADD);
571 <S0>"-"		return (DT_TOK_SUB);
572 <S0>"*"		return (DT_TOK_MUL);
573 <S0>"%"		return (DT_TOK_MOD);
574 <S0>"~"		return (DT_TOK_BNEG);
575 <S0>"!"		return (DT_TOK_LNEG);
576 <S0>"?"		return (DT_TOK_QUESTION);
577 <S0>":"		return (DT_TOK_COLON);
578 <S0>"."		return (DT_TOK_DOT);
579 <S0>"->"	return (DT_TOK_PTR);
580 <S0>"="		return (DT_TOK_ASGN);
581 <S0>"+="	return (DT_TOK_ADD_EQ);
582 <S0>"-="	return (DT_TOK_SUB_EQ);
583 <S0>"*="	return (DT_TOK_MUL_EQ);
584 <S0>"/="	return (DT_TOK_DIV_EQ);
585 <S0>"%="	return (DT_TOK_MOD_EQ);
586 <S0>"&="	return (DT_TOK_AND_EQ);
587 <S0>"^="	return (DT_TOK_XOR_EQ);
588 <S0>"|="	return (DT_TOK_OR_EQ);
589 <S0>"<<="	return (DT_TOK_LSH_EQ);
590 <S0>">>="	return (DT_TOK_RSH_EQ);
591 <S0>"++"	return (DT_TOK_ADDADD);
592 <S0>"--"	return (DT_TOK_SUBSUB);
593 <S0>"..."	return (DT_TOK_ELLIPSIS);
594 <S0>","		return (DT_TOK_COMMA);
595 <S0>";"		return (';');
596 <S0>{RGX_WS}	; /* discard */
597 <S0>"\\"\n	; /* discard */
598 <S0>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
599 
600 <S1>"/*"	yyerror("/* encountered inside a comment\n");
601 <S1>"*/"	BEGIN(yypcb->pcb_cstate);
602 <S1>.|\n	; /* discard */
603 
604 <S2>{RGX_PSPEC}	{
605 			/*
606 			 * S2 has an ambiguity because RGX_PSPEC includes '*'
607 			 * as a glob character and '*' also can be DT_TOK_STAR.
608 			 * Since lex always matches the longest token, this
609 			 * rule can be matched by an input string like "int*",
610 			 * which could begin a global variable declaration such
611 			 * as "int*x;" or could begin a RGX_PSPEC with globbing
612 			 * such as "int* { trace(timestamp); }".  If C_PSPEC is
613 			 * not set, we must resolve the ambiguity in favor of
614 			 * the type and perform lexer pushback if the fragment
615 			 * before '*' or entire fragment matches a type name.
616 			 * If C_PSPEC is set, we always return a PSPEC token.
617 			 * If C_PSPEC is off, the user can avoid ambiguity by
618 			 * including a ':' delimiter in the specifier, which
619 			 * they should be doing anyway to specify the provider.
620 			 */
621 			if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
622 			    strchr(yytext, ':') == NULL) {
623 
624 				char *p = strchr(yytext, '*');
625 				char *q = yytext + yyleng - 1;
626 
627 				if (p != NULL && p > yytext)
628 					*p = '\0'; /* prune yytext */
629 
630 				if (dt_type_lookup(yytext, NULL) == 0) {
631 					yylval.l_str = strdup(yytext);
632 
633 					if (yylval.l_str == NULL) {
634 						longjmp(yypcb->pcb_jmpbuf,
635 						    EDT_NOMEM);
636 					}
637 
638 					if (p != NULL && p > yytext) {
639 						for (*p = '*'; q >= p; q--)
640 							unput(*q);
641 					}
642 
643 					yybegin(YYS_EXPR);
644 					return (DT_TOK_TNAME);
645 				}
646 
647 				if (p != NULL && p > yytext)
648 					*p = '*'; /* restore yytext */
649 			}
650 
651 			if ((yylval.l_str = strdup(yytext)) == NULL)
652 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
653 
654 			return (DT_TOK_PSPEC);
655 		}
656 
657 <S2>"/"		return (DT_TOK_DIV);
658 <S2>","		return (DT_TOK_COMMA);
659 
660 <S2>{RGX_WS}	; /* discard */
661 <S2>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
662 
663 <S3>\n		{
664 			dt_pragma(yypragma);
665 			yypragma = NULL;
666 			BEGIN(yypcb->pcb_cstate);
667 		}
668 
669 <S3>[\f\t\v ]+	; /* discard */
670 
671 <S3>[^\f\n\t\v "]+ {
672 			dt_node_t *dnp;
673 
674 			if ((yylval.l_str = strdup(yytext)) == NULL)
675 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
676 
677 			/*
678 			 * We want to call dt_node_ident() here, but we can't
679 			 * because it will expand inlined identifiers, which we
680 			 * don't want to do from #pragma context in order to
681 			 * support pragmas that apply to the ident itself.  We
682 			 * call dt_node_string() and then reset dn_op instead.
683 			 */
684 			dnp = dt_node_string(yylval.l_str);
685 			dnp->dn_kind = DT_NODE_IDENT;
686 			dnp->dn_op = DT_TOK_IDENT;
687 			yypragma = dt_node_link(yypragma, dnp);
688 		}
689 
690 <S3>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
691 
692 %%
693 
694 /*
695  * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
696  * We use two main states for lexing because probe descriptions use a syntax
697  * that is incompatible with the normal D tokens (e.g. names can contain "-").
698  * yybegin also handles the job of switching between two lists of dt_nodes
699  * as we allocate persistent definitions, like inlines, and transient nodes
700  * that will be freed once we are done parsing the current program file.
701  */
702 void
703 yybegin(yystate_t state)
704 {
705 #ifdef	YYDEBUG
706 	yydebug = _dtrace_debug;
707 #endif
708 	if (yypcb->pcb_yystate == state)
709 		return; /* nothing to do if we're in the state already */
710 
711 	if (yypcb->pcb_yystate == YYS_DEFINE) {
712 		yypcb->pcb_list = yypcb->pcb_hold;
713 		yypcb->pcb_hold = NULL;
714 	}
715 
716 	switch (state) {
717 	case YYS_CLAUSE:
718 		BEGIN(S2);
719 		break;
720 	case YYS_DEFINE:
721 		assert(yypcb->pcb_hold == NULL);
722 		yypcb->pcb_hold = yypcb->pcb_list;
723 		yypcb->pcb_list = NULL;
724 		/*FALLTHRU*/
725 	case YYS_EXPR:
726 		BEGIN(S0);
727 		break;
728 	case YYS_DONE:
729 		break;
730 	case YYS_CONTROL:
731 		BEGIN(S4);
732 		break;
733 	default:
734 		xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
735 	}
736 
737 	yypcb->pcb_yystate = state;
738 }
739 
740 void
741 yyinit(dt_pcb_t *pcb)
742 {
743 	yypcb = pcb;
744 	yylineno = 1;
745 	yypragma = NULL;
746 #ifdef illumos
747 	yysptr = yysbuf;
748 #endif
749 	YY_FLUSH_BUFFER;
750 }
751 
752 /*
753  * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
754  * token to the parser indicating either an identifier or a typedef name.
755  * User-defined global variables always take precedence over types, but we do
756  * use some heuristics because D programs can look at an ever-changing set of
757  * kernel types and also can implicitly instantiate variables by assignment,
758  * unlike in C.  The code here is ordered carefully as lookups are not cheap.
759  */
760 static int
761 id_or_type(const char *s)
762 {
763 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
764 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
765 	int c0, c1, ttok = DT_TOK_TNAME;
766 	dt_ident_t *idp;
767 
768 	if ((s = yylval.l_str = strdup(s)) == NULL)
769 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
770 
771 	/*
772 	 * If the lexeme is a global variable or likely identifier or *not* a
773 	 * type_name, then it is an identifier token.
774 	 */
775 	if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
776 	    dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
777 	    dt_type_lookup(s, NULL) != 0)
778 		return (DT_TOK_IDENT);
779 
780 	/*
781 	 * If we're in the midst of parsing a declaration and a type_specifier
782 	 * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
783 	 * This semantic is necessary to permit valid ISO C code such as:
784 	 *
785 	 * typedef int foo;
786 	 * struct s { foo foo; };
787 	 *
788 	 * without causing shift/reduce conflicts in the direct_declarator part
789 	 * of the grammar.  The result is that we must check for conflicting
790 	 * redeclarations of the same identifier as part of dt_node_decl().
791 	 */
792 	if (ddp != NULL && ddp->dd_name != NULL)
793 		return (DT_TOK_IDENT);
794 
795 	/*
796 	 * If the lexeme is a type name and we are not in a program clause,
797 	 * then always interpret it as a type and return DT_TOK_TNAME.
798 	 */
799 	if ((YYSTATE) != S0)
800 		return (DT_TOK_TNAME);
801 
802 	/*
803 	 * If the lexeme matches a type name but is in a program clause, then
804 	 * it could be a type or it could be an undefined variable.  Peek at
805 	 * the next token to decide.  If we see ++, --, [, or =, we know there
806 	 * might be an assignment that is trying to create a global variable,
807 	 * so we optimistically return DT_TOK_IDENT.  There is no harm in being
808 	 * wrong: a type_name followed by ++, --, [, or = is a syntax error.
809 	 */
810 	while ((c0 = input()) != 0) {
811 		if (strchr("\f\n\r\t\v ", c0) == NULL)
812 			break;
813 	}
814 
815 	switch (c0) {
816 	case '+':
817 	case '-':
818 		if ((c1 = input()) == c0)
819 			ttok = DT_TOK_IDENT;
820 		unput(c1);
821 		break;
822 
823 	case '=':
824 		if ((c1 = input()) != c0)
825 			ttok = DT_TOK_IDENT;
826 		unput(c1);
827 		break;
828 	case '[':
829 		ttok = DT_TOK_IDENT;
830 		break;
831 	}
832 
833 	if (ttok == DT_TOK_IDENT) {
834 		idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
835 		    0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
836 
837 		if (idp == NULL)
838 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
839 	}
840 
841 	unput(c0);
842 	return (ttok);
843 }
844 
845 #ifdef illumos
846 static int
847 input(void)
848 {
849 	int c;
850 
851 	if (yysptr > yysbuf)
852 		c = *--yysptr;
853 	else if (yypcb->pcb_fileptr != NULL)
854 		c = fgetc(yypcb->pcb_fileptr);
855 	else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
856 		c = *(unsigned char *)(yypcb->pcb_strptr++);
857 	else
858 		c = EOF;
859 
860 	if (c == '\n')
861 		yylineno++;
862 
863 	if (c != EOF)
864 		return (c);
865 
866 	if ((YYSTATE) == S1)
867 		yyerror("end-of-file encountered before matching */\n");
868 
869 	if ((YYSTATE) == S3)
870 		yyerror("end-of-file encountered before end of control line\n");
871 
872 	if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
873 		longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
874 
875 	return (0); /* EOF */
876 }
877 
878 static void
879 unput(int c)
880 {
881 	if (c == '\n')
882 		yylineno--;
883 
884 	*yysptr++ = c;
885 	yytchar = c;
886 }
887 #endif /* illumos */
888