dt_lex.l revision 209305
1%{ 2/* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 * 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "%Z%%M% %I% %E% SMI" 27 28#include <string.h> 29#include <stdlib.h> 30#include <stdio.h> 31#include <assert.h> 32#include <ctype.h> 33#include <errno.h> 34 35#include <dt_impl.h> 36#include <dt_grammar.h> 37#include <dt_parser.h> 38#include <dt_string.h> 39 40/* 41 * We need to undefine lex's input and unput macros so that references to these 42 * call the functions provided at the end of this source file. 43 */ 44#if defined(sun) 45#undef input 46#undef unput 47#else 48/* 49 * Define YY_INPUT for flex since input() can't be re-defined. 50 */ 51#define YY_INPUT(buf,result,max_size) \ 52 if (yypcb->pcb_fileptr != NULL) { \ 53 if (((result = fread(buf, 1, max_size, yypcb->pcb_fileptr)) == 0) \ 54 && ferror(yypcb->pcb_fileptr)) \ 55 longjmp(yypcb->pcb_jmpbuf, EDT_FIO); \ 56 } else { \ 57 int n; \ 58 for (n = 0; n < max_size && \ 59 yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen; n++) \ 60 buf[n] = *yypcb->pcb_strptr++; \ 61 result = n; \ 62 } 63/* 64 * Do not EOF let tokens to be put back. This does not work with flex. 65 * On the other hand, leaving current buffer in same state it was when 66 * last EOF was received guarantees that input() will keep returning EOF 67 * for all subsequent invocations, which is the effect desired. 68 */ 69#undef unput 70#define unput(c) \ 71 if (c != EOF) yyunput( c, yytext_ptr ) 72#endif 73 74static int id_or_type(const char *); 75#if defined(sun) 76static int input(void); 77static void unput(int); 78#endif 79 80/* 81 * We first define a set of labeled states for use in the D lexer and then a 82 * set of regular expressions to simplify things below. The lexer states are: 83 * 84 * S0 - D program clause and expression lexing 85 * S1 - D comments (i.e. skip everything until end of comment) 86 * S2 - D program outer scope (probe specifiers and declarations) 87 * S3 - D control line parsing (i.e. after ^# is seen but before \n) 88 * S4 - D control line scan (locate control directives only and invoke S3) 89 */ 90%} 91 92%e 1500 /* maximum nodes */ 93%p 3700 /* maximum positions */ 94%n 600 /* maximum states */ 95 96%s S0 S1 S2 S3 S4 97 98RGX_AGG "@"[a-zA-Z_][0-9a-zA-Z_]* 99RGX_PSPEC [-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]* 100RGX_IDENT [a-zA-Z_`][0-9a-zA-Z_`]* 101RGX_INT ([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]? 102RGX_FP ([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]? 103RGX_WS [\f\n\r\t\v ] 104RGX_STR ([^"\\\n]|\\[^"\n]|\\\")* 105RGX_CHR ([^'\\\n]|\\[^'\n]|\\')* 106RGX_INTERP ^[\f\t\v ]*#!.* 107RGX_CTL ^[\f\t\v ]*# 108 109%% 110 111%{ 112 113/* 114 * We insert a special prologue into yylex() itself: if the pcb contains a 115 * context token, we return that prior to running the normal lexer. This 116 * allows libdtrace to force yacc into one of our three parsing contexts: D 117 * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE). 118 * Once the token is returned, we clear it so this only happens once. 119 */ 120if (yypcb->pcb_token != 0) { 121 int tok = yypcb->pcb_token; 122 yypcb->pcb_token = 0; 123 return (tok); 124} 125 126%} 127 128<S0>auto return (DT_KEY_AUTO); 129<S0>break return (DT_KEY_BREAK); 130<S0>case return (DT_KEY_CASE); 131<S0>char return (DT_KEY_CHAR); 132<S0>const return (DT_KEY_CONST); 133<S0>continue return (DT_KEY_CONTINUE); 134<S0>counter return (DT_KEY_COUNTER); 135<S0>default return (DT_KEY_DEFAULT); 136<S0>do return (DT_KEY_DO); 137<S0>double return (DT_KEY_DOUBLE); 138<S0>else return (DT_KEY_ELSE); 139<S0>enum return (DT_KEY_ENUM); 140<S0>extern return (DT_KEY_EXTERN); 141<S0>float return (DT_KEY_FLOAT); 142<S0>for return (DT_KEY_FOR); 143<S0>goto return (DT_KEY_GOTO); 144<S0>if return (DT_KEY_IF); 145<S0>import return (DT_KEY_IMPORT); 146<S0>inline return (DT_KEY_INLINE); 147<S0>int return (DT_KEY_INT); 148<S0>long return (DT_KEY_LONG); 149<S0>offsetof return (DT_TOK_OFFSETOF); 150<S0>probe return (DT_KEY_PROBE); 151<S0>provider return (DT_KEY_PROVIDER); 152<S0>register return (DT_KEY_REGISTER); 153<S0>restrict return (DT_KEY_RESTRICT); 154<S0>return return (DT_KEY_RETURN); 155<S0>self return (DT_KEY_SELF); 156<S0>short return (DT_KEY_SHORT); 157<S0>signed return (DT_KEY_SIGNED); 158<S0>sizeof return (DT_TOK_SIZEOF); 159<S0>static return (DT_KEY_STATIC); 160<S0>string return (DT_KEY_STRING); 161<S0>stringof return (DT_TOK_STRINGOF); 162<S0>struct return (DT_KEY_STRUCT); 163<S0>switch return (DT_KEY_SWITCH); 164<S0>this return (DT_KEY_THIS); 165<S0>translator return (DT_KEY_XLATOR); 166<S0>typedef return (DT_KEY_TYPEDEF); 167<S0>union return (DT_KEY_UNION); 168<S0>unsigned return (DT_KEY_UNSIGNED); 169<S0>void return (DT_KEY_VOID); 170<S0>volatile return (DT_KEY_VOLATILE); 171<S0>while return (DT_KEY_WHILE); 172<S0>xlate return (DT_TOK_XLATE); 173 174<S2>auto { yybegin(YYS_EXPR); return (DT_KEY_AUTO); } 175<S2>char { yybegin(YYS_EXPR); return (DT_KEY_CHAR); } 176<S2>const { yybegin(YYS_EXPR); return (DT_KEY_CONST); } 177<S2>counter { yybegin(YYS_DEFINE); return (DT_KEY_COUNTER); } 178<S2>double { yybegin(YYS_EXPR); return (DT_KEY_DOUBLE); } 179<S2>enum { yybegin(YYS_EXPR); return (DT_KEY_ENUM); } 180<S2>extern { yybegin(YYS_EXPR); return (DT_KEY_EXTERN); } 181<S2>float { yybegin(YYS_EXPR); return (DT_KEY_FLOAT); } 182<S2>import { yybegin(YYS_EXPR); return (DT_KEY_IMPORT); } 183<S2>inline { yybegin(YYS_DEFINE); return (DT_KEY_INLINE); } 184<S2>int { yybegin(YYS_EXPR); return (DT_KEY_INT); } 185<S2>long { yybegin(YYS_EXPR); return (DT_KEY_LONG); } 186<S2>provider { yybegin(YYS_DEFINE); return (DT_KEY_PROVIDER); } 187<S2>register { yybegin(YYS_EXPR); return (DT_KEY_REGISTER); } 188<S2>restrict { yybegin(YYS_EXPR); return (DT_KEY_RESTRICT); } 189<S2>self { yybegin(YYS_EXPR); return (DT_KEY_SELF); } 190<S2>short { yybegin(YYS_EXPR); return (DT_KEY_SHORT); } 191<S2>signed { yybegin(YYS_EXPR); return (DT_KEY_SIGNED); } 192<S2>static { yybegin(YYS_EXPR); return (DT_KEY_STATIC); } 193<S2>string { yybegin(YYS_EXPR); return (DT_KEY_STRING); } 194<S2>struct { yybegin(YYS_EXPR); return (DT_KEY_STRUCT); } 195<S2>this { yybegin(YYS_EXPR); return (DT_KEY_THIS); } 196<S2>translator { yybegin(YYS_DEFINE); return (DT_KEY_XLATOR); } 197<S2>typedef { yybegin(YYS_EXPR); return (DT_KEY_TYPEDEF); } 198<S2>union { yybegin(YYS_EXPR); return (DT_KEY_UNION); } 199<S2>unsigned { yybegin(YYS_EXPR); return (DT_KEY_UNSIGNED); } 200<S2>void { yybegin(YYS_EXPR); return (DT_KEY_VOID); } 201<S2>volatile { yybegin(YYS_EXPR); return (DT_KEY_VOLATILE); } 202 203<S0>"$$"[0-9]+ { 204 int i = atoi(yytext + 2); 205 char *v = ""; 206 207 /* 208 * A macro argument reference substitutes the text of 209 * an argument in place of the current token. When we 210 * see $$<d> we fetch the saved string from pcb_sargv 211 * (or use the default argument if the option has been 212 * set and the argument hasn't been specified) and 213 * return a token corresponding to this string. 214 */ 215 if (i < 0 || (i >= yypcb->pcb_sargc && 216 !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 217 xyerror(D_MACRO_UNDEF, "macro argument %s is " 218 "not defined\n", yytext); 219 } 220 221 if (i < yypcb->pcb_sargc) { 222 v = yypcb->pcb_sargv[i]; /* get val from pcb */ 223 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 224 } 225 226 if ((yylval.l_str = strdup(v)) == NULL) 227 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 228 229 (void) stresc2chr(yylval.l_str); 230 return (DT_TOK_STRING); 231 } 232 233<S0>"$"[0-9]+ { 234 int i = atoi(yytext + 1); 235 char *p, *v = "0"; 236 237 /* 238 * A macro argument reference substitutes the text of 239 * one identifier or integer pattern for another. When 240 * we see $<d> we fetch the saved string from pcb_sargv 241 * (or use the default argument if the option has been 242 * set and the argument hasn't been specified) and 243 * return a token corresponding to this string. 244 */ 245 if (i < 0 || (i >= yypcb->pcb_sargc && 246 !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 247 xyerror(D_MACRO_UNDEF, "macro argument %s is " 248 "not defined\n", yytext); 249 } 250 251 if (i < yypcb->pcb_sargc) { 252 v = yypcb->pcb_sargv[i]; /* get val from pcb */ 253 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 254 } 255 256 /* 257 * If the macro text is not a valid integer or ident, 258 * then we treat it as a string. The string may be 259 * optionally enclosed in quotes, which we strip. 260 */ 261 if (strbadidnum(v)) { 262 size_t len = strlen(v); 263 264 if (len != 1 && *v == '"' && v[len - 1] == '"') 265 yylval.l_str = strndup(v + 1, len - 2); 266 else 267 yylval.l_str = strndup(v, len); 268 269 if (yylval.l_str == NULL) 270 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 271 272 (void) stresc2chr(yylval.l_str); 273 return (DT_TOK_STRING); 274 } 275 276 /* 277 * If the macro text is not a string an begins with a 278 * digit or a +/- sign, process it as an integer token. 279 */ 280 if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') { 281 if (isdigit(v[0])) 282 yyintprefix = 0; 283 else 284 yyintprefix = *v++; 285 286 errno = 0; 287 yylval.l_int = strtoull(v, &p, 0); 288 (void) strncpy(yyintsuffix, p, 289 sizeof (yyintsuffix)); 290 yyintdecimal = *v != '0'; 291 292 if (errno == ERANGE) { 293 xyerror(D_MACRO_OFLOW, "macro argument" 294 " %s constant %s results in integer" 295 " overflow\n", yytext, v); 296 } 297 298 return (DT_TOK_INT); 299 } 300 301 return (id_or_type(v)); 302 } 303 304<S0>"$$"{RGX_IDENT} { 305 dt_ident_t *idp = dt_idhash_lookup( 306 yypcb->pcb_hdl->dt_macros, yytext + 2); 307 308 char s[16]; /* enough for UINT_MAX + \0 */ 309 310 if (idp == NULL) { 311 xyerror(D_MACRO_UNDEF, "macro variable %s " 312 "is not defined\n", yytext); 313 } 314 315 /* 316 * For the moment, all current macro variables are of 317 * type id_t (refer to dtrace_update() for details). 318 */ 319 (void) snprintf(s, sizeof (s), "%u", idp->di_id); 320 if ((yylval.l_str = strdup(s)) == NULL) 321 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 322 323 return (DT_TOK_STRING); 324 } 325 326<S0>"$"{RGX_IDENT} { 327 dt_ident_t *idp = dt_idhash_lookup( 328 yypcb->pcb_hdl->dt_macros, yytext + 1); 329 330 if (idp == NULL) { 331 xyerror(D_MACRO_UNDEF, "macro variable %s " 332 "is not defined\n", yytext); 333 } 334 335 /* 336 * For the moment, all current macro variables are of 337 * type id_t (refer to dtrace_update() for details). 338 */ 339 yylval.l_int = (intmax_t)(int)idp->di_id; 340 yyintprefix = 0; 341 yyintsuffix[0] = '\0'; 342 yyintdecimal = 1; 343 344 return (DT_TOK_INT); 345 } 346 347<S0>{RGX_IDENT} { 348 return (id_or_type(yytext)); 349 } 350 351<S0>{RGX_AGG} { 352 if ((yylval.l_str = strdup(yytext)) == NULL) 353 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 354 return (DT_TOK_AGG); 355 } 356 357<S0>"@" { 358 if ((yylval.l_str = strdup("@_")) == NULL) 359 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 360 return (DT_TOK_AGG); 361 } 362 363<S0>{RGX_INT} | 364<S2>{RGX_INT} | 365<S3>{RGX_INT} { 366 char *p; 367 368 errno = 0; 369 yylval.l_int = strtoull(yytext, &p, 0); 370 yyintprefix = 0; 371 (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix)); 372 yyintdecimal = yytext[0] != '0'; 373 374 if (errno == ERANGE) { 375 xyerror(D_INT_OFLOW, "constant %s results in " 376 "integer overflow\n", yytext); 377 } 378 379 if (*p != '\0' && strchr("uUlL", *p) == NULL) { 380 xyerror(D_INT_DIGIT, "constant %s contains " 381 "invalid digit %c\n", yytext, *p); 382 } 383 384 if ((YYSTATE) != S3) 385 return (DT_TOK_INT); 386 387 yypragma = dt_node_link(yypragma, 388 dt_node_int(yylval.l_int)); 389 } 390 391<S0>{RGX_FP} yyerror("floating-point constants are not permitted\n"); 392 393<S0>\"{RGX_STR}$ | 394<S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal"); 395 396<S0>\"{RGX_STR}\" | 397<S3>\"{RGX_STR}\" { 398 /* 399 * Quoted string -- convert C escape sequences and 400 * return the string as a token. 401 */ 402 yylval.l_str = strndup(yytext + 1, yyleng - 2); 403 404 if (yylval.l_str == NULL) 405 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 406 407 (void) stresc2chr(yylval.l_str); 408 if ((YYSTATE) != S3) 409 return (DT_TOK_STRING); 410 411 yypragma = dt_node_link(yypragma, 412 dt_node_string(yylval.l_str)); 413 } 414 415<S0>'{RGX_CHR}$ xyerror(D_CHR_NL, "newline encountered in character constant"); 416 417<S0>'{RGX_CHR}' { 418 char *s, *p, *q; 419 size_t nbytes; 420 421 /* 422 * Character constant -- convert C escape sequences and 423 * return the character as an integer immediate value. 424 */ 425 if (yyleng == 2) 426 xyerror(D_CHR_NULL, "empty character constant"); 427 428 s = yytext + 1; 429 yytext[yyleng - 1] = '\0'; 430 nbytes = stresc2chr(s); 431 yylval.l_int = 0; 432 yyintprefix = 0; 433 yyintsuffix[0] = '\0'; 434 yyintdecimal = 1; 435 436 if (nbytes > sizeof (yylval.l_int)) { 437 xyerror(D_CHR_OFLOW, "character constant is " 438 "too long"); 439 } 440#if BYTE_ORDER == _LITTLE_ENDIAN 441 p = ((char *)&yylval.l_int) + nbytes - 1; 442 for (q = s; nbytes != 0; nbytes--) 443 *p-- = *q++; 444#else 445 bcopy(s, ((char *)&yylval.l_int) + 446 sizeof (yylval.l_int) - nbytes, nbytes); 447#endif 448 return (DT_TOK_INT); 449 } 450 451<S0>"/*" | 452<S2>"/*" { 453 yypcb->pcb_cstate = (YYSTATE); 454 BEGIN(S1); 455 } 456 457<S0>{RGX_INTERP} | 458<S2>{RGX_INTERP} ; /* discard any #! lines */ 459 460<S0>{RGX_CTL} | 461<S2>{RGX_CTL} | 462<S4>{RGX_CTL} { 463 assert(yypragma == NULL); 464 yypcb->pcb_cstate = (YYSTATE); 465 BEGIN(S3); 466 } 467 468<S4>. ; /* discard */ 469<S4>"\n" ; /* discard */ 470 471<S0>"/" { 472 int c, tok; 473 474 /* 475 * The use of "/" as the predicate delimiter and as the 476 * integer division symbol requires special lookahead 477 * to avoid a shift/reduce conflict in the D grammar. 478 * We look ahead to the next non-whitespace character. 479 * If we encounter EOF, ";", "{", or "/", then this "/" 480 * closes the predicate and we return DT_TOK_EPRED. 481 * If we encounter anything else, it's DT_TOK_DIV. 482 */ 483 while ((c = input()) != 0) { 484 if (strchr("\f\n\r\t\v ", c) == NULL) 485 break; 486 } 487 488 if (c == 0 || c == ';' || c == '{' || c == '/') { 489 if (yypcb->pcb_parens != 0) { 490 yyerror("closing ) expected in " 491 "predicate before /\n"); 492 } 493 if (yypcb->pcb_brackets != 0) { 494 yyerror("closing ] expected in " 495 "predicate before /\n"); 496 } 497 tok = DT_TOK_EPRED; 498 } else 499 tok = DT_TOK_DIV; 500 501 unput(c); 502 return (tok); 503 } 504 505<S0>"(" { 506 yypcb->pcb_parens++; 507 return (DT_TOK_LPAR); 508 } 509 510<S0>")" { 511 if (--yypcb->pcb_parens < 0) 512 yyerror("extra ) in input stream\n"); 513 return (DT_TOK_RPAR); 514 } 515 516<S0>"[" { 517 yypcb->pcb_brackets++; 518 return (DT_TOK_LBRAC); 519 } 520 521<S0>"]" { 522 if (--yypcb->pcb_brackets < 0) 523 yyerror("extra ] in input stream\n"); 524 return (DT_TOK_RBRAC); 525 } 526 527<S0>"{" | 528<S2>"{" { 529 yypcb->pcb_braces++; 530 return ('{'); 531 } 532 533<S0>"}" { 534 if (--yypcb->pcb_braces < 0) 535 yyerror("extra } in input stream\n"); 536 return ('}'); 537 } 538 539<S0>"|" return (DT_TOK_BOR); 540<S0>"^" return (DT_TOK_XOR); 541<S0>"&" return (DT_TOK_BAND); 542<S0>"&&" return (DT_TOK_LAND); 543<S0>"^^" return (DT_TOK_LXOR); 544<S0>"||" return (DT_TOK_LOR); 545<S0>"==" return (DT_TOK_EQU); 546<S0>"!=" return (DT_TOK_NEQ); 547<S0>"<" return (DT_TOK_LT); 548<S0>"<=" return (DT_TOK_LE); 549<S0>">" return (DT_TOK_GT); 550<S0>">=" return (DT_TOK_GE); 551<S0>"<<" return (DT_TOK_LSH); 552<S0>">>" return (DT_TOK_RSH); 553<S0>"+" return (DT_TOK_ADD); 554<S0>"-" return (DT_TOK_SUB); 555<S0>"*" return (DT_TOK_MUL); 556<S0>"%" return (DT_TOK_MOD); 557<S0>"~" return (DT_TOK_BNEG); 558<S0>"!" return (DT_TOK_LNEG); 559<S0>"?" return (DT_TOK_QUESTION); 560<S0>":" return (DT_TOK_COLON); 561<S0>"." return (DT_TOK_DOT); 562<S0>"->" return (DT_TOK_PTR); 563<S0>"=" return (DT_TOK_ASGN); 564<S0>"+=" return (DT_TOK_ADD_EQ); 565<S0>"-=" return (DT_TOK_SUB_EQ); 566<S0>"*=" return (DT_TOK_MUL_EQ); 567<S0>"/=" return (DT_TOK_DIV_EQ); 568<S0>"%=" return (DT_TOK_MOD_EQ); 569<S0>"&=" return (DT_TOK_AND_EQ); 570<S0>"^=" return (DT_TOK_XOR_EQ); 571<S0>"|=" return (DT_TOK_OR_EQ); 572<S0>"<<=" return (DT_TOK_LSH_EQ); 573<S0>">>=" return (DT_TOK_RSH_EQ); 574<S0>"++" return (DT_TOK_ADDADD); 575<S0>"--" return (DT_TOK_SUBSUB); 576<S0>"..." return (DT_TOK_ELLIPSIS); 577<S0>"," return (DT_TOK_COMMA); 578<S0>";" return (';'); 579<S0>{RGX_WS} ; /* discard */ 580<S0>"\\"\n ; /* discard */ 581<S0>. yyerror("syntax error near \"%c\"\n", yytext[0]); 582 583<S1>"/*" yyerror("/* encountered inside a comment\n"); 584<S1>"*/" BEGIN(yypcb->pcb_cstate); 585<S1>.|\n ; /* discard */ 586 587<S2>{RGX_PSPEC} { 588 /* 589 * S2 has an ambiguity because RGX_PSPEC includes '*' 590 * as a glob character and '*' also can be DT_TOK_STAR. 591 * Since lex always matches the longest token, this 592 * rule can be matched by an input string like "int*", 593 * which could begin a global variable declaration such 594 * as "int*x;" or could begin a RGX_PSPEC with globbing 595 * such as "int* { trace(timestamp); }". If C_PSPEC is 596 * not set, we must resolve the ambiguity in favor of 597 * the type and perform lexer pushback if the fragment 598 * before '*' or entire fragment matches a type name. 599 * If C_PSPEC is set, we always return a PSPEC token. 600 * If C_PSPEC is off, the user can avoid ambiguity by 601 * including a ':' delimiter in the specifier, which 602 * they should be doing anyway to specify the provider. 603 */ 604 if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) && 605 strchr(yytext, ':') == NULL) { 606 607 char *p = strchr(yytext, '*'); 608 char *q = yytext + yyleng - 1; 609 610 if (p != NULL && p > yytext) 611 *p = '\0'; /* prune yytext */ 612 613 if (dt_type_lookup(yytext, NULL) == 0) { 614 yylval.l_str = strdup(yytext); 615 616 if (yylval.l_str == NULL) { 617 longjmp(yypcb->pcb_jmpbuf, 618 EDT_NOMEM); 619 } 620 621 if (p != NULL && p > yytext) { 622 for (*p = '*'; q >= p; q--) 623 unput(*q); 624 } 625 626 yybegin(YYS_EXPR); 627 return (DT_TOK_TNAME); 628 } 629 630 if (p != NULL && p > yytext) 631 *p = '*'; /* restore yytext */ 632 } 633 634 if ((yylval.l_str = strdup(yytext)) == NULL) 635 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 636 637 return (DT_TOK_PSPEC); 638 } 639 640<S2>"/" return (DT_TOK_DIV); 641<S2>"," return (DT_TOK_COMMA); 642 643<S2>{RGX_WS} ; /* discard */ 644<S2>. yyerror("syntax error near \"%c\"\n", yytext[0]); 645 646<S3>\n { 647 dt_pragma(yypragma); 648 yypragma = NULL; 649 BEGIN(yypcb->pcb_cstate); 650 } 651 652<S3>[\f\t\v ]+ ; /* discard */ 653 654<S3>[^\f\n\t\v "]+ { 655 dt_node_t *dnp; 656 657 if ((yylval.l_str = strdup(yytext)) == NULL) 658 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 659 660 /* 661 * We want to call dt_node_ident() here, but we can't 662 * because it will expand inlined identifiers, which we 663 * don't want to do from #pragma context in order to 664 * support pragmas that apply to the ident itself. We 665 * call dt_node_string() and then reset dn_op instead. 666 */ 667 dnp = dt_node_string(yylval.l_str); 668 dnp->dn_kind = DT_NODE_IDENT; 669 dnp->dn_op = DT_TOK_IDENT; 670 yypragma = dt_node_link(yypragma, dnp); 671 } 672 673<S3>. yyerror("syntax error near \"%c\"\n", yytext[0]); 674 675%% 676 677/* 678 * yybegin provides a wrapper for use from C code around the lex BEGIN() macro. 679 * We use two main states for lexing because probe descriptions use a syntax 680 * that is incompatible with the normal D tokens (e.g. names can contain "-"). 681 * yybegin also handles the job of switching between two lists of dt_nodes 682 * as we allocate persistent definitions, like inlines, and transient nodes 683 * that will be freed once we are done parsing the current program file. 684 */ 685void 686yybegin(yystate_t state) 687{ 688#ifdef YYDEBUG 689 yydebug = _dtrace_debug; 690#endif 691 if (yypcb->pcb_yystate == state) 692 return; /* nothing to do if we're in the state already */ 693 694 if (yypcb->pcb_yystate == YYS_DEFINE) { 695 yypcb->pcb_list = yypcb->pcb_hold; 696 yypcb->pcb_hold = NULL; 697 } 698 699 switch (state) { 700 case YYS_CLAUSE: 701 BEGIN(S2); 702 break; 703 case YYS_DEFINE: 704 assert(yypcb->pcb_hold == NULL); 705 yypcb->pcb_hold = yypcb->pcb_list; 706 yypcb->pcb_list = NULL; 707 /*FALLTHRU*/ 708 case YYS_EXPR: 709 BEGIN(S0); 710 break; 711 case YYS_DONE: 712 break; 713 case YYS_CONTROL: 714 BEGIN(S4); 715 break; 716 default: 717 xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state); 718 } 719 720 yypcb->pcb_yystate = state; 721} 722 723void 724yyinit(dt_pcb_t *pcb) 725{ 726 yypcb = pcb; 727 yylineno = 1; 728 yypragma = NULL; 729#if defined(sun) 730 yysptr = yysbuf; 731#endif 732} 733 734/* 735 * Given a lexeme 's' (typically yytext), set yylval and return an appropriate 736 * token to the parser indicating either an identifier or a typedef name. 737 * User-defined global variables always take precedence over types, but we do 738 * use some heuristics because D programs can look at an ever-changing set of 739 * kernel types and also can implicitly instantiate variables by assignment, 740 * unlike in C. The code here is ordered carefully as lookups are not cheap. 741 */ 742static int 743id_or_type(const char *s) 744{ 745 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 746 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 747 int c0, c1, ttok = DT_TOK_TNAME; 748 dt_ident_t *idp; 749 750 if ((s = yylval.l_str = strdup(s)) == NULL) 751 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 752 753 /* 754 * If the lexeme is a global variable or likely identifier or *not* a 755 * type_name, then it is an identifier token. 756 */ 757 if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL || 758 dt_idhash_lookup(yypcb->pcb_idents, s) != NULL || 759 dt_type_lookup(s, NULL) != 0) 760 return (DT_TOK_IDENT); 761 762 /* 763 * If we're in the midst of parsing a declaration and a type_specifier 764 * has already been shifted, then return DT_TOK_IDENT instead of TNAME. 765 * This semantic is necessary to permit valid ISO C code such as: 766 * 767 * typedef int foo; 768 * struct s { foo foo; }; 769 * 770 * without causing shift/reduce conflicts in the direct_declarator part 771 * of the grammar. The result is that we must check for conflicting 772 * redeclarations of the same identifier as part of dt_node_decl(). 773 */ 774 if (ddp != NULL && ddp->dd_name != NULL) 775 return (DT_TOK_IDENT); 776 777 /* 778 * If the lexeme is a type name and we are not in a program clause, 779 * then always interpret it as a type and return DT_TOK_TNAME. 780 */ 781 if ((YYSTATE) != S0) 782 return (DT_TOK_TNAME); 783 784 /* 785 * If the lexeme matches a type name but is in a program clause, then 786 * it could be a type or it could be an undefined variable. Peek at 787 * the next token to decide. If we see ++, --, [, or =, we know there 788 * might be an assignment that is trying to create a global variable, 789 * so we optimistically return DT_TOK_IDENT. There is no harm in being 790 * wrong: a type_name followed by ++, --, [, or = is a syntax error. 791 */ 792 while ((c0 = input()) != 0) { 793 if (strchr("\f\n\r\t\v ", c0) == NULL) 794 break; 795 } 796 797 switch (c0) { 798 case '+': 799 case '-': 800 if ((c1 = input()) == c0) 801 ttok = DT_TOK_IDENT; 802 unput(c1); 803 break; 804 805 case '=': 806 if ((c1 = input()) != c0) 807 ttok = DT_TOK_IDENT; 808 unput(c1); 809 break; 810 case '[': 811 ttok = DT_TOK_IDENT; 812 break; 813 } 814 815 if (ttok == DT_TOK_IDENT) { 816 idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0, 817 0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); 818 819 if (idp == NULL) 820 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 821 } 822 823 unput(c0); 824 return (ttok); 825} 826 827#if defined(sun) 828static int 829input(void) 830{ 831 int c; 832 833 if (yysptr > yysbuf) 834 c = *--yysptr; 835 else if (yypcb->pcb_fileptr != NULL) 836 c = fgetc(yypcb->pcb_fileptr); 837 else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen) 838 c = *yypcb->pcb_strptr++; 839 else 840 c = EOF; 841 842 if (c == '\n') 843 yylineno++; 844 845 if (c != EOF) 846 return (c); 847 848 if ((YYSTATE) == S1) 849 yyerror("end-of-file encountered before matching */\n"); 850 851 if ((YYSTATE) == S3) 852 yyerror("end-of-file encountered before end of control line\n"); 853 854 if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr)) 855 longjmp(yypcb->pcb_jmpbuf, EDT_FIO); 856 857 return (0); /* EOF */ 858} 859 860static void 861unput(int c) 862{ 863 if (c == '\n') 864 yylineno--; 865 866 *yysptr++ = c; 867 yytchar = c; 868} 869#endif 870