xref: /titanic_51/usr/src/lib/libdtrace/common/dt_lex.l (revision a386cc11a86ecb60f5a48078d22c1500e2ad003e)
17c478bd9Sstevel@tonic-gate %{
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * CDDL HEADER START
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
6c9d6cd77Sjhaslam  * Common Development and Distribution License (the "License").
7c9d6cd77Sjhaslam  * You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
23c9a6ea2eSBryan Cantrill /*
24c9a6ea2eSBryan Cantrill  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25c9a6ea2eSBryan Cantrill  */
26*a386cc11SRobert Mustacchi /*
27*a386cc11SRobert Mustacchi  * Copyright (c) 2013 by Delphix. All rights reserved.
28*a386cc11SRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29*a386cc11SRobert Mustacchi  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <dt_impl.h>
397c478bd9Sstevel@tonic-gate #include <dt_grammar.h>
407c478bd9Sstevel@tonic-gate #include <dt_parser.h>
417c478bd9Sstevel@tonic-gate #include <dt_string.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * We need to undefine lex's input and unput macros so that references to these
457c478bd9Sstevel@tonic-gate  * call the functions provided at the end of this source file.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate #undef input
487c478bd9Sstevel@tonic-gate #undef unput
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int id_or_type(const char *);
517c478bd9Sstevel@tonic-gate static int input(void);
527c478bd9Sstevel@tonic-gate static void unput(int);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * We first define a set of labeled states for use in the D lexer and then a
567c478bd9Sstevel@tonic-gate  * set of regular expressions to simplify things below. The lexer states are:
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * S0 - D program clause and expression lexing
597c478bd9Sstevel@tonic-gate  * S1 - D comments (i.e. skip everything until end of comment)
607c478bd9Sstevel@tonic-gate  * S2 - D program outer scope (probe specifiers and declarations)
617c478bd9Sstevel@tonic-gate  * S3 - D control line parsing (i.e. after ^# is seen but before \n)
62c9d6cd77Sjhaslam  * S4 - D control line scan (locate control directives only and invoke S3)
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate %}
657c478bd9Sstevel@tonic-gate 
66c9d6cd77Sjhaslam %e 1500		/* maximum nodes */
67*a386cc11SRobert Mustacchi %p 4900		/* maximum positions */
68c9d6cd77Sjhaslam %n 600		/* maximum states */
69*a386cc11SRobert Mustacchi %a 3000		/* maximum transitions */
707c478bd9Sstevel@tonic-gate 
71c9d6cd77Sjhaslam %s S0 S1 S2 S3 S4
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate RGX_AGG		"@"[a-zA-Z_][0-9a-zA-Z_]*
747c478bd9Sstevel@tonic-gate RGX_PSPEC	[-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
75*a386cc11SRobert Mustacchi RGX_ALTIDENT	[a-zA-Z_][0-9a-zA-Z_]*
76*a386cc11SRobert Mustacchi RGX_LMID	LM[0-9a-fA-F]+`
77*a386cc11SRobert Mustacchi RGX_MOD_IDENT	[a-zA-Z_`][0-9a-z.A-Z_`]*`
787c478bd9Sstevel@tonic-gate RGX_IDENT	[a-zA-Z_`][0-9a-zA-Z_`]*
797c478bd9Sstevel@tonic-gate RGX_INT		([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
807c478bd9Sstevel@tonic-gate RGX_FP		([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
817c478bd9Sstevel@tonic-gate RGX_WS		[\f\n\r\t\v ]
827c478bd9Sstevel@tonic-gate RGX_STR		([^"\\\n]|\\[^"\n]|\\\")*
837c478bd9Sstevel@tonic-gate RGX_CHR		([^'\\\n]|\\[^'\n]|\\')*
847c478bd9Sstevel@tonic-gate RGX_INTERP	^[\f\t\v ]*#!.*
857c478bd9Sstevel@tonic-gate RGX_CTL		^[\f\t\v ]*#
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate %%
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate %{
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * We insert a special prologue into yylex() itself: if the pcb contains a
937c478bd9Sstevel@tonic-gate  * context token, we return that prior to running the normal lexer.  This
947c478bd9Sstevel@tonic-gate  * allows libdtrace to force yacc into one of our three parsing contexts: D
957c478bd9Sstevel@tonic-gate  * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
967c478bd9Sstevel@tonic-gate  * Once the token is returned, we clear it so this only happens once.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate if (yypcb->pcb_token != 0) {
997c478bd9Sstevel@tonic-gate 	int tok = yypcb->pcb_token;
1007c478bd9Sstevel@tonic-gate 	yypcb->pcb_token = 0;
1017c478bd9Sstevel@tonic-gate 	return (tok);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate %}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate <S0>auto	return (DT_KEY_AUTO);
1077c478bd9Sstevel@tonic-gate <S0>break	return (DT_KEY_BREAK);
1087c478bd9Sstevel@tonic-gate <S0>case	return (DT_KEY_CASE);
1097c478bd9Sstevel@tonic-gate <S0>char	return (DT_KEY_CHAR);
1107c478bd9Sstevel@tonic-gate <S0>const	return (DT_KEY_CONST);
1117c478bd9Sstevel@tonic-gate <S0>continue	return (DT_KEY_CONTINUE);
1127c478bd9Sstevel@tonic-gate <S0>counter	return (DT_KEY_COUNTER);
1137c478bd9Sstevel@tonic-gate <S0>default	return (DT_KEY_DEFAULT);
1147c478bd9Sstevel@tonic-gate <S0>do		return (DT_KEY_DO);
1157c478bd9Sstevel@tonic-gate <S0>double	return (DT_KEY_DOUBLE);
1167c478bd9Sstevel@tonic-gate <S0>else	return (DT_KEY_ELSE);
1177c478bd9Sstevel@tonic-gate <S0>enum	return (DT_KEY_ENUM);
1187c478bd9Sstevel@tonic-gate <S0>extern	return (DT_KEY_EXTERN);
1197c478bd9Sstevel@tonic-gate <S0>float	return (DT_KEY_FLOAT);
1207c478bd9Sstevel@tonic-gate <S0>for		return (DT_KEY_FOR);
1217c478bd9Sstevel@tonic-gate <S0>goto	return (DT_KEY_GOTO);
1227c478bd9Sstevel@tonic-gate <S0>if		return (DT_KEY_IF);
1237c478bd9Sstevel@tonic-gate <S0>import	return (DT_KEY_IMPORT);
1247c478bd9Sstevel@tonic-gate <S0>inline	return (DT_KEY_INLINE);
1257c478bd9Sstevel@tonic-gate <S0>int		return (DT_KEY_INT);
1267c478bd9Sstevel@tonic-gate <S0>long	return (DT_KEY_LONG);
1277c478bd9Sstevel@tonic-gate <S0>offsetof	return (DT_TOK_OFFSETOF);
1287c478bd9Sstevel@tonic-gate <S0>probe	return (DT_KEY_PROBE);
1297c478bd9Sstevel@tonic-gate <S0>provider	return (DT_KEY_PROVIDER);
1307c478bd9Sstevel@tonic-gate <S0>register	return (DT_KEY_REGISTER);
1317c478bd9Sstevel@tonic-gate <S0>restrict	return (DT_KEY_RESTRICT);
1327c478bd9Sstevel@tonic-gate <S0>return	return (DT_KEY_RETURN);
1337c478bd9Sstevel@tonic-gate <S0>self	return (DT_KEY_SELF);
1347c478bd9Sstevel@tonic-gate <S0>short	return (DT_KEY_SHORT);
1357c478bd9Sstevel@tonic-gate <S0>signed	return (DT_KEY_SIGNED);
1367c478bd9Sstevel@tonic-gate <S0>sizeof	return (DT_TOK_SIZEOF);
1377c478bd9Sstevel@tonic-gate <S0>static	return (DT_KEY_STATIC);
1387c478bd9Sstevel@tonic-gate <S0>string	return (DT_KEY_STRING);
1397c478bd9Sstevel@tonic-gate <S0>stringof	return (DT_TOK_STRINGOF);
1407c478bd9Sstevel@tonic-gate <S0>struct	return (DT_KEY_STRUCT);
1417c478bd9Sstevel@tonic-gate <S0>switch	return (DT_KEY_SWITCH);
1427c478bd9Sstevel@tonic-gate <S0>this	return (DT_KEY_THIS);
1437c478bd9Sstevel@tonic-gate <S0>translator	return (DT_KEY_XLATOR);
1447c478bd9Sstevel@tonic-gate <S0>typedef	return (DT_KEY_TYPEDEF);
1457c478bd9Sstevel@tonic-gate <S0>union	return (DT_KEY_UNION);
1467c478bd9Sstevel@tonic-gate <S0>unsigned	return (DT_KEY_UNSIGNED);
147*a386cc11SRobert Mustacchi <S0>userland	return (DT_KEY_USERLAND);
1487c478bd9Sstevel@tonic-gate <S0>void	return (DT_KEY_VOID);
1497c478bd9Sstevel@tonic-gate <S0>volatile	return (DT_KEY_VOLATILE);
1507c478bd9Sstevel@tonic-gate <S0>while	return (DT_KEY_WHILE);
1517c478bd9Sstevel@tonic-gate <S0>xlate	return (DT_TOK_XLATE);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate <S2>auto	{ yybegin(YYS_EXPR);	return (DT_KEY_AUTO); }
1547c478bd9Sstevel@tonic-gate <S2>char	{ yybegin(YYS_EXPR);	return (DT_KEY_CHAR); }
1557c478bd9Sstevel@tonic-gate <S2>const	{ yybegin(YYS_EXPR);	return (DT_KEY_CONST); }
1567c478bd9Sstevel@tonic-gate <S2>counter	{ yybegin(YYS_DEFINE);	return (DT_KEY_COUNTER); }
1577c478bd9Sstevel@tonic-gate <S2>double	{ yybegin(YYS_EXPR);	return (DT_KEY_DOUBLE); }
1587c478bd9Sstevel@tonic-gate <S2>enum	{ yybegin(YYS_EXPR);	return (DT_KEY_ENUM); }
1597c478bd9Sstevel@tonic-gate <S2>extern	{ yybegin(YYS_EXPR);	return (DT_KEY_EXTERN); }
1607c478bd9Sstevel@tonic-gate <S2>float	{ yybegin(YYS_EXPR);	return (DT_KEY_FLOAT); }
1617c478bd9Sstevel@tonic-gate <S2>import	{ yybegin(YYS_EXPR);	return (DT_KEY_IMPORT); }
1627c478bd9Sstevel@tonic-gate <S2>inline	{ yybegin(YYS_DEFINE);	return (DT_KEY_INLINE); }
1637c478bd9Sstevel@tonic-gate <S2>int		{ yybegin(YYS_EXPR);	return (DT_KEY_INT); }
1647c478bd9Sstevel@tonic-gate <S2>long	{ yybegin(YYS_EXPR);	return (DT_KEY_LONG); }
1657c478bd9Sstevel@tonic-gate <S2>provider	{ yybegin(YYS_DEFINE);	return (DT_KEY_PROVIDER); }
1667c478bd9Sstevel@tonic-gate <S2>register	{ yybegin(YYS_EXPR);	return (DT_KEY_REGISTER); }
1677c478bd9Sstevel@tonic-gate <S2>restrict	{ yybegin(YYS_EXPR);	return (DT_KEY_RESTRICT); }
1687c478bd9Sstevel@tonic-gate <S2>self	{ yybegin(YYS_EXPR);	return (DT_KEY_SELF); }
1697c478bd9Sstevel@tonic-gate <S2>short	{ yybegin(YYS_EXPR);	return (DT_KEY_SHORT); }
1707c478bd9Sstevel@tonic-gate <S2>signed	{ yybegin(YYS_EXPR);	return (DT_KEY_SIGNED); }
1717c478bd9Sstevel@tonic-gate <S2>static	{ yybegin(YYS_EXPR);	return (DT_KEY_STATIC); }
1727c478bd9Sstevel@tonic-gate <S2>string	{ yybegin(YYS_EXPR);	return (DT_KEY_STRING); }
1737c478bd9Sstevel@tonic-gate <S2>struct	{ yybegin(YYS_EXPR);	return (DT_KEY_STRUCT); }
1747c478bd9Sstevel@tonic-gate <S2>this	{ yybegin(YYS_EXPR);	return (DT_KEY_THIS); }
1757c478bd9Sstevel@tonic-gate <S2>translator	{ yybegin(YYS_DEFINE);	return (DT_KEY_XLATOR); }
1767c478bd9Sstevel@tonic-gate <S2>typedef	{ yybegin(YYS_EXPR);	return (DT_KEY_TYPEDEF); }
1777c478bd9Sstevel@tonic-gate <S2>union	{ yybegin(YYS_EXPR);	return (DT_KEY_UNION); }
1787c478bd9Sstevel@tonic-gate <S2>unsigned	{ yybegin(YYS_EXPR);	return (DT_KEY_UNSIGNED); }
1797c478bd9Sstevel@tonic-gate <S2>void	{ yybegin(YYS_EXPR);	return (DT_KEY_VOID); }
1807c478bd9Sstevel@tonic-gate <S2>volatile	{ yybegin(YYS_EXPR);	return (DT_KEY_VOLATILE); }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate <S0>"$$"[0-9]+	{
1837c478bd9Sstevel@tonic-gate 			int i = atoi(yytext + 2);
1847c478bd9Sstevel@tonic-gate 			char *v = "";
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			/*
1877c478bd9Sstevel@tonic-gate 			 * A macro argument reference substitutes the text of
1887c478bd9Sstevel@tonic-gate 			 * an argument in place of the current token.  When we
1897c478bd9Sstevel@tonic-gate 			 * see $$<d> we fetch the saved string from pcb_sargv
1907c478bd9Sstevel@tonic-gate 			 * (or use the default argument if the option has been
1917c478bd9Sstevel@tonic-gate 			 * set and the argument hasn't been specified) and
1927c478bd9Sstevel@tonic-gate 			 * return a token corresponding to this string.
1937c478bd9Sstevel@tonic-gate 			 */
1947c478bd9Sstevel@tonic-gate 			if (i < 0 || (i >= yypcb->pcb_sargc &&
1957c478bd9Sstevel@tonic-gate 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
1967c478bd9Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
1977c478bd9Sstevel@tonic-gate 				    "not defined\n", yytext);
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 			if (i < yypcb->pcb_sargc) {
2017c478bd9Sstevel@tonic-gate 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
2027c478bd9Sstevel@tonic-gate 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup(v)) == NULL)
2067c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 			(void) stresc2chr(yylval.l_str);
2097c478bd9Sstevel@tonic-gate 			return (DT_TOK_STRING);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate <S0>"$"[0-9]+	{
2137c478bd9Sstevel@tonic-gate 			int i = atoi(yytext + 1);
2147c478bd9Sstevel@tonic-gate 			char *p, *v = "0";
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 			/*
2177c478bd9Sstevel@tonic-gate 			 * A macro argument reference substitutes the text of
2187c478bd9Sstevel@tonic-gate 			 * one identifier or integer pattern for another.  When
2197c478bd9Sstevel@tonic-gate 			 * we see $<d> we fetch the saved string from pcb_sargv
2207c478bd9Sstevel@tonic-gate 			 * (or use the default argument if the option has been
2217c478bd9Sstevel@tonic-gate 			 * set and the argument hasn't been specified) and
2227c478bd9Sstevel@tonic-gate 			 * return a token corresponding to this string.
2237c478bd9Sstevel@tonic-gate 			 */
2247c478bd9Sstevel@tonic-gate 			if (i < 0 || (i >= yypcb->pcb_sargc &&
2257c478bd9Sstevel@tonic-gate 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2267c478bd9Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
2277c478bd9Sstevel@tonic-gate 				    "not defined\n", yytext);
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 			if (i < yypcb->pcb_sargc) {
2317c478bd9Sstevel@tonic-gate 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
2327c478bd9Sstevel@tonic-gate 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			/*
2367c478bd9Sstevel@tonic-gate 			 * If the macro text is not a valid integer or ident,
2377c478bd9Sstevel@tonic-gate 			 * then we treat it as a string.  The string may be
2387c478bd9Sstevel@tonic-gate 			 * optionally enclosed in quotes, which we strip.
2397c478bd9Sstevel@tonic-gate 			 */
2407c478bd9Sstevel@tonic-gate 			if (strbadidnum(v)) {
2417c478bd9Sstevel@tonic-gate 				size_t len = strlen(v);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 				if (len != 1 && *v == '"' && v[len - 1] == '"')
2447c478bd9Sstevel@tonic-gate 					yylval.l_str = strndup(v + 1, len - 2);
2457c478bd9Sstevel@tonic-gate 				else
2467c478bd9Sstevel@tonic-gate 					yylval.l_str = strndup(v, len);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 				if (yylval.l_str == NULL)
2497c478bd9Sstevel@tonic-gate 					longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 				(void) stresc2chr(yylval.l_str);
2527c478bd9Sstevel@tonic-gate 				return (DT_TOK_STRING);
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			/*
2567c478bd9Sstevel@tonic-gate 			 * If the macro text is not a string an begins with a
2577c478bd9Sstevel@tonic-gate 			 * digit or a +/- sign, process it as an integer token.
2587c478bd9Sstevel@tonic-gate 			 */
2597c478bd9Sstevel@tonic-gate 			if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
2607c478bd9Sstevel@tonic-gate 				if (isdigit(v[0]))
2617c478bd9Sstevel@tonic-gate 					yyintprefix = 0;
2627c478bd9Sstevel@tonic-gate 				else
2637c478bd9Sstevel@tonic-gate 					yyintprefix = *v++;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 				errno = 0;
2667c478bd9Sstevel@tonic-gate 				yylval.l_int = strtoull(v, &p, 0);
2677c478bd9Sstevel@tonic-gate 				(void) strncpy(yyintsuffix, p,
2687c478bd9Sstevel@tonic-gate 				    sizeof (yyintsuffix));
2697c478bd9Sstevel@tonic-gate 				yyintdecimal = *v != '0';
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 				if (errno == ERANGE) {
2727c478bd9Sstevel@tonic-gate 					xyerror(D_MACRO_OFLOW, "macro argument"
2737c478bd9Sstevel@tonic-gate 					    " %s constant %s results in integer"
2747c478bd9Sstevel@tonic-gate 					    " overflow\n", yytext, v);
2757c478bd9Sstevel@tonic-gate 				}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 				return (DT_TOK_INT);
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			return (id_or_type(v));
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate <S0>"$$"{RGX_IDENT} {
2847c478bd9Sstevel@tonic-gate 			dt_ident_t *idp = dt_idhash_lookup(
2857c478bd9Sstevel@tonic-gate 			    yypcb->pcb_hdl->dt_macros, yytext + 2);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			char s[16]; /* enough for UINT_MAX + \0 */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			if (idp == NULL) {
2907c478bd9Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro variable %s "
2917c478bd9Sstevel@tonic-gate 				    "is not defined\n", yytext);
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			/*
2957c478bd9Sstevel@tonic-gate 			 * For the moment, all current macro variables are of
2967c478bd9Sstevel@tonic-gate 			 * type id_t (refer to dtrace_update() for details).
2977c478bd9Sstevel@tonic-gate 			 */
2987c478bd9Sstevel@tonic-gate 			(void) snprintf(s, sizeof (s), "%u", idp->di_id);
2997c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup(s)) == NULL)
3007c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			return (DT_TOK_STRING);
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate <S0>"$"{RGX_IDENT} {
3067c478bd9Sstevel@tonic-gate 			dt_ident_t *idp = dt_idhash_lookup(
3077c478bd9Sstevel@tonic-gate 			    yypcb->pcb_hdl->dt_macros, yytext + 1);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			if (idp == NULL) {
3107c478bd9Sstevel@tonic-gate 				xyerror(D_MACRO_UNDEF, "macro variable %s "
3117c478bd9Sstevel@tonic-gate 				    "is not defined\n", yytext);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			/*
3157c478bd9Sstevel@tonic-gate 			 * For the moment, all current macro variables are of
3167c478bd9Sstevel@tonic-gate 			 * type id_t (refer to dtrace_update() for details).
3177c478bd9Sstevel@tonic-gate 			 */
3187c478bd9Sstevel@tonic-gate 			yylval.l_int = (intmax_t)(int)idp->di_id;
3197c478bd9Sstevel@tonic-gate 			yyintprefix = 0;
3207c478bd9Sstevel@tonic-gate 			yyintsuffix[0] = '\0';
3217c478bd9Sstevel@tonic-gate 			yyintdecimal = 1;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			return (DT_TOK_INT);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
326*a386cc11SRobert Mustacchi <S0>{RGX_IDENT} |
327*a386cc11SRobert Mustacchi <S0>{RGX_MOD_IDENT}{RGX_IDENT} |
328*a386cc11SRobert Mustacchi <S0>{RGX_MOD_IDENT} {
3297c478bd9Sstevel@tonic-gate 			return (id_or_type(yytext));
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate <S0>{RGX_AGG}	{
3337c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
3347c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3357c478bd9Sstevel@tonic-gate 			return (DT_TOK_AGG);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate <S0>"@"		{
3397c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup("@_")) == NULL)
3407c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3417c478bd9Sstevel@tonic-gate 			return (DT_TOK_AGG);
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate <S0>{RGX_INT}	|
3457c478bd9Sstevel@tonic-gate <S2>{RGX_INT}	|
3467c478bd9Sstevel@tonic-gate <S3>{RGX_INT}	{
3477c478bd9Sstevel@tonic-gate 			char *p;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 			errno = 0;
3507c478bd9Sstevel@tonic-gate 			yylval.l_int = strtoull(yytext, &p, 0);
3517c478bd9Sstevel@tonic-gate 			yyintprefix = 0;
3527c478bd9Sstevel@tonic-gate 			(void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
3537c478bd9Sstevel@tonic-gate 			yyintdecimal = yytext[0] != '0';
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
3567c478bd9Sstevel@tonic-gate 				xyerror(D_INT_OFLOW, "constant %s results in "
3577c478bd9Sstevel@tonic-gate 				    "integer overflow\n", yytext);
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 			if (*p != '\0' && strchr("uUlL", *p) == NULL) {
3617c478bd9Sstevel@tonic-gate 				xyerror(D_INT_DIGIT, "constant %s contains "
3627c478bd9Sstevel@tonic-gate 				    "invalid digit %c\n", yytext, *p);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			if ((YYSTATE) != S3)
3667c478bd9Sstevel@tonic-gate 				return (DT_TOK_INT);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma,
3697c478bd9Sstevel@tonic-gate 			    dt_node_int(yylval.l_int));
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate <S0>{RGX_FP}	yyerror("floating-point constants are not permitted\n");
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate <S0>\"{RGX_STR}$ |
3757c478bd9Sstevel@tonic-gate <S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate <S0>\"{RGX_STR}\" |
3787c478bd9Sstevel@tonic-gate <S3>\"{RGX_STR}\" {
3797c478bd9Sstevel@tonic-gate 			/*
3807c478bd9Sstevel@tonic-gate 			 * Quoted string -- convert C escape sequences and
3817c478bd9Sstevel@tonic-gate 			 * return the string as a token.
3827c478bd9Sstevel@tonic-gate 			 */
3837c478bd9Sstevel@tonic-gate 			yylval.l_str = strndup(yytext + 1, yyleng - 2);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			if (yylval.l_str == NULL)
3867c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			(void) stresc2chr(yylval.l_str);
3897c478bd9Sstevel@tonic-gate 			if ((YYSTATE) != S3)
3907c478bd9Sstevel@tonic-gate 				return (DT_TOK_STRING);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma,
3937c478bd9Sstevel@tonic-gate 			    dt_node_string(yylval.l_str));
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate <S0>'{RGX_CHR}$	xyerror(D_CHR_NL, "newline encountered in character constant");
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate <S0>'{RGX_CHR}'	{
3997c478bd9Sstevel@tonic-gate 			char *s, *p, *q;
4007c478bd9Sstevel@tonic-gate 			size_t nbytes;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 			/*
4037c478bd9Sstevel@tonic-gate 			 * Character constant -- convert C escape sequences and
4047c478bd9Sstevel@tonic-gate 			 * return the character as an integer immediate value.
4057c478bd9Sstevel@tonic-gate 			 */
4067c478bd9Sstevel@tonic-gate 			if (yyleng == 2)
4077c478bd9Sstevel@tonic-gate 				xyerror(D_CHR_NULL, "empty character constant");
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 			s = yytext + 1;
4107c478bd9Sstevel@tonic-gate 			yytext[yyleng - 1] = '\0';
4117c478bd9Sstevel@tonic-gate 			nbytes = stresc2chr(s);
4127c478bd9Sstevel@tonic-gate 			yylval.l_int = 0;
4137c478bd9Sstevel@tonic-gate 			yyintprefix = 0;
4147c478bd9Sstevel@tonic-gate 			yyintsuffix[0] = '\0';
4157c478bd9Sstevel@tonic-gate 			yyintdecimal = 1;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 			if (nbytes > sizeof (yylval.l_int)) {
4187c478bd9Sstevel@tonic-gate 				xyerror(D_CHR_OFLOW, "character constant is "
4197c478bd9Sstevel@tonic-gate 				    "too long");
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
4227c478bd9Sstevel@tonic-gate 			p = ((char *)&yylval.l_int) + nbytes - 1;
4237c478bd9Sstevel@tonic-gate 			for (q = s; nbytes != 0; nbytes--)
4247c478bd9Sstevel@tonic-gate 				*p-- = *q++;
4257c478bd9Sstevel@tonic-gate #else
4267c478bd9Sstevel@tonic-gate 			bcopy(s, ((char *)&yylval.l_int) +
4277c478bd9Sstevel@tonic-gate 			    sizeof (yylval.l_int) - nbytes, nbytes);
4287c478bd9Sstevel@tonic-gate #endif
4297c478bd9Sstevel@tonic-gate 			return (DT_TOK_INT);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate <S0>"/*"	|
4337c478bd9Sstevel@tonic-gate <S2>"/*"	{
4347c478bd9Sstevel@tonic-gate 			yypcb->pcb_cstate = (YYSTATE);
4357c478bd9Sstevel@tonic-gate 			BEGIN(S1);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate <S0>{RGX_INTERP} |
4397c478bd9Sstevel@tonic-gate <S2>{RGX_INTERP} ;	/* discard any #! lines */
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate <S0>{RGX_CTL}	|
442c9d6cd77Sjhaslam <S2>{RGX_CTL}	|
443c9d6cd77Sjhaslam <S4>{RGX_CTL}	{
4447c478bd9Sstevel@tonic-gate 			assert(yypragma == NULL);
4457c478bd9Sstevel@tonic-gate 			yypcb->pcb_cstate = (YYSTATE);
4467c478bd9Sstevel@tonic-gate 			BEGIN(S3);
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 
449c9d6cd77Sjhaslam <S4>.		;	/* discard */
450c9d6cd77Sjhaslam <S4>"\n"	;	/* discard */
451c9d6cd77Sjhaslam 
4527c478bd9Sstevel@tonic-gate <S0>"/"		{
4537c478bd9Sstevel@tonic-gate 			int c, tok;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * The use of "/" as the predicate delimiter and as the
4577c478bd9Sstevel@tonic-gate 			 * integer division symbol requires special lookahead
4587c478bd9Sstevel@tonic-gate 			 * to avoid a shift/reduce conflict in the D grammar.
4597c478bd9Sstevel@tonic-gate 			 * We look ahead to the next non-whitespace character.
4607c478bd9Sstevel@tonic-gate 			 * If we encounter EOF, ";", "{", or "/", then this "/"
4617c478bd9Sstevel@tonic-gate 			 * closes the predicate and we return DT_TOK_EPRED.
4627c478bd9Sstevel@tonic-gate 			 * If we encounter anything else, it's DT_TOK_DIV.
4637c478bd9Sstevel@tonic-gate 			 */
4647c478bd9Sstevel@tonic-gate 			while ((c = input()) != 0) {
4657c478bd9Sstevel@tonic-gate 				if (strchr("\f\n\r\t\v ", c) == NULL)
4667c478bd9Sstevel@tonic-gate 					break;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			if (c == 0 || c == ';' || c == '{' || c == '/') {
4707c478bd9Sstevel@tonic-gate 				if (yypcb->pcb_parens != 0) {
4717c478bd9Sstevel@tonic-gate 					yyerror("closing ) expected in "
4727c478bd9Sstevel@tonic-gate 					    "predicate before /\n");
4737c478bd9Sstevel@tonic-gate 				}
4747c478bd9Sstevel@tonic-gate 				if (yypcb->pcb_brackets != 0) {
4757c478bd9Sstevel@tonic-gate 					yyerror("closing ] expected in "
4767c478bd9Sstevel@tonic-gate 					    "predicate before /\n");
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 				tok = DT_TOK_EPRED;
4797c478bd9Sstevel@tonic-gate 			} else
4807c478bd9Sstevel@tonic-gate 				tok = DT_TOK_DIV;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 			unput(c);
4837c478bd9Sstevel@tonic-gate 			return (tok);
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate <S0>"("		{
4877c478bd9Sstevel@tonic-gate 			yypcb->pcb_parens++;
4887c478bd9Sstevel@tonic-gate 			return (DT_TOK_LPAR);
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate <S0>")"		{
4927c478bd9Sstevel@tonic-gate 			if (--yypcb->pcb_parens < 0)
4937c478bd9Sstevel@tonic-gate 				yyerror("extra ) in input stream\n");
4947c478bd9Sstevel@tonic-gate 			return (DT_TOK_RPAR);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate <S0>"["		{
4987c478bd9Sstevel@tonic-gate 			yypcb->pcb_brackets++;
4997c478bd9Sstevel@tonic-gate 			return (DT_TOK_LBRAC);
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate <S0>"]"		{
5037c478bd9Sstevel@tonic-gate 			if (--yypcb->pcb_brackets < 0)
5047c478bd9Sstevel@tonic-gate 				yyerror("extra ] in input stream\n");
5057c478bd9Sstevel@tonic-gate 			return (DT_TOK_RBRAC);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate <S0>"{"		|
5097c478bd9Sstevel@tonic-gate <S2>"{"		{
5107c478bd9Sstevel@tonic-gate 			yypcb->pcb_braces++;
5117c478bd9Sstevel@tonic-gate 			return ('{');
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate <S0>"}"		{
5157c478bd9Sstevel@tonic-gate 			if (--yypcb->pcb_braces < 0)
5167c478bd9Sstevel@tonic-gate 				yyerror("extra } in input stream\n");
5177c478bd9Sstevel@tonic-gate 			return ('}');
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate <S0>"|"		return (DT_TOK_BOR);
5217c478bd9Sstevel@tonic-gate <S0>"^"		return (DT_TOK_XOR);
5227c478bd9Sstevel@tonic-gate <S0>"&"		return (DT_TOK_BAND);
5237c478bd9Sstevel@tonic-gate <S0>"&&"	return (DT_TOK_LAND);
5247c478bd9Sstevel@tonic-gate <S0>"^^"	return (DT_TOK_LXOR);
5257c478bd9Sstevel@tonic-gate <S0>"||"	return (DT_TOK_LOR);
5267c478bd9Sstevel@tonic-gate <S0>"=="	return (DT_TOK_EQU);
5277c478bd9Sstevel@tonic-gate <S0>"!="	return (DT_TOK_NEQ);
5287c478bd9Sstevel@tonic-gate <S0>"<"		return (DT_TOK_LT);
5297c478bd9Sstevel@tonic-gate <S0>"<="	return (DT_TOK_LE);
5307c478bd9Sstevel@tonic-gate <S0>">"		return (DT_TOK_GT);
5317c478bd9Sstevel@tonic-gate <S0>">="	return (DT_TOK_GE);
5327c478bd9Sstevel@tonic-gate <S0>"<<"	return (DT_TOK_LSH);
5337c478bd9Sstevel@tonic-gate <S0>">>"	return (DT_TOK_RSH);
5347c478bd9Sstevel@tonic-gate <S0>"+"		return (DT_TOK_ADD);
5357c478bd9Sstevel@tonic-gate <S0>"-"		return (DT_TOK_SUB);
5367c478bd9Sstevel@tonic-gate <S0>"*"		return (DT_TOK_MUL);
5377c478bd9Sstevel@tonic-gate <S0>"%"		return (DT_TOK_MOD);
5387c478bd9Sstevel@tonic-gate <S0>"~"		return (DT_TOK_BNEG);
5397c478bd9Sstevel@tonic-gate <S0>"!"		return (DT_TOK_LNEG);
5407c478bd9Sstevel@tonic-gate <S0>"?"		return (DT_TOK_QUESTION);
5417c478bd9Sstevel@tonic-gate <S0>":"		return (DT_TOK_COLON);
5427c478bd9Sstevel@tonic-gate <S0>"."		return (DT_TOK_DOT);
5437c478bd9Sstevel@tonic-gate <S0>"->"	return (DT_TOK_PTR);
5447c478bd9Sstevel@tonic-gate <S0>"="		return (DT_TOK_ASGN);
5457c478bd9Sstevel@tonic-gate <S0>"+="	return (DT_TOK_ADD_EQ);
5467c478bd9Sstevel@tonic-gate <S0>"-="	return (DT_TOK_SUB_EQ);
5477c478bd9Sstevel@tonic-gate <S0>"*="	return (DT_TOK_MUL_EQ);
5487c478bd9Sstevel@tonic-gate <S0>"/="	return (DT_TOK_DIV_EQ);
5497c478bd9Sstevel@tonic-gate <S0>"%="	return (DT_TOK_MOD_EQ);
5507c478bd9Sstevel@tonic-gate <S0>"&="	return (DT_TOK_AND_EQ);
5517c478bd9Sstevel@tonic-gate <S0>"^="	return (DT_TOK_XOR_EQ);
5527c478bd9Sstevel@tonic-gate <S0>"|="	return (DT_TOK_OR_EQ);
5537c478bd9Sstevel@tonic-gate <S0>"<<="	return (DT_TOK_LSH_EQ);
5547c478bd9Sstevel@tonic-gate <S0>">>="	return (DT_TOK_RSH_EQ);
5557c478bd9Sstevel@tonic-gate <S0>"++"	return (DT_TOK_ADDADD);
5567c478bd9Sstevel@tonic-gate <S0>"--"	return (DT_TOK_SUBSUB);
5577c478bd9Sstevel@tonic-gate <S0>"..."	return (DT_TOK_ELLIPSIS);
5587c478bd9Sstevel@tonic-gate <S0>","		return (DT_TOK_COMMA);
5597c478bd9Sstevel@tonic-gate <S0>";"		return (';');
5607c478bd9Sstevel@tonic-gate <S0>{RGX_WS}	; /* discard */
5617c478bd9Sstevel@tonic-gate <S0>"\\"\n	; /* discard */
5627c478bd9Sstevel@tonic-gate <S0>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
5637c478bd9Sstevel@tonic-gate 
5641a7c1b72Smws <S1>"/*"	yyerror("/* encountered inside a comment\n");
5657c478bd9Sstevel@tonic-gate <S1>"*/"	BEGIN(yypcb->pcb_cstate);
5667c478bd9Sstevel@tonic-gate <S1>.|\n	; /* discard */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate <S2>{RGX_PSPEC}	{
5697c478bd9Sstevel@tonic-gate 			/*
5707c478bd9Sstevel@tonic-gate 			 * S2 has an ambiguity because RGX_PSPEC includes '*'
5717c478bd9Sstevel@tonic-gate 			 * as a glob character and '*' also can be DT_TOK_STAR.
5727c478bd9Sstevel@tonic-gate 			 * Since lex always matches the longest token, this
5737c478bd9Sstevel@tonic-gate 			 * rule can be matched by an input string like "int*",
5747c478bd9Sstevel@tonic-gate 			 * which could begin a global variable declaration such
5757c478bd9Sstevel@tonic-gate 			 * as "int*x;" or could begin a RGX_PSPEC with globbing
5767c478bd9Sstevel@tonic-gate 			 * such as "int* { trace(timestamp); }".  If C_PSPEC is
5777c478bd9Sstevel@tonic-gate 			 * not set, we must resolve the ambiguity in favor of
5787c478bd9Sstevel@tonic-gate 			 * the type and perform lexer pushback if the fragment
5797c478bd9Sstevel@tonic-gate 			 * before '*' or entire fragment matches a type name.
5807c478bd9Sstevel@tonic-gate 			 * If C_PSPEC is set, we always return a PSPEC token.
5817c478bd9Sstevel@tonic-gate 			 * If C_PSPEC is off, the user can avoid ambiguity by
5827c478bd9Sstevel@tonic-gate 			 * including a ':' delimiter in the specifier, which
5837c478bd9Sstevel@tonic-gate 			 * they should be doing anyway to specify the provider.
5847c478bd9Sstevel@tonic-gate 			 */
5857c478bd9Sstevel@tonic-gate 			if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
5867c478bd9Sstevel@tonic-gate 			    strchr(yytext, ':') == NULL) {
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 				char *p = strchr(yytext, '*');
5897c478bd9Sstevel@tonic-gate 				char *q = yytext + yyleng - 1;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 				if (p != NULL && p > yytext)
5927c478bd9Sstevel@tonic-gate 					*p = '\0'; /* prune yytext */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 				if (dt_type_lookup(yytext, NULL) == 0) {
5957c478bd9Sstevel@tonic-gate 					yylval.l_str = strdup(yytext);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 					if (yylval.l_str == NULL) {
5987c478bd9Sstevel@tonic-gate 						longjmp(yypcb->pcb_jmpbuf,
5997c478bd9Sstevel@tonic-gate 						    EDT_NOMEM);
6007c478bd9Sstevel@tonic-gate 					}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 					if (p != NULL && p > yytext) {
6037c478bd9Sstevel@tonic-gate 						for (*p = '*'; q >= p; q--)
6047c478bd9Sstevel@tonic-gate 							unput(*q);
6057c478bd9Sstevel@tonic-gate 					}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 					yybegin(YYS_EXPR);
6087c478bd9Sstevel@tonic-gate 					return (DT_TOK_TNAME);
6097c478bd9Sstevel@tonic-gate 				}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 				if (p != NULL && p > yytext)
6127c478bd9Sstevel@tonic-gate 					*p = '*'; /* restore yytext */
6137c478bd9Sstevel@tonic-gate 			}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
6167c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			return (DT_TOK_PSPEC);
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate <S2>"/"		return (DT_TOK_DIV);
6227c478bd9Sstevel@tonic-gate <S2>","		return (DT_TOK_COMMA);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate <S2>{RGX_WS}	; /* discard */
6257c478bd9Sstevel@tonic-gate <S2>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate <S3>\n		{
6287c478bd9Sstevel@tonic-gate 			dt_pragma(yypragma);
6297c478bd9Sstevel@tonic-gate 			yypragma = NULL;
6307c478bd9Sstevel@tonic-gate 			BEGIN(yypcb->pcb_cstate);
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate <S3>[\f\t\v ]+	; /* discard */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate <S3>[^\f\n\t\v "]+ {
6367c478bd9Sstevel@tonic-gate 			dt_node_t *dnp;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			if ((yylval.l_str = strdup(yytext)) == NULL)
6397c478bd9Sstevel@tonic-gate 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			/*
6427c478bd9Sstevel@tonic-gate 			 * We want to call dt_node_ident() here, but we can't
6437c478bd9Sstevel@tonic-gate 			 * because it will expand inlined identifiers, which we
6447c478bd9Sstevel@tonic-gate 			 * don't want to do from #pragma context in order to
6457c478bd9Sstevel@tonic-gate 			 * support pragmas that apply to the ident itself.  We
6467c478bd9Sstevel@tonic-gate 			 * call dt_node_string() and then reset dn_op instead.
6477c478bd9Sstevel@tonic-gate 			 */
6487c478bd9Sstevel@tonic-gate 			dnp = dt_node_string(yylval.l_str);
6497c478bd9Sstevel@tonic-gate 			dnp->dn_kind = DT_NODE_IDENT;
6507c478bd9Sstevel@tonic-gate 			dnp->dn_op = DT_TOK_IDENT;
6517c478bd9Sstevel@tonic-gate 			yypragma = dt_node_link(yypragma, dnp);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate <S3>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate %%
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate  * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
6607c478bd9Sstevel@tonic-gate  * We use two main states for lexing because probe descriptions use a syntax
6617c478bd9Sstevel@tonic-gate  * that is incompatible with the normal D tokens (e.g. names can contain "-").
6627c478bd9Sstevel@tonic-gate  * yybegin also handles the job of switching between two lists of dt_nodes
6637c478bd9Sstevel@tonic-gate  * as we allocate persistent definitions, like inlines, and transient nodes
6647c478bd9Sstevel@tonic-gate  * that will be freed once we are done parsing the current program file.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate void
6677c478bd9Sstevel@tonic-gate yybegin(yystate_t state)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate #ifdef	YYDEBUG
6707c478bd9Sstevel@tonic-gate 	yydebug = _dtrace_debug;
6717c478bd9Sstevel@tonic-gate #endif
6727c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_yystate == state)
6737c478bd9Sstevel@tonic-gate 		return; /* nothing to do if we're in the state already */
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_yystate == YYS_DEFINE) {
6767c478bd9Sstevel@tonic-gate 		yypcb->pcb_list = yypcb->pcb_hold;
6777c478bd9Sstevel@tonic-gate 		yypcb->pcb_hold = NULL;
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	switch (state) {
6817c478bd9Sstevel@tonic-gate 	case YYS_CLAUSE:
6827c478bd9Sstevel@tonic-gate 		BEGIN(S2);
6837c478bd9Sstevel@tonic-gate 		break;
6847c478bd9Sstevel@tonic-gate 	case YYS_DEFINE:
6857c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_hold == NULL);
6867c478bd9Sstevel@tonic-gate 		yypcb->pcb_hold = yypcb->pcb_list;
6877c478bd9Sstevel@tonic-gate 		yypcb->pcb_list = NULL;
6887c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
6897c478bd9Sstevel@tonic-gate 	case YYS_EXPR:
6907c478bd9Sstevel@tonic-gate 		BEGIN(S0);
6917c478bd9Sstevel@tonic-gate 		break;
6927c478bd9Sstevel@tonic-gate 	case YYS_DONE:
6937c478bd9Sstevel@tonic-gate 		break;
694c9d6cd77Sjhaslam 	case YYS_CONTROL:
695c9d6cd77Sjhaslam 		BEGIN(S4);
696c9d6cd77Sjhaslam 		break;
6977c478bd9Sstevel@tonic-gate 	default:
6987c478bd9Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	yypcb->pcb_yystate = state;
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate void
7057c478bd9Sstevel@tonic-gate yyinit(dt_pcb_t *pcb)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	yypcb = pcb;
7087c478bd9Sstevel@tonic-gate 	yylineno = 1;
7097c478bd9Sstevel@tonic-gate 	yypragma = NULL;
7107c478bd9Sstevel@tonic-gate 	yysptr = yysbuf;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
7157c478bd9Sstevel@tonic-gate  * token to the parser indicating either an identifier or a typedef name.
7167c478bd9Sstevel@tonic-gate  * User-defined global variables always take precedence over types, but we do
7177c478bd9Sstevel@tonic-gate  * use some heuristics because D programs can look at an ever-changing set of
7187c478bd9Sstevel@tonic-gate  * kernel types and also can implicitly instantiate variables by assignment,
7197c478bd9Sstevel@tonic-gate  * unlike in C.  The code here is ordered carefully as lookups are not cheap.
7207c478bd9Sstevel@tonic-gate  */
7217c478bd9Sstevel@tonic-gate static int
7227c478bd9Sstevel@tonic-gate id_or_type(const char *s)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
725e4586ebfSmws 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
7267c478bd9Sstevel@tonic-gate 	int c0, c1, ttok = DT_TOK_TNAME;
7277c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if ((s = yylval.l_str = strdup(s)) == NULL)
7307c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * If the lexeme is a global variable or likely identifier or *not* a
7347c478bd9Sstevel@tonic-gate 	 * type_name, then it is an identifier token.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
7377c478bd9Sstevel@tonic-gate 	    dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
7387c478bd9Sstevel@tonic-gate 	    dt_type_lookup(s, NULL) != 0)
7397c478bd9Sstevel@tonic-gate 		return (DT_TOK_IDENT);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
742e4586ebfSmws 	 * If we're in the midst of parsing a declaration and a type_specifier
743e4586ebfSmws 	 * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
744e4586ebfSmws 	 * This semantic is necessary to permit valid ISO C code such as:
745e4586ebfSmws 	 *
746e4586ebfSmws 	 * typedef int foo;
747e4586ebfSmws 	 * struct s { foo foo; };
748e4586ebfSmws 	 *
749e4586ebfSmws 	 * without causing shift/reduce conflicts in the direct_declarator part
750e4586ebfSmws 	 * of the grammar.  The result is that we must check for conflicting
751e4586ebfSmws 	 * redeclarations of the same identifier as part of dt_node_decl().
752e4586ebfSmws 	 */
753e4586ebfSmws 	if (ddp != NULL && ddp->dd_name != NULL)
754e4586ebfSmws 		return (DT_TOK_IDENT);
755e4586ebfSmws 
756e4586ebfSmws 	/*
7577c478bd9Sstevel@tonic-gate 	 * If the lexeme is a type name and we are not in a program clause,
7587c478bd9Sstevel@tonic-gate 	 * then always interpret it as a type and return DT_TOK_TNAME.
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	if ((YYSTATE) != S0)
7617c478bd9Sstevel@tonic-gate 		return (DT_TOK_TNAME);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * If the lexeme matches a type name but is in a program clause, then
7657c478bd9Sstevel@tonic-gate 	 * it could be a type or it could be an undefined variable.  Peek at
7667c478bd9Sstevel@tonic-gate 	 * the next token to decide.  If we see ++, --, [, or =, we know there
7677c478bd9Sstevel@tonic-gate 	 * might be an assignment that is trying to create a global variable,
7687c478bd9Sstevel@tonic-gate 	 * so we optimistically return DT_TOK_IDENT.  There is no harm in being
7697c478bd9Sstevel@tonic-gate 	 * wrong: a type_name followed by ++, --, [, or = is a syntax error.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	while ((c0 = input()) != 0) {
7727c478bd9Sstevel@tonic-gate 		if (strchr("\f\n\r\t\v ", c0) == NULL)
7737c478bd9Sstevel@tonic-gate 			break;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	switch (c0) {
7777c478bd9Sstevel@tonic-gate 	case '+':
7787c478bd9Sstevel@tonic-gate 	case '-':
7797c478bd9Sstevel@tonic-gate 		if ((c1 = input()) == c0)
7807c478bd9Sstevel@tonic-gate 			ttok = DT_TOK_IDENT;
7817c478bd9Sstevel@tonic-gate 		unput(c1);
7827c478bd9Sstevel@tonic-gate 		break;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	case '=':
7857c478bd9Sstevel@tonic-gate 		if ((c1 = input()) != c0)
7867c478bd9Sstevel@tonic-gate 			ttok = DT_TOK_IDENT;
7877c478bd9Sstevel@tonic-gate 		unput(c1);
7887c478bd9Sstevel@tonic-gate 		break;
7897c478bd9Sstevel@tonic-gate 	case '[':
7907c478bd9Sstevel@tonic-gate 		ttok = DT_TOK_IDENT;
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (ttok == DT_TOK_IDENT) {
7957c478bd9Sstevel@tonic-gate 		idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
7967c478bd9Sstevel@tonic-gate 		    0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		if (idp == NULL)
7997c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	unput(c0);
8037c478bd9Sstevel@tonic-gate 	return (ttok);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static int
8077c478bd9Sstevel@tonic-gate input(void)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	int c;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (yysptr > yysbuf)
8127c478bd9Sstevel@tonic-gate 		c = *--yysptr;
8137c478bd9Sstevel@tonic-gate 	else if (yypcb->pcb_fileptr != NULL)
8147c478bd9Sstevel@tonic-gate 		c = fgetc(yypcb->pcb_fileptr);
8157c478bd9Sstevel@tonic-gate 	else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
816c9a6ea2eSBryan Cantrill 		c = *(unsigned char *)(yypcb->pcb_strptr++);
8177c478bd9Sstevel@tonic-gate 	else
8187c478bd9Sstevel@tonic-gate 		c = EOF;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (c == '\n')
8217c478bd9Sstevel@tonic-gate 		yylineno++;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (c != EOF)
8247c478bd9Sstevel@tonic-gate 		return (c);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if ((YYSTATE) == S1)
8277c478bd9Sstevel@tonic-gate 		yyerror("end-of-file encountered before matching */\n");
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if ((YYSTATE) == S3)
8307c478bd9Sstevel@tonic-gate 		yyerror("end-of-file encountered before end of control line\n");
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
8337c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	return (0); /* EOF */
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static void
8397c478bd9Sstevel@tonic-gate unput(int c)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	if (c == '\n')
8427c478bd9Sstevel@tonic-gate 		yylineno--;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	*yysptr++ = c;
8457c478bd9Sstevel@tonic-gate 	yytchar = c;
8467c478bd9Sstevel@tonic-gate }
847