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

token_spec.cc

Go to the documentation of this file.
00001 /*
00002  *  File:       token_spec.cc
00003  *              $Id: token_spec.cc,v 1.3 2002/06/13 11:42:47 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: token_spec.cc,v $
00012  *  Revision 1.3  2002/06/13 11:42:47  alec
00013  *  added #line stuff
00014  *
00015  *  Revision 1.2  2002/05/10 07:15:10  alec
00016  *  parser parse tree ok
00017  *
00018  *  Revision 1.1  2002/05/04 17:46:53  alec
00019  *  *** empty log message ***
00020  *
00021  */
00022 
00023 /* 
00024   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00025 
00026   This program is free software; you can redistribute it and/or modify
00027   it under the terms of the GNU General Public License as published by
00028   the Free Software Foundation; either version 2 of the License, or
00029   (at your option) any later version.
00030 
00031   This program is distributed in the hope that it will be useful,
00032   but WITHOUT ANY WARRANTY; without even the implied warranty of
00033   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00034   GNU General Public License for more details.
00035 
00036   You should have received a copy of the GNU General Public License
00037   along with this program; if not, write to the Free Software
00038   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00039 
00040  */
00041 
00042 #include <algorithm>
00043 
00044 #include "debug.h"
00045 #include "parse_util.hh"
00046 #include "token_spec.hh"
00047 #include "re_node.hh"
00048 
00049 void TokenSpec::setClassName (const string &className_)
00050 {
00051   className = className_;
00052 }
00053 
00054 
00055 void TokenSpec::setInheritance (const string &inheritance_,
00056                                 const Position &pos)
00057 {
00058   inheritance.pos = pos;
00059   inheritance.code = inheritance_;
00060 }
00061 
00062 
00063 void TokenSpec::setPreambleCode (const string &preambleCode_,
00064                                  const Position &pos)
00065 {
00066   preambleCode.pos = pos;
00067   preambleCode.code = preambleCode_;
00068 }
00069 
00070 
00071 void TokenSpec::addCodeBlock (const string &block, const Position &pos)
00072 {
00073   userCode.push_back(CodeChunk(pos, block));
00074 }
00075 
00076 void TokenSpec::addToken (const string &tokName, Tkinds tokKind,
00077                           ReNode *regexp, const string &tokAction,
00078                           const Position &pos) throw (ParseException)
00079 {
00080   if (isToken(tokName))
00081     throw ParseException(pos, string("Duplicate token name ") + tokName +
00082                          ".");
00083   tokens.push_back(TokenDesc(tokName, tokens.size(), tokKind, regexp,
00084                              tokAction, pos));
00085 }
00086 
00087 
00088 void TokenSpec::addToken (const string &tokName, const Position &pos)
00089   throw (ParseException)
00090 {
00091   if (isToken(tokName))
00092     throw ParseException(pos, string("Duplicate token name ") + tokName +
00093                          ".");
00094   tokens.push_back(TokenDesc(tokName, tokens.size(), pos));
00095 }
00096 
00097 
00098 bool operator == (const TokenDesc &t, const string &s)
00099 {
00100   return t.name() == s;
00101 }
00102 
00103 bool TokenSpec::isToken (const string &tokName)
00104 {
00105   return find(tokens.begin(), tokens.end(), tokName) != tokens.end();
00106 }
00107 
00108 
00109 int TokenSpec::getTokenId (const string &tokName)
00110 {
00111   vector<TokenDesc>::iterator i = find(tokens.begin(), tokens.end(), tokName);
00112 
00113   if (i == tokens.end()) return -1;
00114   else return (*i).id();
00115 }
00116 
00117 
00118 TokenDesc& TokenSpec::operator [] (int tokId)
00119 {
00120   ASSERT((0 <= tokId) && (tokId < tokens.size()), "Bad index in TokenSpec::operator[]");
00121   return tokens[tokId];
00122 }
00123 
00124 
00125 int TokenSpec::count ()
00126 {
00127   return tokens.size();
00128 }
00129 
00130 
00131 #ifdef DEBUG
00132 
00133 
00134 void TokenSpec::dump (ostream &os) const
00135 {
00136   
00137 }
00138 
00139 void TokenDesc::dump (ostream &os) const
00140 {
00141   static char *kindNames[] = {"regular", "skip", "more", "keyword", "special"};
00142   os << "Token: " << _name << "(" << kindNames[_kind] << ") @ " <<
00143     _pos.ln << ":" << _pos.col << endl;
00144 
00145   if (_kind != ITokenSpec::special) _regexp->dump(os);
00146 }
00147 
00148 #endif

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