Skip to content

Commit

Permalink
parser: Remove empty multiline string parts earlier
Browse files Browse the repository at this point in the history
Makes parsing more consistent and is a super minor optimisation
  • Loading branch information
infinisil committed Jul 17, 2024
1 parent 9fae50e commit 23c44d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/libexpr/parser-state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,22 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
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) {
std::visit(overloaded { trimExpr, trimString }, i->second);
}

/* If there is nothing at all, return the empty string directly */
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 (es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0].second)) {
auto *const result = (*es2)[0].second;
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/lang/parse-okay-ind-string.exp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(let string = "str"; in [ (/some/path) ((/some/path)) (("" + /some/path)) ((/some/path + "\n end")) (string) ((string)) (("" + string)) ((string + "\n end")) ("") ("") ("end") ])
(let string = "str"; in [ (/some/path) ((/some/path)) ((/some/path)) ((/some/path + "\n end")) (string) ((string)) ((string)) ((string + "\n end")) ("") ("") ("end") ])

0 comments on commit 23c44d9

Please sign in to comment.