Skip to content

Commit 01aaa07

Browse files
committed
feat: tab support for indentation stripping
1 parent d1dd7ab commit 01aaa07

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

src/libexpr/parser-state.hh

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
///@file
33

44
#include "eval.hh"
5+
#include <limits>
56

67
namespace nix {
78

@@ -173,16 +174,22 @@ inline Formals * ParserState::validateFormals(Formals * formals, PosIdx pos, Sym
173174
return formals;
174175
}
175176

177+
enum IndentChar {
178+
Tab = '\t',
179+
Space = ' ',
180+
};
181+
176182
inline Expr * ParserState::stripIndentation(const PosIdx pos,
177183
std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> && es)
178184
{
179185
if (es.empty()) return new ExprString("");
180186

181187
/* Figure out the minimum indentation. Note that by design
182188
whitespace-only final lines are not taken into account. (So
183-
the " " in "\n ''" is ignored, but the " " in "\n foo''" is.) */
189+
the " " in "\n ''" is ignored, but the " " in "\n foo''" is not.) */
190+
std::optional<IndentChar> indentChar = std::nullopt;
184191
bool atStartOfLine = true; /* = seen only whitespace in the current line */
185-
size_t minIndent = 1000000;
192+
size_t minIndent = std::numeric_limits<size_t>::max();
186193
size_t curIndent = 0;
187194
for (auto & [i_pos, i] : es) {
188195
auto * str = std::get_if<StringToken>(&i);
@@ -195,20 +202,26 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
195202
continue;
196203
}
197204
for (size_t j = 0; j < str->l; ++j) {
198-
if (atStartOfLine) {
199-
if (str->p[j] == ' ')
200-
curIndent++;
201-
else if (str->p[j] == '\n') {
202-
/* Empty line, doesn't influence minimum
203-
indentation. */
204-
curIndent = 0;
205-
} else {
206-
atStartOfLine = false;
207-
if (curIndent < minIndent) minIndent = curIndent;
208-
}
209-
} else if (str->p[j] == '\n') {
205+
auto cur = str->p[j];
206+
if (!atStartOfLine) {
207+
if (cur != '\n') continue;
210208
atStartOfLine = true;
209+
curIndent = true;
210+
} else if (
211+
indentChar == cur
212+
|| (!indentChar && (cur == ' ' || cur == '\t'))
213+
) {
214+
if (!indentChar) {
215+
indentChar = IndentChar(cur);
216+
}
217+
curIndent++;
218+
} else if (cur == '\n') {
219+
/* Empty line, doesn't influence minimum
220+
indentation. */
211221
curIndent = 0;
222+
} else {
223+
atStartOfLine = false;
224+
if (curIndent < minIndent) minIndent = curIndent;
212225
}
213226
}
214227
}
@@ -228,7 +241,7 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
228241
std::string s2;
229242
for (size_t j = 0; j < t.l; ++j) {
230243
if (atStartOfLine) {
231-
if (t.p[j] == ' ') {
244+
if (t.p[j] == indentChar) {
232245
if (curDropped++ >= minIndent)
233246
s2 += t.p[j];
234247
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"This is an indented multi-line string\nliteral. An amount of whitespace at\nthe start of each line matching the minimum\nindentation of all lines in the string\nliteral together will be removed. Thus,\nin this case four spaces will be\nstripped from each line, even though\n THIS LINE is indented six spaces.\n\nAlso, empty lines don't count in the\ndetermination of the indentation level (the\nprevious empty line has indentation 0, but\nit doesn't matter).\nIf the string starts with whitespace\n followed by a newline, it's stripped, but\n that's not the case here. Two spaces are\n stripped because of the \" \" at the start. \nThis line is indented\na bit further.\nAnti-quotations, like so, are\nalso allowed.\n The \\ is not special here.\n' can be followed by any character except another ', e.g. 'x'.\nLikewise for $, e.g. $$ or $varName.\nBut ' followed by ' is special, as is $ followed by {.\nIf you want them, use anti-quotations: '', \${.\n Tabs are not interpreted as whitespace (since we can't guess\n what tab settings are intended), so don't use them.\n\tThis line starts with a space and a tab, so only one\n space will be stripped from each line.\nAlso note that if the last line (just before the closing ' ')\nconsists only of whitespace, it's ignored. But here there is\nsome non-whitespace stuff, so the line isn't removed. \nThis shows a hacky way to preserve an empty line after the start.\nBut there's no reason to do so: you could just repeat the empty\nline.\n Similarly you can force an indentation level,\n in this case to 2 spaces. This works because the anti-quote\n is significant (not whitespace).\nstart on network-interfaces\n\nstart script\n\n rm -f /var/run/opengl-driver\n ln -sf 123 /var/run/opengl-driver\n\n rm -f /var/log/slim.log\n \nend script\n\nenv SLIM_CFGFILE=abc\nenv SLIM_THEMESDIR=def\nenv FONTCONFIG_FILE=/etc/fonts/fonts.conf \t\t\t\t# !!! cleanup\nenv XKB_BINDIR=foo/bin \t\t\t\t# Needed for the Xkb extension.\nenv LD_LIBRARY_PATH=libX11/lib:libXext/lib:/usr/lib/ # related to xorg-sys-opengl - needed to load libglx for (AI)GLX support (for compiz)\n\nenv XORG_DRI_DRIVER_PATH=nvidiaDrivers/X11R6/lib/modules/drivers/ \n\nexec slim/bin/slim\nEscaping of ' followed by ': ''\nEscaping of $ followed by {: \${\nAnd finally to interpret \\n etc. as in a string: \n, \r, \t.\nfoo\n'bla'\nbar\ncut -d $'\\t' -f 1\nending dollar $$\n"
1+
"This is an indented multi-line string\nliteral. An amount of whitespace at\nthe start of each line matching the minimum\nindentation of all lines in the string\nliteral together will be removed. Thus,\nin this case four spaces will be\nstripped from each line, even though\n THIS LINE is indented six spaces.\n\nAlso, empty lines don't count in the\ndetermination of the indentation level (the\nprevious empty line has indentation 0, but\nit doesn't matter).\nIf the string starts with whitespace\n followed by a newline, it's stripped, but\n that's not the case here. Two spaces are\n stripped because of the \" \" at the start. \nThis line is indented\na bit further.\nAnti-quotations, like so, are\nalso allowed.\n The \\ is not special here.\n' can be followed by any character except another ', e.g. 'x'.\nLikewise for $, e.g. $$ or $varName.\nBut ' followed by ' is special, as is $ followed by {.\nIf you want them, use anti-quotations: '', \${.\n Tabs are not interpreted as whitespace (since we can't guess\n what tab settings are intended), so don't use them.\n\tThis line starts with a space and a tab, so only one\n space will be stripped from each line.\nAlso note that if the last line (just before the closing ' ')\nconsists only of whitespace, it's ignored. But here there is\nsome non-whitespace stuff, so the line isn't removed. \nThis shows a hacky way to preserve an empty line after the start.\nBut there's no reason to do so: you could just repeat the empty\nline.\n Similarly you can force an indentation level,\n in this case to 2 spaces. This works because the anti-quote\n is significant (not whitespace).\nstart on network-interfaces\n\nstart script\n\n rm -f /var/run/opengl-driver\n ln -sf 123 /var/run/opengl-driver\n\n rm -f /var/log/slim.log\n \nend script\n\nenv SLIM_CFGFILE=abc\nenv SLIM_THEMESDIR=def\nenv FONTCONFIG_FILE=/etc/fonts/fonts.conf \t\t\t\t# !!! cleanup\nenv XKB_BINDIR=foo/bin \t\t\t\t# Needed for the Xkb extension.\nenv LD_LIBRARY_PATH=libX11/lib:libXext/lib:/usr/lib/ # related to xorg-sys-opengl - needed to load libglx for (AI)GLX support (for compiz)\n\nenv XORG_DRI_DRIVER_PATH=nvidiaDrivers/X11R6/lib/modules/drivers/ \n\nexec slim/bin/slim\nEscaping of ' followed by ': ''\nEscaping of $ followed by {: \${\nAnd finally to interpret \\n etc. as in a string: \n, \r, \t.\nfoo\n'bla'\nbar\ncut -d $'\\t' -f 1\nending dollar $$\nThis text uses\n\ttabs\nfor indentation\nAnd this text uses\n spaces\nbut is indented with tabs\n \tThis text uses\n\t both spaces and tabs\n\t\tso nothing is stripped\n\t"

tests/functional/lang/eval-okay-ind-string.nix

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,22 @@ let
125125
# Accept dollars at end of strings
126126
s17 = ''ending dollar $'' + ''$'' + "\n";
127127

128-
in s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 + s14 + s15 + s16 + s17
128+
s18 = ''
129+
This text uses
130+
tabs
131+
for indentation
132+
'';
133+
134+
s19 = ''
135+
And this text uses
136+
spaces
137+
but is indented with tabs
138+
'';
139+
140+
s20 = ''
141+
This text uses
142+
both spaces and tabs
143+
so nothing is stripped
144+
'';
145+
146+
in s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 + s14 + s15 + s16 + s17 + s18 + s19 + s20

0 commit comments

Comments
 (0)