dt_lex.l revision 210767
18097Sjkh%{
28097Sjkh/*
38097Sjkh * CDDL HEADER START
48097Sjkh *
58097Sjkh * The contents of this file are subject to the terms of the
68097Sjkh * Common Development and Distribution License (the "License").
78594Sjkh * You may not use this file except in compliance with the License.
88097Sjkh *
98097Sjkh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
108097Sjkh * or http://www.opensolaris.org/os/licensing.
118097Sjkh * See the License for the specific language governing permissions
128097Sjkh * and limitations under the License.
138097Sjkh *
148097Sjkh * When distributing Covered Code, include this CDDL HEADER in each
158097Sjkh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
168097Sjkh * If applicable, add the following below this CDDL HEADER, with the
178097Sjkh * fields enclosed by brackets "[]" replaced with your own identifying
188097Sjkh * information: Portions Copyright [yyyy] [name of copyright owner]
198097Sjkh *
208097Sjkh * CDDL HEADER END
218097Sjkh */
228097Sjkh
238097Sjkh/*
248097Sjkh * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
258097Sjkh */
268097Sjkh
278097Sjkh#include <string.h>
288097Sjkh#include <stdlib.h>
298097Sjkh#include <stdio.h>
308097Sjkh#include <assert.h>
318097Sjkh#include <ctype.h>
328097Sjkh#include <errno.h>
338097Sjkh
348097Sjkh#include <dt_impl.h>
358097Sjkh#include <dt_grammar.h>
368097Sjkh#include <dt_parser.h>
378097Sjkh#include <dt_string.h>
388097Sjkh
398097Sjkh/*
408097Sjkh * We need to undefine lex's input and unput macros so that references to these
418097Sjkh * call the functions provided at the end of this source file.
428097Sjkh */
438097Sjkh#if defined(sun)
448097Sjkh#undef input
458097Sjkh#undef unput
468097Sjkh#else
478262Sjkh/*
488262Sjkh * Define YY_INPUT for flex since input() can't be re-defined.
498262Sjkh */
508262Sjkh#define YY_INPUT(buf,result,max_size) \
518262Sjkh	if (yypcb->pcb_fileptr != NULL) { \
528262Sjkh		if (((result = fread(buf, 1, max_size, yypcb->pcb_fileptr)) == 0) \
538262Sjkh		    && ferror(yypcb->pcb_fileptr)) \
548262Sjkh			longjmp(yypcb->pcb_jmpbuf, EDT_FIO); \
558317Sjkh	} else { \
568262Sjkh		int n; \
578347Sjkh		for (n = 0; n < max_size && \
588262Sjkh		    yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen; n++) \
598262Sjkh			buf[n] = *yypcb->pcb_strptr++; \
608314Sjkh		result = n; \
618262Sjkh	}
628262Sjkh/*
638262Sjkh * Do not EOF let tokens to be put back. This does not work with flex.
648262Sjkh * On the other hand, leaving current buffer in same state it was when
658262Sjkh * last EOF was received guarantees that input() will keep returning EOF
668262Sjkh * for all subsequent invocations, which is the effect desired.
678097Sjkh */
688097Sjkh#undef  unput
698097Sjkh#define unput(c)					\
708097Sjkh	do {						\
718097Sjkh		int _c = c;				\
728097Sjkh		if (_c != EOF)				\
738262Sjkh			yyunput(_c, yytext_ptr);	\
748097Sjkh	} while(0)
758317Sjkh#endif
768097Sjkh
778347Sjkhstatic int id_or_type(const char *);
788097Sjkh#if defined(sun)
798262Sjkhstatic int input(void);
808262Sjkhstatic void unput(int);
818262Sjkh#endif
828262Sjkh
838262Sjkh/*
848097Sjkh * We first define a set of labeled states for use in the D lexer and then a
858097Sjkh * set of regular expressions to simplify things below. The lexer states are:
868097Sjkh *
878097Sjkh * S0 - D program clause and expression lexing
888097Sjkh * S1 - D comments (i.e. skip everything until end of comment)
898097Sjkh * S2 - D program outer scope (probe specifiers and declarations)
908097Sjkh * S3 - D control line parsing (i.e. after ^# is seen but before \n)
918097Sjkh * S4 - D control line scan (locate control directives only and invoke S3)
928097Sjkh */
938262Sjkh%}
948097Sjkh
958317Sjkh%e 1500		/* maximum nodes */
968097Sjkh%p 3700		/* maximum positions */
978097Sjkh%n 600		/* maximum states */
988097Sjkh
998097Sjkh%s S0 S1 S2 S3 S4
1008262Sjkh
1018097SjkhRGX_AGG		"@"[a-zA-Z_][0-9a-zA-Z_]*
1028314SjkhRGX_PSPEC	[-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
1038262SjkhRGX_IDENT	[a-zA-Z_`][0-9a-zA-Z_`]*
1048262SjkhRGX_INT		([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
1058262SjkhRGX_FP		([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
1068097SjkhRGX_WS		[\f\n\r\t\v ]
1078097SjkhRGX_STR		([^"\\\n]|\\[^"\n]|\\\")*
1088097SjkhRGX_CHR		([^'\\\n]|\\[^'\n]|\\')*
1098097SjkhRGX_INTERP	^[\f\t\v ]*#!.*
1108097SjkhRGX_CTL		^[\f\t\v ]*#
1118097Sjkh
1128097Sjkh%%
1138097Sjkh
1148097Sjkh%{
1158262Sjkh
1168097Sjkh/*
1178317Sjkh * We insert a special prologue into yylex() itself: if the pcb contains a
1188097Sjkh * context token, we return that prior to running the normal lexer.  This
1198097Sjkh * allows libdtrace to force yacc into one of our three parsing contexts: D
1208097Sjkh * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
1218097Sjkh * Once the token is returned, we clear it so this only happens once.
1228097Sjkh */
1238262Sjkhif (yypcb->pcb_token != 0) {
1248314Sjkh	int tok = yypcb->pcb_token;
1258262Sjkh	yypcb->pcb_token = 0;
1268262Sjkh	return (tok);
1278262Sjkh}
1288097Sjkh
1298097Sjkh%}
1308097Sjkh
1318097Sjkh<S0>auto	return (DT_KEY_AUTO);
1328097Sjkh<S0>break	return (DT_KEY_BREAK);
1338097Sjkh<S0>case	return (DT_KEY_CASE);
1348097Sjkh<S0>char	return (DT_KEY_CHAR);
1358097Sjkh<S0>const	return (DT_KEY_CONST);
1368097Sjkh<S0>continue	return (DT_KEY_CONTINUE);
1378262Sjkh<S0>counter	return (DT_KEY_COUNTER);
1388097Sjkh<S0>default	return (DT_KEY_DEFAULT);
1398317Sjkh<S0>do		return (DT_KEY_DO);
1408097Sjkh<S0>double	return (DT_KEY_DOUBLE);
1418097Sjkh<S0>else	return (DT_KEY_ELSE);
1428097Sjkh<S0>enum	return (DT_KEY_ENUM);
1438097Sjkh<S0>extern	return (DT_KEY_EXTERN);
1448097Sjkh<S0>float	return (DT_KEY_FLOAT);
1458262Sjkh<S0>for		return (DT_KEY_FOR);
1468314Sjkh<S0>goto	return (DT_KEY_GOTO);
1478262Sjkh<S0>if		return (DT_KEY_IF);
1488097Sjkh<S0>import	return (DT_KEY_IMPORT);
1498097Sjkh<S0>inline	return (DT_KEY_INLINE);
1508097Sjkh<S0>int		return (DT_KEY_INT);
1518097Sjkh<S0>long	return (DT_KEY_LONG);
1528097Sjkh<S0>offsetof	return (DT_TOK_OFFSETOF);
1538097Sjkh<S0>probe	return (DT_KEY_PROBE);
1548262Sjkh<S0>provider	return (DT_KEY_PROVIDER);
1558262Sjkh<S0>register	return (DT_KEY_REGISTER);
1568097Sjkh<S0>restrict	return (DT_KEY_RESTRICT);
1578097Sjkh<S0>return	return (DT_KEY_RETURN);
1588097Sjkh<S0>self	return (DT_KEY_SELF);
1598097Sjkh<S0>short	return (DT_KEY_SHORT);
1608097Sjkh<S0>signed	return (DT_KEY_SIGNED);
1618208Sjkh<S0>sizeof	return (DT_TOK_SIZEOF);
1628208Sjkh<S0>static	return (DT_KEY_STATIC);
1638208Sjkh<S0>string	return (DT_KEY_STRING);
1648208Sjkh<S0>stringof	return (DT_TOK_STRINGOF);
1658208Sjkh<S0>struct	return (DT_KEY_STRUCT);
1668208Sjkh<S0>switch	return (DT_KEY_SWITCH);
1678208Sjkh<S0>this	return (DT_KEY_THIS);
1688317Sjkh<S0>translator	return (DT_KEY_XLATOR);
1698208Sjkh<S0>typedef	return (DT_KEY_TYPEDEF);
1708208Sjkh<S0>union	return (DT_KEY_UNION);
1718208Sjkh<S0>unsigned	return (DT_KEY_UNSIGNED);
1728208Sjkh<S0>void	return (DT_KEY_VOID);
1738208Sjkh<S0>volatile	return (DT_KEY_VOLATILE);
1748556Sjkh<S0>while	return (DT_KEY_WHILE);
1758208Sjkh<S0>xlate	return (DT_TOK_XLATE);
1768208Sjkh
1778208Sjkh<S2>auto	{ yybegin(YYS_EXPR);	return (DT_KEY_AUTO); }
1788302Sjkh<S2>char	{ yybegin(YYS_EXPR);	return (DT_KEY_CHAR); }
1798302Sjkh<S2>const	{ yybegin(YYS_EXPR);	return (DT_KEY_CONST); }
1808302Sjkh<S2>counter	{ yybegin(YYS_DEFINE);	return (DT_KEY_COUNTER); }
1818302Sjkh<S2>double	{ yybegin(YYS_EXPR);	return (DT_KEY_DOUBLE); }
1828302Sjkh<S2>enum	{ yybegin(YYS_EXPR);	return (DT_KEY_ENUM); }
1838302Sjkh<S2>extern	{ yybegin(YYS_EXPR);	return (DT_KEY_EXTERN); }
1848556Sjkh<S2>float	{ yybegin(YYS_EXPR);	return (DT_KEY_FLOAT); }
1858302Sjkh<S2>import	{ yybegin(YYS_EXPR);	return (DT_KEY_IMPORT); }
1868317Sjkh<S2>inline	{ yybegin(YYS_DEFINE);	return (DT_KEY_INLINE); }
1878302Sjkh<S2>int		{ yybegin(YYS_EXPR);	return (DT_KEY_INT); }
1888302Sjkh<S2>long	{ yybegin(YYS_EXPR);	return (DT_KEY_LONG); }
1898302Sjkh<S2>provider	{ yybegin(YYS_DEFINE);	return (DT_KEY_PROVIDER); }
1908302Sjkh<S2>register	{ yybegin(YYS_EXPR);	return (DT_KEY_REGISTER); }
1918302Sjkh<S2>restrict	{ yybegin(YYS_EXPR);	return (DT_KEY_RESTRICT); }
1928594Sjkh<S2>self	{ yybegin(YYS_EXPR);	return (DT_KEY_SELF); }
1938556Sjkh<S2>short	{ yybegin(YYS_EXPR);	return (DT_KEY_SHORT); }
1948438Sjkh<S2>signed	{ yybegin(YYS_EXPR);	return (DT_KEY_SIGNED); }
1958556Sjkh<S2>static	{ yybegin(YYS_EXPR);	return (DT_KEY_STATIC); }
1968556Sjkh<S2>string	{ yybegin(YYS_EXPR);	return (DT_KEY_STRING); }
1978556Sjkh<S2>struct	{ yybegin(YYS_EXPR);	return (DT_KEY_STRUCT); }
1988302Sjkh<S2>this	{ yybegin(YYS_EXPR);	return (DT_KEY_THIS); }
1998302Sjkh<S2>translator	{ yybegin(YYS_DEFINE);	return (DT_KEY_XLATOR); }
2008302Sjkh<S2>typedef	{ yybegin(YYS_EXPR);	return (DT_KEY_TYPEDEF); }
2018208Sjkh<S2>union	{ yybegin(YYS_EXPR);	return (DT_KEY_UNION); }
2028208Sjkh<S2>unsigned	{ yybegin(YYS_EXPR);	return (DT_KEY_UNSIGNED); }
2038208Sjkh<S2>void	{ yybegin(YYS_EXPR);	return (DT_KEY_VOID); }
2048208Sjkh<S2>volatile	{ yybegin(YYS_EXPR);	return (DT_KEY_VOLATILE); }
2058208Sjkh
2068208Sjkh<S0>"$$"[0-9]+	{
2078208Sjkh			int i = atoi(yytext + 2);
2088556Sjkh			char *v = "";
2098208Sjkh
2108317Sjkh			/*
2118208Sjkh			 * A macro argument reference substitutes the text of
2128208Sjkh			 * an argument in place of the current token.  When we
2138208Sjkh			 * see $$<d> we fetch the saved string from pcb_sargv
2148208Sjkh			 * (or use the default argument if the option has been
2158208Sjkh			 * set and the argument hasn't been specified) and
2168556Sjkh			 * return a token corresponding to this string.
2178281Sjkh			 */
2188556Sjkh			if (i < 0 || (i >= yypcb->pcb_sargc &&
2198556Sjkh			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2208556Sjkh				xyerror(D_MACRO_UNDEF, "macro argument %s is "
2218594Sjkh				    "not defined\n", yytext);
2228208Sjkh			}
2238208Sjkh
2248208Sjkh			if (i < yypcb->pcb_sargc) {
2258262Sjkh				v = yypcb->pcb_sargv[i]; /* get val from pcb */
2268262Sjkh				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2278262Sjkh			}
2288262Sjkh
2298262Sjkh			if ((yylval.l_str = strdup(v)) == NULL)
2308262Sjkh				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2318262Sjkh
2328278Sjkh			(void) stresc2chr(yylval.l_str);
2338262Sjkh			return (DT_TOK_STRING);
2348556Sjkh		}
2358262Sjkh
2368317Sjkh<S0>"$"[0-9]+	{
2378262Sjkh			int i = atoi(yytext + 1);
2388262Sjkh			char *p, *v = "0";
2398262Sjkh
2408262Sjkh			/*
2418262Sjkh			 * A macro argument reference substitutes the text of
2428302Sjkh			 * one identifier or integer pattern for another.  When
2438302Sjkh			 * we see $<d> we fetch the saved string from pcb_sargv
2448302Sjkh			 * (or use the default argument if the option has been
2458302Sjkh			 * set and the argument hasn't been specified) and
2468556Sjkh			 * return a token corresponding to this string.
2478262Sjkh			 */
2488556Sjkh			if (i < 0 || (i >= yypcb->pcb_sargc &&
2498556Sjkh			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2508556Sjkh				xyerror(D_MACRO_UNDEF, "macro argument %s is "
2518594Sjkh				    "not defined\n", yytext);
2528262Sjkh			}
2538262Sjkh
2548262Sjkh			if (i < yypcb->pcb_sargc) {
2558262Sjkh				v = yypcb->pcb_sargv[i]; /* get val from pcb */
2568262Sjkh				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2578262Sjkh			}
2588262Sjkh
2598347Sjkh			/*
2608347Sjkh			 * If the macro text is not a valid integer or ident,
2618347Sjkh			 * then we treat it as a string.  The string may be
2628347Sjkh			 * optionally enclosed in quotes, which we strip.
2638347Sjkh			 */
2648347Sjkh			if (strbadidnum(v)) {
2658347Sjkh				size_t len = strlen(v);
2668594Sjkh
2678594Sjkh				if (len != 1 && *v == '"' && v[len - 1] == '"')
2688347Sjkh					yylval.l_str = strndup(v + 1, len - 2);
2698347Sjkh				else
2708347Sjkh					yylval.l_str = strndup(v, len);
2718347Sjkh
2728347Sjkh				if (yylval.l_str == NULL)
2738347Sjkh					longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2748347Sjkh
2758347Sjkh				(void) stresc2chr(yylval.l_str);
276				return (DT_TOK_STRING);
277			}
278
279			/*
280			 * If the macro text is not a string an begins with a
281			 * digit or a +/- sign, process it as an integer token.
282			 */
283			if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
284				if (isdigit(v[0]))
285					yyintprefix = 0;
286				else
287					yyintprefix = *v++;
288
289				errno = 0;
290				yylval.l_int = strtoull(v, &p, 0);
291				(void) strncpy(yyintsuffix, p,
292				    sizeof (yyintsuffix));
293				yyintdecimal = *v != '0';
294
295				if (errno == ERANGE) {
296					xyerror(D_MACRO_OFLOW, "macro argument"
297					    " %s constant %s results in integer"
298					    " overflow\n", yytext, v);
299				}
300
301				return (DT_TOK_INT);
302			}
303
304			return (id_or_type(v));
305		}
306
307<S0>"$$"{RGX_IDENT} {
308			dt_ident_t *idp = dt_idhash_lookup(
309			    yypcb->pcb_hdl->dt_macros, yytext + 2);
310
311			char s[16]; /* enough for UINT_MAX + \0 */
312
313			if (idp == NULL) {
314				xyerror(D_MACRO_UNDEF, "macro variable %s "
315				    "is not defined\n", yytext);
316			}
317
318			/*
319			 * For the moment, all current macro variables are of
320			 * type id_t (refer to dtrace_update() for details).
321			 */
322			(void) snprintf(s, sizeof (s), "%u", idp->di_id);
323			if ((yylval.l_str = strdup(s)) == NULL)
324				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
325
326			return (DT_TOK_STRING);
327		}
328
329<S0>"$"{RGX_IDENT} {
330			dt_ident_t *idp = dt_idhash_lookup(
331			    yypcb->pcb_hdl->dt_macros, yytext + 1);
332
333			if (idp == NULL) {
334				xyerror(D_MACRO_UNDEF, "macro variable %s "
335				    "is not defined\n", yytext);
336			}
337
338			/*
339			 * For the moment, all current macro variables are of
340			 * type id_t (refer to dtrace_update() for details).
341			 */
342			yylval.l_int = (intmax_t)(int)idp->di_id;
343			yyintprefix = 0;
344			yyintsuffix[0] = '\0';
345			yyintdecimal = 1;
346
347			return (DT_TOK_INT);
348		}
349
350<S0>{RGX_IDENT}	{
351			return (id_or_type(yytext));
352		}
353
354<S0>{RGX_AGG}	{
355			if ((yylval.l_str = strdup(yytext)) == NULL)
356				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
357			return (DT_TOK_AGG);
358		}
359
360<S0>"@"		{
361			if ((yylval.l_str = strdup("@_")) == NULL)
362				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
363			return (DT_TOK_AGG);
364		}
365
366<S0>{RGX_INT}	|
367<S2>{RGX_INT}	|
368<S3>{RGX_INT}	{
369			char *p;
370
371			errno = 0;
372			yylval.l_int = strtoull(yytext, &p, 0);
373			yyintprefix = 0;
374			(void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
375			yyintdecimal = yytext[0] != '0';
376
377			if (errno == ERANGE) {
378				xyerror(D_INT_OFLOW, "constant %s results in "
379				    "integer overflow\n", yytext);
380			}
381
382			if (*p != '\0' && strchr("uUlL", *p) == NULL) {
383				xyerror(D_INT_DIGIT, "constant %s contains "
384				    "invalid digit %c\n", yytext, *p);
385			}
386
387			if ((YYSTATE) != S3)
388				return (DT_TOK_INT);
389
390			yypragma = dt_node_link(yypragma,
391			    dt_node_int(yylval.l_int));
392		}
393
394<S0>{RGX_FP}	yyerror("floating-point constants are not permitted\n");
395
396<S0>\"{RGX_STR}$ |
397<S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
398
399<S0>\"{RGX_STR}\" |
400<S3>\"{RGX_STR}\" {
401			/*
402			 * Quoted string -- convert C escape sequences and
403			 * return the string as a token.
404			 */
405			yylval.l_str = strndup(yytext + 1, yyleng - 2);
406
407			if (yylval.l_str == NULL)
408				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
409
410			(void) stresc2chr(yylval.l_str);
411			if ((YYSTATE) != S3)
412				return (DT_TOK_STRING);
413
414			yypragma = dt_node_link(yypragma,
415			    dt_node_string(yylval.l_str));
416		}
417
418<S0>'{RGX_CHR}$	xyerror(D_CHR_NL, "newline encountered in character constant");
419
420<S0>'{RGX_CHR}'	{
421			char *s, *p, *q;
422			size_t nbytes;
423
424			/*
425			 * Character constant -- convert C escape sequences and
426			 * return the character as an integer immediate value.
427			 */
428			if (yyleng == 2)
429				xyerror(D_CHR_NULL, "empty character constant");
430
431			s = yytext + 1;
432			yytext[yyleng - 1] = '\0';
433			nbytes = stresc2chr(s);
434			yylval.l_int = 0;
435			yyintprefix = 0;
436			yyintsuffix[0] = '\0';
437			yyintdecimal = 1;
438
439			if (nbytes > sizeof (yylval.l_int)) {
440				xyerror(D_CHR_OFLOW, "character constant is "
441				    "too long");
442			}
443#if BYTE_ORDER == _LITTLE_ENDIAN
444			p = ((char *)&yylval.l_int) + nbytes - 1;
445			for (q = s; nbytes != 0; nbytes--)
446				*p-- = *q++;
447#else
448			bcopy(s, ((char *)&yylval.l_int) +
449			    sizeof (yylval.l_int) - nbytes, nbytes);
450#endif
451			return (DT_TOK_INT);
452		}
453
454<S0>"/*"	|
455<S2>"/*"	{
456			yypcb->pcb_cstate = (YYSTATE);
457			BEGIN(S1);
458		}
459
460<S0>{RGX_INTERP} |
461<S2>{RGX_INTERP} ;	/* discard any #! lines */
462
463<S0>{RGX_CTL}	|
464<S2>{RGX_CTL}	|
465<S4>{RGX_CTL}	{
466			assert(yypragma == NULL);
467			yypcb->pcb_cstate = (YYSTATE);
468			BEGIN(S3);
469		}
470
471<S4>.		;	/* discard */
472<S4>"\n"	;	/* discard */
473
474<S0>"/"		{
475			int c, tok;
476
477			/*
478			 * The use of "/" as the predicate delimiter and as the
479			 * integer division symbol requires special lookahead
480			 * to avoid a shift/reduce conflict in the D grammar.
481			 * We look ahead to the next non-whitespace character.
482			 * If we encounter EOF, ";", "{", or "/", then this "/"
483			 * closes the predicate and we return DT_TOK_EPRED.
484			 * If we encounter anything else, it's DT_TOK_DIV.
485			 */
486			while ((c = input()) != 0) {
487				if (strchr("\f\n\r\t\v ", c) == NULL)
488					break;
489			}
490
491			if (c == 0 || c == ';' || c == '{' || c == '/') {
492				if (yypcb->pcb_parens != 0) {
493					yyerror("closing ) expected in "
494					    "predicate before /\n");
495				}
496				if (yypcb->pcb_brackets != 0) {
497					yyerror("closing ] expected in "
498					    "predicate before /\n");
499				}
500				tok = DT_TOK_EPRED;
501			} else
502				tok = DT_TOK_DIV;
503
504			unput(c);
505			return (tok);
506		}
507
508<S0>"("		{
509			yypcb->pcb_parens++;
510			return (DT_TOK_LPAR);
511		}
512
513<S0>")"		{
514			if (--yypcb->pcb_parens < 0)
515				yyerror("extra ) in input stream\n");
516			return (DT_TOK_RPAR);
517		}
518
519<S0>"["		{
520			yypcb->pcb_brackets++;
521			return (DT_TOK_LBRAC);
522		}
523
524<S0>"]"		{
525			if (--yypcb->pcb_brackets < 0)
526				yyerror("extra ] in input stream\n");
527			return (DT_TOK_RBRAC);
528		}
529
530<S0>"{"		|
531<S2>"{"		{
532			yypcb->pcb_braces++;
533			return ('{');
534		}
535
536<S0>"}"		{
537			if (--yypcb->pcb_braces < 0)
538				yyerror("extra } in input stream\n");
539			return ('}');
540		}
541
542<S0>"|"		return (DT_TOK_BOR);
543<S0>"^"		return (DT_TOK_XOR);
544<S0>"&"		return (DT_TOK_BAND);
545<S0>"&&"	return (DT_TOK_LAND);
546<S0>"^^"	return (DT_TOK_LXOR);
547<S0>"||"	return (DT_TOK_LOR);
548<S0>"=="	return (DT_TOK_EQU);
549<S0>"!="	return (DT_TOK_NEQ);
550<S0>"<"		return (DT_TOK_LT);
551<S0>"<="	return (DT_TOK_LE);
552<S0>">"		return (DT_TOK_GT);
553<S0>">="	return (DT_TOK_GE);
554<S0>"<<"	return (DT_TOK_LSH);
555<S0>">>"	return (DT_TOK_RSH);
556<S0>"+"		return (DT_TOK_ADD);
557<S0>"-"		return (DT_TOK_SUB);
558<S0>"*"		return (DT_TOK_MUL);
559<S0>"%"		return (DT_TOK_MOD);
560<S0>"~"		return (DT_TOK_BNEG);
561<S0>"!"		return (DT_TOK_LNEG);
562<S0>"?"		return (DT_TOK_QUESTION);
563<S0>":"		return (DT_TOK_COLON);
564<S0>"."		return (DT_TOK_DOT);
565<S0>"->"	return (DT_TOK_PTR);
566<S0>"="		return (DT_TOK_ASGN);
567<S0>"+="	return (DT_TOK_ADD_EQ);
568<S0>"-="	return (DT_TOK_SUB_EQ);
569<S0>"*="	return (DT_TOK_MUL_EQ);
570<S0>"/="	return (DT_TOK_DIV_EQ);
571<S0>"%="	return (DT_TOK_MOD_EQ);
572<S0>"&="	return (DT_TOK_AND_EQ);
573<S0>"^="	return (DT_TOK_XOR_EQ);
574<S0>"|="	return (DT_TOK_OR_EQ);
575<S0>"<<="	return (DT_TOK_LSH_EQ);
576<S0>">>="	return (DT_TOK_RSH_EQ);
577<S0>"++"	return (DT_TOK_ADDADD);
578<S0>"--"	return (DT_TOK_SUBSUB);
579<S0>"..."	return (DT_TOK_ELLIPSIS);
580<S0>","		return (DT_TOK_COMMA);
581<S0>";"		return (';');
582<S0>{RGX_WS}	; /* discard */
583<S0>"\\"\n	; /* discard */
584<S0>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
585
586<S1>"/*"	yyerror("/* encountered inside a comment\n");
587<S1>"*/"	BEGIN(yypcb->pcb_cstate);
588<S1>.|\n	; /* discard */
589
590<S2>{RGX_PSPEC}	{
591			/*
592			 * S2 has an ambiguity because RGX_PSPEC includes '*'
593			 * as a glob character and '*' also can be DT_TOK_STAR.
594			 * Since lex always matches the longest token, this
595			 * rule can be matched by an input string like "int*",
596			 * which could begin a global variable declaration such
597			 * as "int*x;" or could begin a RGX_PSPEC with globbing
598			 * such as "int* { trace(timestamp); }".  If C_PSPEC is
599			 * not set, we must resolve the ambiguity in favor of
600			 * the type and perform lexer pushback if the fragment
601			 * before '*' or entire fragment matches a type name.
602			 * If C_PSPEC is set, we always return a PSPEC token.
603			 * If C_PSPEC is off, the user can avoid ambiguity by
604			 * including a ':' delimiter in the specifier, which
605			 * they should be doing anyway to specify the provider.
606			 */
607			if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
608			    strchr(yytext, ':') == NULL) {
609
610				char *p = strchr(yytext, '*');
611				char *q = yytext + yyleng - 1;
612
613				if (p != NULL && p > yytext)
614					*p = '\0'; /* prune yytext */
615
616				if (dt_type_lookup(yytext, NULL) == 0) {
617					yylval.l_str = strdup(yytext);
618
619					if (yylval.l_str == NULL) {
620						longjmp(yypcb->pcb_jmpbuf,
621						    EDT_NOMEM);
622					}
623
624					if (p != NULL && p > yytext) {
625						for (*p = '*'; q >= p; q--)
626							unput(*q);
627					}
628
629					yybegin(YYS_EXPR);
630					return (DT_TOK_TNAME);
631				}
632
633				if (p != NULL && p > yytext)
634					*p = '*'; /* restore yytext */
635			}
636
637			if ((yylval.l_str = strdup(yytext)) == NULL)
638				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
639
640			return (DT_TOK_PSPEC);
641		}
642
643<S2>"/"		return (DT_TOK_DIV);
644<S2>","		return (DT_TOK_COMMA);
645
646<S2>{RGX_WS}	; /* discard */
647<S2>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
648
649<S3>\n		{
650			dt_pragma(yypragma);
651			yypragma = NULL;
652			BEGIN(yypcb->pcb_cstate);
653		}
654
655<S3>[\f\t\v ]+	; /* discard */
656
657<S3>[^\f\n\t\v "]+ {
658			dt_node_t *dnp;
659
660			if ((yylval.l_str = strdup(yytext)) == NULL)
661				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
662
663			/*
664			 * We want to call dt_node_ident() here, but we can't
665			 * because it will expand inlined identifiers, which we
666			 * don't want to do from #pragma context in order to
667			 * support pragmas that apply to the ident itself.  We
668			 * call dt_node_string() and then reset dn_op instead.
669			 */
670			dnp = dt_node_string(yylval.l_str);
671			dnp->dn_kind = DT_NODE_IDENT;
672			dnp->dn_op = DT_TOK_IDENT;
673			yypragma = dt_node_link(yypragma, dnp);
674		}
675
676<S3>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
677
678%%
679
680/*
681 * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
682 * We use two main states for lexing because probe descriptions use a syntax
683 * that is incompatible with the normal D tokens (e.g. names can contain "-").
684 * yybegin also handles the job of switching between two lists of dt_nodes
685 * as we allocate persistent definitions, like inlines, and transient nodes
686 * that will be freed once we are done parsing the current program file.
687 */
688void
689yybegin(yystate_t state)
690{
691#ifdef	YYDEBUG
692	yydebug = _dtrace_debug;
693#endif
694	if (yypcb->pcb_yystate == state)
695		return; /* nothing to do if we're in the state already */
696
697	if (yypcb->pcb_yystate == YYS_DEFINE) {
698		yypcb->pcb_list = yypcb->pcb_hold;
699		yypcb->pcb_hold = NULL;
700	}
701
702	switch (state) {
703	case YYS_CLAUSE:
704		BEGIN(S2);
705		break;
706	case YYS_DEFINE:
707		assert(yypcb->pcb_hold == NULL);
708		yypcb->pcb_hold = yypcb->pcb_list;
709		yypcb->pcb_list = NULL;
710		/*FALLTHRU*/
711	case YYS_EXPR:
712		BEGIN(S0);
713		break;
714	case YYS_DONE:
715		break;
716	case YYS_CONTROL:
717		BEGIN(S4);
718		break;
719	default:
720		xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
721	}
722
723	yypcb->pcb_yystate = state;
724}
725
726void
727yyinit(dt_pcb_t *pcb)
728{
729	yypcb = pcb;
730	yylineno = 1;
731	yypragma = NULL;
732#if defined(sun)
733	yysptr = yysbuf;
734#endif
735}
736
737/*
738 * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
739 * token to the parser indicating either an identifier or a typedef name.
740 * User-defined global variables always take precedence over types, but we do
741 * use some heuristics because D programs can look at an ever-changing set of
742 * kernel types and also can implicitly instantiate variables by assignment,
743 * unlike in C.  The code here is ordered carefully as lookups are not cheap.
744 */
745static int
746id_or_type(const char *s)
747{
748	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
749	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
750	int c0, c1, ttok = DT_TOK_TNAME;
751	dt_ident_t *idp;
752
753	if ((s = yylval.l_str = strdup(s)) == NULL)
754		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
755
756	/*
757	 * If the lexeme is a global variable or likely identifier or *not* a
758	 * type_name, then it is an identifier token.
759	 */
760	if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
761	    dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
762	    dt_type_lookup(s, NULL) != 0)
763		return (DT_TOK_IDENT);
764
765	/*
766	 * If we're in the midst of parsing a declaration and a type_specifier
767	 * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
768	 * This semantic is necessary to permit valid ISO C code such as:
769	 *
770	 * typedef int foo;
771	 * struct s { foo foo; };
772	 *
773	 * without causing shift/reduce conflicts in the direct_declarator part
774	 * of the grammar.  The result is that we must check for conflicting
775	 * redeclarations of the same identifier as part of dt_node_decl().
776	 */
777	if (ddp != NULL && ddp->dd_name != NULL)
778		return (DT_TOK_IDENT);
779
780	/*
781	 * If the lexeme is a type name and we are not in a program clause,
782	 * then always interpret it as a type and return DT_TOK_TNAME.
783	 */
784	if ((YYSTATE) != S0)
785		return (DT_TOK_TNAME);
786
787	/*
788	 * If the lexeme matches a type name but is in a program clause, then
789	 * it could be a type or it could be an undefined variable.  Peek at
790	 * the next token to decide.  If we see ++, --, [, or =, we know there
791	 * might be an assignment that is trying to create a global variable,
792	 * so we optimistically return DT_TOK_IDENT.  There is no harm in being
793	 * wrong: a type_name followed by ++, --, [, or = is a syntax error.
794	 */
795	while ((c0 = input()) != 0) {
796		if (strchr("\f\n\r\t\v ", c0) == NULL)
797			break;
798	}
799
800	switch (c0) {
801	case '+':
802	case '-':
803		if ((c1 = input()) == c0)
804			ttok = DT_TOK_IDENT;
805		unput(c1);
806		break;
807
808	case '=':
809		if ((c1 = input()) != c0)
810			ttok = DT_TOK_IDENT;
811		unput(c1);
812		break;
813	case '[':
814		ttok = DT_TOK_IDENT;
815		break;
816	}
817
818	if (ttok == DT_TOK_IDENT) {
819		idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
820		    0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
821
822		if (idp == NULL)
823			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
824	}
825
826	unput(c0);
827	return (ttok);
828}
829
830#if defined(sun)
831static int
832input(void)
833{
834	int c;
835
836	if (yysptr > yysbuf)
837		c = *--yysptr;
838	else if (yypcb->pcb_fileptr != NULL)
839		c = fgetc(yypcb->pcb_fileptr);
840	else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
841		c = *(unsigned char *)(yypcb->pcb_strptr++);
842	else
843		c = EOF;
844
845	if (c == '\n')
846		yylineno++;
847
848	if (c != EOF)
849		return (c);
850
851	if ((YYSTATE) == S1)
852		yyerror("end-of-file encountered before matching */\n");
853
854	if ((YYSTATE) == S3)
855		yyerror("end-of-file encountered before end of control line\n");
856
857	if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
858		longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
859
860	return (0); /* EOF */
861}
862
863static void
864unput(int c)
865{
866	if (c == '\n')
867		yylineno--;
868
869	*yysptr++ = c;
870	yytchar = c;
871}
872#endif
873