xref: /illumos-gate/usr/src/cmd/fm/fminject/common/inj_lex.l (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
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, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://www.opensolaris.org/os/licensing.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include <inj.h>
33 #include <inj_lex.h>
34 #include <inj_string.h>
35 #include <inj_event.h>
36 #include <inj_grammar.h>
37 
38 int yynerrors;
39 const char *yyinname;
40 
41 %}
42 
43 /*
44  * S0 is for normal input processing.  SCOMMENT is used to process comments.
45  * We need a separate state for comments to prevent the lex regexp engine from
46  * overflowing its own buffers as it searches for the end of comments.
47  */
48 %s		S0 SCOMMENT
49 
50 RGX_IMM_SEQ	-?([0-9]+|0[xX][0-9A-Fa-f]+)
51 RGX_STR_SEQ	([^"\\\n]|\\[^"\n]|\\\")*
52 RGX_IDENT	[a-zA-Z][a-zA-Z0-9\-_]*
53 
54 %%
55 
56 <S0>"/*"		{ BEGIN(SCOMMENT); }
57 <SCOMMENT>.|\n		; /* discard */
58 <SCOMMENT>"*/"		{ BEGIN(S0); }
59 
60 <S0>evdef		{ return (INJ_TOK_EVDEF); }
61 <S0>fmridef		{ return (INJ_TOK_FMRIDEF); }
62 <S0>authdef		{ return (INJ_TOK_AUTHDEF); }
63 <S0>listdef		{ return (INJ_TOK_LISTDEF); }
64 
65 <S0>int8_t		{ return (INJ_TOK_INT8); }
66 <S0>int16_t		{ return (INJ_TOK_INT16); }
67 <S0>int32_t		{ return (INJ_TOK_INT32); }
68 <S0>int64_t		{ return (INJ_TOK_INT64); }
69 <S0>uint8_t		{ return (INJ_TOK_UINT8); }
70 <S0>uint16_t		{ return (INJ_TOK_UINT16); }
71 <S0>uint32_t		{ return (INJ_TOK_UINT32); }
72 <S0>uint64_t		{ return (INJ_TOK_UINT64); }
73 <S0>boolean		{ return (INJ_TOK_BOOLEAN); }
74 <S0>boolean_t		{ return (INJ_TOK_BOOLEAN); }
75 <S0>string		{ return (INJ_TOK_STRING); }
76 <S0>enum		{ return (INJ_TOK_ENUM); }
77 
78 <S0>event		{ return (INJ_TOK_EVENT); }
79 <S0>fmri		{ return (INJ_TOK_FMRI); }
80 <S0>auth		{ return (INJ_TOK_AUTH); }
81 <S0>list		{ return (INJ_TOK_LIST); }
82 
83 <S0>addhrtime		{ return (INJ_TOK_ADDHRT); }
84 <S0>endhrtime		{ return (INJ_TOK_ENDHRT); }
85 <S0>sleep		{ return (INJ_TOK_SLEEP); }
86 <S0>repeat		{ return (INJ_TOK_REPEAT); }
87 <S0>randomize		{ return (INJ_TOK_RANDOMIZE); }
88 
89 <S0>\"{RGX_STR_SEQ}$	{ yyerror("syntax error: \" unmatched"); }
90 
91 <S0>\"{RGX_STR_SEQ}\" {
92 		/* Quoted string */
93 		yylval.l_string = inj_strndup(yytext + 1, yyleng - 2);
94 		return (INJ_TOK_QSTRING);
95 	}
96 
97 <S0>{RGX_IDENT}("."{RGX_IDENT})+ {
98 		yylval.l_string = inj_strdup(yytext);
99 		return (INJ_TOK_FMACLASS);
100 	}
101 
102 <S0>{RGX_IDENT}	{
103 		yylval.l_string = inj_strdup(yytext);
104 		return (INJ_TOK_IDENT);
105 	}
106 
107 <S0>{RGX_IMM_SEQ} {
108 		yylval.l_string = inj_strdup(yytext);
109 		return (INJ_TOK_IMM);
110 	}
111 
112 <S0>[ \t\n]		;	/* Ignore whitespace */
113 
114 .			{ return (yytext[0]); }
115 
116 %%
117 
118 void
119 yyerror(const char *format, ...)
120 {
121 	int err = errno;
122 	va_list ap;
123 	char *s;
124 
125 	/* Don't print the line number if the message begins with a space */
126 	if (*format == ' ') {
127 		(void) fprintf(stderr, "%s: ", yyinname, yylineno);
128 		format++;
129 	} else
130 		(void) fprintf(stderr, "%s: %d: ", yyinname, yylineno);
131 
132 	va_start(ap, format);
133 	(void) vfprintf(stderr, format, ap);
134 	va_end(ap);
135 
136 	if (strchr(format, '\n') == NULL)
137 		(void) fprintf(stderr, " near \"%s\"\n", yytext);
138 
139 	yynerrors++;
140 	errno = err;
141 }
142 
143 int
144 yywrap(void)
145 {
146 	return (1);
147 }
148 
149 void
150 yyreset(void)
151 {
152 	BEGIN(S0);
153 }
154