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 #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