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