172445Sassar%{
272445Sassar/*
3233294Sstas * Copyright (c) 1998 - 2000 Kungliga Tekniska H��gskolan
4233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
5233294Sstas * All rights reserved.
672445Sassar *
7233294Sstas * Redistribution and use in source and binary forms, with or without
8233294Sstas * modification, are permitted provided that the following conditions
9233294Sstas * are met:
1072445Sassar *
11233294Sstas * 1. Redistributions of source code must retain the above copyright
12233294Sstas *    notice, this list of conditions and the following disclaimer.
1372445Sassar *
14233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
15233294Sstas *    notice, this list of conditions and the following disclaimer in the
16233294Sstas *    documentation and/or other materials provided with the distribution.
1772445Sassar *
18233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
19233294Sstas *    may be used to endorse or promote products derived from this software
20233294Sstas *    without specific prior written permission.
2172445Sassar *
22233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32233294Sstas * SUCH DAMAGE.
3372445Sassar */
3472445Sassar
3572445Sassar#include "compile_et.h"
3672445Sassar#include "lex.h"
3772445Sassar
3872445Sassarvoid yyerror (char *s);
3972445Sassarstatic long name2number(const char *str);
4072445Sassar
4172445Sassarextern char *yytext;
4272445Sassar
4372445Sassar/* This is for bison */
4472445Sassar
4572445Sassar#if !defined(alloca) && !defined(HAVE_ALLOCA)
4672445Sassar#define alloca(x) malloc(x)
4772445Sassar#endif
4872445Sassar
49233294Sstas#define YYMALLOC malloc
50233294Sstas#define YYFREE free
51233294Sstas
5272445Sassar%}
5372445Sassar
5472445Sassar%union {
5572445Sassar  char *string;
5672445Sassar  int number;
5772445Sassar}
5872445Sassar
5972445Sassar%token ET INDEX PREFIX EC ID END
6072445Sassar%token <string> STRING
6172445Sassar%token <number> NUMBER
6272445Sassar
6372445Sassar%%
6472445Sassar
65233294Sstasfile		: /* */
6672445Sassar		| header statements
6772445Sassar		;
6872445Sassar
6972445Sassarheader		: id et
7072445Sassar		| et
7172445Sassar		;
7272445Sassar
7372445Sassarid		: ID STRING
7472445Sassar		{
7572445Sassar		    id_str = $2;
7672445Sassar		}
7772445Sassar		;
7872445Sassar
7972445Sassaret		: ET STRING
8072445Sassar		{
81178825Sdfr		    base_id = name2number($2);
82178825Sdfr		    strlcpy(name, $2, sizeof(name));
8372445Sassar		    free($2);
8472445Sassar		}
8572445Sassar		| ET STRING STRING
8672445Sassar		{
87178825Sdfr		    base_id = name2number($2);
88178825Sdfr		    strlcpy(name, $3, sizeof(name));
8972445Sassar		    free($2);
9072445Sassar		    free($3);
9172445Sassar		}
9272445Sassar		;
9372445Sassar
9472445Sassarstatements	: statement
9572445Sassar		| statements statement
9672445Sassar		;
9772445Sassar
98233294Sstasstatement	: INDEX NUMBER
9972445Sassar		{
10072445Sassar			number = $2;
10172445Sassar		}
10272445Sassar		| PREFIX STRING
10372445Sassar		{
104178825Sdfr		    free(prefix);
105178825Sdfr		    asprintf (&prefix, "%s_", $2);
106178825Sdfr		    if (prefix == NULL)
107178825Sdfr			errx(1, "malloc");
10872445Sassar		    free($2);
10972445Sassar		}
11072445Sassar		| PREFIX
11172445Sassar		{
11272445Sassar		    prefix = realloc(prefix, 1);
113178825Sdfr		    if (prefix == NULL)
114178825Sdfr			errx(1, "malloc");
11572445Sassar		    *prefix = '\0';
11672445Sassar		}
11772445Sassar		| EC STRING ',' STRING
11872445Sassar		{
11972445Sassar		    struct error_code *ec = malloc(sizeof(*ec));
120233294Sstas
121178825Sdfr		    if (ec == NULL)
122178825Sdfr			errx(1, "malloc");
12372445Sassar
12472445Sassar		    ec->next = NULL;
12572445Sassar		    ec->number = number;
12672445Sassar		    if(prefix && *prefix != '\0') {
12772445Sassar			asprintf (&ec->name, "%s%s", prefix, $2);
128178825Sdfr			if (ec->name == NULL)
129178825Sdfr			    errx(1, "malloc");
13072445Sassar			free($2);
13172445Sassar		    } else
13272445Sassar			ec->name = $2;
13372445Sassar		    ec->string = $4;
13472445Sassar		    APPEND(codes, ec);
13572445Sassar		    number++;
13672445Sassar		}
13772445Sassar		| END
13872445Sassar		{
13972445Sassar			YYACCEPT;
14072445Sassar		}
14172445Sassar		;
14272445Sassar
14372445Sassar%%
14472445Sassar
14572445Sassarstatic long
14672445Sassarname2number(const char *str)
14772445Sassar{
14872445Sassar    const char *p;
149178825Sdfr    long num = 0;
15072445Sassar    const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
15172445Sassar	"abcdefghijklmnopqrstuvwxyz0123456789_";
15272445Sassar    if(strlen(str) > 4) {
15372445Sassar	yyerror("table name too long");
15472445Sassar	return 0;
15572445Sassar    }
15672445Sassar    for(p = str; *p; p++){
15772445Sassar	char *q = strchr(x, *p);
15872445Sassar	if(q == NULL) {
15972445Sassar	    yyerror("invalid character in table name");
16072445Sassar	    return 0;
16172445Sassar	}
162178825Sdfr	num = (num << 6) + (q - x) + 1;
16372445Sassar    }
164178825Sdfr    num <<= 8;
165178825Sdfr    if(num > 0x7fffffff)
166178825Sdfr	num = -(0xffffffff - num + 1);
167178825Sdfr    return num;
16872445Sassar}
16972445Sassar
17072445Sassarvoid
17172445Sassaryyerror (char *s)
17272445Sassar{
173233294Sstas     _lex_error_message ("%s\n", s);
17472445Sassar}
175