dt_lex.l revision 2
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 * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 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 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 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 * 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 * 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%e
1500 /* maximum nodes */ 2N/A%p
3700 /* maximum positions */ 2N/A%n
600 /* maximum states */ 2N/ARGX_FP ([0-
9]+(
"."?)[0-
9]*|
"."[0-
9]+)((e|E)(
"+"|-)?[0-
9]+)?[
fFlL]?
2N/ARGX_CHR ([^'\\\n]|\\[^'\n]|\\')* 2N/ARGX_INTERP ^[\f\t\v ]*#!.* 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/Aif (yypcb->pcb_token != 0) { 2N/A int tok = yypcb->pcb_token; 2N/A yypcb->pcb_token = 0; 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<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 int i = atoi(yytext + 2); 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 if (i < 0 || (i >= yypcb->pcb_sargc && 2N/A !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 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 if ((yylval.l_str = strdup(v)) == NULL) 2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2N/A (void) stresc2chr(yylval.l_str); 2N/A return (DT_TOK_STRING); 2N/A int i = atoi(yytext + 1); 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 if (i < 0 || (i >= yypcb->pcb_sargc && 2N/A !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 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 * 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 if (strbadidnum(v)) { 2N/A size_t len = strlen(v); 2N/A if (len != 1 && *v == '"' && v[len - 1] == '"') 2N/A yylval.l_str = strndup(v + 1, len - 2); 2N/A yylval.l_str = strndup(v, len); 2N/A if (yylval.l_str == NULL) 2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2N/A (void) stresc2chr(yylval.l_str); 2N/A return (DT_TOK_STRING); 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 if (isdigit(v[0]) || v[0] == '-' || v[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 if (errno == ERANGE) { 2N/A return (DT_TOK_INT); 2N/A return (id_or_type(v)); 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 char s[16]; /* enough for UINT_MAX + \0 */ 2N/A * For the moment, all current macro variables are of 2N/A * type id_t (refer to dtrace_update() for details). 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 return (DT_TOK_STRING); 2N/A dt_ident_t *idp = dt_idhash_lookup( 2N/A yypcb->pcb_hdl->dt_macros, yytext + 1); 2N/A * For the moment, all current macro variables are of 2N/A * type id_t (refer to dtrace_update() for details). 2N/A yylval.l_int = (intmax_t)(int)idp->di_id; 2N/A yyintsuffix[0] = '\0'; 2N/A return (DT_TOK_INT); 2N/A return (id_or_type(yytext)); 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 if ((yylval.l_str = strdup("@_
")) == NULL) 2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2N/A return (DT_TOK_AGG); 2N/A yylval.l_int = strtoull(yytext, &p, 0); 2N/A (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix)); 2N/A yyintdecimal = yytext[0] != '0'; 2N/A if (errno == ERANGE) { 2N/A if (*p != '\0' && strchr("uUlL", *p) == NULL) { 2N/A if ((YYSTATE) != S3) 2N/A return (DT_TOK_INT); 2N/A yypragma = dt_node_link(yypragma, 2N/A dt_node_int(yylval.l_int)); 2N/A * Quoted string -- convert C escape sequences and 2N/A * return the string as a token. 2N/A yylval.l_str = strndup(yytext + 1, yyleng - 2); 2N/A if (yylval.l_str == NULL) 2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 2N/A (void) stresc2chr(yylval.l_str); 2N/A if ((YYSTATE) != S3) 2N/A return (DT_TOK_STRING); 2N/A yypragma = dt_node_link(yypragma, 2N/A dt_node_string(yylval.l_str)); 2N/A * Character constant -- convert C escape sequences and 2N/A * return the character as an integer immediate value. 2N/A yytext[yyleng - 1] = '\0'; 2N/A nbytes = stresc2chr(s); 2N/A yyintsuffix[0] = '\0'; 2N/A if (nbytes > sizeof (yylval.l_int)) { 2N/A#ifdef _LITTLE_ENDIAN 2N/A p = ((char *)&yylval.l_int) + nbytes - 1; 2N/A for (q = s; nbytes != 0; nbytes--) 2N/A bcopy(s, ((char *)&yylval.l_int) + 2N/A sizeof (yylval.l_int) - nbytes, nbytes); 2N/A return (DT_TOK_INT); 2N/A yypcb->pcb_cstate = (YYSTATE); 2N/A<S2>{RGX_INTERP} ; /* discard any #! lines */ 2N/A * The use of "/" as the predicate delimiter and as the 2N/A * integer division symbol requires special lookahead 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 if (c == 0 || c ==
';' || c ==
'{' || c ==
'/') {
2N/A "predicate before /\n");
2N/A "predicate before /\n");
2N/A<
S0>
"\\"\n ;
/* discard */ 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 *p =
'\0';
/* prune yytext */ 2N/A for (*p =
'*'; q >= p; q--)
2N/A *p =
'*';
/* restore yytext */ 2N/A<
S3>[\f\t\v ]+ ;
/* discard */ 2N/A if ((yylval.l_str = strdup(yytext)) == NULL) 2N/A longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 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 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 * 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 return;
/* nothing to do if we're in the state already */ 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 * 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 * 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 * struct s { foo foo; }; 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 * 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 * 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 yyerror(
"end-of-file encountered before matching */\n");
2N/A yyerror(
"end-of-file encountered before end of control line\n");
2N/A return (0);
/* EOF */