1847a7462SMark Murray %{
2847a7462SMark Murray /*
3*ae771770SStanislav Sedov * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
4847a7462SMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
5847a7462SMark Murray * All rights reserved.
6847a7462SMark Murray *
7847a7462SMark Murray * Redistribution and use in source and binary forms, with or without
8847a7462SMark Murray * modification, are permitted provided that the following conditions
9847a7462SMark Murray * are met:
10847a7462SMark Murray *
11847a7462SMark Murray * 1. Redistributions of source code must retain the above copyright
12847a7462SMark Murray * notice, this list of conditions and the following disclaimer.
13847a7462SMark Murray *
14847a7462SMark Murray * 2. Redistributions in binary form must reproduce the above copyright
15847a7462SMark Murray * notice, this list of conditions and the following disclaimer in the
16847a7462SMark Murray * documentation and/or other materials provided with the distribution.
17847a7462SMark Murray *
182b2b3720SJacques Vidrine * 3. Neither the name of the Institute nor the names of its contributors
19847a7462SMark Murray * may be used to endorse or promote products derived from this software
20847a7462SMark Murray * without specific prior written permission.
21847a7462SMark Murray *
22847a7462SMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23847a7462SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24847a7462SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25847a7462SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26847a7462SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27847a7462SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28847a7462SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29847a7462SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30847a7462SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31847a7462SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32847a7462SMark Murray * SUCH DAMAGE.
33847a7462SMark Murray */
34847a7462SMark Murray
35847a7462SMark Murray #include "compile_et.h"
362b2b3720SJacques Vidrine #include "lex.h"
37847a7462SMark Murray
38847a7462SMark Murray void yyerror (char *s);
392b2b3720SJacques Vidrine static long name2number(const char *str);
40847a7462SMark Murray
41847a7462SMark Murray extern char *yytext;
42847a7462SMark Murray
43847a7462SMark Murray /* This is for bison */
44847a7462SMark Murray
45847a7462SMark Murray #if !defined(alloca) && !defined(HAVE_ALLOCA)
46847a7462SMark Murray #define alloca(x) malloc(x)
47847a7462SMark Murray #endif
48847a7462SMark Murray
49*ae771770SStanislav Sedov #define YYMALLOC malloc
50*ae771770SStanislav Sedov #define YYFREE free
51*ae771770SStanislav Sedov
52847a7462SMark Murray %}
53847a7462SMark Murray
54847a7462SMark Murray %union {
55847a7462SMark Murray char *string;
56847a7462SMark Murray int number;
57847a7462SMark Murray }
58847a7462SMark Murray
59847a7462SMark Murray %token ET INDEX PREFIX EC ID END
60847a7462SMark Murray %token <string> STRING
61847a7462SMark Murray %token <number> NUMBER
62847a7462SMark Murray
63847a7462SMark Murray %%
64847a7462SMark Murray
65847a7462SMark Murray file : /* */
66847a7462SMark Murray | header statements
67847a7462SMark Murray ;
68847a7462SMark Murray
69847a7462SMark Murray header : id et
70847a7462SMark Murray | et
71847a7462SMark Murray ;
72847a7462SMark Murray
73847a7462SMark Murray id : ID STRING
74847a7462SMark Murray {
75847a7462SMark Murray id_str = $2;
76847a7462SMark Murray }
77847a7462SMark Murray ;
78847a7462SMark Murray
79847a7462SMark Murray et : ET STRING
80847a7462SMark Murray {
8157680329SDoug Rabson base_id = name2number($2);
8257680329SDoug Rabson strlcpy(name, $2, sizeof(name));
83847a7462SMark Murray free($2);
84847a7462SMark Murray }
85847a7462SMark Murray | ET STRING STRING
86847a7462SMark Murray {
8757680329SDoug Rabson base_id = name2number($2);
8857680329SDoug Rabson strlcpy(name, $3, sizeof(name));
89847a7462SMark Murray free($2);
90847a7462SMark Murray free($3);
91847a7462SMark Murray }
92847a7462SMark Murray ;
93847a7462SMark Murray
94847a7462SMark Murray statements : statement
95847a7462SMark Murray | statements statement
96847a7462SMark Murray ;
97847a7462SMark Murray
98847a7462SMark Murray statement : INDEX NUMBER
99847a7462SMark Murray {
100847a7462SMark Murray number = $2;
101847a7462SMark Murray }
102847a7462SMark Murray | PREFIX STRING
103847a7462SMark Murray {
10457680329SDoug Rabson free(prefix);
10557680329SDoug Rabson asprintf (&prefix, "%s_", $2);
10657680329SDoug Rabson if (prefix == NULL)
10757680329SDoug Rabson errx(1, "malloc");
108847a7462SMark Murray free($2);
109847a7462SMark Murray }
110847a7462SMark Murray | PREFIX
111847a7462SMark Murray {
112847a7462SMark Murray prefix = realloc(prefix, 1);
11357680329SDoug Rabson if (prefix == NULL)
11457680329SDoug Rabson errx(1, "malloc");
115847a7462SMark Murray *prefix = '\0';
116847a7462SMark Murray }
117847a7462SMark Murray | EC STRING ',' STRING
118847a7462SMark Murray {
119847a7462SMark Murray struct error_code *ec = malloc(sizeof(*ec));
120847a7462SMark Murray
12157680329SDoug Rabson if (ec == NULL)
12257680329SDoug Rabson errx(1, "malloc");
12357680329SDoug Rabson
124847a7462SMark Murray ec->next = NULL;
125847a7462SMark Murray ec->number = number;
126847a7462SMark Murray if(prefix && *prefix != '\0') {
127847a7462SMark Murray asprintf (&ec->name, "%s%s", prefix, $2);
12857680329SDoug Rabson if (ec->name == NULL)
12957680329SDoug Rabson errx(1, "malloc");
130847a7462SMark Murray free($2);
131847a7462SMark Murray } else
132847a7462SMark Murray ec->name = $2;
133847a7462SMark Murray ec->string = $4;
134847a7462SMark Murray APPEND(codes, ec);
135847a7462SMark Murray number++;
136847a7462SMark Murray }
137847a7462SMark Murray | END
138847a7462SMark Murray {
139847a7462SMark Murray YYACCEPT;
140847a7462SMark Murray }
141847a7462SMark Murray ;
142847a7462SMark Murray
143847a7462SMark Murray %%
144847a7462SMark Murray
1452b2b3720SJacques Vidrine static long
146847a7462SMark Murray name2number(const char *str)
147847a7462SMark Murray {
148847a7462SMark Murray const char *p;
14957680329SDoug Rabson long num = 0;
150847a7462SMark Murray const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
151847a7462SMark Murray "abcdefghijklmnopqrstuvwxyz0123456789_";
152847a7462SMark Murray if(strlen(str) > 4) {
153847a7462SMark Murray yyerror("table name too long");
154847a7462SMark Murray return 0;
155847a7462SMark Murray }
156847a7462SMark Murray for(p = str; *p; p++){
157847a7462SMark Murray char *q = strchr(x, *p);
158847a7462SMark Murray if(q == NULL) {
159847a7462SMark Murray yyerror("invalid character in table name");
160847a7462SMark Murray return 0;
161847a7462SMark Murray }
16257680329SDoug Rabson num = (num << 6) + (q - x) + 1;
163847a7462SMark Murray }
16457680329SDoug Rabson num <<= 8;
16557680329SDoug Rabson if(num > 0x7fffffff)
16657680329SDoug Rabson num = -(0xffffffff - num + 1);
16757680329SDoug Rabson return num;
168847a7462SMark Murray }
169847a7462SMark Murray
170847a7462SMark Murray void
yyerror(char * s)171847a7462SMark Murray yyerror (char *s)
172847a7462SMark Murray {
173*ae771770SStanislav Sedov _lex_error_message ("%s\n", s);
174847a7462SMark Murray }
175