1 /* $NetBSD: lex.l,v 1.1.1.3 2014/04/24 12:45:28 pettai Exp $ */
2
3 %{
4 /*
5 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the Institute nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /* Id */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #undef ECHO
50 #include "symbol.h"
51 #include "asn1parse.h"
52 #include "lex.h"
53 #include "gen_locl.h"
54
55 static unsigned lineno = 1;
56
57 #undef ECHO
58
59 static void unterminated(const char *, unsigned);
60
61 %}
62
63 /* This is for broken old lexes (solaris 10 and hpux) */
64 %e 2000
65 %p 5000
66 %a 5000
67 %n 1000
68 %o 10000
69
70 %%
71 ABSENT { return kw_ABSENT; }
72 ABSTRACT-SYNTAX { return kw_ABSTRACT_SYNTAX; }
73 ALL { return kw_ALL; }
74 APPLICATION { return kw_APPLICATION; }
75 AUTOMATIC { return kw_AUTOMATIC; }
76 BEGIN { return kw_BEGIN; }
77 BIT { return kw_BIT; }
78 BMPString { return kw_BMPString; }
79 BOOLEAN { return kw_BOOLEAN; }
80 BY { return kw_BY; }
81 CHARACTER { return kw_CHARACTER; }
82 CHOICE { return kw_CHOICE; }
83 CLASS { return kw_CLASS; }
84 COMPONENT { return kw_COMPONENT; }
85 COMPONENTS { return kw_COMPONENTS; }
86 CONSTRAINED { return kw_CONSTRAINED; }
87 CONTAINING { return kw_CONTAINING; }
88 DEFAULT { return kw_DEFAULT; }
89 DEFINITIONS { return kw_DEFINITIONS; }
90 EMBEDDED { return kw_EMBEDDED; }
91 ENCODED { return kw_ENCODED; }
92 END { return kw_END; }
93 ENUMERATED { return kw_ENUMERATED; }
94 EXCEPT { return kw_EXCEPT; }
95 EXPLICIT { return kw_EXPLICIT; }
96 EXPORTS { return kw_EXPORTS; }
97 EXTENSIBILITY { return kw_EXTENSIBILITY; }
98 EXTERNAL { return kw_EXTERNAL; }
99 FALSE { return kw_FALSE; }
100 FROM { return kw_FROM; }
101 GeneralString { return kw_GeneralString; }
102 GeneralizedTime { return kw_GeneralizedTime; }
103 GraphicString { return kw_GraphicString; }
104 IA5String { return kw_IA5String; }
105 IDENTIFIER { return kw_IDENTIFIER; }
106 IMPLICIT { return kw_IMPLICIT; }
107 IMPLIED { return kw_IMPLIED; }
108 IMPORTS { return kw_IMPORTS; }
109 INCLUDES { return kw_INCLUDES; }
110 INSTANCE { return kw_INSTANCE; }
111 INTEGER { return kw_INTEGER; }
112 INTERSECTION { return kw_INTERSECTION; }
113 ISO646String { return kw_ISO646String; }
114 MAX { return kw_MAX; }
115 MIN { return kw_MIN; }
116 MINUS-INFINITY { return kw_MINUS_INFINITY; }
117 NULL { return kw_NULL; }
118 NumericString { return kw_NumericString; }
119 OBJECT { return kw_OBJECT; }
120 OCTET { return kw_OCTET; }
121 OF { return kw_OF; }
122 OPTIONAL { return kw_OPTIONAL; }
123 ObjectDescriptor { return kw_ObjectDescriptor; }
124 PATTERN { return kw_PATTERN; }
125 PDV { return kw_PDV; }
126 PLUS-INFINITY { return kw_PLUS_INFINITY; }
127 PRESENT { return kw_PRESENT; }
128 PRIVATE { return kw_PRIVATE; }
129 PrintableString { return kw_PrintableString; }
130 REAL { return kw_REAL; }
131 RELATIVE_OID { return kw_RELATIVE_OID; }
132 SEQUENCE { return kw_SEQUENCE; }
133 SET { return kw_SET; }
134 SIZE { return kw_SIZE; }
135 STRING { return kw_STRING; }
136 SYNTAX { return kw_SYNTAX; }
137 T61String { return kw_T61String; }
138 TAGS { return kw_TAGS; }
139 TRUE { return kw_TRUE; }
140 TYPE-IDENTIFIER { return kw_TYPE_IDENTIFIER; }
141 TeletexString { return kw_TeletexString; }
142 UNION { return kw_UNION; }
143 UNIQUE { return kw_UNIQUE; }
144 UNIVERSAL { return kw_UNIVERSAL; }
145 UTCTime { return kw_UTCTime; }
146 UTF8String { return kw_UTF8String; }
147 UniversalString { return kw_UniversalString; }
148 VideotexString { return kw_VideotexString; }
149 VisibleString { return kw_VisibleString; }
150 WITH { return kw_WITH; }
151 [-,;{}()|] { return *yytext; }
152 "[" { return *yytext; }
153 "]" { return *yytext; }
154 ::= { return EEQUAL; }
155 -- {
156 int c, start_lineno = lineno;
157 int f = 0;
158 while((c = input()) != EOF) {
159 if(f && c == '-')
160 break;
161 if(c == '-') {
162 f = 1;
163 continue;
164 }
165 if(c == '\n') {
166 lineno++;
167 break;
168 }
169 f = 0;
170 }
171 if(c == EOF)
172 unterminated("comment", start_lineno);
173 }
174 \/\* {
175 int c, start_lineno = lineno;
176 int level = 1;
177 int seen_star = 0;
178 int seen_slash = 0;
179 while((c = input()) != EOF) {
180 if(c == '/') {
181 if(seen_star) {
182 if(--level == 0)
183 break;
184 seen_star = 0;
185 continue;
186 }
187 seen_slash = 1;
188 continue;
189 }
190 if(seen_star && c == '/') {
191 if(--level == 0)
192 break;
193 seen_star = 0;
194 continue;
195 }
196 if(c == '*') {
197 if(seen_slash) {
198 level++;
199 seen_star = seen_slash = 0;
200 continue;
201 }
202 seen_star = 1;
203 continue;
204 }
205 seen_star = seen_slash = 0;
206 if(c == '\n') {
207 lineno++;
208 continue;
209 }
210 }
211 if(c == EOF)
212 unterminated("comment", start_lineno);
213 }
214 "\"" {
215 int start_lineno = lineno;
216 int c;
217 char buf[1024];
218 char *p = buf;
219 int f = 0;
220 int skip_ws = 0;
221
222 while((c = input()) != EOF) {
223 if(isspace(c) && skip_ws) {
224 if(c == '\n')
225 lineno++;
226 continue;
227 }
228 skip_ws = 0;
229
230 if(c == '"') {
231 if(f) {
232 *p++ = '"';
233 f = 0;
234 } else
235 f = 1;
236 continue;
237 }
238 if(f == 1) {
239 unput(c);
240 break;
241 }
242 if(c == '\n') {
243 lineno++;
244 while(p > buf && isspace((unsigned char)p[-1]))
245 p--;
246 skip_ws = 1;
247 continue;
248 }
249 *p++ = c;
250 }
251 if(c == EOF)
252 unterminated("string", start_lineno);
253 *p++ = '\0';
254 fprintf(stderr, "string -- %s\n", buf);
255 yylval.name = estrdup(buf);
256 return STRING;
257 }
258
259 -?0x[0-9A-Fa-f]+|-?[0-9]+ { char *e, *y = yytext;
260 yylval.constant = strtol((const char *)yytext,
261 &e, 0);
262 if(e == y)
263 lex_error_message("malformed constant (%s)", yytext);
264 else
265 return NUMBER;
266 }
267 [A-Za-z][-A-Za-z0-9_]* {
268 yylval.name = estrdup ((const char *)yytext);
269 return IDENTIFIER;
270 }
271 [ \t] ;
272 \n { ++lineno; }
273 \.\.\. { return ELLIPSIS; }
274 \.\. { return RANGE; }
275 . { lex_error_message("Ignoring char(%c)\n", *yytext); }
276 %%
277
278 #ifndef yywrap /* XXX */
279 int
280 yywrap ()
281 {
282 return 1;
283 }
284 #endif
285
286 void
lex_error_message(const char * format,...)287 lex_error_message (const char *format, ...)
288 {
289 va_list args;
290
291 va_start (args, format);
292 fprintf (stderr, "%s:%d: ", get_filename(), lineno);
293 vfprintf (stderr, format, args);
294 va_end (args);
295 error_flag++;
296 }
297
298 static void
unterminated(const char * type,unsigned start_lineno)299 unterminated(const char *type, unsigned start_lineno)
300 {
301 lex_error_message("unterminated %s, possibly started on line %d\n", type, start_lineno);
302 }
303