source: icGREP/icgrep-devel/icgrep/re_reducer.cpp @ 3955

Last change on this file since 3955 was 3955, checked in by daled, 5 years ago

icGREP now uses scanthru for multibyte unicode character classes.

File size: 2.4 KB
Line 
1#include "re_reducer.h"
2
3
4RE* RE_Reducer::reduce(RE* re, std::map<std::string, RE*>& re_map)
5{
6    RE* retVal = 0;
7
8    if (Alt* re_alt = dynamic_cast<Alt*>(re))
9    {
10        std::list<RE*> re_list;
11        std::list<RE*>::reverse_iterator rit = re_alt->GetREList()->rbegin();
12
13        for (rit = re_alt->GetREList()->rbegin(); rit != re_alt->GetREList()->rend(); ++rit)
14        {
15            re_list.push_back(reduce(*rit, re_map));
16        }
17
18        retVal = new Alt(&re_list);
19    }
20    else if (Seq* re_seq = dynamic_cast<Seq*>(re))
21    {
22
23        if (re_seq->getType() == Seq::Byte)
24        {
25            //If this is a sequence of byte classes then this is a multibyte sequence for a Unicode character class.
26            std::list<RE*> re_list;
27            std::list<RE*>::iterator it;
28
29            for (it = re_seq->GetREList()->begin(); it != re_seq->GetREList()->end(); ++it)
30            {
31                re_list.push_front(reduce(*it, re_map));
32            }
33
34            Seq* new_seq =  new Seq(&re_list);
35            new_seq->setType(Seq::Byte);
36            std::string seqname = new_seq->getName();
37            re_map.insert(make_pair(seqname, new_seq));
38            Name* name = new Name(seqname);
39            name->setType(Name::Unicode);
40            retVal = name;
41        }
42        else
43        {
44            std::list<RE*> re_list;
45            std::list<RE*>::iterator it;
46
47            for (it = re_seq->GetREList()->begin(); it != re_seq->GetREList()->end(); ++it)
48            {
49                re_list.push_front(reduce(*it, re_map));
50            }
51
52            retVal = new Seq(&re_list);
53        }
54    }
55    else if (Rep* re_rep = dynamic_cast<Rep*>(re))
56    {
57        retVal = new Rep(reduce(re_rep->getRE(), re_map), re_rep->getLB(), re_rep->getUB());
58    }
59    else if (CC* re_cc = dynamic_cast<CC*>(re))
60    {
61        std::string ccname = re_cc->getName();
62        //If the character class isn't in the map then add it.
63        re_map.insert(make_pair(ccname, re_cc));
64        //return a new name class with the name of the character class.
65        retVal = new Name(ccname);
66    }
67    else if (Name* re_name = dynamic_cast<Name*>(re))
68    {
69        Name* name = new Name(re_name->getName());
70        name->setType(re_name->getType());
71        retVal = name;
72    }
73    else if (Start* re_start = dynamic_cast<Start*>(re))
74    {
75        retVal = new Start();
76    }
77    else if (End* re_end = dynamic_cast<End*>(re))
78    {
79        retVal = new End();
80    }
81
82    return retVal;
83}
Note: See TracBrowser for help on using the repository browser.