Commit 690ce725 authored by Michaël Cadilhac's avatar Michaël Cadilhac
Browse files

2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>

	* src/parser.h: Use `GList' for namespaces and include related
	features.
	* src/emit_files.c,
	* src/parser.y:,
	* src/main.c: Update accordingly.
parent 3767e0e3
2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* src/parser.h: Use `GList' for namespaces and include related
features.
* src/emit_files.c,
* src/parser.y:,
* src/main.c: Update accordingly.
2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* NOTICE: Correct information.
......
......@@ -25,27 +25,37 @@
/** Output header file data. */
void emit_header_file ()
{
int i;
int n_namespace = 0;
GList *namespace = namespaces;
emit_includes ();
emit_header ();
for (i = 0; i < n_namespace; ++i)
output ("namespace %s {\n", namespaces[i]);
while (namespace)
{
output ("namespace %s {\n", (char *) namespace->data);
namespace = namespace->next;
++n_namespace;
}
emit_term ();
emit_nonterm ();
emit_state ();
emit_prototypes ();
for (i = 0; i < n_namespace; ++i)
while (n_namespace--)
output ("}\n");
}
/** Output C file data. */
void emit_code_file ()
{
int i;
int n_namespace = 0;
GList *namespace = namespaces;
for (i = 0; i < n_namespace; ++i)
output ("namespace %s {\n", namespaces[i]);
for (namespace = namespaces; namespace; namespace = namespace->next) {
output ("namespace %s {\n", (char *) namespace->data);
g_free (namespace->data);
++n_namespace;
}
g_list_free (namespaces);
emit_vardefs ();
emit_cost_func ();
emit_emitter_func ();
......@@ -55,6 +65,6 @@ void emit_code_file ()
emit_label_func ();
emit_kids ();
for (i = 0; i < n_namespace; ++i)
while (n_namespace--)
output ("}\n");
}
......@@ -55,9 +55,7 @@ GOPTION_CALLBACK (add_to_defined_vars,
return TRUE;);
GOPTION_CALLBACK (add_to_include_dirs,
if (n_include_dir % 10 == 0)
include_dirs = g_renew (char *, include_dirs, n_include_dir + 10);
include_dirs [n_include_dir++] = (char *) g_strdup (value);
include_dirs = g_list_append (include_dirs, g_strdup (value));
return TRUE);
static void bad_use (const char *program_name, const char *use)
......@@ -155,6 +153,7 @@ static void parse_options (int argc, char **argv)
int main (int argc, char **argv)
{
File input;
FILE *deffd = 0;
FILE *cfd = 0;
guint handler_id;
......@@ -191,28 +190,31 @@ int main (int argc, char **argv)
outputfd = stdout;
/* Parse burg files. */
inputs[0].yylineno = 0;
if (infiles) {
GList *l = infiles;
inputs = g_list_append (inputs, &input);
while (l) {
char *infile = (char *)l->data;
if (!(inputs[0].fd = fopen (infile, "r"))) {
input.yylineno = 0;
if (!(input.fd = fopen (infile, "r"))) {
perror ("cant open input file");
exit (-1);
}
inputs[0].filename = infile;
input.filename = infile;
output ("#line %d \"%s\"\n", 1, infile);
yyparse ();
reset_parser ();
l->data = inputs[0].fd;
l->data = g_memdup (&input, sizeof (File));
l = l->next;
}
} else {
inputs[0].fd = stdin;
inputs[0].filename = "stdin";
input.fd = stdin;
input.filename = "stdin";
input.yylineno = 0;
inputs = g_list_append (inputs, &input);
yyparse ();
}
......@@ -251,9 +253,10 @@ int main (int argc, char **argv)
if (infiles) {
GList *l = infiles;
while (l) {
inputs[0].fd = l->data;
input = *((File *) l->data);
yyparsetail ();
fclose (inputs[0].fd);
fclose (input.fd);
g_free (l->data);
l = l->next;
}
} else {
......
......@@ -23,9 +23,7 @@
# include "monoburg.h"
# define MAX_FDS 10
# define MAX_FILENAME_LEN 100
# define MAX_NAMESPACES 10
typedef struct _File File;
struct _File {
......@@ -35,13 +33,10 @@ struct _File {
FILE *fd;
};
extern File inputs[MAX_FDS];
extern GList *inputs;
extern char *namespaces[MAX_NAMESPACES];
extern char **include_dirs;
extern int n_namespace;
extern int n_include_dir;
extern GList *namespaces;
extern GList *include_dirs;
extern GHashTable *definedvars;
......
......@@ -33,13 +33,10 @@
#include "parser.h"
#include "emit.h"
File inputs[MAX_FDS];
GHashTable *definedvars;
char *namespaces[MAX_NAMESPACES];
char **include_dirs;
int n_include_dir = 0;
int n_namespace = 0;
GList *inputs = NULL;
GHashTable *definedvars= NULL;
GList *namespaces = NULL;
GList *include_dirs = NULL;
%}
......@@ -77,10 +74,7 @@ decls : /* empty */
| TERMPREFIX plist decls
| NAMESPACE IDENT {
warn_cxx ("`%namespace' directive");
if (n_namespace == MAX_NAMESPACES)
yyerror ("maximum namespace depth reached.");
else
namespaces[n_namespace++] = g_strdup($2);
namespaces = g_list_append (namespaces, $2);
} decls
| rule_list optcost optcode optcfunc {
GList *tmp;
......@@ -137,60 +131,72 @@ optvarname: /* empty */ { $$ = NULL; }
%%
#define LASTINPUT ((File *) g_list_last (inputs)->data)
/* We assume that there's no line larger than 2048 chars. */
static char input[2048];
static char *next = input;
static int n_input = 0;
static int state = 0;
char *
fgets_inc(char *s, int size)
{
int n_length, i;
int n_length;
char filename[MAX_FILENAME_LEN];
int b_found = FALSE;
char path[MAX_FILENAME_LEN * 2];
File *new_include;
GList *include_dir = include_dirs;
/* 9 == strlen("%include "); */
assert (size > 9 + MAX_FILENAME_LEN);
if (fgets (s, size, inputs[n_input].fd) == NULL) {
if (n_input == 0)
if (fgets (s, size, LASTINPUT->fd) == NULL) {
if (inputs->next == NULL)
return 0;
free (inputs[n_input].filename);
fclose (inputs[n_input--].fd);
free (LASTINPUT->filename);
fclose (LASTINPUT->fd);
inputs = g_list_delete_link (inputs, g_list_last (inputs));
if (state != 1)
output ("#line %d \"%s\"\n",
inputs[n_input].yylineno + 1,
inputs[n_input].filename);
LASTINPUT->yylineno + 1,
LASTINPUT->filename);
return fgets_inc(s, size);
}
inputs[n_input].yylineno++;
inputs[n_input].yylinepos = 1;
LASTINPUT->yylineno++;
LASTINPUT->yylinepos = 1;
n_length = strlen(s);
if (strncmp (s, "%include ", 9) == 0) {
if (n_input == MAX_FDS - 1)
yyerror ("maximum include depth exceeded");
if (n_length - 9 > MAX_FILENAME_LEN)
yyerror ("`%%include' is referring to a too long filename");
if (s[n_length - 1] == '\n')
s[n_length - 1] = 0;
strcpy (filename, s + 9);
for (i = 0; (b_found == FALSE) && i < n_include_dir; ++i)
{
sprintf (path, "%s/%s", include_dirs[i], filename);
if ((inputs[n_input + 1].fd = fopen (path, "r")))
b_found = TRUE;
}
new_include = g_new (File, 1);
if (filename[0] == '/')
b_found = (new_include->fd = fopen (filename, "r")) != 0;
else
while ((b_found == FALSE) && include_dir)
{
sprintf (path, "%s/%s", (char *) include_dir->data, filename);
if ((new_include->fd = fopen (path, "r")))
b_found = TRUE;
include_dir = include_dir->next;
}
if (b_found == FALSE)
{
g_free (new_include);
yyerror ("`%%include %s': %s",
filename, strerror(errno));
}
if (state != 1)
output ("#line %d \"%s\"\n", 1, path);
inputs[++n_input].yylineno = 0;
inputs[n_input].filename = strdup (path);
new_include->yylineno = 0;
new_include->filename = strdup (path);
inputs = g_list_append (inputs, new_include);
return fgets_inc(s, size);
}
return s;
......@@ -203,8 +209,8 @@ yyerror (char *fmt, ...)
va_start(ap, fmt);
fprintf (stderr, "%s: line %d(%d): ",
inputs[n_input].filename,
inputs[n_input].yylineno, inputs[n_input].yylinepos);
LASTINPUT->filename,
LASTINPUT->yylineno, LASTINPUT->yylinepos);
vfprintf (stderr, fmt, ap);
fprintf(stderr, "\n");
......@@ -350,8 +356,8 @@ nextchar ()
void
yyparsetail (void)
{
output ("#line %d \"%s\"\n", inputs[n_input].yylineno,
inputs[n_input].filename);
output ("#line %d \"%s\"\n", LASTINPUT->yylineno,
LASTINPUT->filename);
while (fgets_inc (input, sizeof (input)))
output ("%s", input);
}
......@@ -366,7 +372,7 @@ yylex (void)
if (!(c = nextchar ()))
return 0;
inputs[n_input].yylinepos = next - input;
LASTINPUT->yylinepos = next - input;
if (isspace (c))
continue;
......@@ -441,8 +447,8 @@ yylex (void)
unsigned i = 0, d = 1;
static char buf [100000];
i = sprintf (buf, "#line %d \"%s\"\n", inputs[n_input].yylineno,
inputs[n_input].filename);
i = sprintf (buf, "#line %d \"%s\"\n", LASTINPUT->yylineno,
LASTINPUT->filename);
while (d && (c = nextchar ())) {
buf [i++] = c;
assert (i < sizeof (buf));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment