view regexParser/generateSequentialSearch.cc @ 296:28c695dac305

Todo
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 02 Feb 2016 11:50:33 +0900
parents f2491681914e
children 63213964502a
line wrap: on
line source

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

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

void exportState(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);
    }
    for (;state;state = state->next) {
        fprintf(fp,"void state%lx(Buffer buff);\n",state->bitState.bitContainer);
    }
    fputs("\n",fp);
    for (state = tg->stateList;state;state = state->next) {
        fprintf(fp,"void state%lx(Buffer buff) {\n",state->bitState.bitContainer);
        if (state->bitState.bitContainer == 2) { // Accept
            fputs("    stateMatch(buff);\n",fp);
        } else {  // not Accept
            fputs("    if (buff.buffptr >= buff.buffend) return;\n",fp);
            fputs("    unsigned char c = *buff.buffptr++;\n",fp);
            CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
            bool flag = true;
            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 (flag) {
                    flag = false;
                    fputs("    ",fp);
                } else {
                    fputs("    else ",fp);
                }
                if (begin == end) {
                    fprintf(fp,"if (c=='%c') state%lx(buff);\n",(unsigned char)begin, bi.bitContainer);
                } else {
                    if (state->accept) {
                        fprintf(fp,"if (c<'%c') stateMatch(buff);\n",(unsigned char)begin);
                    } else {
                        fprintf(fp,"if (c<'%c') stateSkip(buff);\n",(unsigned char)begin);
                    }
                    fprintf(fp,"    else if (c<='%c') state%lx(buff);\n",(unsigned char)end, bi.bitContainer);
                }
            }
            free(ccw);
            if (state->bitState.bitContainer & 2) {
                fprintf(fp,"    else stateMatch(buff);\n");
            } else {
                fprintf(fp,"    else stateSkip(buff);\n");
            }
        }
        fputs("}\n\n",fp);
    }
    fclose(fp);
}