Annotation of src/usr.bin/asn1_compile/lex.l, Revision 1.2
1.1 hin 1: %{
2: /*
1.2 ! biorn 3: * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
1.1 hin 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:
1.2 ! biorn 35: /* $KTH: lex.l,v 1.24 2004/10/13 17:40:21 lha Exp $ */
1.1 hin 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 "parse.h"
50: #include "lex.h"
51: #include "gen_locl.h"
52:
53: static unsigned lineno = 1;
54:
55: #define YY_NO_UNPUT
56:
57: #undef ECHO
58:
1.2 ! biorn 59: static void handle_comment(int type);
! 60:
1.1 hin 61: %}
62:
63:
64: %%
65: INTEGER { return INTEGER; }
1.2 ! biorn 66: BOOLEAN { return BOOLEAN; }
1.1 hin 67: IMPORTS { return IMPORTS; }
68: FROM { return FROM; }
69: SEQUENCE { return SEQUENCE; }
1.2 ! biorn 70: CHOICE { return CHOICE; }
1.1 hin 71: OF { return OF; }
72: OCTET { return OCTET; }
73: STRING { return STRING; }
74: GeneralizedTime { return GeneralizedTime; }
75: GeneralString { return GeneralString; }
1.2 ! biorn 76: UTF8String { return UTF8String; }
! 77: NULL { return NULLTYPE; }
1.1 hin 78: BIT { return BIT; }
79: APPLICATION { return APPLICATION; }
80: OPTIONAL { return OPTIONAL; }
81: BEGIN { return TBEGIN; }
82: END { return END; }
1.2 ! biorn 83: DEFAULT { return DEFAULT; }
1.1 hin 84: DEFINITIONS { return DEFINITIONS; }
85: ENUMERATED { return ENUMERATED; }
86: EXTERNAL { return EXTERNAL; }
87: OBJECT { return OBJECT; }
88: IDENTIFIER { return IDENTIFIER; }
1.2 ! biorn 89: [-,;{}()|\"] { return *yytext; }
1.1 hin 90: "[" { return *yytext; }
91: "]" { return *yytext; }
92: ::= { return EEQUAL; }
1.2 ! biorn 93: -- { handle_comment(0); }
! 94: \/\* { handle_comment(1); }
! 95: 0x[0-9A-Fa-f]+|[0-9]+ { char *e, *y = yytext;
1.1 hin 96: yylval.constant = strtol((const char *)yytext,
97: &e, 0);
98: if(e == y)
99: error_message("malformed constant (%s)", yytext);
100: else
101: return CONSTANT;
102: }
103: [A-Za-z][-A-Za-z0-9_]* {
104: yylval.name = strdup ((const char *)yytext);
105: return IDENT;
106: }
107: [ \t] ;
108: \n { ++lineno; }
1.2 ! biorn 109: \.\.\. { return DOTDOTDOT; }
1.1 hin 110: \.\. { return DOTDOT; }
111: . { error_message("Ignoring char(%c)\n", *yytext); }
112: %%
113:
114: #ifndef yywrap /* XXX */
115: int
116: yywrap ()
117: {
118: return 1;
119: }
120: #endif
121:
122: void
123: error_message (const char *format, ...)
124: {
125: va_list args;
126:
127: va_start (args, format);
128: fprintf (stderr, "%s:%d: ", filename(), lineno);
129: vfprintf (stderr, format, args);
130: va_end (args);
1.2 ! biorn 131: }
! 132:
! 133: static void
! 134: handle_comment(int type)
! 135: {
! 136: int c;
! 137: int start_lineno = lineno;
! 138: if(type == 0) {
! 139: int f = 0;
! 140: while((c = input()) != EOF) {
! 141: if(f && c == '-')
! 142: return;
! 143: if(c == '-') {
! 144: f = 1;
! 145: continue;
! 146: }
! 147: if(c == '\n') {
! 148: lineno++;
! 149: return;
! 150: }
! 151: f = 0;
! 152: }
! 153: } else {
! 154: int level = 1;
! 155: int seen_star = 0;
! 156: int seen_slash = 0;
! 157: while((c = input()) != EOF) {
! 158: if(c == '/') {
! 159: if(seen_star) {
! 160: if(--level == 0)
! 161: return;
! 162: seen_star = 0;
! 163: continue;
! 164: }
! 165: seen_slash = 1;
! 166: continue;
! 167: }
! 168: if(c == '*') {
! 169: if(seen_slash) {
! 170: level++;
! 171: seen_star = seen_slash = 0;
! 172: continue;
! 173: }
! 174: seen_star = 1;
! 175: continue;
! 176: }
! 177: seen_star = seen_slash = 0;
! 178: if(c == '\n') {
! 179: lineno++;
! 180: continue;
! 181: }
! 182: }
! 183: }
! 184: if(c == EOF)
! 185: error_message("unterminated comment, possibly started on line %d\n", start_lineno);
1.1 hin 186: }