mirror of
https://github.com/NixOS/nix.git
synced 2024-11-25 08:12:29 +00:00
Merge pull request #11120 from tweag/early-string-cutoff
parser: Remove empty multiline string parts earlier
This commit is contained in:
commit
584f8cb065
@ -291,12 +291,23 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
|
|||||||
s2 = std::string(s2, 0, p + 1);
|
s2 = std::string(s2, 0, p + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
es2->emplace_back(i->first, new ExprString(std::move(s2)));
|
// Ignore empty strings for a minor optimisation and AST simplification
|
||||||
|
if (s2 != "") {
|
||||||
|
es2->emplace_back(i->first, new ExprString(std::move(s2)));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
for (; i != es.end(); ++i, --n) {
|
for (; i != es.end(); ++i, --n) {
|
||||||
std::visit(overloaded { trimExpr, trimString }, i->second);
|
std::visit(overloaded { trimExpr, trimString }, i->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there is nothing at all, return the empty string directly.
|
||||||
|
// This also ensures that equivalent empty strings result in the same ast, which is helpful when testing formatters.
|
||||||
|
if (es2->size() == 0) {
|
||||||
|
auto *const result = new ExprString("");
|
||||||
|
delete es2;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* If this is a single string, then don't do a concatenation. */
|
/* If this is a single string, then don't do a concatenation. */
|
||||||
if (es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0].second)) {
|
if (es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0].second)) {
|
||||||
auto *const result = (*es2)[0].second;
|
auto *const result = (*es2)[0].second;
|
||||||
|
1
tests/functional/lang/parse-okay-ind-string.exp
Normal file
1
tests/functional/lang/parse-okay-ind-string.exp
Normal file
@ -0,0 +1 @@
|
|||||||
|
(let string = "str"; in [ (/some/path) ((/some/path)) ((/some/path)) ((/some/path + "\n end")) (string) ((string)) ((string)) ((string + "\n end")) ("") ("") ("end") ])
|
31
tests/functional/lang/parse-okay-ind-string.nix
Normal file
31
tests/functional/lang/parse-okay-ind-string.nix
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
let
|
||||||
|
string = "str";
|
||||||
|
in [
|
||||||
|
/some/path
|
||||||
|
|
||||||
|
''${/some/path}''
|
||||||
|
|
||||||
|
''
|
||||||
|
${/some/path}''
|
||||||
|
|
||||||
|
''${/some/path}
|
||||||
|
end''
|
||||||
|
|
||||||
|
string
|
||||||
|
|
||||||
|
''${string}''
|
||||||
|
|
||||||
|
''
|
||||||
|
${string}''
|
||||||
|
|
||||||
|
''${string}
|
||||||
|
end''
|
||||||
|
|
||||||
|
''''
|
||||||
|
|
||||||
|
''
|
||||||
|
''
|
||||||
|
|
||||||
|
''
|
||||||
|
end''
|
||||||
|
]
|
Loading…
Reference in New Issue
Block a user