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] Unwrap GhcMessage in diagnostic parsers #299

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
13 changes: 5 additions & 8 deletions src/ghci/parse/ghc_message/cant_find_file_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::ghci::parse::lines::until_newline;

use crate::ghci::parse::ghc_message::position;
use crate::ghci::parse::ghc_message::severity;
use crate::ghci::parse::ghc_message::GhcMessage;

use super::GhcDiagnostic;

Expand All @@ -16,20 +15,20 @@ use super::GhcDiagnostic;
/// ```plain
/// <no location info>: error: can't find file: Why.hs
/// ```
pub fn cant_find_file_diagnostic(input: &mut &str) -> PResult<GhcMessage> {
pub fn cant_find_file_diagnostic(input: &mut &str) -> PResult<GhcDiagnostic> {
let _ = position::parse_unhelpful_position.parse_next(input)?;
let _ = space1.parse_next(input)?;
let severity = severity::parse_severity_colon.parse_next(input)?;
let _ = space1.parse_next(input)?;
let _ = "can't find file: ".parse_next(input)?;
let path = until_newline.parse_next(input)?;

Ok(GhcMessage::Diagnostic(GhcDiagnostic {
Ok(GhcDiagnostic {
severity,
path: Some(Utf8PathBuf::from(path)),
span: Default::default(),
message: "can't find file".to_owned(),
}))
})
}

#[cfg(test)]
Expand All @@ -46,12 +45,12 @@ mod tests {
cant_find_file_diagnostic
.parse("<no location info>: error: can't find file: Why.hs\n")
.unwrap(),
GhcMessage::Diagnostic(GhcDiagnostic {
GhcDiagnostic {
severity: Severity::Error,
path: Some("Why.hs".into()),
span: Default::default(),
message: "can't find file".to_owned()
})
}
);

// Doesn't parse another error message.
Expand All @@ -71,8 +70,6 @@ mod tests {
cant_find_file_diagnostic
.parse("<no location info>: error: can't find file: Why.hs\n")
.unwrap()
.into_diagnostic()
.unwrap()
.to_string(),
"Why.hs: error: can't find file"
);
Expand Down
32 changes: 15 additions & 17 deletions src/ghci/parse/ghc_message/compilation_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use winnow::Parser;

use crate::ghci::parse::CompilationResult;

use super::GhcMessage;

/// Compilation finished.
///
/// ```text
Expand All @@ -29,7 +27,7 @@ pub struct CompilationSummary {
}

/// Parse a compilation summary, like `Ok, one module loaded.`.
pub fn compilation_summary(input: &mut &str) -> PResult<GhcMessage> {
pub fn compilation_summary(input: &mut &str) -> PResult<CompilationSummary> {
let result = alt((
"Ok".map(|_| CompilationResult::Ok),
"Failed".map(|_| CompilationResult::Err),
Expand All @@ -56,10 +54,10 @@ pub fn compilation_summary(input: &mut &str) -> PResult<GhcMessage> {
let _ = " loaded.".parse_next(input)?;
let _ = line_ending.parse_next(input)?;

Ok(GhcMessage::Summary(CompilationSummary {
Ok(CompilationSummary {
result,
modules_loaded,
}))
})
}

#[cfg(test)]
Expand All @@ -75,60 +73,60 @@ mod tests {
compilation_summary
.parse("Ok, 123 modules loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Ok,
modules_loaded: 123,
})
}
);

assert_eq!(
compilation_summary
.parse("Ok, no modules loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Ok,
modules_loaded: 0,
})
}
);

assert_eq!(
compilation_summary
.parse("Ok, one module loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Ok,
modules_loaded: 1,
})
}
);

assert_eq!(
compilation_summary
.parse("Ok, six modules loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Ok,
modules_loaded: 6,
})
}
);

assert_eq!(
compilation_summary
.parse("Failed, 7 modules loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Err,
modules_loaded: 7,
})
}
);

assert_eq!(
compilation_summary
.parse("Failed, one module loaded.\n")
.unwrap(),
GhcMessage::Summary(CompilationSummary {
CompilationSummary {
result: CompilationResult::Err,
modules_loaded: 1,
})
}
);

// Negative cases
Expand Down
19 changes: 9 additions & 10 deletions src/ghci/parse/ghc_message/compiling.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use module_and_files::CompilingModule;
use winnow::ascii::digit1;
use winnow::ascii::space0;
use winnow::PResult;
Expand All @@ -6,10 +7,8 @@ use winnow::Parser;
use crate::ghci::parse::lines::rest_of_line;
use crate::ghci::parse::module_and_files;

use super::GhcMessage;

/// Parse a `[1 of 3] Compiling Foo ( Foo.hs, Foo.o, interpreted )` message.
pub fn compiling(input: &mut &str) -> PResult<GhcMessage> {
pub fn compiling(input: &mut &str) -> PResult<CompilingModule> {
let _ = "[".parse_next(input)?;
let _ = space0.parse_next(input)?;
let _ = digit1.parse_next(input)?;
Expand All @@ -20,7 +19,7 @@ pub fn compiling(input: &mut &str) -> PResult<GhcMessage> {
let module = module_and_files.parse_next(input)?;
let _ = rest_of_line.parse_next(input)?;

Ok(GhcMessage::Compiling(module))
Ok(module)
}

#[cfg(test)]
Expand All @@ -37,10 +36,10 @@ mod tests {
compiling
.parse("[1 of 3] Compiling Foo ( Foo.hs, Foo.o, interpreted )\n")
.unwrap(),
GhcMessage::Compiling(CompilingModule {
CompilingModule {
name: "Foo".into(),
path: "Foo.hs".into()
})
}
);

assert_eq!(
Expand All @@ -51,20 +50,20 @@ mod tests {
/Users/wiggles/doggy-web-backend6/dist-newstyle/build/aarch64-osx/ghc-9.6.2/doggy-web-backend-0/l/test-dev/noopt/build/test-dev/A/DoggyPrelude/Puppy.dyn_o \
) [Doggy.Lint package changed]\n")
.unwrap(),
GhcMessage::Compiling(CompilingModule {
CompilingModule {
name: "A.DoggyPrelude.Puppy".into(),
path: "src/A/DoggyPrelude/Puppy.hs".into()
})
}
);

assert_eq!(
compiling
.parse("[1 of 4] Compiling MyLib ( src/MyLib.hs )\n")
.unwrap(),
GhcMessage::Compiling(CompilingModule {
CompilingModule {
name: "MyLib".into(),
path: "src/MyLib.hs".into()
})
}
);

// Shouldn't parse multiple lines.
Expand Down
11 changes: 5 additions & 6 deletions src/ghci/parse/ghc_message/generic_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::ghci::parse::ghc_message::message_body::parse_message_body;
use crate::ghci::parse::ghc_message::path_colon;
use crate::ghci::parse::ghc_message::position;
use crate::ghci::parse::ghc_message::severity;
use crate::ghci::parse::ghc_message::GhcMessage;

use super::GhcDiagnostic;

Expand All @@ -23,7 +22,7 @@ use super::GhcDiagnostic;
/// 6 | deriving MyClass
/// | ^^^^^^^
/// ```
pub fn generic_diagnostic(input: &mut &str) -> PResult<GhcMessage> {
pub fn generic_diagnostic(input: &mut &str) -> PResult<GhcDiagnostic> {
// TODO: Confirm that the input doesn't start with space?
let path = path_colon.parse_next(input)?;
let span = position::parse_position_range.parse_next(input)?;
Expand All @@ -32,12 +31,12 @@ pub fn generic_diagnostic(input: &mut &str) -> PResult<GhcMessage> {
let _ = space0.parse_next(input)?;
let message = parse_message_body.parse_next(input)?;

Ok(GhcMessage::Diagnostic(GhcDiagnostic {
Ok(GhcDiagnostic {
severity,
path: Some(path.to_owned()),
span,
message: message.to_owned(),
}))
})
}

#[cfg(test)]
Expand Down Expand Up @@ -65,7 +64,7 @@ mod tests {
"
))
.unwrap(),
GhcMessage::Diagnostic(GhcDiagnostic {
GhcDiagnostic {
severity: Severity::Error,
path: Some("NotStockDeriveable.hs".into()),
span: PositionRange::new(6, 12, 6, 12),
Expand All @@ -81,7 +80,7 @@ mod tests {
"
)
.into()
})
}
);

// Doesn't parse another error message.
Expand Down
16 changes: 11 additions & 5 deletions src/ghci/parse/ghc_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ fn parse_messages_inner(input: &mut &str) -> PResult<Vec<GhcMessage>> {
repeat(
0..,
alt((
compiling.map(Item::One),
generic_diagnostic.map(Item::One),
cant_find_file_diagnostic.map(Item::One),
no_location_info_diagnostic.map(Item::One),
compiling.map(GhcMessage::Compiling).map(Item::One),
generic_diagnostic
.map(GhcMessage::Diagnostic)
.map(Item::One),
compilation_summary.map(GhcMessage::Summary).map(Item::One),
cant_find_file_diagnostic
.map(GhcMessage::Diagnostic)
.map(Item::One),
no_location_info_diagnostic
.map(GhcMessage::Diagnostic)
.map(Item::One),
module_import_cycle_diagnostic.map(Item::Many),
loaded_configuration.map(Item::One),
compilation_summary.map(Item::One),
rest_of_line.map(|line| {
tracing::debug!(line, "Ignoring GHC output line");
Item::Ignore
Expand Down
17 changes: 7 additions & 10 deletions src/ghci/parse/ghc_message/no_location_info_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use winnow::Parser;
use crate::ghci::parse::ghc_message::message_body::parse_message_body;
use crate::ghci::parse::ghc_message::position;
use crate::ghci::parse::ghc_message::severity;
use crate::ghci::parse::ghc_message::GhcMessage;

use super::GhcDiagnostic;

Expand All @@ -17,19 +16,19 @@ use super::GhcDiagnostic;
/// Could not find module ‘Example’
/// It is not a module in the current program, or in any known package.
/// ```
pub fn no_location_info_diagnostic(input: &mut &str) -> PResult<GhcMessage> {
pub fn no_location_info_diagnostic(input: &mut &str) -> PResult<GhcDiagnostic> {
let _ = position::parse_unhelpful_position.parse_next(input)?;
let _ = space1.parse_next(input)?;
let severity = severity::parse_severity_colon.parse_next(input)?;
let _ = space0.parse_next(input)?;
let message = parse_message_body.parse_next(input)?;

Ok(GhcMessage::Diagnostic(GhcDiagnostic {
Ok(GhcDiagnostic {
severity,
path: None,
span: Default::default(),
message: message.to_owned(),
}))
})
}

#[cfg(test)]
Expand All @@ -52,15 +51,15 @@ mod tests {
);
assert_eq!(
no_location_info_diagnostic.parse(message).unwrap(),
GhcMessage::Diagnostic(GhcDiagnostic {
GhcDiagnostic {
severity: Severity::Error,
path: None,
span: Default::default(),
message: "\n Could not find module ‘Example’\
\n It is not a module in the current program, or in any known package.\
\n"
.into()
})
}
);

assert_eq!(
Expand All @@ -73,7 +72,7 @@ mod tests {
"
))
.unwrap(),
GhcMessage::Diagnostic(GhcDiagnostic {
GhcDiagnostic {
severity: Severity::Error,
path: None,
span: Default::default(),
Expand All @@ -85,7 +84,7 @@ mod tests {
"
)
.into()
})
}
);

// Shouldn't parse another error.
Expand Down Expand Up @@ -115,8 +114,6 @@ mod tests {
no_location_info_diagnostic
.parse(message)
.unwrap()
.into_diagnostic()
.unwrap()
.to_string(),
message
);
Expand Down
Loading