#include "global.h" #include "source.h" #include "lex.h" #include "list.h" #include "irep.h" #include "parser.h" #include "lexparse.h" #include "fsa.h" #include "fsatab.h" #include "gen.h" // The copy() member function allows the substitution of markers // signified by %%x markers in the template file. In those places, the // various member functions of the FsaTable class are invoked. void Copier::copy() { // The markers to be substituted are specified in the following lookup // table: static struct entry { char *label; void (FsaTable::*f)(FILE *); } lookup[] = { { "nomodsWarning", &FsaTable::pr_nomodsWarning }, { "initialState", &FsaTable::pr_initialState }, { "states", &FsaTable::pr_states }, { "charSet", &FsaTable::pr_charSet }, { "prelude", &FsaTable::pr_prelude }, { "postlude", &FsaTable::pr_postlude }, { "stateTransition", &FsaTable::pr_stateTransition }, { "filters", &FsaTable::pr_filters }, { "finalStates", &FsaTable::pr_finalStates } }; int c; start: while ((c = getc(fd)) != EOF) if (c == '%') if ((c = getc(fd)) == '%') { char mark[1024]; if (fscanf(fd, "%[a-zA-Z]", mark) == 1) { for (int i=0; i<(sizeof(lookup)/sizeof(struct entry)); i++) if (strcmp(lookup[i].label, mark) == 0) { (table->*lookup[i].f)(out); goto start; } fprintf(out, "[cannot act on %s]", mark); } else { putc('%', out); putc('%', out); } } else { putc('%', out); putc(c, out); } else { if (c == '\n') position++; putc(c, out); } } Copier::Copier(FsaTable *t, char *inf, char *outf) { currentfile = inf; position = 0; table = t; fd = fopen(inf, "r"); if (!fd) error(ABORT, "cannot read %s\n", inf); out = fopen(outf, "w"); if (!out) error(ABORT, "cannot write %s\n", outf); copy(); fclose(fd); fclose(out); } char *Copier::currentfile = 0; int Copier::position = 0;