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