xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_lex.l (revision 12902:3bb859a7330c)
10Sstevel@tonic-gate %{
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate  * CDDL HEADER START
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
65478Sjhaslam  * Common Development and Distribution License (the "License").
75478Sjhaslam  * You may not use this file except in compliance with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
23*12902SBryan.Cantrill@Sun.COM /*
24*12902SBryan.Cantrill@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25*12902SBryan.Cantrill@Sun.COM  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <assert.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <dt_impl.h>
350Sstevel@tonic-gate #include <dt_grammar.h>
360Sstevel@tonic-gate #include <dt_parser.h>
370Sstevel@tonic-gate #include <dt_string.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * We need to undefine lex's input and unput macros so that references to these
410Sstevel@tonic-gate  * call the functions provided at the end of this source file.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate #undef input
440Sstevel@tonic-gate #undef unput
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static int id_or_type(const char *);
470Sstevel@tonic-gate static int input(void);
480Sstevel@tonic-gate static void unput(int);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * We first define a set of labeled states for use in the D lexer and then a
525478Sjhaslam  * set of regular expressions to simplify things below. The lexer states are:
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * S0 - D program clause and expression lexing
550Sstevel@tonic-gate  * S1 - D comments (i.e. skip everything until end of comment)
560Sstevel@tonic-gate  * S2 - D program outer scope (probe specifiers and declarations)
570Sstevel@tonic-gate  * S3 - D control line parsing (i.e. after ^# is seen but before \n)
585478Sjhaslam  * S4 - D control line scan (locate control directives only and invoke S3)
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate %}
610Sstevel@tonic-gate 
625478Sjhaslam %e 1500		/* maximum nodes */
630Sstevel@tonic-gate %p 3700		/* maximum positions */
645478Sjhaslam %n 600		/* maximum states */
650Sstevel@tonic-gate 
665478Sjhaslam %s S0 S1 S2 S3 S4
670Sstevel@tonic-gate 
680Sstevel@tonic-gate RGX_AGG		"@"[a-zA-Z_][0-9a-zA-Z_]*
690Sstevel@tonic-gate RGX_PSPEC	[-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
700Sstevel@tonic-gate RGX_IDENT	[a-zA-Z_`][0-9a-zA-Z_`]*
710Sstevel@tonic-gate RGX_INT		([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
720Sstevel@tonic-gate RGX_FP		([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
730Sstevel@tonic-gate RGX_WS		[\f\n\r\t\v ]
740Sstevel@tonic-gate RGX_STR		([^"\\\n]|\\[^"\n]|\\\")*
750Sstevel@tonic-gate RGX_CHR		([^'\\\n]|\\[^'\n]|\\')*
760Sstevel@tonic-gate RGX_INTERP	^[\f\t\v ]*#!.*
770Sstevel@tonic-gate RGX_CTL		^[\f\t\v ]*#
780Sstevel@tonic-gate 
790Sstevel@tonic-gate %%
800Sstevel@tonic-gate 
810Sstevel@tonic-gate %{
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * We insert a special prologue into yylex() itself: if the pcb contains a
850Sstevel@tonic-gate  * context token, we return that prior to running the normal lexer.  This
860Sstevel@tonic-gate  * allows libdtrace to force yacc into one of our three parsing contexts: D
870Sstevel@tonic-gate  * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
880Sstevel@tonic-gate  * Once the token is returned, we clear it so this only happens once.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate if (yypcb->pcb_token != 0) {
910Sstevel@tonic-gate 	int tok = yypcb->pcb_token;
920Sstevel@tonic-gate 	yypcb->pcb_token = 0;
930Sstevel@tonic-gate 	return (tok);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate %}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate <S0>auto	return (DT_KEY_AUTO);
990Sstevel@tonic-gate <S0>break	return (DT_KEY_BREAK);
1000Sstevel@tonic-gate <S0>case	return (DT_KEY_CASE);
1010Sstevel@tonic-gate <S0>char	return (DT_KEY_CHAR);
1020Sstevel@tonic-gate <S0>const	return (DT_KEY_CONST);
1030Sstevel@tonic-gate <S0>continue	return (DT_KEY_CONTINUE);
1040Sstevel@tonic-gate <S0>counter	return (DT_KEY_COUNTER);
1050Sstevel@tonic-gate <S0>default	return (DT_KEY_DEFAULT);
1060Sstevel@tonic-gate <S0>do		return (DT_KEY_DO);
1070Sstevel@tonic-gate <S0>double	return (DT_KEY_DOUBLE);
1080Sstevel@tonic-gate <S0>else	return (DT_KEY_ELSE);
1090Sstevel@tonic-gate <S0>enum	return (DT_KEY_ENUM);
1100Sstevel@tonic-gate <S0>extern	return (DT_KEY_EXTERN);
1110Sstevel@tonic-gate <S0>float	return (DT_KEY_FLOAT);
1120Sstevel@tonic-gate <S0>for		return (DT_KEY_FOR);
1130Sstevel@tonic-gate <S0>goto	return (DT_KEY_GOTO);
1140Sstevel@tonic-gate <S0>if		return (DT_KEY_IF);
1150Sstevel@tonic-gate <S0>import	return (DT_KEY_IMPORT);
1160Sstevel@tonic-gate <S0>inline	return (DT_KEY_INLINE);
1170Sstevel@tonic-gate <S0>int		return (DT_KEY_INT);
1180Sstevel@tonic-gate <S0>long	return (DT_KEY_LONG);
1190Sstevel@tonic-gate <S0>offsetof	return (DT_TOK_OFFSETOF);
1200Sstevel@tonic-gate <S0>probe	return (DT_KEY_PROBE);
1210Sstevel@tonic-gate <S0>provider	return (DT_KEY_PROVIDER);
1220Sstevel@tonic-gate <S0>register	return (DT_KEY_REGISTER);
1230Sstevel@tonic-gate <S0>restrict	return (DT_KEY_RESTRICT);
1240Sstevel@tonic-gate <S0>return	return (DT_KEY_RETURN);
1250Sstevel@tonic-gate <S0>self	return (DT_KEY_SELF);
1260Sstevel@tonic-gate <S0>short	return (DT_KEY_SHORT);
1270Sstevel@tonic-gate <S0>signed	return (DT_KEY_SIGNED);
1280Sstevel@tonic-gate <S0>sizeof	return (DT_TOK_SIZEOF);
1290Sstevel@tonic-gate <S0>static	return (DT_KEY_STATIC);
1300Sstevel@tonic-gate <S0>string	return (DT_KEY_STRING);
1310Sstevel@tonic-gate <S0>stringof	return (DT_TOK_STRINGOF);
1320Sstevel@tonic-gate <S0>struct	return (DT_KEY_STRUCT);
1330Sstevel@tonic-gate <S0>switch	return (DT_KEY_SWITCH);
1340Sstevel@tonic-gate <S0>this	return (DT_KEY_THIS);
1350Sstevel@tonic-gate <S0>translator	return (DT_KEY_XLATOR);
1360Sstevel@tonic-gate <S0>typedef	return (DT_KEY_TYPEDEF);
1370Sstevel@tonic-gate <S0>union	return (DT_KEY_UNION);
1380Sstevel@tonic-gate <S0>unsigned	return (DT_KEY_UNSIGNED);
1390Sstevel@tonic-gate <S0>void	return (DT_KEY_VOID);
1400Sstevel@tonic-gate <S0>volatile	return (DT_KEY_VOLATILE);
1410Sstevel@tonic-gate <S0>while	return (DT_KEY_WHILE);
1420Sstevel@tonic-gate <S0>xlate	return (DT_TOK_XLATE);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate <S2>auto	{ yybegin(YYS_EXPR);	return (DT_KEY_AUTO); }
1450Sstevel@tonic-gate <S2>char	{ yybegin(YYS_EXPR);	return (DT_KEY_CHAR); }
1460Sstevel@tonic-gate <S2>const	{ yybegin(YYS_EXPR);	return (DT_KEY_CONST); }
1470Sstevel@tonic-gate <S2>counter	{ yybegin(YYS_DEFINE);	return (DT_KEY_COUNTER); }
1480Sstevel@tonic-gate <S2>double	{ yybegin(YYS_EXPR);	return (DT_KEY_DOUBLE); }
1490Sstevel@tonic-gate <S2>enum	{ yybegin(YYS_EXPR);	return (DT_KEY_ENUM); }
1500Sstevel@tonic-gate <S2>extern	{ yybegin(YYS_EXPR);	return (DT_KEY_EXTERN); }
1510Sstevel@tonic-gate <S2>float	{ yybegin(YYS_EXPR);	return (DT_KEY_FLOAT); }
1520Sstevel@tonic-gate <S2>import	{ yybegin(YYS_EXPR);	return (DT_KEY_IMPORT); }
1530Sstevel@tonic-gate <S2>inline	{ yybegin(YYS_DEFINE);	return (DT_KEY_INLINE); }
1540Sstevel@tonic-gate <S2>int		{ yybegin(YYS_EXPR);	return (DT_KEY_INT); }
1550Sstevel@tonic-gate <S2>long	{ yybegin(YYS_EXPR);	return (DT_KEY_LONG); }
1560Sstevel@tonic-gate <S2>provider	{ yybegin(YYS_DEFINE);	return (DT_KEY_PROVIDER); }
1570Sstevel@tonic-gate <S2>register	{ yybegin(YYS_EXPR);	return (DT_KEY_REGISTER); }
1580Sstevel@tonic-gate <S2>restrict	{ yybegin(YYS_EXPR);	return (DT_KEY_RESTRICT); }
1590Sstevel@tonic-gate <S2>self	{ yybegin(YYS_EXPR);	return (DT_KEY_SELF); }
1600Sstevel@tonic-gate <S2>short	{ yybegin(YYS_EXPR);	return (DT_KEY_SHORT); }
1610Sstevel@tonic-gate <S2>signed	{ yybegin(YYS_EXPR);	return (DT_KEY_SIGNED); }
1620Sstevel@tonic-gate <S2>static	{ yybegin(YYS_EXPR);	return (DT_KEY_STATIC); }
1630Sstevel@tonic-gate <S2>string	{ yybegin(YYS_EXPR);	return (DT_KEY_STRING); }
1640Sstevel@tonic-gate <S2>struct	{ yybegin(YYS_EXPR);	return (DT_KEY_STRUCT); }
1650Sstevel@tonic-gate <S2>this	{ yybegin(YYS_EXPR);	return (DT_KEY_THIS); }
1660Sstevel@tonic-gate <S2>translator	{ yybegin(YYS_DEFINE);	return (DT_KEY_XLATOR); }
1670Sstevel@tonic-gate <S2>typedef	{ yybegin(YYS_EXPR);	return (DT_KEY_TYPEDEF); }
1680Sstevel@tonic-gate <S2>union	{ yybegin(YYS_EXPR);	return (DT_KEY_UNION); }
1690Sstevel@tonic-gate <S2>unsigned	{ yybegin(YYS_EXPR);	return (DT_KEY_UNSIGNED); }
1700Sstevel@tonic-gate <S2>void	{ yybegin(YYS_EXPR);	return (DT_KEY_VOID); }
1710Sstevel@tonic-gate <S2>volatile	{ yybegin(YYS_EXPR);	return (DT_KEY_VOLATILE); }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate <S0>"$$"[0-9]+	{
1740Sstevel@tonic-gate 			int i = atoi(yytext + 2);
1750Sstevel@tonic-gate 			char *v = "";
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 			/*
1780Sstevel@tonic-gate 			 * A macro argument reference substitutes the text of
1790Sstevel@tonic-gate 			 * an argument in place of the current token.  When we
1800Sstevel@tonic-gate 			 * see $$<d> we fetch the saved string from pcb_sargv
1810Sstevel@tonic-gate 			 * (or use the default argument if the option has been
1820Sstevel@tonic-gate 			 * set and the argument hasn't been specified) and
1830Sstevel@tonic-gate 			 * return a token corresponding to this string.
1840Sstevel@tonic-gate 			 */
1850Sstevel@tonic-gate 			if (i < 0 || (i >= yypcb->pcb_sargc &&
1860Sstevel@tonic-gate 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
1870Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
1880Sstevel@tonic-gate 				    "not defined\n", yytext);
1890Sstevel@tonic-gate 			}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 			if (i < yypcb->pcb_sargc) {
1920Sstevel@tonic-gate 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
1930Sstevel@tonic-gate 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 			if ((yylval.l_str = strdup(v)) == NULL)
1970Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 			(void) stresc2chr(yylval.l_str);
2000Sstevel@tonic-gate 			return (DT_TOK_STRING);
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate <S0>"$"[0-9]+	{
2040Sstevel@tonic-gate 			int i = atoi(yytext + 1);
2050Sstevel@tonic-gate 			char *p, *v = "0";
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 			/*
2080Sstevel@tonic-gate 			 * A macro argument reference substitutes the text of
2090Sstevel@tonic-gate 			 * one identifier or integer pattern for another.  When
2100Sstevel@tonic-gate 			 * we see $<d> we fetch the saved string from pcb_sargv
2110Sstevel@tonic-gate 			 * (or use the default argument if the option has been
2120Sstevel@tonic-gate 			 * set and the argument hasn't been specified) and
2130Sstevel@tonic-gate 			 * return a token corresponding to this string.
2140Sstevel@tonic-gate 			 */
2150Sstevel@tonic-gate 			if (i < 0 || (i >= yypcb->pcb_sargc &&
2160Sstevel@tonic-gate 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2170Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
2180Sstevel@tonic-gate 				    "not defined\n", yytext);
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 			if (i < yypcb->pcb_sargc) {
2220Sstevel@tonic-gate 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
2230Sstevel@tonic-gate 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 			/*
2270Sstevel@tonic-gate 			 * If the macro text is not a valid integer or ident,
2280Sstevel@tonic-gate 			 * then we treat it as a string.  The string may be
2290Sstevel@tonic-gate 			 * optionally enclosed in quotes, which we strip.
2300Sstevel@tonic-gate 			 */
2310Sstevel@tonic-gate 			if (strbadidnum(v)) {
2320Sstevel@tonic-gate 				size_t len = strlen(v);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 				if (len != 1 && *v == '"' && v[len - 1] == '"')
2350Sstevel@tonic-gate 					yylval.l_str = strndup(v + 1, len - 2);
2360Sstevel@tonic-gate 				else
2370Sstevel@tonic-gate 					yylval.l_str = strndup(v, len);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 				if (yylval.l_str == NULL)
2400Sstevel@tonic-gate 					longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 				(void) stresc2chr(yylval.l_str);
2430Sstevel@tonic-gate 				return (DT_TOK_STRING);
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 			/*
2470Sstevel@tonic-gate 			 * If the macro text is not a string an begins with a
2480Sstevel@tonic-gate 			 * digit or a +/- sign, process it as an integer token.
2490Sstevel@tonic-gate 			 */
2500Sstevel@tonic-gate 			if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
2510Sstevel@tonic-gate 				if (isdigit(v[0]))
2520Sstevel@tonic-gate 					yyintprefix = 0;
2530Sstevel@tonic-gate 				else
2540Sstevel@tonic-gate 					yyintprefix = *v++;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 				errno = 0;
2570Sstevel@tonic-gate 				yylval.l_int = strtoull(v, &p, 0);
2580Sstevel@tonic-gate 				(void) strncpy(yyintsuffix, p,
2590Sstevel@tonic-gate 				    sizeof (yyintsuffix));
2600Sstevel@tonic-gate 				yyintdecimal = *v != '0';
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 				if (errno == ERANGE) {
2630Sstevel@tonic-gate 					xyerror(D_MACRO_OFLOW, "macro argument"
2640Sstevel@tonic-gate 					    " %s constant %s results in integer"
2650Sstevel@tonic-gate 					    " overflow\n", yytext, v);
2660Sstevel@tonic-gate 				}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 				return (DT_TOK_INT);
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 			return (id_or_type(v));
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate <S0>"$$"{RGX_IDENT} {
2750Sstevel@tonic-gate 			dt_ident_t *idp = dt_idhash_lookup(
2760Sstevel@tonic-gate 			    yypcb->pcb_hdl->dt_macros, yytext + 2);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 			char s[16]; /* enough for UINT_MAX + \0 */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 			if (idp == NULL) {
2810Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro variable %s "
2820Sstevel@tonic-gate 				    "is not defined\n", yytext);
2830Sstevel@tonic-gate 			}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 			/*
2860Sstevel@tonic-gate 			 * For the moment, all current macro variables are of
2870Sstevel@tonic-gate 			 * type id_t (refer to dtrace_update() for details).
2880Sstevel@tonic-gate 			 */
2890Sstevel@tonic-gate 			(void) snprintf(s, sizeof (s), "%u", idp->di_id);
2900Sstevel@tonic-gate 			if ((yylval.l_str = strdup(s)) == NULL)
2910Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 			return (DT_TOK_STRING);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate <S0>"$"{RGX_IDENT} {
2970Sstevel@tonic-gate 			dt_ident_t *idp = dt_idhash_lookup(
2980Sstevel@tonic-gate 			    yypcb->pcb_hdl->dt_macros, yytext + 1);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 			if (idp == NULL) {
3010Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro variable %s "
3020Sstevel@tonic-gate 				    "is not defined\n", yytext);
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * For the moment, all current macro variables are of
3070Sstevel@tonic-gate 			 * type id_t (refer to dtrace_update() for details).
3080Sstevel@tonic-gate 			 */
3090Sstevel@tonic-gate 			yylval.l_int = (intmax_t)(int)idp->di_id;
3100Sstevel@tonic-gate 			yyintprefix = 0;
3110Sstevel@tonic-gate 			yyintsuffix[0] = '\0';
3120Sstevel@tonic-gate 			yyintdecimal = 1;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			return (DT_TOK_INT);
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate <S0>{RGX_IDENT}	{
3180Sstevel@tonic-gate 			return (id_or_type(yytext));
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate <S0>{RGX_AGG}	{
3220Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
3230Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3240Sstevel@tonic-gate 			return (DT_TOK_AGG);
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate <S0>"@"		{
3280Sstevel@tonic-gate 			if ((yylval.l_str = strdup("@_")) == NULL)
3290Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3300Sstevel@tonic-gate 			return (DT_TOK_AGG);
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate <S0>{RGX_INT}	|
3340Sstevel@tonic-gate <S2>{RGX_INT}	|
3350Sstevel@tonic-gate <S3>{RGX_INT}	{
3360Sstevel@tonic-gate 			char *p;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 			errno = 0;
3390Sstevel@tonic-gate 			yylval.l_int = strtoull(yytext, &p, 0);
3400Sstevel@tonic-gate 			yyintprefix = 0;
3410Sstevel@tonic-gate 			(void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
3420Sstevel@tonic-gate 			yyintdecimal = yytext[0] != '0';
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			if (errno == ERANGE) {
3450Sstevel@tonic-gate 				xyerror(D_INT_OFLOW, "constant %s results in "
3460Sstevel@tonic-gate 				    "integer overflow\n", yytext);
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 			if (*p != '\0' && strchr("uUlL", *p) == NULL) {
3500Sstevel@tonic-gate 				xyerror(D_INT_DIGIT, "constant %s contains "
3510Sstevel@tonic-gate 				    "invalid digit %c\n", yytext, *p);
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			if ((YYSTATE) != S3)
3550Sstevel@tonic-gate 				return (DT_TOK_INT);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma,
3580Sstevel@tonic-gate 			    dt_node_int(yylval.l_int));
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate <S0>{RGX_FP}	yyerror("floating-point constants are not permitted\n");
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate <S0>\"{RGX_STR}$ |
3640Sstevel@tonic-gate <S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate <S0>\"{RGX_STR}\" |
3670Sstevel@tonic-gate <S3>\"{RGX_STR}\" {
3680Sstevel@tonic-gate 			/*
3690Sstevel@tonic-gate 			 * Quoted string -- convert C escape sequences and
3700Sstevel@tonic-gate 			 * return the string as a token.
3710Sstevel@tonic-gate 			 */
3720Sstevel@tonic-gate 			yylval.l_str = strndup(yytext + 1, yyleng - 2);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 			if (yylval.l_str == NULL)
3750Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 			(void) stresc2chr(yylval.l_str);
3780Sstevel@tonic-gate 			if ((YYSTATE) != S3)
3790Sstevel@tonic-gate 				return (DT_TOK_STRING);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma,
3820Sstevel@tonic-gate 			    dt_node_string(yylval.l_str));
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate <S0>'{RGX_CHR}$	xyerror(D_CHR_NL, "newline encountered in character constant");
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate <S0>'{RGX_CHR}'	{
3880Sstevel@tonic-gate 			char *s, *p, *q;
3890Sstevel@tonic-gate 			size_t nbytes;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 			/*
3920Sstevel@tonic-gate 			 * Character constant -- convert C escape sequences and
3930Sstevel@tonic-gate 			 * return the character as an integer immediate value.
3940Sstevel@tonic-gate 			 */
3950Sstevel@tonic-gate 			if (yyleng == 2)
3960Sstevel@tonic-gate 				xyerror(D_CHR_NULL, "empty character constant");
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 			s = yytext + 1;
3990Sstevel@tonic-gate 			yytext[yyleng - 1] = '\0';
4000Sstevel@tonic-gate 			nbytes = stresc2chr(s);
4010Sstevel@tonic-gate 			yylval.l_int = 0;
4020Sstevel@tonic-gate 			yyintprefix = 0;
4030Sstevel@tonic-gate 			yyintsuffix[0] = '\0';
4040Sstevel@tonic-gate 			yyintdecimal = 1;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 			if (nbytes > sizeof (yylval.l_int)) {
4070Sstevel@tonic-gate 				xyerror(D_CHR_OFLOW, "character constant is "
4080Sstevel@tonic-gate 				    "too long");
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
4110Sstevel@tonic-gate 			p = ((char *)&yylval.l_int) + nbytes - 1;
4120Sstevel@tonic-gate 			for (q = s; nbytes != 0; nbytes--)
4130Sstevel@tonic-gate 				*p-- = *q++;
4140Sstevel@tonic-gate #else
4150Sstevel@tonic-gate 			bcopy(s, ((char *)&yylval.l_int) +
4160Sstevel@tonic-gate 			    sizeof (yylval.l_int) - nbytes, nbytes);
4170Sstevel@tonic-gate #endif
4180Sstevel@tonic-gate 			return (DT_TOK_INT);
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate <S0>"/*"	|
4220Sstevel@tonic-gate <S2>"/*"	{
4230Sstevel@tonic-gate 			yypcb->pcb_cstate = (YYSTATE);
4240Sstevel@tonic-gate 			BEGIN(S1);
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate <S0>{RGX_INTERP} |
4285478Sjhaslam <S2>{RGX_INTERP} ;	/* discard any #! lines */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate <S0>{RGX_CTL}	|
4315478Sjhaslam <S2>{RGX_CTL}	|
4325478Sjhaslam <S4>{RGX_CTL}	{
4330Sstevel@tonic-gate 			assert(yypragma == NULL);
4340Sstevel@tonic-gate 			yypcb->pcb_cstate = (YYSTATE);
4350Sstevel@tonic-gate 			BEGIN(S3);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 
4385478Sjhaslam <S4>.		;	/* discard */
4395478Sjhaslam <S4>"\n"	;	/* discard */
4405478Sjhaslam 
4410Sstevel@tonic-gate <S0>"/"		{
4420Sstevel@tonic-gate 			int c, tok;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 			/*
4450Sstevel@tonic-gate 			 * The use of "/" as the predicate delimiter and as the
4460Sstevel@tonic-gate 			 * integer division symbol requires special lookahead
4470Sstevel@tonic-gate 			 * to avoid a shift/reduce conflict in the D grammar.
4480Sstevel@tonic-gate 			 * We look ahead to the next non-whitespace character.
4490Sstevel@tonic-gate 			 * If we encounter EOF, ";", "{", or "/", then this "/"
4500Sstevel@tonic-gate 			 * closes the predicate and we return DT_TOK_EPRED.
4510Sstevel@tonic-gate 			 * If we encounter anything else, it's DT_TOK_DIV.
4520Sstevel@tonic-gate 			 */
4530Sstevel@tonic-gate 			while ((c = input()) != 0) {
4540Sstevel@tonic-gate 				if (strchr("\f\n\r\t\v ", c) == NULL)
4550Sstevel@tonic-gate 					break;
4560Sstevel@tonic-gate 			}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 			if (c == 0 || c == ';' || c == '{' || c == '/') {
4590Sstevel@tonic-gate 				if (yypcb->pcb_parens != 0) {
4600Sstevel@tonic-gate 					yyerror("closing ) expected in "
4610Sstevel@tonic-gate 					    "predicate before /\n");
4620Sstevel@tonic-gate 				}
4630Sstevel@tonic-gate 				if (yypcb->pcb_brackets != 0) {
4640Sstevel@tonic-gate 					yyerror("closing ] expected in "
4650Sstevel@tonic-gate 					    "predicate before /\n");
4660Sstevel@tonic-gate 				}
4670Sstevel@tonic-gate 				tok = DT_TOK_EPRED;
4680Sstevel@tonic-gate 			} else
4690Sstevel@tonic-gate 				tok = DT_TOK_DIV;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 			unput(c);
4720Sstevel@tonic-gate 			return (tok);
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate <S0>"("		{
4760Sstevel@tonic-gate 			yypcb->pcb_parens++;
4770Sstevel@tonic-gate 			return (DT_TOK_LPAR);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate <S0>")"		{
4810Sstevel@tonic-gate 			if (--yypcb->pcb_parens < 0)
4820Sstevel@tonic-gate 				yyerror("extra ) in input stream\n");
4830Sstevel@tonic-gate 			return (DT_TOK_RPAR);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate <S0>"["		{
4870Sstevel@tonic-gate 			yypcb->pcb_brackets++;
4880Sstevel@tonic-gate 			return (DT_TOK_LBRAC);
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate <S0>"]"		{
4920Sstevel@tonic-gate 			if (--yypcb->pcb_brackets < 0)
4930Sstevel@tonic-gate 				yyerror("extra ] in input stream\n");
4940Sstevel@tonic-gate 			return (DT_TOK_RBRAC);
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate <S0>"{"		|
4980Sstevel@tonic-gate <S2>"{"		{
4990Sstevel@tonic-gate 			yypcb->pcb_braces++;
5000Sstevel@tonic-gate 			return ('{');
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate <S0>"}"		{
5040Sstevel@tonic-gate 			if (--yypcb->pcb_braces < 0)
5050Sstevel@tonic-gate 				yyerror("extra } in input stream\n");
5060Sstevel@tonic-gate 			return ('}');
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate <S0>"|"		return (DT_TOK_BOR);
5100Sstevel@tonic-gate <S0>"^"		return (DT_TOK_XOR);
5110Sstevel@tonic-gate <S0>"&"		return (DT_TOK_BAND);
5120Sstevel@tonic-gate <S0>"&&"	return (DT_TOK_LAND);
5130Sstevel@tonic-gate <S0>"^^"	return (DT_TOK_LXOR);
5140Sstevel@tonic-gate <S0>"||"	return (DT_TOK_LOR);
5150Sstevel@tonic-gate <S0>"=="	return (DT_TOK_EQU);
5160Sstevel@tonic-gate <S0>"!="	return (DT_TOK_NEQ);
5170Sstevel@tonic-gate <S0>"<"		return (DT_TOK_LT);
5180Sstevel@tonic-gate <S0>"<="	return (DT_TOK_LE);
5190Sstevel@tonic-gate <S0>">"		return (DT_TOK_GT);
5200Sstevel@tonic-gate <S0>">="	return (DT_TOK_GE);
5210Sstevel@tonic-gate <S0>"<<"	return (DT_TOK_LSH);
5220Sstevel@tonic-gate <S0>">>"	return (DT_TOK_RSH);
5230Sstevel@tonic-gate <S0>"+"		return (DT_TOK_ADD);
5240Sstevel@tonic-gate <S0>"-"		return (DT_TOK_SUB);
5250Sstevel@tonic-gate <S0>"*"		return (DT_TOK_MUL);
5260Sstevel@tonic-gate <S0>"%"		return (DT_TOK_MOD);
5270Sstevel@tonic-gate <S0>"~"		return (DT_TOK_BNEG);
5280Sstevel@tonic-gate <S0>"!"		return (DT_TOK_LNEG);
5290Sstevel@tonic-gate <S0>"?"		return (DT_TOK_QUESTION);
5300Sstevel@tonic-gate <S0>":"		return (DT_TOK_COLON);
5310Sstevel@tonic-gate <S0>"."		return (DT_TOK_DOT);
5320Sstevel@tonic-gate <S0>"->"	return (DT_TOK_PTR);
5330Sstevel@tonic-gate <S0>"="		return (DT_TOK_ASGN);
5340Sstevel@tonic-gate <S0>"+="	return (DT_TOK_ADD_EQ);
5350Sstevel@tonic-gate <S0>"-="	return (DT_TOK_SUB_EQ);
5360Sstevel@tonic-gate <S0>"*="	return (DT_TOK_MUL_EQ);
5370Sstevel@tonic-gate <S0>"/="	return (DT_TOK_DIV_EQ);
5380Sstevel@tonic-gate <S0>"%="	return (DT_TOK_MOD_EQ);
5390Sstevel@tonic-gate <S0>"&="	return (DT_TOK_AND_EQ);
5400Sstevel@tonic-gate <S0>"^="	return (DT_TOK_XOR_EQ);
5410Sstevel@tonic-gate <S0>"|="	return (DT_TOK_OR_EQ);
5420Sstevel@tonic-gate <S0>"<<="	return (DT_TOK_LSH_EQ);
5430Sstevel@tonic-gate <S0>">>="	return (DT_TOK_RSH_EQ);
5440Sstevel@tonic-gate <S0>"++"	return (DT_TOK_ADDADD);
5450Sstevel@tonic-gate <S0>"--"	return (DT_TOK_SUBSUB);
5460Sstevel@tonic-gate <S0>"..."	return (DT_TOK_ELLIPSIS);
5470Sstevel@tonic-gate <S0>","		return (DT_TOK_COMMA);
5480Sstevel@tonic-gate <S0>";"		return (';');
5490Sstevel@tonic-gate <S0>{RGX_WS}	; /* discard */
5500Sstevel@tonic-gate <S0>"\\"\n	; /* discard */
5510Sstevel@tonic-gate <S0>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
5520Sstevel@tonic-gate 
553265Smws <S1>"/*"	yyerror("/* encountered inside a comment\n");
5540Sstevel@tonic-gate <S1>"*/"	BEGIN(yypcb->pcb_cstate);
5550Sstevel@tonic-gate <S1>.|\n	; /* discard */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate <S2>{RGX_PSPEC}	{
5580Sstevel@tonic-gate 			/*
5590Sstevel@tonic-gate 			 * S2 has an ambiguity because RGX_PSPEC includes '*'
5600Sstevel@tonic-gate 			 * as a glob character and '*' also can be DT_TOK_STAR.
5610Sstevel@tonic-gate 			 * Since lex always matches the longest token, this
5620Sstevel@tonic-gate 			 * rule can be matched by an input string like "int*",
5630Sstevel@tonic-gate 			 * which could begin a global variable declaration such
5640Sstevel@tonic-gate 			 * as "int*x;" or could begin a RGX_PSPEC with globbing
5650Sstevel@tonic-gate 			 * such as "int* { trace(timestamp); }".  If C_PSPEC is
5660Sstevel@tonic-gate 			 * not set, we must resolve the ambiguity in favor of
5670Sstevel@tonic-gate 			 * the type and perform lexer pushback if the fragment
5680Sstevel@tonic-gate 			 * before '*' or entire fragment matches a type name.
5690Sstevel@tonic-gate 			 * If C_PSPEC is set, we always return a PSPEC token.
5700Sstevel@tonic-gate 			 * If C_PSPEC is off, the user can avoid ambiguity by
5710Sstevel@tonic-gate 			 * including a ':' delimiter in the specifier, which
5720Sstevel@tonic-gate 			 * they should be doing anyway to specify the provider.
5730Sstevel@tonic-gate 			 */
5740Sstevel@tonic-gate 			if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
5750Sstevel@tonic-gate 			    strchr(yytext, ':') == NULL) {
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 				char *p = strchr(yytext, '*');
5780Sstevel@tonic-gate 				char *q = yytext + yyleng - 1;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 				if (p != NULL && p > yytext)
5810Sstevel@tonic-gate 					*p = '\0'; /* prune yytext */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 				if (dt_type_lookup(yytext, NULL) == 0) {
5840Sstevel@tonic-gate 					yylval.l_str = strdup(yytext);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 					if (yylval.l_str == NULL) {
5870Sstevel@tonic-gate 						longjmp(yypcb->pcb_jmpbuf,
5880Sstevel@tonic-gate 						    EDT_NOMEM);
5890Sstevel@tonic-gate 					}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 					if (p != NULL && p > yytext) {
5920Sstevel@tonic-gate 						for (*p = '*'; q >= p; q--)
5930Sstevel@tonic-gate 							unput(*q);
5940Sstevel@tonic-gate 					}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 					yybegin(YYS_EXPR);
5970Sstevel@tonic-gate 					return (DT_TOK_TNAME);
5980Sstevel@tonic-gate 				}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 				if (p != NULL && p > yytext)
6010Sstevel@tonic-gate 					*p = '*'; /* restore yytext */
6020Sstevel@tonic-gate 			}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
6050Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 			return (DT_TOK_PSPEC);
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate <S2>"/"		return (DT_TOK_DIV);
6110Sstevel@tonic-gate <S2>","		return (DT_TOK_COMMA);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate <S2>{RGX_WS}	; /* discard */
6140Sstevel@tonic-gate <S2>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate <S3>\n		{
6170Sstevel@tonic-gate 			dt_pragma(yypragma);
6180Sstevel@tonic-gate 			yypragma = NULL;
6190Sstevel@tonic-gate 			BEGIN(yypcb->pcb_cstate);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate <S3>[\f\t\v ]+	; /* discard */
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate <S3>[^\f\n\t\v "]+ {
6250Sstevel@tonic-gate 			dt_node_t *dnp;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
6280Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 			/*
6310Sstevel@tonic-gate 			 * We want to call dt_node_ident() here, but we can't
6320Sstevel@tonic-gate 			 * because it will expand inlined identifiers, which we
6330Sstevel@tonic-gate 			 * don't want to do from #pragma context in order to
6340Sstevel@tonic-gate 			 * support pragmas that apply to the ident itself.  We
6350Sstevel@tonic-gate 			 * call dt_node_string() and then reset dn_op instead.
6360Sstevel@tonic-gate 			 */
6370Sstevel@tonic-gate 			dnp = dt_node_string(yylval.l_str);
6380Sstevel@tonic-gate 			dnp->dn_kind = DT_NODE_IDENT;
6390Sstevel@tonic-gate 			dnp->dn_op = DT_TOK_IDENT;
6400Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma, dnp);
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate <S3>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate %%
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate  * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
6490Sstevel@tonic-gate  * We use two main states for lexing because probe descriptions use a syntax
6500Sstevel@tonic-gate  * that is incompatible with the normal D tokens (e.g. names can contain "-").
6510Sstevel@tonic-gate  * yybegin also handles the job of switching between two lists of dt_nodes
6520Sstevel@tonic-gate  * as we allocate persistent definitions, like inlines, and transient nodes
6530Sstevel@tonic-gate  * that will be freed once we are done parsing the current program file.
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate void
6560Sstevel@tonic-gate yybegin(yystate_t state)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate #ifdef	YYDEBUG
6590Sstevel@tonic-gate 	yydebug = _dtrace_debug;
6600Sstevel@tonic-gate #endif
6610Sstevel@tonic-gate 	if (yypcb->pcb_yystate == state)
6620Sstevel@tonic-gate 		return; /* nothing to do if we're in the state already */
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (yypcb->pcb_yystate == YYS_DEFINE) {
6655478Sjhaslam 		yypcb->pcb_list = yypcb->pcb_hold;
6665478Sjhaslam 		yypcb->pcb_hold = NULL;
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	switch (state) {
6700Sstevel@tonic-gate 	case YYS_CLAUSE:
6710Sstevel@tonic-gate 		BEGIN(S2);
6720Sstevel@tonic-gate 		break;
6730Sstevel@tonic-gate 	case YYS_DEFINE:
6740Sstevel@tonic-gate 		assert(yypcb->pcb_hold == NULL);
6750Sstevel@tonic-gate 		yypcb->pcb_hold = yypcb->pcb_list;
6760Sstevel@tonic-gate 		yypcb->pcb_list = NULL;
6770Sstevel@tonic-gate 		/*FALLTHRU*/
6780Sstevel@tonic-gate 	case YYS_EXPR:
6790Sstevel@tonic-gate 		BEGIN(S0);
6800Sstevel@tonic-gate 		break;
6810Sstevel@tonic-gate 	case YYS_DONE:
6820Sstevel@tonic-gate 		break;
6835478Sjhaslam 	case YYS_CONTROL:
6845478Sjhaslam 		BEGIN(S4);
6855478Sjhaslam 		break;
6860Sstevel@tonic-gate 	default:
6870Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	yypcb->pcb_yystate = state;
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate void
6940Sstevel@tonic-gate yyinit(dt_pcb_t *pcb)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	yypcb = pcb;
6970Sstevel@tonic-gate 	yylineno = 1;
6980Sstevel@tonic-gate 	yypragma = NULL;
6990Sstevel@tonic-gate 	yysptr = yysbuf;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
7040Sstevel@tonic-gate  * token to the parser indicating either an identifier or a typedef name.
7050Sstevel@tonic-gate  * User-defined global variables always take precedence over types, but we do
7060Sstevel@tonic-gate  * use some heuristics because D programs can look at an ever-changing set of
7070Sstevel@tonic-gate  * kernel types and also can implicitly instantiate variables by assignment,
7080Sstevel@tonic-gate  * unlike in C.  The code here is ordered carefully as lookups are not cheap.
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate static int
7110Sstevel@tonic-gate id_or_type(const char *s)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
7141222Smws 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
7150Sstevel@tonic-gate 	int c0, c1, ttok = DT_TOK_TNAME;
7160Sstevel@tonic-gate 	dt_ident_t *idp;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if ((s = yylval.l_str = strdup(s)) == NULL)
7190Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/*
7220Sstevel@tonic-gate 	 * If the lexeme is a global variable or likely identifier or *not* a
7230Sstevel@tonic-gate 	 * type_name, then it is an identifier token.
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 	if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
7260Sstevel@tonic-gate 	    dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
7270Sstevel@tonic-gate 	    dt_type_lookup(s, NULL) != 0)
7280Sstevel@tonic-gate 		return (DT_TOK_IDENT);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/*
7311222Smws 	 * If we're in the midst of parsing a declaration and a type_specifier
7321222Smws 	 * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
7331222Smws 	 * This semantic is necessary to permit valid ISO C code such as:
7341222Smws 	 *
7351222Smws 	 * typedef int foo;
7361222Smws 	 * struct s { foo foo; };
7371222Smws 	 *
7381222Smws 	 * without causing shift/reduce conflicts in the direct_declarator part
7391222Smws 	 * of the grammar.  The result is that we must check for conflicting
7401222Smws 	 * redeclarations of the same identifier as part of dt_node_decl().
7411222Smws 	 */
7421222Smws 	if (ddp != NULL && ddp->dd_name != NULL)
7431222Smws 		return (DT_TOK_IDENT);
7441222Smws 
7451222Smws 	/*
7460Sstevel@tonic-gate 	 * If the lexeme is a type name and we are not in a program clause,
7470Sstevel@tonic-gate 	 * then always interpret it as a type and return DT_TOK_TNAME.
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	if ((YYSTATE) != S0)
7500Sstevel@tonic-gate 		return (DT_TOK_TNAME);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/*
7530Sstevel@tonic-gate 	 * If the lexeme matches a type name but is in a program clause, then
7540Sstevel@tonic-gate 	 * it could be a type or it could be an undefined variable.  Peek at
7550Sstevel@tonic-gate 	 * the next token to decide.  If we see ++, --, [, or =, we know there
7560Sstevel@tonic-gate 	 * might be an assignment that is trying to create a global variable,
7570Sstevel@tonic-gate 	 * so we optimistically return DT_TOK_IDENT.  There is no harm in being
7580Sstevel@tonic-gate 	 * wrong: a type_name followed by ++, --, [, or = is a syntax error.
7590Sstevel@tonic-gate 	 */
7600Sstevel@tonic-gate 	while ((c0 = input()) != 0) {
7610Sstevel@tonic-gate 		if (strchr("\f\n\r\t\v ", c0) == NULL)
7620Sstevel@tonic-gate 			break;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	switch (c0) {
7660Sstevel@tonic-gate 	case '+':
7670Sstevel@tonic-gate 	case '-':
7680Sstevel@tonic-gate 		if ((c1 = input()) == c0)
7690Sstevel@tonic-gate 			ttok = DT_TOK_IDENT;
7700Sstevel@tonic-gate 		unput(c1);
7710Sstevel@tonic-gate 		break;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	case '=':
7740Sstevel@tonic-gate 		if ((c1 = input()) != c0)
7750Sstevel@tonic-gate 			ttok = DT_TOK_IDENT;
7760Sstevel@tonic-gate 		unput(c1);
7770Sstevel@tonic-gate 		break;
7780Sstevel@tonic-gate 	case '[':
7790Sstevel@tonic-gate 		ttok = DT_TOK_IDENT;
7800Sstevel@tonic-gate 		break;
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (ttok == DT_TOK_IDENT) {
7840Sstevel@tonic-gate 		idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
7850Sstevel@tonic-gate 		    0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		if (idp == NULL)
7880Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	unput(c0);
7920Sstevel@tonic-gate 	return (ttok);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate static int
7960Sstevel@tonic-gate input(void)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	int c;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (yysptr > yysbuf)
8010Sstevel@tonic-gate 		c = *--yysptr;
8020Sstevel@tonic-gate 	else if (yypcb->pcb_fileptr != NULL)
8030Sstevel@tonic-gate 		c = fgetc(yypcb->pcb_fileptr);
8040Sstevel@tonic-gate 	else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
805*12902SBryan.Cantrill@Sun.COM 		c = *(unsigned char *)(yypcb->pcb_strptr++);
8060Sstevel@tonic-gate 	else
8070Sstevel@tonic-gate 		c = EOF;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (c == '\n')
8100Sstevel@tonic-gate 		yylineno++;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if (c != EOF)
8130Sstevel@tonic-gate 		return (c);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if ((YYSTATE) == S1)
8160Sstevel@tonic-gate 		yyerror("end-of-file encountered before matching */\n");
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if ((YYSTATE) == S3)
8190Sstevel@tonic-gate 		yyerror("end-of-file encountered before end of control line\n");
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
8220Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	return (0); /* EOF */
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static void
8280Sstevel@tonic-gate unput(int c)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	if (c == '\n')
8310Sstevel@tonic-gate 		yylineno--;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	*yysptr++ = c;
8340Sstevel@tonic-gate 	yytchar = c;
8350Sstevel@tonic-gate }
836