Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

dfa_re_node_builder.cc

Go to the documentation of this file.
00001 /*
00002  *  File:       dfa_re_node_builder.cc
00003  *              $Id: dfa_re_node_builder.cc,v 1.6 2002/05/27 02:59:27 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: dfa_re_node_builder.cc,v $
00012  *  Revision 1.6  2002/05/27 02:59:27  alec
00013  *  doc update
00014  *
00015  *  Revision 1.5  2002/05/22 01:23:12  alec
00016  *  case sensitivity support
00017  *
00018  *  Revision 1.4  2002/05/01 09:18:26  alec
00019  *  - vBitset fixes
00020  *  - FOLLOWPOS written (not tested yet)
00021  *
00022  *  Revision 1.3  2002/04/30 16:24:38  alec
00023  *  tested the scanner ptree & vBitVector implementation -> ok
00024  *
00025  *  Revision 1.2  2002/04/30 09:22:32  alec
00026  *  bitset fixes
00027  *
00028  *  Revision 1.1  2002/04/29 09:40:01  alec
00029  *  *** empty log message ***
00030  *
00031  */
00032 
00033 /* 
00034   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00035 
00036   This program is free software; you can redistribute it and/or modify
00037   it under the terms of the GNU General Public License as published by
00038   the Free Software Foundation; either version 2 of the License, or
00039   (at your option) any later version.
00040 
00041   This program is distributed in the hope that it will be useful,
00042   but WITHOUT ANY WARRANTY; without even the implied warranty of
00043   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00044   GNU General Public License for more details.
00045 
00046   You should have received a copy of the GNU General Public License
00047   along with this program; if not, write to the Free Software
00048   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 { // r+ -> rr*
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 { // r? -> r | lambda
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) //empty string => lambda
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) // avoid unnecesasry duplicate nodes.
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 }

Generated at Tue Jul 9 21:05:43 2002 for CppCC by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001