/* ** (C) 2008 by Remo Dentato (rdentato@users.sourceforge.net) ** ** Permission to use, copy, modify and distribute this code and ** its documentation for any purpose is hereby granted without ** fee, provided that the above copyright notice, or equivalent ** attribution acknowledgement, appears in all copies and ** supporting documentation. ** ** Copyright holder makes no representations about the suitability ** of this software for any purpose. It is provided "as is" without ** express or implied warranty. ** */ #include #include #include #include #define RX_MAXNFA 4096 #include "rx.h" unsigned char nfa[RX_MAXNFA]; #define ln_len 2048 char ln[ln_len]; FILE *tmpf; FILE *inf; FILE *outf; char resvar[128]; char strvar[128]; int swnum = 0; int rxnum = 0; int lnnum = 0; int error(char *errmsg) { fprintf(stderr,errmsg); exit(1); } void outrx(int sym) { if (*nfa) { fprintf(outf,"static unsigned char RX_%d[]= { /* %d */\n",swnum,strlen(nfa)+1); rx_dump_num(outf,nfa); fprintf(outf,"\n};\n"); if (sym) { fprintf(outf,"\n/*\n"); rx_dump_asm(outf,nfa); fprintf(outf,"\n*/\n"); } } } #define IN_CODE 1 #define IN_COMMENT 2 int main(int argc, char *argv[]) { char *s,*t; char *err; int c; int state = IN_CODE; int argn = 1; int sym = 0; rx_result r; char *fname = "(stdin)"; *nfa = '\0'; inf = stdin; outf = stdout; tmpf = tmpfile(); if (tmpf == NULL) error("ERR202: Unable to open temp file"); while ((argn < argc) && (argv[argn][0] == '-')) { if (argv[argn][1] == 's') sym = 1; argn++; } if (argn < argc) { fname = argv[argn]; inf = fopen(fname,"r"); if (inf == NULL) error("ERR203: Unable to open input file"); } while ((s = fgets(ln,ln_len,inf))) { ++lnnum; while (*s) { switch(state) { case IN_CODE : RX_SWITCH(r,s) { case "^\/\*" : state = IN_COMMENT; case "^\s+" : case "^\Q" : case "^//.*" : case "^[^R/c\"]+": rx_write(r,0,tmpf); break; case "^case *{\Q} *:" : fprintf(tmpf,"case %d :",++rxnum); rx_end(r,1)[-1] = '\0'; err = rx_compile_add(rx_start(r,1)+1,nfa); if (err) { fprintf(stderr,"%d:ERR205: %s\n",lnnum,err); exit(1); } break; case "^RX_SWITCH *\( *{\I} *, *{\I} *\)" : outrx(sym); rx_cpy(r,1,resvar); rx_cpy(r,2,strvar); fprintf(tmpf,"RX_SWITCH(%s,%s,%d)",resvar,strvar,++swnum); rxnum = 0; *nfa = '\0'; break; case "^RX_{\u+} *\(" : fputs("rx_",tmpf); t = rx_start(r,1); while (t < rx_end(r,1)) fputc(tolower(*t++),tmpf); fprintf(tmpf,"(%s,",resvar); break; default : if (*s) fputc(*s++,tmpf); break; } break; case IN_COMMENT : RX_SWITCH(r,s) { case "^\*\/" : state = IN_CODE; case "^[^\*]+" : rx_write(r,0,tmpf); break; default : if (*s) fputc(*s++,tmpf); break; } break; default : error("Unexpected state!"); break; } } } outrx(sym); fprintf(outf,"#define RX_SWITCH(r,s,n) switch ((r = rx_exec(RX_##n,s))? (s=rx_end(r,0),rx_matched(r)) : 0)\n"); fprintf(outf,"#line 1 \"%s\"\n",fname); fseek(tmpf,SEEK_SET,0); while ((c = fgetc(tmpf)) != EOF) fputc(c,outf); if (outf != stdout) fclose(outf); if (inf != stdin) fclose(inf); fclose(tmpf); exit(0); }