Mercurial > hg > Applications > Grep
view c/regexParser/regexParser.cc @ 142:de0f332d560c pairPro
insert charClassMerge function
author | masa |
---|---|
date | Fri, 11 Dec 2015 14:54:00 +0900 |
parents | 15815fcb6c2f |
children | 32977f5a2ed0 |
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.w.word = ri->tokenValue; cc->cond.w.length = ri->ptr - ri->tokenValue; cc->nextState.bitContainer = 0; 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; } CharClassPtr charClassMerge(CharClassPtr src, CharClassPtr add) { // 重なっているccの領域を分割する // 必要ならばnextStateを重ねあわせる // 変更があった場合は新しくリストを作って返す if (src->type == 'a') { if (add->type == 'a') { if (src->cond.w.word[0] > add->cond.w.word[0]) { // add のほうが小さいので小さい順のccをつくる CharClassPtr left = charClassMerge(add->left.src); return createCharClassWord(add->cond->w.word, left, add->right); } else { } } else if (add->type == 'c') { // if (src->cond.w.word[0] < add->cond.range.begin) { } else (src->cond->w.word[0] < add->end) { } else if (src->type == 'c') { } } // <charClass> ::= '['<literal>'-'<literal>']' static NodePtr charClass(RegexInfoPtr ri) { CharClassPtr cc = NEW(CharClass); NodePtr n = createNode(ri,'c',cc,0,0); cc->type = 'r'; cc->nextState.bitContainer = 0; RangeListPtr rangeList = &cc->cond.range; rangeList->begin = ri->ptr; rangeList->end = ri->ptr; for (ri->ptr++; *ri->ptr && *ri->ptr != ']'; ri->ptr++) { if (*ri->ptr == '-') { rangeList->end = ri->ptr + 1; ri->ptr++; continue; } if (ri->ptr[0] == 0 || ri->ptr[0] == ']') break; if (ri->ptr[0] == rangeList->end[0] + 1) { rangeList->end = ri->ptr; continue; } rangeList->next = NEW(RangeList); rangeList = rangeList->next; rangeList->begin = ri->ptr; rangeList->end = ri->ptr; rangeList->next = NULL; } // TODO literal support // merge rangeList here if (*ri->ptr) ri->ptr++; 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 = ']'; 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 if (ri->ptr[0] == '[') { ri->ptr++; ri->tokenType = 'c'; ri->tokenValue = ri->ptr; return; } 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; }