view regexParser/threadedSearch.cc @ 318:c9458ffecb87

word mode in tSearch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 08 May 2016 11:56:49 +0900
parents 1188debbef10
children 7b8234c090f7
line wrap: on
line source

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

#include "regexParser.h"
#include "CharClass.h"
#include "threadedSearch.h"
#include "subsetConstruction.h"

static
TSValue stateNothing(TSValue tsv) {
    return tsv;
}

static
TSValue stateSkip(TSValue tsv) {
    if (tsv.matchEnd) {
        fwrite(tsv.matchBegin,tsv.matchEnd-tsv.matchBegin,1,stdout);
        puts("");
        tsv.matchEnd = NULL;
    }
    tsv.matchBegin = tsv.buff.buffptr;  // next char may be matchBegin
    return tsv;
}

static
TSValue stateMatch(TSValue tsv) {
    tsv.matchEnd = tsv.buff.buffptr;    // next char of the match
    return tsv;
}

TStatePtr generateTState(StatePtr state, TransitionGeneratorPtr tg) {
    TStatePtr tState = NEW(TState);
    tState->state = state;
    int ccvSize = 0;
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    while (hasNext(ccw)) {
        getNext(ccw);
        ccvSize++;
    }
    tState->ccvSize = ccvSize;
    if (state->accept) {
        tState->stateMatch  = tg->stateMatch;
        tState->stateSkip  = tg->stateSkip;
    } else {
        tState->stateMatch  = tg->stateNothing;
        tState->stateSkip  = tg->stateSkip;
    }
    if (ccvSize == 0) {
        tState->ccv = NULL;
        state->tState = tState;
        return tState;
    } else tState->ccv = (ccv*)malloc(sizeof(ccv)*ccvSize);
    ccw = createCharClassWalker(state->cc);
    int i = 0;
    while (hasNext(ccw)) {
        CharClassPtr cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        struct ccv *ccv = &tState->ccv[i++];
        ccv->begin = begin;
        ccv->end = end;
        ccv->tState = NULL;
        ccv->state = cc->nextState;
        ccv->w = cc->cond.w;
    }
    free(ccw);
    state->tState = tState;
    return tState;
}

TStatePtr nextTState(BitVector bi,TransitionGeneratorPtr tg) {
    // create tSearch in next state.
    StatePtr state = tg->stateArray[bi.bitContainer];
    if (state == NULL) {
        // on the fly subset construction.
        state = createState(tg,bi);
        determinize(state,tg);
        tg->stateArray[bi.bitContainer] = state;
    }
    if (state->tState == NULL) {
        generateTState(state,tg);
    }
    return state->tState;
}

#define DEBUG 0

TSValue tSearch(TSValue tsv) {
#if DEBUG
    TSValuePtr tsvp = &tsv;   // make tsv visible in lldb
#endif
    next: while (tsv.buff.buffptr < tsv.buff.buffend) {
        tsv = tsv.current->stateMatch(tsv);
        if (tsv.current->ccvSize==0) {
            // matched start again
            tsv.current = tsv.tg->stateStart->tState;
        }
        unsigned char c = *tsv.buff.buffptr++;
//        printState(tsv.current->state);
        for (int i = 0; i < tsv.current->ccvSize; i++) {
            CCVPtr ccv = &tsv.current->ccv[i];
            if (c<ccv->begin) {
                tsv.current = tsv.tg->stateStart->tState;
                tsv = tsv.current->stateSkip(tsv);
                goto next;
            } else if (c<=ccv->end) {
                // range matched.
                if (ccv->w.word) {
                    WordPtr w;
                    for (w = &ccv->w;w;w = w->next) {
                        // match the word.
                        if (strncmp((const char *)w->word,(const char *)tsv.buff.buffptr-1,w->length)==0) break;
                    }
                    if (!w) continue; // if (not match) continue;
                    tsv.buff.buffptr += w->length - 1;
                }
                if (ccv->tState) {
                    tsv.current = ccv->tState;
                } else {
                    tsv.current = nextTState(ccv->state,tsv.tg);
                    ccv->tState = tsv.current;
                }
                goto next;
            }
        }
        tsv.current = tsv.tg->stateStart->tState;
        tsv = tsv.current->stateSkip(tsv);
    }
#if DEBUG
    *tsvp = tsv;
    return *tsvp;
#else
    return tsv;
#endif
}

TSValue
createTSValue(TransitionGeneratorPtr tg, Buffer buff) {
    TSValue tsv;
    if (!tg) {
        tg = createTransitionGenerator();
    }
    tsv.buff = buff;
    tsv.tg = tg;
    tsv.blk = NULL;
    tsv.matchBegin = buff.buffptr;
    tsv.matchEnd = NULL;
    tsv.tg->stateSkip = stateSkip;
    tsv.tg->stateMatch = stateMatch;
    tsv.tg->stateNothing = stateNothing;
    return tsv;
}


void threadedSearch(TransitionGeneratorPtr tg, Buffer buff) {
    TSValue tsv = createTSValue(tg,buff);
    tsv.current = generateTState(tg->stateList,tg);
    tg->stateStart = NEW(State);
    *tg->stateStart = *tg->stateList;
    tg->stateStart->accept = false; // Start state never accept
    generateTState(tg->stateStart,tg);
    tSearch(tsv);
}

/* end */