Commit 8cb9d1ab authored by Michaël Cadilhac's avatar Michaël Cadilhac
Browse files

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

	* parser.y:  Add  a  `%include'  directive  which  includes  files
	verbatim. Syntax is:
	%include filename
	without double quote or anything.
	
	This directive could be used anywhere.
	
	* parser.y (yyerror):   Enhance    verbosity   on   error  message
	accordingly.

	* monoburg.h: Add the info about included file storage.
	* monoburg.c: Update accordingly use of `inputfd'.
parent 338aa647
2005-06-04 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* parser.y: Add a `%include' directive which includes files
verbatim. Syntax is:
%include filename
without double quote or anything.
This directive could be used anywhere.
* parser.y (yyerror): Enhance verbosity on error message
accordingly.
* monoburg.h: Add the info about included file storage.
* monoburg.c: Update accordingly use of `inputfd'.
2005-06-04 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* parser.y: Add a `%namespace' directive which makes monoburg
......
......@@ -21,7 +21,7 @@ static GList *nonterm_list;
static GList *rule_list;
static GList *prefix_list;
FILE *inputfd;
File inputs[MAX_FDS];
FILE *outputfd;
GHashTable *definedvars;
static FILE *deffd;
......@@ -1217,24 +1217,27 @@ main (int argc, char *argv [])
emit_includes ();
inputs[0].yylineno = 0;
if (infiles) {
GList *l = infiles;
while (l) {
char *infile = (char *)l->data;
if (!(inputfd = fopen (infile, "r"))) {
if (!(inputs[0].fd = fopen (infile, "r"))) {
perror ("cant open input file");
exit (-1);
}
inputs[0].filename = infile;
yyparse ();
reset_parser ();
l->data = inputfd;
l->data = inputs[0].fd;
l = l->next;
}
} else {
inputfd = stdin;
inputs[0].fd = stdin;
inputs[0].filename = "stdin";
yyparse ();
}
......@@ -1288,9 +1291,9 @@ main (int argc, char *argv [])
if (infiles) {
GList *l = infiles;
while (l) {
inputfd = l->data;
inputs[0].fd = l->data;
yyparsetail ();
fclose (inputfd);
fclose (inputs[0].fd);
l = l->next;
}
} else {
......
......@@ -3,12 +3,13 @@
#include <glib.h>
#define MAX_FDS 10
#define MAX_FILENAME_LEN 100
#define MAX_NAMESPACES 10
void yyerror (char *fmt, ...);
int yylex (void);
extern FILE *inputfd;
extern FILE *outputfd;
extern GHashTable *definedvars;
extern char *namespaces[MAX_NAMESPACES];
......@@ -51,6 +52,17 @@ struct _Rule {
char *cfunc;
};
typedef struct _File File;
struct _File {
int yylineno;
int yylinepos;
char *filename;
FILE *fd;
};
extern File inputs[MAX_FDS];
Tree *create_tree (char *id, Tree *left, Tree *right);
......
......@@ -15,12 +15,10 @@
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#include <errno.h>
#include "monoburg.h"
static int yylineno = 0;
static int yylinepos = 0;
%}
%union {
......@@ -79,7 +77,7 @@ rule_list : rule { $$ = g_list_append (NULL, $1); }
optcode : /* empty */ { $$ = NULL; }
| ';' { $$ = NULL; }
| CODE
| CODE
;
plist : /* empty */
......@@ -108,6 +106,45 @@ optcfunc : /*empty */ { $$ = NULL; }
static char input[2048];
static char *next = input;
static int n_input = 0;
char *fgets_inc(char *s, int size)
{
int n_length;
char filename[MAX_FILENAME_LEN];
/* 9 == strlen("%include "); */
assert (size > 9 + MAX_FILENAME_LEN);
if (fgets (s, size, inputs[n_input].fd) == NULL) {
if (n_input == 0)
return 0;
free (inputs[n_input].filename);
fclose (inputs[n_input--].fd);
return fgets_inc(s, size);
}
inputs[n_input].yylineno++;
inputs[n_input].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);
if (!(inputs[n_input + 1].fd = fopen (filename, "r")))
yyerror ("`%%include %s': %s",
filename, strerror(errno));
inputs[++n_input].yylineno = 0;
inputs[n_input].filename = strdup (filename);
return fgets_inc(s, size);
}
return s;
}
void
yyerror (char *fmt, ...)
......@@ -115,8 +152,9 @@ yyerror (char *fmt, ...)
va_list ap;
va_start(ap, fmt);
fprintf (stderr, "line %d(%d): ", yylineno, yylinepos);
fprintf (stderr, "%s: line %d(%d): ",
inputs[n_input].filename,
inputs[n_input].yylineno, inputs[n_input].yylinepos);
vfprintf (stderr, fmt, ap);
fprintf(stderr, "\n");
......@@ -201,7 +239,7 @@ nextchar ()
next = input;
*next = 0;
do {
if (!fgets (input, sizeof (input), inputfd))
if (!fgets_inc (input, sizeof (input)))
return 0;
ll = (input[0] == '%' && input[1] == '%');
......@@ -254,7 +292,6 @@ nextchar ()
}
ll = state != 1 || input[0] == '#';
state = next_state;
yylineno++;
} while (next_state == 2 || ll);
}
......@@ -265,9 +302,8 @@ void
yyparsetail (void)
{
fputs (input, outputfd);
while (fgets (input, sizeof (input), inputfd))
while (fgets_inc (input, sizeof (input)))
fputs (input, outputfd);
input[0] = '\0';
}
int
......@@ -280,7 +316,7 @@ yylex (void)
if (!(c = nextchar ()))
return 0;
yylinepos = next - input + 1;
inputs[n_input].yylinepos = next - input;
if (isspace (c))
continue;
......
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