20 operator std::string_view()
const {
return {p, l}; }
30 int stashedBeginOffset, stashedEndOffset;
33 stashedBeginOffset = beginOffset;
34 stashedEndOffset = endOffset;
38 beginOffset = stashedBeginOffset;
39 endOffset = stashedEndOffset;
88 void dupAttr(
const AttrPath & attrPath,
const PosIdx pos,
const PosIdx prevPos);
94 std::vector<std::pair<
PosIdx, std::variant<Expr *, StringToken>>> && es);
98inline void ParserState::dupAttr(
const AttrPath & attrPath,
const PosIdx pos,
const PosIdx prevPos)
101 .msg =
HintFmt(
"attribute '%1%' already defined at %2%",
102 showAttrPath(
symbols, attrPath), positions[prevPos]),
103 .pos = positions[pos]
110 .msg = HintFmt(
"attribute '%1%' already defined at %2%", symbols[attr], positions[prevPos]),
111 .pos = positions[pos]
117 AttrPath::iterator
i;
119 assert(!attrPath.empty());
123 for (
i = attrPath.begin();
i + 1 < attrPath.end();
i++) {
126 ExprAttrs::AttrDefs::iterator
j = attrs->attrs.find(
i->symbol);
127 if (
j != attrs->attrs.end()) {
128 nested =
dynamic_cast<ExprAttrs *
>(
j->second.e);
130 attrPath.erase(
i + 1, attrPath.end());
131 dupAttr(attrPath, pos,
j->second.pos);
134 nested =
new ExprAttrs;
135 attrs->attrs[
i->symbol] = ExprAttrs::AttrDef(nested, pos);
138 nested =
new ExprAttrs;
139 attrs->dynamicAttrs.push_back(ExprAttrs::DynamicAttrDef(
i->expr, nested, pos));
146 addAttr(attrs, attrPath,
i->symbol, ExprAttrs::AttrDef(e, pos));
148 attrs->dynamicAttrs.push_back(ExprAttrs::DynamicAttrDef(
i->expr, e, pos));
151 auto it = lexerState.positionToDocComment.find(pos);
152 if (it != lexerState.positionToDocComment.end()) {
153 e->setDocComment(it->second);
154 lexerState.positionToDocComment.emplace(at(exprLoc), it->second);
164 ExprAttrs::AttrDefs::iterator
j = attrs->attrs.find(
symbol);
165 if (
j != attrs->attrs.end()) {
170 auto ae =
dynamic_cast<ExprAttrs *
>(def.e);
171 auto jAttrs =
dynamic_cast<ExprAttrs *
>(
j->second.e);
179 if (ae->inheritFromExprs && !jAttrs->inheritFromExprs)
180 jAttrs->inheritFromExprs = std::make_unique<std::vector<Expr *>>();
181 for (
auto & ad : ae->attrs) {
183 auto & sel =
dynamic_cast<ExprSelect &
>(*ad.second.e);
185 from.displ += jAttrs->inheritFromExprs->size();
187 attrPath.emplace_back(
AttrName(ad.first));
188 addAttr(jAttrs, attrPath, ad.first, std::move(ad.second));
192 jAttrs->dynamicAttrs.insert(jAttrs->dynamicAttrs.end(),
193 std::make_move_iterator(ae->dynamicAttrs.begin()),
194 std::make_move_iterator(ae->dynamicAttrs.end()));
195 ae->dynamicAttrs.clear();
196 if (ae->inheritFromExprs) {
197 jAttrs->inheritFromExprs->insert(jAttrs->inheritFromExprs->end(),
198 std::make_move_iterator(ae->inheritFromExprs->begin()),
199 std::make_move_iterator(ae->inheritFromExprs->end()));
200 ae->inheritFromExprs =
nullptr;
203 dupAttr(attrPath, def.pos,
j->second.pos);
207 attrs->attrs.emplace(
symbol, def);
214 std::sort(formals->formals.begin(), formals->formals.end(),
215 [] (
const auto & a,
const auto & b) {
216 return std::tie(a.name, a.pos) < std::tie(b.name, b.pos);
219 std::optional<std::pair<Symbol, PosIdx>> duplicate;
220 for (
size_t i = 0;
i + 1 < formals->formals.size();
i++) {
221 if (formals->formals[
i].name != formals->formals[
i + 1].name)
223 std::pair thisDup{formals->formals[
i].name, formals->formals[
i + 1].pos};
224 duplicate = std::min(thisDup, duplicate.value_or(thisDup));
228 .msg = HintFmt(
"duplicate formal function argument '%1%'", symbols[duplicate->first]),
229 .pos = positions[duplicate->second]
232 if (arg && formals->has(arg))
234 .msg = HintFmt(
"duplicate formal function argument '%1%'", symbols[arg]),
235 .pos = positions[pos]
241inline Expr * ParserState::stripIndentation(
const PosIdx pos,
242 std::vector<std::pair<
PosIdx, std::variant<Expr *, StringToken>>> && es)
244 if (es.empty())
return new ExprString(
"");
249 bool atStartOfLine =
true;
250 size_t minIndent = 1000000;
251 size_t curIndent = 0;
252 for (
auto & [i_pos,
i] : es) {
253 auto *
str = std::get_if<StringToken>(&
i);
254 if (!
str || !
str->hasIndentation) {
257 atStartOfLine =
false;
258 if (curIndent < minIndent) minIndent = curIndent;
262 for (
size_t j = 0;
j <
str->l; ++
j) {
264 if (
str->p[
j] ==
' ')
266 else if (
str->p[
j] ==
'\n') {
271 atStartOfLine =
false;
272 if (curIndent < minIndent) minIndent = curIndent;
274 }
else if (
str->p[
j] ==
'\n') {
275 atStartOfLine =
true;
282 auto * es2 =
new std::vector<std::pair<PosIdx, Expr *>>;
283 atStartOfLine =
true;
284 size_t curDropped = 0;
285 size_t n = es.size();
287 const auto trimExpr = [&] (Expr * e) {
288 atStartOfLine =
false;
290 es2->emplace_back(
i->first, e);
292 const auto trimString = [&] (
const StringToken &
t) {
294 for (
size_t j = 0;
j <
t.l; ++
j) {
297 if (curDropped++ >= minIndent)
300 else if (
t.p[
j] ==
'\n') {
304 atStartOfLine =
false;
310 if (
t.p[
j] ==
'\n') atStartOfLine =
true;
317 std::string::size_type
p = s2.find_last_of(
'\n');
318 if (
p != std::string::npos && s2.find_first_not_of(
' ',
p + 1) == std::string::npos)
319 s2 = std::string(s2, 0,
p + 1);
324 es2->emplace_back(
i->first,
new ExprString(std::move(s2)));
327 for (;
i != es.end(); ++
i, --n) {
328 std::visit(overloaded { trimExpr, trimString },
i->second);
333 if (es2->size() == 0) {
334 auto *
const result =
new ExprString(
"");
340 if (es2->size() == 1 &&
dynamic_cast<ExprString *
>((*es2)[0].second)) {
341 auto *
const result = (*es2)[0].second;
345 return new ExprConcatStrings(pos,
true, es2);
350 return positions.add(origin, loc.beginOffset);
355 return positions.add(origin, loc.beginOffset);
Definition pos-table.hh:16
Definition pos-table.hh:13
Definition symbol-table.hh:82
Definition symbol-table.hh:58
auto i
Definition lexer.l:2745
std::ostream & str
Definition lexer.l:1728
Symbol symbol
Definition lexer.l:5834
std::shared_ptr< T > p
Definition lexer.l:1269
std::string std::string_view from
Definition lexer.l:2591
T t
Definition lexer.l:154
ExprAttrs::AttrDefs::iterator j
Definition lexer.l:8572
std::unordered_map< std::string_view, std::pair< const std::string *, uint32_t > > symbols
Definition lexer.l:1010
Definition eval-settings.hh:13
Definition nixexpr.hh:232
@ InheritedFrom
Definition nixexpr.hh:239
Definition nixexpr.hh:229
Definition nixexpr.hh:186
Definition nixexpr.hh:198
Definition parser-state.hh:47
int docCommentDistance
Definition parser-state.hh:56
std::unordered_map< PosIdx, DocComment > & positionToDocComment
Maps some positions to a DocComment, where the comment is relevant to the location.
Definition parser-state.hh:68
ParserLocation lastDocCommentLoc
Definition parser-state.hh:63
Definition parser-state.hh:25
int doc_comment_first_column
Definition parser-state.hh:43
Definition parser-state.hh:77
Definition source-path.hh:22
Definition parser-state.hh:16