xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/asn1/lex.l (revision 241bea01a19bbb306af27777a870b86d41cb3fda)
1 /*	$NetBSD: lex.l,v 1.3 2019/12/15 22:50:47 christos Exp $	*/
2 
3 %{
4 /*
5  * Copyright (c) 1997 - 2017 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 <stdint.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #undef ECHO
51 #include "symbol.h"
52 #include "asn1parse.h"
53 #include "lex.h"
54 #include "gen_locl.h"
55 
56 static unsigned lineno = 1;
57 
58 #undef ECHO
59 
60 static void unterminated(const char *, unsigned);
61 
62 %}
63 
64 /* This is for broken old lexes (solaris 10 and hpux) */
65 %e 2000
66 %p 5000
67 %a 5000
68 %n 1000
69 %o 10000
70 
71 %%
72 ABSENT			{ return kw_ABSENT; }
73 ABSTRACT-SYNTAX		{ return kw_ABSTRACT_SYNTAX; }
74 ALL			{ return kw_ALL; }
75 APPLICATION		{ return kw_APPLICATION; }
76 AUTOMATIC		{ return kw_AUTOMATIC; }
77 BEGIN			{ return kw_BEGIN; }
78 BIT			{ return kw_BIT; }
79 BMPString		{ return kw_BMPString; }
80 BOOLEAN			{ return kw_BOOLEAN; }
81 BY			{ return kw_BY; }
82 CHARACTER		{ return kw_CHARACTER; }
83 CHOICE			{ return kw_CHOICE; }
84 CLASS			{ return kw_CLASS; }
85 COMPONENT		{ return kw_COMPONENT; }
86 COMPONENTS		{ return kw_COMPONENTS; }
87 CONSTRAINED		{ return kw_CONSTRAINED; }
88 CONTAINING		{ return kw_CONTAINING; }
89 DEFAULT			{ return kw_DEFAULT; }
90 DEFINITIONS		{ return kw_DEFINITIONS; }
91 EMBEDDED		{ return kw_EMBEDDED; }
92 ENCODED			{ return kw_ENCODED; }
93 END			{ return kw_END; }
94 ENUMERATED		{ return kw_ENUMERATED; }
95 EXCEPT			{ return kw_EXCEPT; }
96 EXPLICIT		{ return kw_EXPLICIT; }
97 EXPORTS			{ return kw_EXPORTS; }
98 EXTENSIBILITY		{ return kw_EXTENSIBILITY; }
99 EXTERNAL		{ return kw_EXTERNAL; }
100 FALSE			{ return kw_FALSE; }
101 FROM			{ return kw_FROM; }
102 GeneralString		{ return kw_GeneralString; }
103 GeneralizedTime		{ return kw_GeneralizedTime; }
104 GraphicString		{ return kw_GraphicString; }
105 IA5String		{ return kw_IA5String; }
106 IDENTIFIER		{ return kw_IDENTIFIER; }
107 IMPLICIT		{ return kw_IMPLICIT; }
108 IMPLIED			{ return kw_IMPLIED; }
109 IMPORTS			{ return kw_IMPORTS; }
110 INCLUDES		{ return kw_INCLUDES; }
111 INSTANCE		{ return kw_INSTANCE; }
112 INTEGER			{ return kw_INTEGER; }
113 INTERSECTION		{ return kw_INTERSECTION; }
114 ISO646String		{ return kw_ISO646String; }
115 MAX			{ return kw_MAX; }
116 MIN			{ return kw_MIN; }
117 MINUS-INFINITY		{ return kw_MINUS_INFINITY; }
118 NULL			{ return kw_NULL; }
119 NumericString		{ return kw_NumericString; }
120 OBJECT			{ return kw_OBJECT; }
121 OCTET			{ return kw_OCTET; }
122 OF			{ return kw_OF; }
123 OPTIONAL		{ return kw_OPTIONAL; }
124 ObjectDescriptor	{ return kw_ObjectDescriptor; }
125 PATTERN			{ return kw_PATTERN; }
126 PDV			{ return kw_PDV; }
127 PLUS-INFINITY		{ return kw_PLUS_INFINITY; }
128 PRESENT			{ return kw_PRESENT; }
129 PRIVATE			{ return kw_PRIVATE; }
130 PrintableString		{ return kw_PrintableString; }
131 REAL			{ return kw_REAL; }
132 RELATIVE_OID		{ return kw_RELATIVE_OID; }
133 SEQUENCE		{ return kw_SEQUENCE; }
134 SET			{ return kw_SET; }
135 SIZE			{ return kw_SIZE; }
136 STRING			{ return kw_STRING; }
137 SYNTAX			{ return kw_SYNTAX; }
138 T61String		{ return kw_T61String; }
139 TAGS			{ return kw_TAGS; }
140 TRUE			{ return kw_TRUE; }
141 TYPE-IDENTIFIER		{ return kw_TYPE_IDENTIFIER; }
142 TeletexString		{ return kw_TeletexString; }
143 UNION			{ return kw_UNION; }
144 UNIQUE			{ return kw_UNIQUE; }
145 UNIVERSAL		{ return kw_UNIVERSAL; }
146 UTCTime			{ return kw_UTCTime; }
147 UTF8String		{ return kw_UTF8String; }
148 UniversalString		{ return kw_UniversalString; }
149 VideotexString		{ return kw_VideotexString; }
150 VisibleString		{ return kw_VisibleString; }
151 WITH			{ return kw_WITH; }
152 [-,;{}()|]		{ return *yytext; }
153 "["			{ return *yytext; }
154 "]"			{ return *yytext; }
155 ::=			{ return EEQUAL; }
156 --			{
157 			    int c, start_lineno = lineno;
158 			    int f = 0;
159 			    while((c = input()) != EOF) {
160 				if(f && c == '-')
161 				    break;
162 				if(c == '-') {
163 				    f = 1;
164 				    continue;
165 				}
166 				if(c == '\n') {
167 				    lineno++;
168 				    break;
169 				}
170 				f = 0;
171 			    }
172 			    if(c == EOF)
173 				unterminated("comment", start_lineno);
174 			}
175 \/\*			{
176 			    int c, start_lineno = lineno;
177 			    int level = 1;
178 			    int seen_star = 0;
179 			    int seen_slash = 0;
180 			    while((c = input()) != EOF) {
181 				if(c == '/') {
182 				    if(seen_star) {
183 					if(--level == 0)
184 					    break;
185 					seen_star = 0;
186 					continue;
187 				    }
188 				    seen_slash = 1;
189 				    continue;
190 				}
191 				if(seen_star && c == '/') {
192 				    if(--level == 0)
193 					break;
194 				    seen_star = 0;
195 				    continue;
196 				}
197 				if(c == '*') {
198 				    if(seen_slash) {
199 					level++;
200 					seen_star = seen_slash = 0;
201 					continue;
202 				    }
203 				    seen_star = 1;
204 				    continue;
205 				}
206 				seen_star = seen_slash = 0;
207 				if(c == '\n') {
208 				    lineno++;
209 				    continue;
210 				}
211 			    }
212 			    if(c == EOF)
213 				unterminated("comment", start_lineno);
214 			}
215 "\""			{
216 			    int start_lineno = lineno;
217 			    int c;
218 			    char buf[1024];
219 			    char *p = buf;
220 			    int f = 0;
221 			    int skip_ws = 0;
222 
223 			    while((c = input()) != EOF) {
224 				if(isspace(c) && skip_ws) {
225 				    if(c == '\n')
226 					lineno++;
227 				    continue;
228 				}
229 				skip_ws = 0;
230 
231 				if(c == '"') {
232 				    if(f) {
233 					*p++ = '"';
234 					f = 0;
235 				    } else
236 					f = 1;
237 				    continue;
238 				}
239 				if(f == 1) {
240 				    unput(c);
241 				    break;
242 				}
243 				if(c == '\n') {
244 				    lineno++;
245 				    while(p > buf && isspace((unsigned char)p[-1]))
246 					p--;
247 				    skip_ws = 1;
248 				    continue;
249 				}
250 				*p++ = c;
251 			    }
252 			    if(c == EOF)
253 				unterminated("string", start_lineno);
254 			    *p++ = '\0';
255 			    fprintf(stderr, "string -- %s\n", buf);
256 			    yylval.name = estrdup(buf);
257 			    return STRING;
258 			}
259 
260 -?0x[0-9A-Fa-f]+|-?[0-9]+ { char *e, *y = yytext;
261 			  yylval.constant = strtoll((const char *)yytext,
262 						   &e, 0);
263 			  if(e == y)
264 			    lex_error_message("malformed constant (%s)", yytext);
265 			  else
266 			    return NUMBER;
267 			}
268 [A-Za-z][-A-Za-z0-9_]*	{
269 			  yylval.name =  estrdup ((const char *)yytext);
270 			  return IDENTIFIER;
271 			}
272 [ \t]			;
273 \n			{ ++lineno; }
274 \.\.\.			{ return ELLIPSIS; }
275 \.\.			{ return RANGE; }
276 .			{ lex_error_message("Ignoring char(%c)\n", *yytext); }
277 %%
278 
279 int
280 yywrap ()
281 {
282      return 1;
283 }
284 
285 void
lex_error_message(const char * format,...)286 lex_error_message (const char *format, ...)
287 {
288     va_list args;
289 
290     va_start (args, format);
291     fprintf (stderr, "%s:%d: ", get_filename(), lineno);
292     vfprintf (stderr, format, args);
293     va_end (args);
294     error_flag++;
295 }
296 
297 static void
unterminated(const char * type,unsigned start_lineno)298 unterminated(const char *type, unsigned start_lineno)
299 {
300     lex_error_message("unterminated %s, possibly started on line %d\n", type, start_lineno);
301 }
302