dt_lex.l revision 2
2N/A%{
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <assert.h>
2N/A#include <ctype.h>
2N/A#include <errno.h>
2N/A
2N/A#include <dt_impl.h>
2N/A#include <dt_grammar.h>
2N/A#include <dt_parser.h>
2N/A#include <dt_string.h>
2N/A
2N/A/*
2N/A * We need to undefine lex's input and unput macros so that references to these
2N/A * call the functions provided at the end of this source file.
2N/A */
2N/A#undef input
2N/A#undef unput
2N/A
2N/Astatic int id_or_type(const char *);
2N/Astatic int input(void);
2N/Astatic void unput(int);
2N/A
2N/A/*
2N/A * We first define a set of labeled states for use in the D lexer and then a
2N/A * set of regular expressions to simplify things below. The lexer states are:
2N/A *
2N/A * S0 - D program clause and expression lexing
2N/A * S1 - D comments (i.e. skip everything until end of comment)
2N/A * S2 - D program outer scope (probe specifiers and declarations)
2N/A * S3 - D control line parsing (i.e. after ^# is seen but before \n)
2N/A * S4 - D control line scan (locate control directives only and invoke S3)
2N/A */
2N/A%}
2N/A
2N/A%e 1500 /* maximum nodes */
2N/A%p 3700 /* maximum positions */
2N/A%n 600 /* maximum states */
2N/A
2N/A%s S0 S1 S2 S3 S4
2N/A
2N/ARGX_AGG "@"[a-zA-Z_][0-9a-zA-Z_]*
2N/ARGX_PSPEC [-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
2N/ARGX_IDENT [a-zA-Z_`][0-9a-zA-Z_`]*
2N/ARGX_INT ([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
2N/ARGX_FP ([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
2N/ARGX_WS [\f\n\r\t\v ]
2N/ARGX_STR ([^"\\\n]|\\[^"\n]|\\\")*
2N/ARGX_CHR ([^'\\\n]|\\[^'\n]|\\')*
2N/ARGX_INTERP ^[\f\t\v ]*#!.*
2N/ARGX_CTL ^[\f\t\v ]*#
2N/A
2N/A%%
2N/A
2N/A%{
2N/A
2N/A/*
2N/A * We insert a special prologue into yylex() itself: if the pcb contains a
2N/A * context token, we return that prior to running the normal lexer. This
2N/A * allows libdtrace to force yacc into one of our three parsing contexts: D
2N/A * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
2N/A * Once the token is returned, we clear it so this only happens once.
2N/A */
2N/Aif (yypcb->pcb_token != 0) {
2N/A int tok = yypcb->pcb_token;
2N/A yypcb->pcb_token = 0;
2N/A return (tok);
2N/A}
2N/A
2N/A%}
2N/A
2N/A<S0>auto return (DT_KEY_AUTO);
2N/A<S0>break return (DT_KEY_BREAK);
2N/A<S0>case return (DT_KEY_CASE);
2N/A<S0>char return (DT_KEY_CHAR);
2N/A<S0>const return (DT_KEY_CONST);
2N/A<S0>continue return (DT_KEY_CONTINUE);
2N/A<S0>counter return (DT_KEY_COUNTER);
2N/A<S0>default return (DT_KEY_DEFAULT);
2N/A<S0>do return (DT_KEY_DO);
2N/A<S0>double return (DT_KEY_DOUBLE);
2N/A<S0>else return (DT_KEY_ELSE);
2N/A<S0>enum return (DT_KEY_ENUM);
2N/A<S0>extern return (DT_KEY_EXTERN);
2N/A<S0>float return (DT_KEY_FLOAT);
2N/A<S0>for return (DT_KEY_FOR);
2N/A<S0>goto return (DT_KEY_GOTO);
2N/A<S0>if return (DT_KEY_IF);
2N/A<S0>import return (DT_KEY_IMPORT);
2N/A<S0>inline return (DT_KEY_INLINE);
2N/A<S0>int return (DT_KEY_INT);
2N/A<S0>long return (DT_KEY_LONG);
2N/A<S0>offsetof return (DT_TOK_OFFSETOF);
2N/A<S0>probe return (DT_KEY_PROBE);
2N/A<S0>provider return (DT_KEY_PROVIDER);
2N/A<S0>register return (DT_KEY_REGISTER);
2N/A<S0>restrict return (DT_KEY_RESTRICT);
2N/A<S0>return return (DT_KEY_RETURN);
2N/A<S0>self return (DT_KEY_SELF);
2N/A<S0>short return (DT_KEY_SHORT);
2N/A<S0>signed return (DT_KEY_SIGNED);
2N/A<S0>sizeof return (DT_TOK_SIZEOF);
2N/A<S0>static return (DT_KEY_STATIC);
2N/A<S0>string return (DT_KEY_STRING);
2N/A<S0>stringof return (DT_TOK_STRINGOF);
2N/A<S0>struct return (DT_KEY_STRUCT);
2N/A<S0>switch return (DT_KEY_SWITCH);
2N/A<S0>this return (DT_KEY_THIS);
2N/A<S0>translator return (DT_KEY_XLATOR);
2N/A<S0>typedef return (DT_KEY_TYPEDEF);
2N/A<S0>union return (DT_KEY_UNION);
2N/A<S0>unsigned return (DT_KEY_UNSIGNED);
2N/A<S0>void return (DT_KEY_VOID);
2N/A<S0>volatile return (DT_KEY_VOLATILE);
2N/A<S0>while return (DT_KEY_WHILE);
2N/A<S0>xlate return (DT_TOK_XLATE);
2N/A
2N/A<S2>auto { yybegin(YYS_EXPR); return (DT_KEY_AUTO); }
2N/A<S2>char { yybegin(YYS_EXPR); return (DT_KEY_CHAR); }
2N/A<S2>const { yybegin(YYS_EXPR); return (DT_KEY_CONST); }
2N/A<S2>counter { yybegin(YYS_DEFINE); return (DT_KEY_COUNTER); }
2N/A<S2>double { yybegin(YYS_EXPR); return (DT_KEY_DOUBLE); }
2N/A<S2>enum { yybegin(YYS_EXPR); return (DT_KEY_ENUM); }
2N/A<S2>extern { yybegin(YYS_EXPR); return (DT_KEY_EXTERN); }
2N/A<S2>float { yybegin(YYS_EXPR); return (DT_KEY_FLOAT); }
2N/A<S2>import { yybegin(YYS_EXPR); return (DT_KEY_IMPORT); }
2N/A<S2>inline { yybegin(YYS_DEFINE); return (DT_KEY_INLINE); }
2N/A<S2>int { yybegin(YYS_EXPR); return (DT_KEY_INT); }
2N/A<S2>long { yybegin(YYS_EXPR); return (DT_KEY_LONG); }
2N/A<S2>provider { yybegin(YYS_DEFINE); return (DT_KEY_PROVIDER); }
2N/A<S2>register { yybegin(YYS_EXPR); return (DT_KEY_REGISTER); }
2N/A<S2>restrict { yybegin(YYS_EXPR); return (DT_KEY_RESTRICT); }
2N/A<S2>self { yybegin(YYS_EXPR); return (DT_KEY_SELF); }
2N/A<S2>short { yybegin(YYS_EXPR); return (DT_KEY_SHORT); }
2N/A<S2>signed { yybegin(YYS_EXPR); return (DT_KEY_SIGNED); }
2N/A<S2>static { yybegin(YYS_EXPR); return (DT_KEY_STATIC); }
2N/A<S2>string { yybegin(YYS_EXPR); return (DT_KEY_STRING); }
2N/A<S2>struct { yybegin(YYS_EXPR); return (DT_KEY_STRUCT); }
2N/A<S2>this { yybegin(YYS_EXPR); return (DT_KEY_THIS); }
2N/A<S2>translator { yybegin(YYS_DEFINE); return (DT_KEY_XLATOR); }
2N/A<S2>typedef { yybegin(YYS_EXPR); return (DT_KEY_TYPEDEF); }
2N/A<S2>union { yybegin(YYS_EXPR); return (DT_KEY_UNION); }
2N/A<S2>unsigned { yybegin(YYS_EXPR); return (DT_KEY_UNSIGNED); }
2N/A<S2>void { yybegin(YYS_EXPR); return (DT_KEY_VOID); }
2N/A<S2>volatile { yybegin(YYS_EXPR); return (DT_KEY_VOLATILE); }
2N/A
2N/A<S0>"$$"[0-9]+ {
2N/A int i = atoi(yytext + 2);
2N/A char *v = "";
2N/A
2N/A /*
2N/A * A macro argument reference substitutes the text of
2N/A * an argument in place of the current token. When we
2N/A * see $$<d> we fetch the saved string from pcb_sargv
2N/A * (or use the default argument if the option has been
2N/A * set and the argument hasn't been specified) and
2N/A * return a token corresponding to this string.
2N/A */
2N/A if (i < 0 || (i >= yypcb->pcb_sargc &&
2N/A !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2N/A xyerror(D_MACRO_UNDEF, "macro argument %s is "
2N/A "not defined\n", yytext);
2N/A }
2N/A
2N/A if (i < yypcb->pcb_sargc) {
2N/A v = yypcb->pcb_sargv[i]; /* get val from pcb */
2N/A yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2N/A }
2N/A
2N/A if ((yylval.l_str = strdup(v)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A (void) stresc2chr(yylval.l_str);
2N/A return (DT_TOK_STRING);
2N/A }
2N/A
2N/A<S0>"$"[0-9]+ {
2N/A int i = atoi(yytext + 1);
2N/A char *p, *v = "0";
2N/A
2N/A /*
2N/A * A macro argument reference substitutes the text of
2N/A * one identifier or integer pattern for another. When
2N/A * we see $<d> we fetch the saved string from pcb_sargv
2N/A * (or use the default argument if the option has been
2N/A * set and the argument hasn't been specified) and
2N/A * return a token corresponding to this string.
2N/A */
2N/A if (i < 0 || (i >= yypcb->pcb_sargc &&
2N/A !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
2N/A xyerror(D_MACRO_UNDEF, "macro argument %s is "
2N/A "not defined\n", yytext);
2N/A }
2N/A
2N/A if (i < yypcb->pcb_sargc) {
2N/A v = yypcb->pcb_sargv[i]; /* get val from pcb */
2N/A yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
2N/A }
2N/A
2N/A /*
2N/A * If the macro text is not a valid integer or ident,
2N/A * then we treat it as a string. The string may be
2N/A * optionally enclosed in quotes, which we strip.
2N/A */
2N/A if (strbadidnum(v)) {
2N/A size_t len = strlen(v);
2N/A
2N/A if (len != 1 && *v == '"' && v[len - 1] == '"')
2N/A yylval.l_str = strndup(v + 1, len - 2);
2N/A else
2N/A yylval.l_str = strndup(v, len);
2N/A
2N/A if (yylval.l_str == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A (void) stresc2chr(yylval.l_str);
2N/A return (DT_TOK_STRING);
2N/A }
2N/A
2N/A /*
2N/A * If the macro text is not a string an begins with a
2N/A * digit or a +/- sign, process it as an integer token.
2N/A */
2N/A if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
2N/A if (isdigit(v[0]))
2N/A yyintprefix = 0;
2N/A else
2N/A yyintprefix = *v++;
2N/A
2N/A errno = 0;
2N/A yylval.l_int = strtoull(v, &p, 0);
2N/A (void) strncpy(yyintsuffix, p,
2N/A sizeof (yyintsuffix));
2N/A yyintdecimal = *v != '0';
2N/A
2N/A if (errno == ERANGE) {
2N/A xyerror(D_MACRO_OFLOW, "macro argument"
2N/A " %s constant %s results in integer"
2N/A " overflow\n", yytext, v);
2N/A }
2N/A
2N/A return (DT_TOK_INT);
2N/A }
2N/A
2N/A return (id_or_type(v));
2N/A }
2N/A
2N/A<S0>"$$"{RGX_IDENT} {
2N/A dt_ident_t *idp = dt_idhash_lookup(
2N/A yypcb->pcb_hdl->dt_macros, yytext + 2);
2N/A
2N/A char s[16]; /* enough for UINT_MAX + \0 */
2N/A
2N/A if (idp == NULL) {
2N/A xyerror(D_MACRO_UNDEF, "macro variable %s "
2N/A "is not defined\n", yytext);
2N/A }
2N/A
2N/A /*
2N/A * For the moment, all current macro variables are of
2N/A * type id_t (refer to dtrace_update() for details).
2N/A */
2N/A (void) snprintf(s, sizeof (s), "%u", idp->di_id);
2N/A if ((yylval.l_str = strdup(s)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A return (DT_TOK_STRING);
2N/A }
2N/A
2N/A<S0>"$"{RGX_IDENT} {
2N/A dt_ident_t *idp = dt_idhash_lookup(
2N/A yypcb->pcb_hdl->dt_macros, yytext + 1);
2N/A
2N/A if (idp == NULL) {
2N/A xyerror(D_MACRO_UNDEF, "macro variable %s "
2N/A "is not defined\n", yytext);
2N/A }
2N/A
2N/A /*
2N/A * For the moment, all current macro variables are of
2N/A * type id_t (refer to dtrace_update() for details).
2N/A */
2N/A yylval.l_int = (intmax_t)(int)idp->di_id;
2N/A yyintprefix = 0;
2N/A yyintsuffix[0] = '\0';
2N/A yyintdecimal = 1;
2N/A
2N/A return (DT_TOK_INT);
2N/A }
2N/A
2N/A<S0>{RGX_IDENT} {
2N/A return (id_or_type(yytext));
2N/A }
2N/A
2N/A<S0>{RGX_AGG} {
2N/A if ((yylval.l_str = strdup(yytext)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A return (DT_TOK_AGG);
2N/A }
2N/A
2N/A<S0>"@" {
2N/A if ((yylval.l_str = strdup("@_")) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A return (DT_TOK_AGG);
2N/A }
2N/A
2N/A<S0>{RGX_INT} |
2N/A<S2>{RGX_INT} |
2N/A<S3>{RGX_INT} {
2N/A char *p;
2N/A
2N/A errno = 0;
2N/A yylval.l_int = strtoull(yytext, &p, 0);
2N/A yyintprefix = 0;
2N/A (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
2N/A yyintdecimal = yytext[0] != '0';
2N/A
2N/A if (errno == ERANGE) {
2N/A xyerror(D_INT_OFLOW, "constant %s results in "
2N/A "integer overflow\n", yytext);
2N/A }
2N/A
2N/A if (*p != '\0' && strchr("uUlL", *p) == NULL) {
2N/A xyerror(D_INT_DIGIT, "constant %s contains "
2N/A "invalid digit %c\n", yytext, *p);
2N/A }
2N/A
2N/A if ((YYSTATE) != S3)
2N/A return (DT_TOK_INT);
2N/A
2N/A yypragma = dt_node_link(yypragma,
2N/A dt_node_int(yylval.l_int));
2N/A }
2N/A
2N/A<S0>{RGX_FP} yyerror("floating-point constants are not permitted\n");
2N/A
2N/A<S0>\"{RGX_STR}$ |
2N/A<S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
2N/A
2N/A<S0>\"{RGX_STR}\" |
2N/A<S3>\"{RGX_STR}\" {
2N/A /*
2N/A * Quoted string -- convert C escape sequences and
2N/A * return the string as a token.
2N/A */
2N/A yylval.l_str = strndup(yytext + 1, yyleng - 2);
2N/A
2N/A if (yylval.l_str == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A (void) stresc2chr(yylval.l_str);
2N/A if ((YYSTATE) != S3)
2N/A return (DT_TOK_STRING);
2N/A
2N/A yypragma = dt_node_link(yypragma,
2N/A dt_node_string(yylval.l_str));
2N/A }
2N/A
2N/A<S0>'{RGX_CHR}$ xyerror(D_CHR_NL, "newline encountered in character constant");
2N/A
2N/A<S0>'{RGX_CHR}' {
2N/A char *s, *p, *q;
2N/A size_t nbytes;
2N/A
2N/A /*
2N/A * Character constant -- convert C escape sequences and
2N/A * return the character as an integer immediate value.
2N/A */
2N/A if (yyleng == 2)
2N/A xyerror(D_CHR_NULL, "empty character constant");
2N/A
2N/A s = yytext + 1;
2N/A yytext[yyleng - 1] = '\0';
2N/A nbytes = stresc2chr(s);
2N/A yylval.l_int = 0;
2N/A yyintprefix = 0;
2N/A yyintsuffix[0] = '\0';
2N/A yyintdecimal = 1;
2N/A
2N/A if (nbytes > sizeof (yylval.l_int)) {
2N/A xyerror(D_CHR_OFLOW, "character constant is "
2N/A "too long");
2N/A }
2N/A#ifdef _LITTLE_ENDIAN
2N/A p = ((char *)&yylval.l_int) + nbytes - 1;
2N/A for (q = s; nbytes != 0; nbytes--)
2N/A *p-- = *q++;
2N/A#else
2N/A bcopy(s, ((char *)&yylval.l_int) +
2N/A sizeof (yylval.l_int) - nbytes, nbytes);
2N/A#endif
2N/A return (DT_TOK_INT);
2N/A }
2N/A
2N/A<S0>"/*" |
2N/A<S2>"/*" {
2N/A yypcb->pcb_cstate = (YYSTATE);
2N/A BEGIN(S1);
2N/A }
2N/A
2N/A<S0>{RGX_INTERP} |
2N/A<S2>{RGX_INTERP} ; /* discard any #! lines */
2N/A
2N/A<S0>{RGX_CTL} |
2N/A<S2>{RGX_CTL} |
2N/A<S4>{RGX_CTL} {
2N/A assert(yypragma == NULL);
2N/A yypcb->pcb_cstate = (YYSTATE);
2N/A BEGIN(S3);
2N/A }
2N/A
2N/A<S4>. ; /* discard */
2N/A<S4>"\n" ; /* discard */
2N/A
2N/A<S0>"/" {
2N/A int c, tok;
2N/A
2N/A /*
2N/A * The use of "/" as the predicate delimiter and as the
2N/A * integer division symbol requires special lookahead
2N/A * to avoid a shift/reduce conflict in the D grammar.
2N/A * We look ahead to the next non-whitespace character.
2N/A * If we encounter EOF, ";", "{", or "/", then this "/"
2N/A * closes the predicate and we return DT_TOK_EPRED.
2N/A * If we encounter anything else, it's DT_TOK_DIV.
2N/A */
2N/A while ((c = input()) != 0) {
2N/A if (strchr("\f\n\r\t\v ", c) == NULL)
2N/A break;
2N/A }
2N/A
2N/A if (c == 0 || c == ';' || c == '{' || c == '/') {
2N/A if (yypcb->pcb_parens != 0) {
2N/A yyerror("closing ) expected in "
2N/A "predicate before /\n");
2N/A }
2N/A if (yypcb->pcb_brackets != 0) {
2N/A yyerror("closing ] expected in "
2N/A "predicate before /\n");
2N/A }
2N/A tok = DT_TOK_EPRED;
2N/A } else
2N/A tok = DT_TOK_DIV;
2N/A
2N/A unput(c);
2N/A return (tok);
2N/A }
2N/A
2N/A<S0>"(" {
2N/A yypcb->pcb_parens++;
2N/A return (DT_TOK_LPAR);
2N/A }
2N/A
2N/A<S0>")" {
2N/A if (--yypcb->pcb_parens < 0)
2N/A yyerror("extra ) in input stream\n");
2N/A return (DT_TOK_RPAR);
2N/A }
2N/A
2N/A<S0>"[" {
2N/A yypcb->pcb_brackets++;
2N/A return (DT_TOK_LBRAC);
2N/A }
2N/A
2N/A<S0>"]" {
2N/A if (--yypcb->pcb_brackets < 0)
2N/A yyerror("extra ] in input stream\n");
2N/A return (DT_TOK_RBRAC);
2N/A }
2N/A
2N/A<S0>"{" |
2N/A<S2>"{" {
2N/A yypcb->pcb_braces++;
2N/A return ('{');
2N/A }
2N/A
2N/A<S0>"}" {
2N/A if (--yypcb->pcb_braces < 0)
2N/A yyerror("extra } in input stream\n");
2N/A return ('}');
2N/A }
2N/A
2N/A<S0>"|" return (DT_TOK_BOR);
2N/A<S0>"^" return (DT_TOK_XOR);
2N/A<S0>"&" return (DT_TOK_BAND);
2N/A<S0>"&&" return (DT_TOK_LAND);
2N/A<S0>"^^" return (DT_TOK_LXOR);
2N/A<S0>"||" return (DT_TOK_LOR);
2N/A<S0>"==" return (DT_TOK_EQU);
2N/A<S0>"!=" return (DT_TOK_NEQ);
2N/A<S0>"<" return (DT_TOK_LT);
2N/A<S0>"<=" return (DT_TOK_LE);
2N/A<S0>">" return (DT_TOK_GT);
2N/A<S0>">=" return (DT_TOK_GE);
2N/A<S0>"<<" return (DT_TOK_LSH);
2N/A<S0>">>" return (DT_TOK_RSH);
2N/A<S0>"+" return (DT_TOK_ADD);
2N/A<S0>"-" return (DT_TOK_SUB);
2N/A<S0>"*" return (DT_TOK_MUL);
2N/A<S0>"%" return (DT_TOK_MOD);
2N/A<S0>"~" return (DT_TOK_BNEG);
2N/A<S0>"!" return (DT_TOK_LNEG);
2N/A<S0>"?" return (DT_TOK_QUESTION);
2N/A<S0>":" return (DT_TOK_COLON);
2N/A<S0>"." return (DT_TOK_DOT);
2N/A<S0>"->" return (DT_TOK_PTR);
2N/A<S0>"=" return (DT_TOK_ASGN);
2N/A<S0>"+=" return (DT_TOK_ADD_EQ);
2N/A<S0>"-=" return (DT_TOK_SUB_EQ);
2N/A<S0>"*=" return (DT_TOK_MUL_EQ);
2N/A<S0>"/=" return (DT_TOK_DIV_EQ);
2N/A<S0>"%=" return (DT_TOK_MOD_EQ);
2N/A<S0>"&=" return (DT_TOK_AND_EQ);
2N/A<S0>"^=" return (DT_TOK_XOR_EQ);
2N/A<S0>"|=" return (DT_TOK_OR_EQ);
2N/A<S0>"<<=" return (DT_TOK_LSH_EQ);
2N/A<S0>">>=" return (DT_TOK_RSH_EQ);
2N/A<S0>"++" return (DT_TOK_ADDADD);
2N/A<S0>"--" return (DT_TOK_SUBSUB);
2N/A<S0>"..." return (DT_TOK_ELLIPSIS);
2N/A<S0>"," return (DT_TOK_COMMA);
2N/A<S0>";" return (';');
2N/A<S0>{RGX_WS} ; /* discard */
2N/A<S0>"\\"\n ; /* discard */
2N/A<S0>. yyerror("syntax error near \"%c\"\n", yytext[0]);
2N/A
2N/A<S1>"/*" yyerror("/* encountered inside a comment\n");
2N/A<S1>"*/" BEGIN(yypcb->pcb_cstate);
2N/A<S1>.|\n ; /* discard */
2N/A
2N/A<S2>{RGX_PSPEC} {
2N/A /*
2N/A * S2 has an ambiguity because RGX_PSPEC includes '*'
2N/A * as a glob character and '*' also can be DT_TOK_STAR.
2N/A * Since lex always matches the longest token, this
2N/A * rule can be matched by an input string like "int*",
2N/A * which could begin a global variable declaration such
2N/A * as "int*x;" or could begin a RGX_PSPEC with globbing
2N/A * such as "int* { trace(timestamp); }". If C_PSPEC is
2N/A * not set, we must resolve the ambiguity in favor of
2N/A * the type and perform lexer pushback if the fragment
2N/A * before '*' or entire fragment matches a type name.
2N/A * If C_PSPEC is set, we always return a PSPEC token.
2N/A * If C_PSPEC is off, the user can avoid ambiguity by
2N/A * including a ':' delimiter in the specifier, which
2N/A * they should be doing anyway to specify the provider.
2N/A */
2N/A if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
2N/A strchr(yytext, ':') == NULL) {
2N/A
2N/A char *p = strchr(yytext, '*');
2N/A char *q = yytext + yyleng - 1;
2N/A
2N/A if (p != NULL && p > yytext)
2N/A *p = '\0'; /* prune yytext */
2N/A
2N/A if (dt_type_lookup(yytext, NULL) == 0) {
2N/A yylval.l_str = strdup(yytext);
2N/A
2N/A if (yylval.l_str == NULL) {
2N/A longjmp(yypcb->pcb_jmpbuf,
2N/A EDT_NOMEM);
2N/A }
2N/A
2N/A if (p != NULL && p > yytext) {
2N/A for (*p = '*'; q >= p; q--)
2N/A unput(*q);
2N/A }
2N/A
2N/A yybegin(YYS_EXPR);
2N/A return (DT_TOK_TNAME);
2N/A }
2N/A
2N/A if (p != NULL && p > yytext)
2N/A *p = '*'; /* restore yytext */
2N/A }
2N/A
2N/A if ((yylval.l_str = strdup(yytext)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A return (DT_TOK_PSPEC);
2N/A }
2N/A
2N/A<S2>"/" return (DT_TOK_DIV);
2N/A<S2>"," return (DT_TOK_COMMA);
2N/A
2N/A<S2>{RGX_WS} ; /* discard */
2N/A<S2>. yyerror("syntax error near \"%c\"\n", yytext[0]);
2N/A
2N/A<S3>\n {
2N/A dt_pragma(yypragma);
2N/A yypragma = NULL;
2N/A BEGIN(yypcb->pcb_cstate);
2N/A }
2N/A
2N/A<S3>[\f\t\v ]+ ; /* discard */
2N/A
2N/A<S3>[^\f\n\t\v "]+ {
2N/A dt_node_t *dnp;
2N/A
2N/A if ((yylval.l_str = strdup(yytext)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A /*
2N/A * We want to call dt_node_ident() here, but we can't
2N/A * because it will expand inlined identifiers, which we
2N/A * don't want to do from #pragma context in order to
2N/A * support pragmas that apply to the ident itself. We
2N/A * call dt_node_string() and then reset dn_op instead.
2N/A */
2N/A dnp = dt_node_string(yylval.l_str);
2N/A dnp->dn_kind = DT_NODE_IDENT;
2N/A dnp->dn_op = DT_TOK_IDENT;
2N/A yypragma = dt_node_link(yypragma, dnp);
2N/A }
2N/A
2N/A<S3>. yyerror("syntax error near \"%c\"\n", yytext[0]);
2N/A
2N/A%%
2N/A
2N/A/*
2N/A * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
2N/A * We use two main states for lexing because probe descriptions use a syntax
2N/A * that is incompatible with the normal D tokens (e.g. names can contain "-").
2N/A * yybegin also handles the job of switching between two lists of dt_nodes
2N/A * as we allocate persistent definitions, like inlines, and transient nodes
2N/A * that will be freed once we are done parsing the current program file.
2N/A */
2N/Avoid
2N/Ayybegin(yystate_t state)
2N/A{
2N/A#ifdef YYDEBUG
2N/A yydebug = _dtrace_debug;
2N/A#endif
2N/A if (yypcb->pcb_yystate == state)
2N/A return; /* nothing to do if we're in the state already */
2N/A
2N/A if (yypcb->pcb_yystate == YYS_DEFINE) {
2N/A yypcb->pcb_list = yypcb->pcb_hold;
2N/A yypcb->pcb_hold = NULL;
2N/A }
2N/A
2N/A switch (state) {
2N/A case YYS_CLAUSE:
2N/A BEGIN(S2);
2N/A break;
2N/A case YYS_DEFINE:
2N/A assert(yypcb->pcb_hold == NULL);
2N/A yypcb->pcb_hold = yypcb->pcb_list;
2N/A yypcb->pcb_list = NULL;
2N/A /*FALLTHRU*/
2N/A case YYS_EXPR:
2N/A BEGIN(S0);
2N/A break;
2N/A case YYS_DONE:
2N/A break;
2N/A case YYS_CONTROL:
2N/A BEGIN(S4);
2N/A break;
2N/A default:
2N/A xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
2N/A }
2N/A
2N/A yypcb->pcb_yystate = state;
2N/A}
2N/A
2N/Avoid
2N/Ayyinit(dt_pcb_t *pcb)
2N/A{
2N/A yypcb = pcb;
2N/A yylineno = 1;
2N/A yypragma = NULL;
2N/A yysptr = yysbuf;
2N/A}
2N/A
2N/A/*
2N/A * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
2N/A * token to the parser indicating either an identifier or a typedef name.
2N/A * User-defined global variables always take precedence over types, but we do
2N/A * use some heuristics because D programs can look at an ever-changing set of
2N/A * kernel types and also can implicitly instantiate variables by assignment,
2N/A * unlike in C. The code here is ordered carefully as lookups are not cheap.
2N/A */
2N/Astatic int
2N/Aid_or_type(const char *s)
2N/A{
2N/A dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2N/A dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
2N/A int c0, c1, ttok = DT_TOK_TNAME;
2N/A dt_ident_t *idp;
2N/A
2N/A if ((s = yylval.l_str = strdup(s)) == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A
2N/A /*
2N/A * If the lexeme is a global variable or likely identifier or *not* a
2N/A * type_name, then it is an identifier token.
2N/A */
2N/A if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
2N/A dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
2N/A dt_type_lookup(s, NULL) != 0)
2N/A return (DT_TOK_IDENT);
2N/A
2N/A /*
2N/A * If we're in the midst of parsing a declaration and a type_specifier
2N/A * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
2N/A * This semantic is necessary to permit valid ISO C code such as:
2N/A *
2N/A * typedef int foo;
2N/A * struct s { foo foo; };
2N/A *
2N/A * without causing shift/reduce conflicts in the direct_declarator part
2N/A * of the grammar. The result is that we must check for conflicting
2N/A * redeclarations of the same identifier as part of dt_node_decl().
2N/A */
2N/A if (ddp != NULL && ddp->dd_name != NULL)
2N/A return (DT_TOK_IDENT);
2N/A
2N/A /*
2N/A * If the lexeme is a type name and we are not in a program clause,
2N/A * then always interpret it as a type and return DT_TOK_TNAME.
2N/A */
2N/A if ((YYSTATE) != S0)
2N/A return (DT_TOK_TNAME);
2N/A
2N/A /*
2N/A * If the lexeme matches a type name but is in a program clause, then
2N/A * it could be a type or it could be an undefined variable. Peek at
2N/A * the next token to decide. If we see ++, --, [, or =, we know there
2N/A * might be an assignment that is trying to create a global variable,
2N/A * so we optimistically return DT_TOK_IDENT. There is no harm in being
2N/A * wrong: a type_name followed by ++, --, [, or = is a syntax error.
2N/A */
2N/A while ((c0 = input()) != 0) {
2N/A if (strchr("\f\n\r\t\v ", c0) == NULL)
2N/A break;
2N/A }
2N/A
2N/A switch (c0) {
2N/A case '+':
2N/A case '-':
2N/A if ((c1 = input()) == c0)
2N/A ttok = DT_TOK_IDENT;
2N/A unput(c1);
2N/A break;
2N/A
2N/A case '=':
2N/A if ((c1 = input()) != c0)
2N/A ttok = DT_TOK_IDENT;
2N/A unput(c1);
2N/A break;
2N/A case '[':
2N/A ttok = DT_TOK_IDENT;
2N/A break;
2N/A }
2N/A
2N/A if (ttok == DT_TOK_IDENT) {
2N/A idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
2N/A 0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2N/A
2N/A if (idp == NULL)
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2N/A }
2N/A
2N/A unput(c0);
2N/A return (ttok);
2N/A}
2N/A
2N/Astatic int
2N/Ainput(void)
2N/A{
2N/A int c;
2N/A
2N/A if (yysptr > yysbuf)
2N/A c = *--yysptr;
2N/A else if (yypcb->pcb_fileptr != NULL)
2N/A c = fgetc(yypcb->pcb_fileptr);
2N/A else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
2N/A c = *(unsigned char *)(yypcb->pcb_strptr++);
2N/A else
2N/A c = EOF;
2N/A
2N/A if (c == '\n')
2N/A yylineno++;
2N/A
2N/A if (c != EOF)
2N/A return (c);
2N/A
2N/A if ((YYSTATE) == S1)
2N/A yyerror("end-of-file encountered before matching */\n");
2N/A
2N/A if ((YYSTATE) == S3)
2N/A yyerror("end-of-file encountered before end of control line\n");
2N/A
2N/A if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
2N/A longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
2N/A
2N/A return (0); /* EOF */
2N/A}
2N/A
2N/Astatic void
2N/Aunput(int c)
2N/A{
2N/A if (c == '\n')
2N/A yylineno--;
2N/A
2N/A *yysptr++ = c;
2N/A yytchar = c;
2N/A}