Skip to content

Commit

Permalink
Fiddle Spanned into CommentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jul 23, 2024
1 parent 58d428c commit 6d845d5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/aux_builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Build for AuxBuilder {
stdout: vec![],
})?
.map(|s| s.into_bytes());
let comments = Comments::parse(&file_contents.content, &config, &self.aux_file)
let comments = Comments::parse(file_contents.as_ref(), &config)
.map_err(|errors| Errored::new(errors, "parse aux comments"))?;
assert_eq!(
comments.revisions, None,
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ pub fn test_command(mut config: Config, path: &Path) -> Result<Command> {

config.fill_host_and_target()?;

let content =
std::fs::read(path).wrap_err_with(|| format!("failed to read {}", display(path)))?;
let comments = Comments::parse(&content, &config, path)
let content = Spanned::read_from_file(path)
.wrap_err_with(|| format!("failed to read {}", display(path)))?
.map(|s| s.into_bytes());
let comments = Comments::parse(content.as_ref(), &config)
.map_err(|errors| color_eyre::eyre::eyre!("{errors:#?}"))?;
let config = TestConfig {
config,
Expand Down Expand Up @@ -322,7 +323,7 @@ fn parse_and_test_file(
config: Config,
file_contents: Spanned<Vec<u8>>,
) -> Result<Vec<TestRun>, Errored> {
let comments = Comments::parse(&file_contents.content, &config, status.path())
let comments = Comments::parse(file_contents.as_ref(), &config)
.map_err(|errors| Errored::new(errors, "parse comments"))?;
const EMPTY: &[String] = &[String::new()];
// Run the test for all revisions
Expand Down
23 changes: 5 additions & 18 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::{BTreeMap, HashMap},
num::NonZeroUsize,
path::Path,
};

use bstr::{ByteSlice, Utf8Error};
Expand Down Expand Up @@ -300,11 +299,10 @@ impl Comments {
/// Parse comments in `content`.
/// `path` is only used to emit diagnostics if parsing fails.
pub(crate) fn parse(
content: &(impl AsRef<[u8]> + ?Sized),
content: Spanned<&[u8]>,
config: &Config,
file: &Path,
) -> std::result::Result<Self, Vec<Error>> {
CommentParser::new(config).parse(content, file)
CommentParser::new(config).parse(content)
}
}

Expand All @@ -320,27 +318,16 @@ impl CommentParser<Comments> {
this
}

fn parse(
mut self,
content: &(impl AsRef<[u8]> + ?Sized),
file: &Path,
) -> std::result::Result<Comments, Vec<Error>> {
fn parse(mut self, content: Spanned<&[u8]>) -> std::result::Result<Comments, Vec<Error>> {
let defaults = std::mem::take(self.comments.revisioned.get_mut(&[][..]).unwrap());

let mut delayed_fallthrough = Vec::new();
let mut fallthrough_to = None; // The line that a `|` will refer to.
let mut last_line = 0;
for (l, line) in content.as_ref().lines().enumerate() {
for (l, line) in content.lines().enumerate() {
last_line = l + 1;
let l = NonZeroUsize::new(l + 1).unwrap(); // enumerate starts at 0, but line numbers start at 1
let span = Span {
file: file.to_path_buf(),
line_start: l,
line_end: l,
col_start: NonZeroUsize::new(1).unwrap(),
col_end: NonZeroUsize::new(line.len() + 1).unwrap(),
};
match self.parse_checked_line(fallthrough_to, l, Spanned::new(line, span)) {
match self.parse_checked_line(fallthrough_to, l, line) {
Ok(ParsePatternResult::Other) => {
fallthrough_to = None;
}
Expand Down
18 changes: 9 additions & 9 deletions src/parser/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use spanned::Spanned;

use crate::{
parser::{Condition, ErrorMatchKind, Pattern},
Expand All @@ -16,7 +16,7 @@ fn main() {
let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR: encountered a dangling reference (address $HEX is unallocated)
}
";
let comments = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap();
let comments = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap();
println!("parsed comments: {:#?}", comments);
assert_eq!(comments.revisioned.len(), 1);
let revisioned = &comments.revisioned[&vec![]];
Expand All @@ -42,7 +42,7 @@ fn main() {
let _x: i32 = 0u32; //~ E0308
}
";
let comments = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap();
let comments = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap();
println!("parsed comments: {:#?}", comments);
assert_eq!(comments.revisioned.len(), 1);
let revisioned = &comments.revisioned[&vec![]];
Expand All @@ -62,7 +62,7 @@ fn main() {
let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ encountered a dangling reference (address $HEX is unallocated)
}
";
let errors = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap_err();
let errors = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap_err();
println!("parsed comments: {:#?}", errors);
assert_eq!(errors.len(), 1);
match &errors[0] {
Expand All @@ -80,7 +80,7 @@ fn parse_slash_slash_at() {
use std::mem;
";
let comments = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap();
let comments = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap();
println!("parsed comments: {:#?}", comments);
assert_eq!(comments.revisioned.len(), 1);
let revisioned = &comments.revisioned[&vec![]];
Expand All @@ -96,7 +96,7 @@ fn parse_regex_error_pattern() {
use std::mem;
";
let comments = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap();
let comments = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap();
println!("parsed comments: {:#?}", comments);
assert_eq!(comments.revisioned.len(), 1);
let revisioned = &comments.revisioned[&vec![]];
Expand All @@ -112,7 +112,7 @@ fn parse_slash_slash_at_fail() {
use std::mem;
";
let errors = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap_err();
let errors = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap_err();
println!("parsed comments: {:#?}", errors);
assert_eq!(errors.len(), 2);
match &errors[0] {
Expand All @@ -136,7 +136,7 @@ fn missing_colon_fail() {
use std::mem;
";
let errors = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap_err();
let errors = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap_err();
println!("parsed comments: {:#?}", errors);
assert_eq!(errors.len(), 1);
match &errors[0] {
Expand All @@ -150,7 +150,7 @@ use std::mem;
#[test]
fn parse_x86_64() {
let s = r"//@ only-target-x86_64-unknown-linux";
let comments = Comments::parse(s, &Config::rustc(""), Path::new("")).unwrap();
let comments = Comments::parse(Spanned::dummy(s.as_bytes()), &Config::rustc("")).unwrap();
println!("parsed comments: {:#?}", comments);
assert_eq!(comments.revisioned.len(), 1);
let revisioned = &comments.revisioned[&vec![]];
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn config() -> Config {
macro_rules! config {
($config:ident = $s:expr) => {
let path = Path::new("moobar");
let comments = Comments::parse($s, &$config, path).unwrap();
let comments = Comments::parse(Spanned::dummy($s.as_bytes()), &$config).unwrap();
#[allow(unused_mut)]
let mut $config = TestConfig {
config: $config,
Expand Down

0 comments on commit 6d845d5

Please sign in to comment.