use super::core::Lexer;
use crate::parser::core::Result;
use crate::parser::error::Error;
use crate::parser::error::SyntaxError;
use crate::syntax::TextUnit;
impl Lexer<'_> {
pub async fn command_substitution(&mut self, start_index: usize) -> Result<Option<TextUnit>> {
let opening_location = match self.consume_char_if(|c| c == '(').await? {
Some(ch) => ch.location.clone(),
None => return Ok(None),
};
let content = self.inner_program_boxed().await?.into();
if !self.skip_if(|c| c == ')').await? {
let cause = SyntaxError::UnclosedCommandSubstitution { opening_location }.into();
let location = self.location().await?.clone();
return Err(Error { cause, location });
}
let location = self.location_range(start_index..self.index());
Ok(Some(TextUnit::CommandSubst { content, location }))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::error::ErrorCause;
use crate::source::Source;
use assert_matches::assert_matches;
use futures_executor::block_on;
#[test]
fn lexer_command_substitution_success() {
let mut lexer = Lexer::from_memory("$( foo bar )baz", Source::Unknown);
block_on(lexer.peek_char()).unwrap();
lexer.consume_char();
let result = block_on(lexer.command_substitution(0)).unwrap().unwrap();
assert_matches!(result, TextUnit::CommandSubst { location, content } => {
assert_eq!(*location.code.value.borrow(), "$( foo bar )baz");
assert_eq!(location.code.start_line_number.get(), 1);
assert_eq!(location.code.source, Source::Unknown);
assert_eq!(location.range, 0..12);
assert_eq!(&*content, " foo bar ");
});
let next = block_on(lexer.location()).unwrap();
assert_eq!(*next.code.value.borrow(), "$( foo bar )baz");
assert_eq!(next.code.start_line_number.get(), 1);
assert_eq!(next.code.source, Source::Unknown);
assert_eq!(next.range, 12..13);
}
#[test]
fn lexer_command_substitution_none() {
let mut lexer = Lexer::from_memory("$ foo bar )baz", Source::Unknown);
block_on(lexer.peek_char()).unwrap();
lexer.consume_char();
let result = block_on(lexer.command_substitution(0)).unwrap();
assert_eq!(result, None);
let next = block_on(lexer.location()).unwrap();
assert_eq!(*next.code.value.borrow(), "$ foo bar )baz");
assert_eq!(next.code.start_line_number.get(), 1);
assert_eq!(next.code.source, Source::Unknown);
assert_eq!(next.range, 1..2);
}
#[test]
fn lexer_command_substitution_unclosed() {
let mut lexer = Lexer::from_memory("$( foo bar baz", Source::Unknown);
block_on(lexer.peek_char()).unwrap();
lexer.consume_char();
let e = block_on(lexer.command_substitution(0)).unwrap_err();
assert_matches!(e.cause,
ErrorCause::Syntax(SyntaxError::UnclosedCommandSubstitution { opening_location }) => {
assert_eq!(*opening_location.code.value.borrow(), "$( foo bar baz");
assert_eq!(opening_location.code.start_line_number.get(), 1);
assert_eq!(opening_location.code.source, Source::Unknown);
assert_eq!(opening_location.range, 1..2);
});
assert_eq!(*e.location.code.value.borrow(), "$( foo bar baz");
assert_eq!(e.location.code.start_line_number.get(), 1);
assert_eq!(e.location.code.source, Source::Unknown);
assert_eq!(e.location.range, 14..14);
}
}