Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove leading tabs from <<- heredoc content #25

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/parser/lex/heredoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::Lexer;
use crate::parser::core::Result;
use crate::syntax::HereDoc;
use crate::syntax::Text;
use crate::syntax::TextUnit::Literal;
use crate::syntax::TextUnit::{self, Literal};
use crate::syntax::Unquote;
use crate::syntax::Word;

Expand All @@ -42,6 +42,13 @@ pub struct PartialHereDoc {

const NEWLINE: char = '\n';

/// Counts the number of leading literal tab characters in `i`.
fn leading_tabs<'a, I: IntoIterator<Item = &'a TextUnit>>(i: I) -> usize {
i.into_iter()
.take_while(|&unit| unit == &Literal('\t'))
.count()
}

impl Lexer {
/// Reads a line literally.
///
Expand Down Expand Up @@ -78,21 +85,25 @@ impl Lexer {
let line_string = line_text.to_string();
(line_text, line_string)
};
// TODO Strip leading tabs depending on the here-doc operator type

if !self.skip_if(|c| c == NEWLINE).await? {
todo!("Return an error: unexpected EOF, the delimiter missing");
}

if line_string == delimiter_string {
let skip_count = if remove_tabs {
leading_tabs(&line_text.0)
} else {
0
};
if line_string[skip_count..] == delimiter_string {
return Ok(HereDoc {
delimiter,
remove_tabs,
content,
});
}

content.0.extend(line_text.0);
content.0.extend({ line_text }.0.drain(skip_count..));
content.0.push(Literal(NEWLINE));
}
}
Expand All @@ -105,6 +116,16 @@ mod tests {
use crate::syntax::TextUnit::*;
use futures::executor::block_on;

#[test]
fn leading_tabs_test() {
let c = leading_tabs(std::iter::empty());
assert_eq!(c, 0);
let c = leading_tabs(&[Literal('\t'), Literal('a')]);
assert_eq!(c, 1);
let c = leading_tabs(&[Literal('\t'), Literal('\t'), Literal('\t')]);
assert_eq!(c, 3);
}

#[test]
fn lexer_line() {
let mut lexer = Lexer::with_source(Source::Unknown, "\n");
Expand Down Expand Up @@ -234,4 +255,19 @@ END
]
);
}

#[test]
fn lexer_here_doc_content_with_tabs_removed() {
let heredoc = partial_here_doc("BAR", true);

let mut lexer = Lexer::with_source(Source::Unknown, "\t\t\tfoo\n\tBAR\n\nbaz\nBAR\nX");
let heredoc = block_on(lexer.here_doc_content(heredoc)).unwrap();
assert_eq!(heredoc.delimiter.to_string(), "BAR");
assert_eq!(heredoc.remove_tabs, true);
assert_eq!(heredoc.content.to_string(), "foo\n");

let location = block_on(lexer.location()).unwrap();
assert_eq!(location.line.number.get(), 3);
assert_eq!(location.column.get(), 1);
}
}