mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
Restore sanity to line continuations
Line continuations (i.e. escaped new lines) now make sense again. With
the smart pipe support (pipes continue on to next line) recently added,
this hack to have continuations ignore comments makes no sense.
This is valid code:
```fish
echo hello |
# comment here
tr -d 'l'
```
this isn't:
```fish
echo hello | \
# comment here
tr -d 'l'
```
Reverts @snnw's 318daaffb2
Closes #2928. Closes #2929.
This commit is contained in:
parent
0b96b516d5
commit
2cdacbdce4
2 changed files with 7 additions and 11 deletions
|
@ -416,11 +416,12 @@ maybe_t<tok_t> tokenizer_t::tok_next() {
|
||||||
return none();
|
return none();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool line_continued = false;
|
||||||
// Consume non-newline whitespace. If we get an escaped newline, mark it and continue past it.
|
// Consume non-newline whitespace. If we get an escaped newline, mark it and continue past it.
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (this->buff[0] == L'\\' && this->buff[1] == L'\n') {
|
if (this->buff[0] == L'\\' && this->buff[1] == L'\n') {
|
||||||
|
line_continued = true;
|
||||||
this->buff += 2;
|
this->buff += 2;
|
||||||
this->continue_line_after_comment = true;
|
|
||||||
} else if (iswspace_not_nl(this->buff[0])) {
|
} else if (iswspace_not_nl(this->buff[0])) {
|
||||||
this->buff++;
|
this->buff++;
|
||||||
} else {
|
} else {
|
||||||
|
@ -428,15 +429,12 @@ maybe_t<tok_t> tokenizer_t::tok_next() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (*this->buff == L'#') {
|
if (*this->buff == L'#') {
|
||||||
// We have a comment, walk over the comment.
|
// We have a comment, walk over the comment.
|
||||||
const wchar_t *comment_start = this->buff;
|
const wchar_t *comment_start = this->buff;
|
||||||
while (this->buff[0] != L'\n' && this->buff[0] != L'\0') this->buff++;
|
while (this->buff[0] != L'\n' && this->buff[0] != L'\0') this->buff++;
|
||||||
size_t comment_len = this->buff - comment_start;
|
size_t comment_len = this->buff - comment_start;
|
||||||
|
|
||||||
// If we are going to continue after the comment, skip any trailing newline.
|
|
||||||
if (this->buff[0] == L'\n' && this->continue_line_after_comment) this->buff++;
|
|
||||||
|
|
||||||
// Maybe return the comment.
|
// Maybe return the comment.
|
||||||
if (this->show_comments) {
|
if (this->show_comments) {
|
||||||
tok_t result;
|
tok_t result;
|
||||||
|
@ -445,11 +443,12 @@ maybe_t<tok_t> tokenizer_t::tok_next() {
|
||||||
result.length = comment_len;
|
result.length = comment_len;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
if (line_continued) {
|
||||||
|
return none();
|
||||||
|
}
|
||||||
while (iswspace_not_nl(this->buff[0])) this->buff++;
|
while (iswspace_not_nl(this->buff[0])) this->buff++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We made it past the comments and ate any trailing newlines we wanted to ignore.
|
|
||||||
this->continue_line_after_comment = false;
|
|
||||||
size_t start_pos = this->buff - this->start;
|
size_t start_pos = this->buff - this->start;
|
||||||
|
|
||||||
tok_t result;
|
tok_t result;
|
||||||
|
@ -468,8 +467,7 @@ maybe_t<tok_t> tokenizer_t::tok_next() {
|
||||||
// Hack: when we get a newline, swallow as many as we can. This compresses multiple
|
// Hack: when we get a newline, swallow as many as we can. This compresses multiple
|
||||||
// subsequent newlines into a single one.
|
// subsequent newlines into a single one.
|
||||||
if (!this->show_blank_lines) {
|
if (!this->show_blank_lines) {
|
||||||
while (*this->buff == L'\n' || *this->buff == 13 /* CR */ || *this->buff == ' ' ||
|
while (is_whitespace(*this->buff)) {
|
||||||
*this->buff == '\t') {
|
|
||||||
this->buff++;
|
this->buff++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,8 +104,6 @@ class tokenizer_t {
|
||||||
bool show_comments{false};
|
bool show_comments{false};
|
||||||
/// Whether all blank lines are returned.
|
/// Whether all blank lines are returned.
|
||||||
bool show_blank_lines{false};
|
bool show_blank_lines{false};
|
||||||
/// Whether to continue the previous line after the comment.
|
|
||||||
bool continue_line_after_comment{false};
|
|
||||||
|
|
||||||
tok_t call_error(tokenizer_error *error_type, const wchar_t *token_start,
|
tok_t call_error(tokenizer_error *error_type, const wchar_t *token_start,
|
||||||
const wchar_t *error_loc);
|
const wchar_t *error_loc);
|
||||||
|
|
Loading…
Reference in a new issue