Skip to content

Commit

Permalink
use unordered set
Browse files Browse the repository at this point in the history
  • Loading branch information
aakropotkin committed Mar 27, 2023
1 parent 5d631da commit 4e50e4c
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,17 @@ RootValue allocRootValue(Value * v)
#endif
}

// Reserved symbols that should be quoted when printing expressions.
static inline bool isReservedSymbol(const std::string_view str)
// Returns `true' is a string is a reserved keyword which requires quotation
// when printing attribute set field names.
static inline bool isReservedKeyword(const std::string_view str)
{
return (str == "if") ||
(str == "then") ||
(str == "else") ||
(str == "assert") ||
(str == "with") ||
(str == "let") ||
(str == "in") ||
(str == "rec") ||
(str == "inherit") ||
(str == "or") ||
(str == "throw") ||
(str == "import");
static const std::unordered_set<std::string_view> reservedKeywords = {
"if", "then", "else", "assert", "with", "let", "in", "rec", "inherit",
"or", "throw", "import"
};
return reservedKeywords.contains(str);
}


void Value::print(const SymbolTable & symbols, std::ostream & str,
std::set<const void *> * seen) const
{
Expand Down Expand Up @@ -145,7 +138,9 @@ void Value::print(const SymbolTable & symbols, std::ostream & str,
else {
str << "{ ";
for (auto & i : attrs->lexicographicOrder(symbols)) {
if (isReservedSymbol(symbols[i->name])) {
// Quote reserved keywords so that the output can be
// re-evaluated later without upsetting the lexer.
if (isReservedKeyword(symbols[i->name])) {
str << "\"" << symbols[i->name] << "\" = ";
} else {
str << symbols[i->name] << " = ";
Expand Down

0 comments on commit 4e50e4c

Please sign in to comment.