2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2010-04-13 15:25:42 +03:00
|
|
|
|
2021-12-20 12:29:14 +02:00
|
|
|
#include <list>
|
2010-04-13 15:25:42 +03:00
|
|
|
#include <map>
|
2021-12-20 12:29:14 +02:00
|
|
|
#include <unordered_map>
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/* Symbol table used by the parser and evaluator to represent and look
|
2013-10-24 17:41:04 +03:00
|
|
|
up identifiers and attributes efficiently. SymbolTable::create()
|
|
|
|
converts a string into a symbol. Symbols have the property that
|
|
|
|
they can be compared efficiently (using a pointer equality test),
|
|
|
|
because the symbol table stores only one copy of each string. */
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
class Symbol
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const string * s; // pointer into SymbolTable
|
|
|
|
Symbol(const string * s) : s(s) { };
|
|
|
|
friend class SymbolTable;
|
|
|
|
|
|
|
|
public:
|
2010-10-24 03:41:29 +03:00
|
|
|
Symbol() : s(0) { };
|
2013-05-16 20:08:02 +03:00
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
bool operator == (const Symbol & s2) const
|
|
|
|
{
|
|
|
|
return s == s2.s;
|
|
|
|
}
|
2013-05-16 20:08:02 +03:00
|
|
|
|
2020-04-17 02:02:29 +03:00
|
|
|
// FIXME: remove
|
|
|
|
bool operator == (std::string_view s2) const
|
|
|
|
{
|
|
|
|
return s->compare(s2) == 0;
|
|
|
|
}
|
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
bool operator != (const Symbol & s2) const
|
|
|
|
{
|
|
|
|
return s != s2.s;
|
|
|
|
}
|
2013-05-16 20:08:02 +03:00
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
bool operator < (const Symbol & s2) const
|
|
|
|
{
|
|
|
|
return s < s2.s;
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
operator const std::string & () const
|
|
|
|
{
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const std::string_view () const
|
2010-04-13 15:25:42 +03:00
|
|
|
{
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:08:02 +03:00
|
|
|
bool set() const
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return s->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream & operator << (std::ostream & str, const Symbol & sym);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SymbolTable
|
|
|
|
{
|
|
|
|
private:
|
2021-12-20 12:29:14 +02:00
|
|
|
std::unordered_map<std::string_view, Symbol> symbols;
|
|
|
|
std::list<string> store;
|
2010-04-13 15:25:42 +03:00
|
|
|
|
|
|
|
public:
|
2020-04-17 02:02:29 +03:00
|
|
|
Symbol create(std::string_view s)
|
2010-04-13 15:25:42 +03:00
|
|
|
{
|
2021-12-20 12:29:14 +02:00
|
|
|
// Most symbols are looked up more than once, so we trade off insertion performance
|
|
|
|
// for lookup performance.
|
|
|
|
// TODO: could probably be done more efficiently with transparent Hash and Equals
|
|
|
|
// on the original implementation using unordered_set
|
|
|
|
auto it = symbols.find(s);
|
|
|
|
if (it != symbols.end()) return it->second;
|
|
|
|
|
|
|
|
const string & rawSym = store.emplace_back(s);
|
|
|
|
return symbols.emplace(rawSym, Symbol(&rawSym)).first->second;
|
2010-04-13 15:25:42 +03:00
|
|
|
}
|
2010-04-13 17:34:11 +03:00
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t size() const
|
2010-04-13 17:34:11 +03:00
|
|
|
{
|
|
|
|
return symbols.size();
|
|
|
|
}
|
2013-10-08 16:34:57 +03:00
|
|
|
|
|
|
|
size_t totalSize() const;
|
2019-04-12 00:04:13 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void dump(T callback)
|
|
|
|
{
|
2021-12-20 12:29:14 +02:00
|
|
|
for (auto & s : store)
|
2019-04-12 00:04:13 +03:00
|
|
|
callback(s);
|
|
|
|
}
|
2010-04-13 15:25:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|