Select Git revision
SymbolTable.cc
SymbolTable.cc 6.04 KiB
/*
* Copyright (C) 2003-2009 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <sstream>
#include "SymbolTable.hh"
SymbolTable::SymbolTable() : frozen(false), size(0)
{
}
void
SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException)
{
if (frozen)
throw FrozenException();
if (exists(name))
{
if (type_table[getID(name)] == type)
throw AlreadyDeclaredException(name, true);
else
throw AlreadyDeclaredException(name, false);
}
int id = size++;
symbol_table[name] = id;
type_table.push_back(type);
name_table.push_back(name);
tex_name_table.push_back(tex_name);
}
void
SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException)
{
// Construct "tex_name" by prepending an antislash to all underscores in "name"
string tex_name = name;
size_t pos = 0;
while((pos = tex_name.find('_', pos)) != string::npos)
{
tex_name.insert(pos, "\\");
pos += 2;
}
addSymbol(name, type, tex_name);
}
void
SymbolTable::freeze() throw (FrozenException)
{
if (frozen)
throw FrozenException();