dalet-rs/examples/daleth_parser.rs

36 lines
1.1 KiB
Rust
Raw Normal View History

use ariadne::{Color, Label, Report, ReportKind, Source};
2024-08-12 17:46:57 +03:00
use chumsky::{error::RichReason, input::Input, Parser};
use dalet::daleth::{lexer::lexer, parser::parser};
fn main() {
let src_file = "daleth.dlth";
let src = include_str!("./daleth.dlth");
let lexed = lexer().parse(src).unwrap();
let parsed = parser().parse(lexed.as_slice().spanned((0..src.len()).into()));
match parsed.into_result() {
Ok(t) => {
println!("{:#?}", t);
}
Err(e) => e.into_iter().for_each(|e| {
2024-08-12 17:46:57 +03:00
let msg = match e.reason() {
RichReason::Many(errs) => errs[0].to_string(),
_ => e.to_string(),
};
Report::build(ReportKind::Error, src_file, e.span().start)
.with_code("Parser")
.with_message(e.to_string())
.with_label(
Label::new((src_file, e.span().into_range()))
2024-08-12 17:46:57 +03:00
.with_message(&msg)
.with_color(Color::Red),
)
.finish()
.print((src_file, Source::from(&src)))
.unwrap()
}),
};
}