1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
*
* Copyright (C) 2007 Vivien Malerba
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* This program generates tokens'ID transformation because the GdaSqlParser object uses 2 Lemon generated
* parsers at once, but with only one tokenizer (because each Lemon generated parser generates it own IDs for
* tokens).
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MAX_SYMBOLS 500
#define IMPOSED_HEADER "parser_tokens.h"
#define PARSER_HEADER "parser.h"
#define DELIM_HEADER "delimiter.h"
#define FALSE 0
#define TRUE (!FALSE)
typedef struct {
char *key;
int parser_value;
int delim_value;
} HashEntry;
HashEntry entries[MAX_SYMBOLS];
int nb_entries; /* must remain < MAX_SYMBOLS */
typedef enum {
TYPE_IMPOSED,
TYPE_PARSER,
TYPE_DELIM
} SourceType;
static void parse_contents (FILE *stream, SourceType type);
static HashEntry *find_entry_for_token (char *token);
int
main (int argc,char** argv)
{
int i;
FILE *fd_imposed;
FILE *fd_parser;
FILE *fd_delim;
HashEntry *illegal_entry;
HashEntry *rawstring_entry;
char *file_imposed;
memset (entries, 0, sizeof (entries));
#ifdef SRCDIR
#ifdef __WIN32__
file_imposed = SRCDIR "\\" IMPOSED_HEADER;
#else
file_imposed = SRCDIR "/" IMPOSED_HEADER;
#endif
#else
file_imposed = IMPOSED_HEADER;
#endif
fd_imposed = fopen (file_imposed, "r");
if (!fd_imposed) {
printf ("Can't open '%s':%s\n", file_imposed, strerror (errno));
return 1;
}
fd_parser = fopen (PARSER_HEADER, "r");
if (!fd_parser) {
printf ("Can't open '%s':%s\n", PARSER_HEADER, strerror (errno));
return 1;
}
fd_delim = fopen (DELIM_HEADER, "r");
if (!fd_delim) {
printf ("Can't open '%s':%s\n", DELIM_HEADER, strerror (errno));
return 1;
}
nb_entries = 0;
parse_contents (fd_imposed, TYPE_IMPOSED);
parse_contents (fd_parser, TYPE_PARSER);
parse_contents (fd_delim, TYPE_DELIM);
fclose (fd_imposed);
fclose (fd_parser);
fclose (fd_delim);
/* output notice */
printf ("/*\n * This file is generated by the gen_def program (see the gen_def.c file \n"
" * for some explanations)\n"
" * DO NOT EDIT MANUALLY\n */\n\n\n");
/* output */
for (i = 0; i < nb_entries; i++) {
HashEntry *entry = &(entries[i]);
printf ("#define L_%s \t\t %d\n", entry->key, i);
}
illegal_entry = find_entry_for_token ("ILLEGAL");
rawstring_entry = find_entry_for_token ("RAWSTRING");
printf ("gint parser_tokens[] = {\n");
for (i = 0; i < nb_entries; i++) {
HashEntry *entry = &(entries[i]);
if (i!= 0)
printf (",");
if (entry->parser_value >= 0)
printf ("%d", entry->parser_value);
else
printf ("%d", illegal_entry->parser_value);
}
printf ("};\n");
printf ("gint delim_tokens[] = {\n");
for (i = 0; i < nb_entries; i++) {
HashEntry *entry = &(entries[i]);
if (i != 0)
printf (",");
if (entry->delim_value >= 0)
printf ("%d", entry->delim_value);
else
printf ("%d", rawstring_entry->delim_value);
}
printf ("};\n");
return 0;
}
static HashEntry *
find_entry_for_token (char *token)
{
int i;
for (i = 0; i < nb_entries; i++) {
HashEntry *e = &(entries[i]);
if (!strcmp (e->key, token))
return e;
}
return NULL;
}
static void
parse_line (char *line, SourceType type)
{
char *z, *token;
int value;
HashEntry *entry;
z = line;
if (strncmp (z, "#define ", 8)) {
printf ("Expected '#define', not found");
exit (1);
}
z += 8;
token = z + 2;
for (; *z && *z != ' '; z++);
*z = 0;
z++;
for (; *z == ' '; z++);
value = atoi (z);
/*printf ("%d Token: /%s/, value=%d\n", type, token, value);*/
entry = find_entry_for_token (token);
if (!entry) {
nb_entries++;
entry = &(entries[nb_entries - 1]);
entry->key = malloc (sizeof (char) * (strlen (token) + 1));
memcpy (entry->key, token, strlen (token) + 1);
entry->parser_value = -1;
entry->delim_value = -1;
}
if (type == TYPE_PARSER)
entry->parser_value = value;
else if (type == TYPE_DELIM)
entry->delim_value = value;
}
static void
parse_contents (FILE *stream, SourceType type)
{
#define BUFSIZE 500
char buffer[BUFSIZE];
int read;
char *end;
read = fread (buffer, 1, BUFSIZE, stream);
end = buffer + read;
while (read > 0) {
char *ptr;
/* read all complete lines in buffer */
while (end > buffer) {
char *hold = NULL;
for (ptr = buffer; (ptr < end) && *ptr && (*ptr != '\n'); ptr++);
if (ptr == end)
break;
if (*ptr)
hold = ptr+1;
*ptr = 0;
/* treat the line */
parse_line (buffer, type);
if (hold) {
int l = end - hold;
end -= hold - buffer;
memmove (buffer, hold, l);
}
else
break;
}
read = fread (end, 1, BUFSIZE - (end - buffer), stream);
end += read;
}
}
|