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