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