2024-03-08 04:20:55 +00:00
|
|
|
#pragma once
|
2024-03-08 04:37:58 +00:00
|
|
|
///@file
|
2024-03-08 04:20:55 +00:00
|
|
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-03-08 04:37:58 +00:00
|
|
|
/**
|
|
|
|
* @note Storing a C-style `char *` and `size_t` allows us to avoid
|
|
|
|
* having to define the special members that using string_view here
|
|
|
|
* would implicitly delete.
|
|
|
|
*/
|
|
|
|
struct StringToken
|
|
|
|
{
|
|
|
|
const char * p;
|
|
|
|
size_t l;
|
|
|
|
bool hasIndentation;
|
|
|
|
operator std::string_view() const { return {p, l}; }
|
2024-03-08 04:20:55 +00:00
|
|
|
};
|
|
|
|
|
2024-03-08 04:37:58 +00:00
|
|
|
struct ParserLocation
|
|
|
|
{
|
2024-03-08 04:20:55 +00:00
|
|
|
int first_line, first_column;
|
|
|
|
int last_line, last_column;
|
|
|
|
|
|
|
|
// backup to recover from yyless(0)
|
2024-01-29 05:19:23 +00:00
|
|
|
int stashed_first_column, stashed_last_column;
|
2024-03-08 04:20:55 +00:00
|
|
|
|
|
|
|
void stash() {
|
|
|
|
stashed_first_column = first_column;
|
|
|
|
stashed_last_column = last_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unstash() {
|
|
|
|
first_column = stashed_first_column;
|
|
|
|
last_column = stashed_last_column;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-08 04:37:58 +00:00
|
|
|
struct ParserState
|
|
|
|
{
|
2024-03-08 04:20:55 +00:00
|
|
|
SymbolTable & symbols;
|
2024-03-08 04:35:12 +00:00
|
|
|
PosTable & positions;
|
2024-03-08 04:20:55 +00:00
|
|
|
Expr * result;
|
|
|
|
SourcePath basePath;
|
|
|
|
PosTable::Origin origin;
|
2024-03-08 04:37:19 +00:00
|
|
|
const Expr::AstSymbols & s;
|
2024-06-16 21:10:09 +00:00
|
|
|
std::unique_ptr<Error> error;
|
2024-03-08 04:20:55 +00:00
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
[[nodiscard]] ParseError dupAttr(const AttrPath & attrPath, const PosIdx pos, const PosIdx prevPos);
|
|
|
|
[[nodiscard]] ParseError dupAttr(Symbol attr, const PosIdx pos, const PosIdx prevPos);
|
|
|
|
[[nodiscard]] std::optional<ParseError> addAttr(ExprAttrs * attrs, AttrPath && attrPath, Expr * e, const PosIdx pos);
|
|
|
|
[[nodiscard]] std::optional<ParseError> validateFormals(Formals * formals, PosIdx pos = noPos, Symbol arg = {});
|
2024-03-08 04:20:55 +00:00
|
|
|
Expr * stripIndentation(const PosIdx pos,
|
|
|
|
std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> && es);
|
2024-03-08 04:21:15 +00:00
|
|
|
PosIdx at(const ParserLocation & loc);
|
2024-03-08 04:20:55 +00:00
|
|
|
};
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
inline ParseError ParserState::dupAttr(const AttrPath & attrPath, const PosIdx pos, const PosIdx prevPos)
|
2024-03-08 04:20:55 +00:00
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
return ParseError({
|
2024-03-08 07:10:05 +00:00
|
|
|
.msg = HintFmt("attribute '%1%' already defined at %2%",
|
2024-03-08 04:35:12 +00:00
|
|
|
showAttrPath(symbols, attrPath), positions[prevPos]),
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
.pos = positions[pos]
|
2024-03-08 04:20:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
inline ParseError ParserState::dupAttr(Symbol attr, const PosIdx pos, const PosIdx prevPos)
|
2024-03-08 04:20:55 +00:00
|
|
|
{
|
2024-06-16 21:10:09 +00:00
|
|
|
return ParseError({
|
2024-03-08 07:10:05 +00:00
|
|
|
.msg = HintFmt("attribute '%1%' already defined at %2%", symbols[attr], positions[prevPos]),
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
.pos = positions[pos]
|
2024-03-08 04:20:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
inline std::optional<ParseError> ParserState::addAttr(ExprAttrs * attrs, AttrPath && attrPath, Expr * e, const PosIdx pos)
|
2024-03-08 04:20:55 +00:00
|
|
|
{
|
|
|
|
AttrPath::iterator i;
|
|
|
|
// All attrpaths have at least one attr
|
|
|
|
assert(!attrPath.empty());
|
|
|
|
// Checking attrPath validity.
|
|
|
|
// ===========================
|
|
|
|
for (i = attrPath.begin(); i + 1 < attrPath.end(); i++) {
|
|
|
|
if (i->symbol) {
|
|
|
|
ExprAttrs::AttrDefs::iterator j = attrs->attrs.find(i->symbol);
|
|
|
|
if (j != attrs->attrs.end()) {
|
2024-03-08 08:52:29 +00:00
|
|
|
if (j->second.kind != ExprAttrs::AttrDef::Kind::Inherited) {
|
2024-03-08 04:20:55 +00:00
|
|
|
ExprAttrs * attrs2 = dynamic_cast<ExprAttrs *>(j->second.e);
|
2024-06-16 21:10:09 +00:00
|
|
|
if (!attrs2) return dupAttr({attrPath.begin(), i + 1}, pos, j->second.pos);
|
2024-03-08 04:20:55 +00:00
|
|
|
attrs = attrs2;
|
|
|
|
} else
|
2024-06-16 21:10:09 +00:00
|
|
|
return dupAttr({attrPath.begin(), i + 1}, pos, j->second.pos);
|
2024-03-08 04:20:55 +00:00
|
|
|
} else {
|
|
|
|
ExprAttrs * nested = new ExprAttrs;
|
|
|
|
attrs->attrs[i->symbol] = ExprAttrs::AttrDef(nested, pos);
|
|
|
|
attrs = nested;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ExprAttrs *nested = new ExprAttrs;
|
|
|
|
attrs->dynamicAttrs.push_back(ExprAttrs::DynamicAttrDef(i->expr, nested, pos));
|
|
|
|
attrs = nested;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Expr insertion.
|
|
|
|
// ==========================
|
|
|
|
if (i->symbol) {
|
|
|
|
ExprAttrs::AttrDefs::iterator j = attrs->attrs.find(i->symbol);
|
|
|
|
if (j != attrs->attrs.end()) {
|
|
|
|
// This attr path is already defined. However, if both
|
|
|
|
// e and the expr pointed by the attr path are two attribute sets,
|
|
|
|
// we want to merge them.
|
|
|
|
// Otherwise, throw an error.
|
|
|
|
auto ae = dynamic_cast<ExprAttrs *>(e);
|
|
|
|
auto jAttrs = dynamic_cast<ExprAttrs *>(j->second.e);
|
|
|
|
if (jAttrs && ae) {
|
2024-03-08 08:52:15 +00:00
|
|
|
if (ae->inheritFromExprs && !jAttrs->inheritFromExprs)
|
|
|
|
jAttrs->inheritFromExprs = std::make_unique<std::vector<Expr *>>();
|
2024-03-08 04:20:55 +00:00
|
|
|
for (auto & ad : ae->attrs) {
|
|
|
|
auto j2 = jAttrs->attrs.find(ad.first);
|
|
|
|
if (j2 != jAttrs->attrs.end()) // Attr already defined in iAttrs, error.
|
2024-06-16 21:10:09 +00:00
|
|
|
return dupAttr(ad.first, j2->second.pos, ad.second.pos);
|
2024-03-08 04:20:55 +00:00
|
|
|
jAttrs->attrs.emplace(ad.first, ad.second);
|
2024-03-08 08:52:15 +00:00
|
|
|
if (ad.second.kind == ExprAttrs::AttrDef::Kind::InheritedFrom) {
|
|
|
|
auto & sel = dynamic_cast<ExprSelect &>(*ad.second.e);
|
|
|
|
auto & from = dynamic_cast<ExprInheritFrom &>(*sel.e);
|
|
|
|
from.displ += jAttrs->inheritFromExprs->size();
|
|
|
|
}
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
jAttrs->dynamicAttrs.insert(jAttrs->dynamicAttrs.end(), ae->dynamicAttrs.begin(), ae->dynamicAttrs.end());
|
2024-03-08 08:52:15 +00:00
|
|
|
if (ae->inheritFromExprs) {
|
|
|
|
jAttrs->inheritFromExprs->insert(jAttrs->inheritFromExprs->end(),
|
|
|
|
ae->inheritFromExprs->begin(), ae->inheritFromExprs->end());
|
|
|
|
}
|
2024-03-08 04:20:55 +00:00
|
|
|
} else {
|
2024-06-16 21:10:09 +00:00
|
|
|
return dupAttr(attrPath, pos, j->second.pos);
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This attr path is not defined. Let's create it.
|
|
|
|
attrs->attrs.emplace(i->symbol, ExprAttrs::AttrDef(e, pos));
|
|
|
|
e->setName(i->symbol);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attrs->dynamicAttrs.push_back(ExprAttrs::DynamicAttrDef(i->expr, e, pos));
|
|
|
|
}
|
2024-06-16 21:10:09 +00:00
|
|
|
|
|
|
|
return {};
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
inline std::optional<ParseError> ParserState::validateFormals(Formals * formals, PosIdx pos, Symbol arg)
|
2024-03-08 04:20:55 +00:00
|
|
|
{
|
|
|
|
std::sort(formals->formals.begin(), formals->formals.end(),
|
|
|
|
[] (const auto & a, const auto & b) {
|
|
|
|
return std::tie(a.name, a.pos) < std::tie(b.name, b.pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::optional<std::pair<Symbol, PosIdx>> duplicate;
|
|
|
|
for (size_t i = 0; i + 1 < formals->formals.size(); i++) {
|
|
|
|
if (formals->formals[i].name != formals->formals[i + 1].name)
|
|
|
|
continue;
|
|
|
|
std::pair thisDup{formals->formals[i].name, formals->formals[i + 1].pos};
|
|
|
|
duplicate = std::min(thisDup, duplicate.value_or(thisDup));
|
|
|
|
}
|
|
|
|
if (duplicate)
|
2024-06-16 21:10:09 +00:00
|
|
|
return ParseError({
|
2024-03-08 07:10:05 +00:00
|
|
|
.msg = HintFmt("duplicate formal function argument '%1%'", symbols[duplicate->first]),
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
.pos = positions[duplicate->second]
|
2024-03-08 04:20:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (arg && formals->has(arg))
|
2024-06-16 21:10:09 +00:00
|
|
|
return ParseError({
|
2024-03-08 07:10:05 +00:00
|
|
|
.msg = HintFmt("duplicate formal function argument '%1%'", symbols[arg]),
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
.pos = positions[pos]
|
2024-03-08 04:20:55 +00:00
|
|
|
});
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
return {};
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Expr * ParserState::stripIndentation(const PosIdx pos,
|
|
|
|
std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> && es)
|
|
|
|
{
|
|
|
|
if (es.empty()) return new ExprString("");
|
|
|
|
|
|
|
|
/* Figure out the minimum indentation. Note that by design
|
|
|
|
whitespace-only final lines are not taken into account. (So
|
|
|
|
the " " in "\n ''" is ignored, but the " " in "\n foo''" is.) */
|
|
|
|
bool atStartOfLine = true; /* = seen only whitespace in the current line */
|
|
|
|
size_t minIndent = 1000000;
|
|
|
|
size_t curIndent = 0;
|
|
|
|
for (auto & [i_pos, i] : es) {
|
|
|
|
auto * str = std::get_if<StringToken>(&i);
|
|
|
|
if (!str || !str->hasIndentation) {
|
|
|
|
/* Anti-quotations and escaped characters end the current start-of-line whitespace. */
|
|
|
|
if (atStartOfLine) {
|
|
|
|
atStartOfLine = false;
|
|
|
|
if (curIndent < minIndent) minIndent = curIndent;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (size_t j = 0; j < str->l; ++j) {
|
|
|
|
if (atStartOfLine) {
|
|
|
|
if (str->p[j] == ' ')
|
|
|
|
curIndent++;
|
|
|
|
else if (str->p[j] == '\n') {
|
|
|
|
/* Empty line, doesn't influence minimum
|
|
|
|
indentation. */
|
|
|
|
curIndent = 0;
|
|
|
|
} else {
|
|
|
|
atStartOfLine = false;
|
|
|
|
if (curIndent < minIndent) minIndent = curIndent;
|
|
|
|
}
|
|
|
|
} else if (str->p[j] == '\n') {
|
|
|
|
atStartOfLine = true;
|
|
|
|
curIndent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip spaces from each line. */
|
2024-06-16 21:10:09 +00:00
|
|
|
std::vector<std::pair<PosIdx, Expr *>> es2;
|
2024-03-08 04:20:55 +00:00
|
|
|
atStartOfLine = true;
|
|
|
|
size_t curDropped = 0;
|
|
|
|
size_t n = es.size();
|
|
|
|
auto i = es.begin();
|
|
|
|
const auto trimExpr = [&] (Expr * e) {
|
|
|
|
atStartOfLine = false;
|
|
|
|
curDropped = 0;
|
2024-06-16 21:10:09 +00:00
|
|
|
es2.emplace_back(i->first, e);
|
2024-03-08 04:20:55 +00:00
|
|
|
};
|
|
|
|
const auto trimString = [&] (const StringToken & t) {
|
|
|
|
std::string s2;
|
|
|
|
for (size_t j = 0; j < t.l; ++j) {
|
|
|
|
if (atStartOfLine) {
|
|
|
|
if (t.p[j] == ' ') {
|
|
|
|
if (curDropped++ >= minIndent)
|
|
|
|
s2 += t.p[j];
|
|
|
|
}
|
|
|
|
else if (t.p[j] == '\n') {
|
|
|
|
curDropped = 0;
|
|
|
|
s2 += t.p[j];
|
|
|
|
} else {
|
|
|
|
atStartOfLine = false;
|
|
|
|
curDropped = 0;
|
|
|
|
s2 += t.p[j];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s2 += t.p[j];
|
|
|
|
if (t.p[j] == '\n') atStartOfLine = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the last line if it is empty and consists only of
|
|
|
|
spaces. */
|
|
|
|
if (n == 1) {
|
|
|
|
std::string::size_type p = s2.find_last_of('\n');
|
|
|
|
if (p != std::string::npos && s2.find_first_not_of(' ', p + 1) == std::string::npos)
|
|
|
|
s2 = std::string(s2, 0, p + 1);
|
|
|
|
}
|
|
|
|
|
2024-06-16 21:10:09 +00:00
|
|
|
es2.emplace_back(i->first, new ExprString(std::move(s2)));
|
2024-03-08 04:20:55 +00:00
|
|
|
};
|
|
|
|
for (; i != es.end(); ++i, --n) {
|
|
|
|
std::visit(overloaded { trimExpr, trimString }, i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is a single string, then don't do a concatenation. */
|
2024-06-16 21:10:09 +00:00
|
|
|
if (es2.size() == 1 && dynamic_cast<ExprString *>(es2[0].second)) {
|
|
|
|
return es2[0].second;
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
2024-06-16 21:10:09 +00:00
|
|
|
return new ExprConcatStrings(pos, true, std::move(es2));
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 04:21:15 +00:00
|
|
|
inline PosIdx ParserState::at(const ParserLocation & loc)
|
2024-03-08 04:20:55 +00:00
|
|
|
{
|
2024-01-29 05:19:23 +00:00
|
|
|
return positions.add(origin, loc.first_column);
|
2024-03-08 04:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|