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

[NFCI] Use line_ending_or_eof consistently in parsers #300

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/ghci/parse/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use miette::miette;
use winnow::ascii::line_ending;
use winnow::ascii::space0;
use winnow::combinator::alt;
use winnow::combinator::eof;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::combinator::repeat;
Expand All @@ -16,6 +15,7 @@ use winnow::Located;
use winnow::PResult;
use winnow::Parser;

use crate::ghci::parse::lines::line_ending_or_eof;
use crate::ghci::GhciCommand;

use super::lines::rest_of_line;
Expand Down Expand Up @@ -173,7 +173,7 @@ fn multiline_eval_command(input: &mut Located<&str>) -> PResult<ByteSpanCommand>
.with_span()
.parse_next(input)?;
multiline_eval_end.parse_next(input)?;
let _ = (space0, alt((line_ending, eof))).parse_next(input)?;
let _ = (space0, line_ending_or_eof).parse_next(input)?;

Ok(ByteSpanCommand {
// `command` ends with a newline so we put a newline after the `:{` but not before the
Expand Down
4 changes: 2 additions & 2 deletions src/ghci/parse/ghc_message/compilation_summary.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use winnow::ascii::digit1;
use winnow::ascii::line_ending;
use winnow::combinator::alt;
use winnow::combinator::opt;
use winnow::PResult;
use winnow::Parser;

use crate::ghci::parse::lines::line_ending_or_eof;
use crate::ghci::parse::CompilationResult;

use super::GhcMessage;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn compilation_summary(input: &mut &str) -> PResult<GhcMessage> {
let _ = " module".parse_next(input)?;
let _ = opt("s").parse_next(input)?;
let _ = " loaded.".parse_next(input)?;
let _ = line_ending.parse_next(input)?;
let _ = line_ending_or_eof.parse_next(input)?;

Ok(GhcMessage::Summary(CompilationSummary {
result,
Expand Down
4 changes: 2 additions & 2 deletions src/ghci/parse/ghc_message/module_import_cycle_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use camino::Utf8PathBuf;
use itertools::Itertools;
use winnow::ascii::line_ending;
use winnow::ascii::space1;
use winnow::combinator::alt;
use winnow::combinator::opt;
Expand All @@ -10,6 +9,7 @@ use winnow::PResult;
use winnow::Parser;

use crate::ghci::parse::haskell_grammar::module_name;
use crate::ghci::parse::lines::line_ending_or_eof;
use crate::ghci::parse::lines::rest_of_line;
use crate::ghci::parse::Severity;

Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn module_import_cycle_diagnostic(input: &mut &str) -> PResult<Vec<GhcMessag
"Module graph contains a cycle:",
))
.parse_next(input)?;
let _ = line_ending.parse_next(input)?;
let _ = line_ending_or_eof.parse_next(input)?;
repeat(1.., parse_import_cycle_line).parse_next(input)
}

Expand Down
20 changes: 19 additions & 1 deletion src/ghci/parse/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
<I as Stream>::Slice: SliceLen,
{
let line = till_line_ending.parse_next(input)?;
let ending = alt((line_ending, eof)).parse_next(input)?;
let ending = line_ending_or_eof.parse_next(input)?;

if line.slice_len() == 0 && ending.slice_len() == 0 {
Err(ErrMode::Backtrack(ContextError::new()))
Expand All @@ -43,6 +43,24 @@ where
}
}

/// Parse a line ending or the end of the file.
///
/// This is useful for line-oriented parsers that may consume input with no trailing newline
/// character. While this is a [violation of the POSIX spec][posix], VS Code [does it by
/// default][vscode].
///
/// [posix]: https://stackoverflow.com/a/729795
/// [vscode]: https://stackoverflow.com/questions/44704968/visual-studio-code-insert-newline-at-the-end-of-files
pub fn line_ending_or_eof<I>(input: &mut I) -> PResult<<I as Stream>::Slice>
where
I: Stream + StreamIsPartial + for<'i> FindSlice<&'i str> + for<'i> Compare<&'i str>,
<I as Stream>::Token: AsChar,
<I as Stream>::Token: Clone,
<I as Stream>::Slice: SliceLen,
{
alt((line_ending, eof)).parse_next(input)
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
Expand Down
Loading