view regexParser/generateLoop.cc @ 324:879dc5d1cb6a default tip

fix
author mir3636
date Fri, 27 May 2016 21:21:09 +0900
parents 90ccd94906c0
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include "generateSequentialSearch.h"
#include "CharClass.h"
#include "subsetConstruction.h"


static void
generateState1(StatePtr state,long stateNum, bool accept, FILE *fp, TransitionGeneratorPtr tg) {

    fprintf(fp,"static TSValue state%lx(TSValue tsv) {\n",stateNum);
    if (accept && state->accept) {
        fputs("    tsv=tsv.tg->stateMatch(tsv);\n",fp);
    }
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    if (hasNext(ccw)) fputs("    unsigned char c = *tsv.buff.buffptr++;\n",fp);
    fputs("    if (0) ;\n",fp);
    while (hasNext(ccw)) {
        CharClassPtr cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        BitVector bi = cc->nextState;
        if (begin == end) {
            fprintf(fp,"    else if (c=='%c') { tsv.current=(TState*)state%lx;}\n",(unsigned char)begin, bi.bitContainer);
        } else {
            fprintf(fp,"    else if (c<'%c')  { tsv=tsv.tg->stateSkip(tsv);tsv.current=(TState*) state0;}\n",(unsigned char)begin);
            fprintf(fp,"    else if (c<='%c') { tsv.current=(TState*) state%lx;} \n",(unsigned char)end,  bi.bitContainer);
        }
    }
    free(ccw);
    fprintf(fp,"    else { tsv=tsv.tg->stateSkip(tsv); tsv.current=(TState*)state0; }\n    return tsv;\n");
    fputs("}\n\n",fp);
} 

void
exportStateLoop(TransitionGeneratorPtr tg) {
    StatePtr state = tg->stateList;
    FILE *fp = fopen("state.cc","w");
    if (fp==NULL) {
        perror("");
        fprintf(stderr,"cannot write state.cc\n");
        exit(1);
    }
    fprintf(fp,"static TSValue state%lx(TSValue tsv) ;\n",0L);
    for (;state;state = state->next) {
        if (state->bitState.bitContainer!=1)  // state1 always unused but state0
            fprintf(fp,"static TSValue state%lx(TSValue tsv);\n",state->bitState.bitContainer);
    }
    fputs("\n",fp);

    fprintf(fp,
"\n"
"static TSValue \n"
"stateLoop(TSValue tsv) {\n"
"    tsv.current = (TState*)state0;\n"
"    while(tsv.buff.buffptr < tsv.buff.buffend) {\n"
"        TSValue (*next)(TSValue) = (TSValue (*)(TSValue))tsv.current;\n"
"        tsv = next(tsv);\n"
"    }\n"
"    return tsv;\n"
"}\n\n" );


    // initial state must not accept empty string
    generateState1(tg->stateList,0L,false,fp,tg);
    for (state = tg->stateList;state;state = state->next) {
        if (state->bitState.bitContainer!=1)  // state1 always unused but state0
            generateState1(state,state->bitState.bitContainer,true,fp,tg);
    }
    fprintf(fp,"#define state0 stateLoop\n");
    fclose(fp);
}