00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "dfa_re_node_builder.hh"
00054 #include "dfa_source_re.hh"
00055 #include "vbitset.hh"
00056 #include "parse_util.hh"
00057 #include "prop_registry.hh"
00058
00059
00060 ReNode* DfaReNodeBuilder::createOrNode (ReNode *pre,
00061 ReNode *post, const Position &pos)
00062 {
00063 return new ReOrNode(dynamic_cast<DfaSourceRe*>(pre),
00064 dynamic_cast<DfaSourceRe*>(post), pos);
00065 }
00066
00067
00068 ReNode* DfaReNodeBuilder::createCatNode (ReNode *pre, ReNode *post,
00069 const Position &pos)
00070 {
00071 return new ReCatNode(dynamic_cast<DfaSourceRe*>(pre),
00072 dynamic_cast<DfaSourceRe*>(post), pos);
00073 }
00074
00075
00076 ReNode* DfaReNodeBuilder::createPlusNode (ReNode *in, const Position &pos)
00077 {
00078 return new ReCatNode(dynamic_cast<DfaSourceRe*>(in->clone()),
00079 new ReStarNode(dynamic_cast<DfaSourceRe*>(in), pos),
00080 pos);
00081 }
00082
00083
00084 ReNode* DfaReNodeBuilder::createOptionalNode (ReNode *in, const Position &pos)
00085 {
00086 return new ReOrNode(dynamic_cast<DfaSourceRe*>(in),
00087 new ReLambdaNode(pos), pos);
00088 }
00089
00090
00091 ReNode* DfaReNodeBuilder::createStarNode (ReNode *in, const Position &pos)
00092 {
00093 return new ReStarNode(dynamic_cast<DfaSourceRe*>(in), pos);
00094 }
00095
00096
00097 ReNode* DfaReNodeBuilder::createStringLiteralNode (const string& s,
00098 const Position &pos)
00099 {
00100 if (s.size() == 0)
00101 return new ReLambdaNode(pos);
00102 else {
00103 DfaSourceRe *res = new ReCharNode(s[0], pos);
00104 for (int i = 1; i < s.size(); i++)
00105 res = new ReCatNode(res, createCharNode(s[i], pos), pos);
00106 return res;
00107 }
00108 }
00109
00110
00111 ReNode* DfaReNodeBuilder::createCharListNode (bool negated,
00112 const vector<CharClassDescriptor> &chars,
00113 const Position &pos)
00114 {
00115 vBitset chset(256);
00116 for (int i = 0; i < chars.size(); i++)
00117 for (int c = chars[i].first; c <= chars[i].last; c++)
00118 chset[c].set();
00119 if (negated) chset.flip();
00120
00121 if (chset.count1() == 0)
00122 die(formatError(pos, "Character class does not match any character."));
00123
00124 DfaSourceRe *res = NULL;
00125
00126 bool caseSensitive = registry["CASE_SENSITIVE"];
00127
00128 for (int c = 0; c < 256; c++)
00129 if (chset[c].get()) {
00130 if (!caseSensitive)
00131 if (isupper(c) && chset[tolower(c)].get()) continue;
00132 if (res == NULL) res = createCharNode(c, pos);
00133 else res = new ReOrNode(res, createCharNode(c, pos), pos);
00134 }
00135
00136 return res;
00137 }
00138
00139
00140 DfaSourceRe* DfaReNodeBuilder::createEotNode (int tokId, const Position &pos)
00141 {
00142 return new ReEotNode(tokId, pos);
00143 }
00144
00145
00146 DfaSourceRe* DfaReNodeBuilder::createCharNode (unsigned char match,
00147 const Position &pos)
00148 {
00149 if (!registry["CASE_SENSITIVE"])
00150 {
00151 if (isupper(match)) return new ReOrNode(new ReCharNode(match, pos),
00152 new ReCharNode(tolower( match),
00153 pos),
00154 pos);
00155 if (islower(match)) return new ReOrNode(new ReCharNode(match, pos),
00156 new ReCharNode(toupper( match),
00157 pos),
00158 pos);
00159 }
00160 return new ReCharNode(match, pos);
00161 }