prepare from last year's setup
This commit is contained in:
commit
ab26e1e1a1
5 changed files with 143 additions and 0 deletions
14
src/bin/day1.rs
Normal file
14
src/bin/day1.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
|
||||
|
||||
use aoc23::read;
|
||||
use chumsky::prelude::*;
|
||||
|
||||
fn parser() -> impl Parser<char, (), Error = Simple<char>> {
|
||||
end()
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let _ = read("input.txt", parser())?;
|
||||
|
||||
Ok(())
|
||||
}
|
72
src/lib.rs
Normal file
72
src/lib.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use anyhow::{anyhow, Context};
|
||||
use ariadne::{Label, Report, ReportKind, Source};
|
||||
use chumsky::{error::SimpleReason, prelude::*};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
fn report_err(buf: &str, path_str: &str, err: Vec<Simple<char>>) {
|
||||
for e in err {
|
||||
let mut report = Report::build(ReportKind::Error, path_str, e.span().start);
|
||||
match (e.reason(), e.found()) {
|
||||
(SimpleReason::Unexpected, Some(found)) => {
|
||||
report.set_message("Unexpected token");
|
||||
report.add_label(
|
||||
Label::new((path_str, e.span()))
|
||||
.with_message(format!("Unexpected token {found}"))
|
||||
);
|
||||
if e.expected().len() > 0 {
|
||||
report.set_note(format!(
|
||||
"Expected {}",
|
||||
e.expected()
|
||||
.map(|ex| match ex {
|
||||
Some(ex) => format!("{ex:?}"),
|
||||
None => "end of file".to_owned()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
));
|
||||
}
|
||||
},
|
||||
|
||||
(SimpleReason::Unexpected, None) => {
|
||||
report.set_message("Unexpected end of file");
|
||||
},
|
||||
|
||||
(SimpleReason::Unclosed { span, delimiter }, found) => {
|
||||
report.set_message("Unclosed delimiter");
|
||||
report.add_label(
|
||||
Label::new((path_str, span.clone()))
|
||||
.with_message(format!("Unclosed delimiter {}", delimiter))
|
||||
);
|
||||
if let Some(found) = found {
|
||||
report.add_label(
|
||||
Label::new((path_str, e.span()))
|
||||
.with_message(format!("Must be closed before this {found}"))
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
(SimpleReason::Custom(msg), _) => {
|
||||
report.set_message(msg);
|
||||
report.add_label(Label::new((path_str, e.span())).with_message(msg));
|
||||
}
|
||||
};
|
||||
report
|
||||
.finish()
|
||||
.print((path_str, Source::from(buf)))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read<P, C, T>(path: P, parser: C) -> anyhow::Result<T>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
C: Parser<char, T, Error = Simple<char>>
|
||||
{
|
||||
let path = path.as_ref();
|
||||
let buf = fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read {}", path.display()))?;
|
||||
parser.parse(buf.as_str()).map_err(|errors| {
|
||||
report_err(&buf, &path.to_string_lossy(), errors);
|
||||
anyhow!("Failed to parse input")
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue