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