Skip to content

Commit

Permalink
Remove Lexer::line_continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed May 22, 2021
1 parent 0007547 commit 334732a
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 111 deletions.
1 change: 0 additions & 1 deletion yash-syntax/src/parser/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ impl Parser<'_> {
/// peeked.
pub async fn has_blank(&mut self) -> Result<bool> {
assert!(self.token.is_none(), "There should be no pending token");
self.lexer.line_continuations().await?;
let c = self.lexer.peek_char().await?;
Ok(c.map_or(false, |c| is_blank(c.value)))
}
Expand Down
2 changes: 0 additions & 2 deletions yash-syntax/src/parser/lex/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl Lexer {
if !self.skip_if(|c| c == '(').await? {
return Ok(Err(location));
}
self.line_continuations().await?;
if !self.skip_if(|c| c == '(').await? {
self.rewind(index);
return Ok(Err(location));
Expand All @@ -77,7 +76,6 @@ impl Lexer {
return Err(Error { cause, location });
}
}
self.line_continuations().await?;
match self.peek_char().await? {
Some(sc) if sc.value == ')' => self.consume_char(),
Some(_) => {
Expand Down
2 changes: 0 additions & 2 deletions yash-syntax/src/parser/lex/backquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ impl Lexer {
&mut self,
double_quote_escapable: bool,
) -> Result<Option<BackquoteUnit>> {
self.line_continuations().await?;

if self.skip_if(|c| c == '\\').await? {
let is_escapable =
|c| matches!(c, '$' | '`' | '\\') || c == '"' && double_quote_escapable;
Expand Down
15 changes: 1 addition & 14 deletions yash-syntax/src/parser/lex/braced_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ pub fn is_name_char(c: char) -> bool {
impl Lexer {
/// Consumes a length prefix (`#`) if any.
async fn length_prefix(&mut self) -> Result<bool> {
self.line_continuations().await?;
let index = self.index();
if !self.skip_if(|c| c == '#').await? {
return Ok(false);
}

self.line_continuations().await?;
if let Some(&SourceChar { value: '}', .. }) = self.peek_char().await? {
self.rewind(index);
Ok(false)
Expand All @@ -54,13 +52,6 @@ impl Lexer {
}
}

/// Consumes a POSIXly-portable name character optionally preceded by line
/// continuations.
async fn consume_name_char(&mut self) -> Result<Option<&SourceChar>> {
self.line_continuations().await?;
self.consume_char_if(is_name_char).await
}

/// Parses a parameter expansion that is enclosed in braces.
///
/// The initial `$` must have been consumed before calling this function.
Expand All @@ -85,8 +76,6 @@ impl Lexer {

let has_length_prefix = self.length_prefix().await?;

self.line_continuations().await?;

let sc = self.peek_char().await?.unwrap();
let c = sc.value;
let name = if is_special_parameter_char(c) {
Expand All @@ -95,7 +84,7 @@ impl Lexer {
} else if is_name_char(c) {
self.consume_char();
let mut name = c.to_string();
while let Some(c) = self.consume_name_char().await? {
while let Some(c) = self.consume_char_if(is_name_char).await? {
name.push(c.value);
}
name
Expand All @@ -110,8 +99,6 @@ impl Lexer {
return Err(Error { cause, location });
};

self.line_continuations().await?;

if !self.skip_if(|c| c == '}').await? {
let opening_location = location;
let cause = SyntaxError::UnclosedParam { opening_location }.into();
Expand Down
2 changes: 0 additions & 2 deletions yash-syntax/src/parser/lex/dollar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ impl Lexer {
Some(c) => c.location.clone(),
};

self.line_continuations().await?;

let location = match self.raw_param(location).await? {
Ok(result) => return Ok(Some(result)),
Err(location) => location,
Expand Down
79 changes: 2 additions & 77 deletions yash-syntax/src/parser/lex/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,12 @@ impl Lexer {
Ok(self.consume_char_if(f).await?.is_some())
}

/// Skips line continuations, if any.
pub async fn line_continuations(&mut self) -> Result<()> {
async fn line_continuation(this: &mut Lexer) -> Result<Option<()>> {
let ok = this.skip_if(|c| c == '\\').await? && this.skip_if(|c| c == '\n').await?;
Ok(if ok { Some(()) } else { None })
}
self.many(line_continuation).await.map(drop)
}

/// Skips blank characters until reaching a non-blank.
///
/// This function also skips line continuations.
pub async fn skip_blanks(&mut self) -> Result<()> {
loop {
self.line_continuations().await?;
if !self.skip_if(is_blank).await? {
break Ok(());
}
}
while self.skip_if(is_blank).await? {}
Ok(())
}

/// Skips a comment, if any.
Expand Down Expand Up @@ -85,68 +72,6 @@ mod tests {
use crate::source::Source;
use futures::executor::block_on;

#[test]
fn lexer_line_continuations_success() {
let mut lexer = Lexer::with_source(Source::Unknown, "\\\n");
assert!(block_on(lexer.line_continuations()).is_ok());
assert_eq!(block_on(lexer.peek_char()), Ok(None));

let mut lexer = Lexer::with_source(Source::Unknown, "\\\n\\\n\\\n");
assert!(block_on(lexer.line_continuations()).is_ok());
assert_eq!(block_on(lexer.peek_char()), Ok(None));
}

#[test]
fn lexer_line_continuations_empty() {
let mut lexer = Lexer::with_source(Source::Unknown, "");
assert!(block_on(lexer.line_continuations()).is_ok());
assert_eq!(block_on(lexer.peek_char()), Ok(None));
}

#[test]
fn lexer_line_continuations_not_backslash() {
let mut lexer = Lexer::with_source(Source::Unknown, "\n");
assert!(block_on(lexer.line_continuations()).is_ok());

let c = block_on(lexer.peek_char()).unwrap().unwrap();
assert_eq!(c.value, '\n');
assert_eq!(c.location.line.number.get(), 1);
assert_eq!(c.location.column.get(), 1);
}

#[test]
fn lexer_line_continuations_only_backslash() {
let mut lexer = Lexer::with_source(Source::Unknown, "\\");
assert!(block_on(lexer.line_continuations()).is_ok());

let c = block_on(lexer.peek_char()).unwrap().unwrap();
assert_eq!(c.value, '\\');
assert_eq!(c.location.line.number.get(), 1);
assert_eq!(c.location.column.get(), 1);
}

#[test]
fn lexer_line_continuations_not_newline() {
let mut lexer = Lexer::with_source(Source::Unknown, "\\\\");
assert!(block_on(lexer.line_continuations()).is_ok());

let c = block_on(lexer.peek_char()).unwrap().unwrap();
assert_eq!(c.value, '\\');
assert_eq!(c.location.line.number.get(), 1);
assert_eq!(c.location.column.get(), 1);
}

#[test]
fn lexer_line_continuations_partial_match_after_success() {
let mut lexer = Lexer::with_source(Source::Unknown, "\\\n\\\\");
assert!(block_on(lexer.line_continuations()).is_ok());

let c = block_on(lexer.peek_char()).unwrap().unwrap();
assert_eq!(c.value, '\\');
assert_eq!(c.location.line.number.get(), 2);
assert_eq!(c.location.column.get(), 1);
}

#[test]
fn lexer_skip_blanks() {
let mut lexer = Lexer::with_source(Source::Unknown, " \t w");
Expand Down
2 changes: 0 additions & 2 deletions yash-syntax/src/parser/lex/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ impl Lexer {
return Ok(None);
}

self.line_continuations().await?;

let sc = match self.peek_char().await? {
None => return Ok(None),
Some(sc) => sc.clone(),
Expand Down
10 changes: 1 addition & 9 deletions yash-syntax/src/parser/lex/raw_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use super::core::Lexer;
use crate::parser::core::Result;
use crate::source::Location;
use crate::source::SourceChar;
use crate::syntax::TextUnit;

/// Tests if a character can be part of a POSIXly-portable name.
Expand Down Expand Up @@ -50,13 +49,6 @@ pub fn is_single_char_name(c: char) -> bool {
}

impl Lexer {
/// Consumes a POSIXly-portable name character optionally preceded by line
/// continuations.
async fn consume_portable_name_char(&mut self) -> Result<Option<&SourceChar>> {
self.line_continuations().await?;
self.consume_char_if(is_portable_name_char).await
}

/// Parses a parameter expansion that is not enclosed in braces.
///
/// The initial `$` must have been consumed before calling this function.
Expand All @@ -79,7 +71,7 @@ impl Lexer {
Ok(Ok(TextUnit::RawParam { name, location }))
} else if let Some(c) = self.consume_char_if(is_portable_name_char).await? {
let mut name = c.value.to_string();
while let Some(c) = self.consume_portable_name_char().await? {
while let Some(c) = self.consume_char_if(is_portable_name_char).await? {
name.push(c.value);
}
Ok(Ok(TextUnit::RawParam { name, location }))
Expand Down
2 changes: 0 additions & 2 deletions yash-syntax/src/parser/lex/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ impl Lexer {
F: FnOnce(char) -> bool,
G: FnOnce(char) -> bool,
{
self.line_continuations().await?;

if self.skip_if(|c| c == '\\').await? {
if let Some(c) = self.consume_char_if(is_escapable).await? {
return Ok(Some(Backslashed(c.value)));
Expand Down

0 comments on commit 334732a

Please sign in to comment.