view c/regexParser/regexParser.cc @ 134:dbafc753078e pairPro

fix concatination & selection & grouping
author masa
date Fri, 04 Dec 2015 17:45:09 +0900
parents ccc673449351
children e1a262ec75f0
line wrap: on
line source

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "regexParser.h"
#include "error.h"

static NodePtr charClass(RegexInfoPtr);
static void token(RegexInfoPtr);
static NodePtr regexAtom(RegexInfoPtr);
NodePtr regex(RegexInfoPtr);

/**
 * Create a node of regex parse tree.
 *     tokenType
 *     regexPosition(state)
 *     stateTransitionTable
 */

static
NodePtr allocateNode() {
    NodePtr n = NEW(Node);
    n->cc = NULL;
    n->left = NULL;
    n->right = NULL;
    return n;
}

static
CharClassPtr createCharClassWord(RegexInfoPtr ri) {
    CharClassPtr cc = NEW(CharClass);
    cc->type = 'a';
    cc->cond = NEW(Condition);
    cc->cond->w = NEW(Word);
    cc->cond->w->word = ri->tokenValue;
    cc->cond->w->length = ri->ptr - ri->tokenValue;
    token(ri);

    return cc;
}

static
NodePtr createNode(RegexInfoPtr ri,unsigned char type,CharClassPtr cc, NodePtr left, NodePtr right) {
    NodePtr n = allocateNode();

    n->tokenType = type;
    n->cc = cc;
    n->left = left;
    n->right = right;
    n->nodeNumber = ri->nodeNumber;
    ri->nodeNumber++;

    return n;
}


// <charClass> ::= '['<literal>'-'<literal>']'
static
NodePtr charClass(RegexInfoPtr ri) {

    CharClassPtr cc = NEW(CharClass);
    cc->type = 'r';
    cc->cond = NEW(Condition);
    cc->cond->range = NEW(RangeList);
    cc->cond->range->begin = ri->ptr;
    cc->cond->range->end = ri->ptr + 1;
    cc->cond->range->next = NULL;

    int i = 0;

    RangeListPtr rangeList = cc->cond->range;

    while (ri->ptr[i] != ']') {
        if (ri->ptr[i] == '-') i++;

        rangeList->end = ri->ptr + i;
        rangeList->next = NEW(RangeList);
        rangeList = rangeList->next;
        rangeList->begin = ri->ptr+i+1;
        rangeList->next = NULL;
        i++;
    }
            // TODO literal support

    rangeList->end = ri->ptr + i - 1;
    NodePtr n = createNode(ri,'c',cc,0,0);
    token(ri);
    return n;
}

// <literal> ::= [a-z][A-Z][0-9]
static
NodePtr literal(RegexInfoPtr ri) {
    CharClassPtr cc = createCharClassWord(ri);
    NodePtr n = createNode(ri,'a',cc,0,0);
    return n;
}

static
void token(RegexInfoPtr ri) {
    while (ri->ptr[0] != '\0') {
        if (ri->ptr[0] == '('){
            ri->ptr++;
            ri->tokenType = '(';
            ri->tokenValue = NULL;
            return;
        } else if (ri->ptr[0] == ')') {
            ri->ptr++;
            ri->tokenType = ')';
            ri->tokenValue = ri->ptr;
            return;
        } else if (ri->ptr[0] == '[') {
            ri->ptr++;
            ri->tokenType = 'c';
            ri->tokenValue = ri->ptr;
            return;
        } else if (ri->ptr[0] == '|'){
            ri->ptr++;
            ri->tokenType = '|';
            ri->tokenValue = NULL;
            ri->orNum++;
            return;
        } else if (ri->ptr[0] == '*'){
            ri->ptr++;
            ri->tokenType = '*';
            ri->tokenValue = NULL;
            return;
        } else if (ri->ptr[0] == '\\'){
            // need more proccesing 
            /*
                \277
                \0xa5
                \[
                \\
                \utf-8 etc...
            */
        } else {
            ri->tokenType = 'a';
            ri->tokenValue = ri->ptr;
            if (isalnum(ri->ptr[0])) {
                ri->ptr++;
            }
            return;
        }
    }
    ri->tokenType = 0;
    ri->tokenValue = NULL;
    return;
}

// <regexAtom> ::= <literal>|<charClass>|<group>
static
NodePtr regexAtom(RegexInfoPtr ri) {

    NodePtr n = NULL;
    if (ri->tokenType == 'c') n = charClass(ri);
    else if (ri->tokenType == 'a') n = literal(ri);
    else if (ri->tokenType == '(') {
        n = regex(ri);
        if (ri->tokenType != ')') {
            // error
        }
        token(ri);
    }
    if (ri->tokenType == '*') {
        n = createNode(ri,'*',0,n,0);
        token(ri);
    }

    return n;
}

// <regex> ::= <regexAtom> | <regexAtom>'*'<regex> | <regexAtom>'|'<regex> | <regexAtom><regexAtom>'*' | <regexAtom><regex>
NodePtr regex(RegexInfoPtr ri) {
    token(ri);
    NodePtr n = regexAtom(ri);
    while (ri->tokenType) {
        if (ri->tokenType == '*') {
            n = createNode(ri,'*',0,n,0);
            token(ri);
            return n;
        } else if (ri->tokenType == '|') {
            n = createNode(ri,'|',0,n,0);
            NodePtr n1 = regex(ri);
            n->right = n1;
        } else if (ri->tokenType == ')') {
            return n;
        } else {
            n = createNode(ri,'+',0,n,0);
            NodePtr n1 = regexAtom(ri);
            n->right = n1;
        }
    }
    return n;
}