fix: errors

This commit is contained in:
Artemy Egorov 2024-08-12 17:46:57 +03:00
parent 50f5c7fe19
commit ebc47a2c27
2 changed files with 58 additions and 20 deletions

View file

@ -1,5 +1,5 @@
use ariadne::{Color, Label, Report, ReportKind, Source}; use ariadne::{Color, Label, Report, ReportKind, Source};
use chumsky::{input::Input, Parser}; use chumsky::{error::RichReason, input::Input, Parser};
use dalet::daleth::{lexer::lexer, parser::parser}; use dalet::daleth::{lexer::lexer, parser::parser};
fn main() { fn main() {
@ -12,16 +12,19 @@ fn main() {
match parsed.into_result() { match parsed.into_result() {
Ok(t) => { Ok(t) => {
println!("{:#?}", t); println!("{:#?}", t);
// println!("{}", format(&t));
} }
Err(e) => e.into_iter().for_each(|e| { Err(e) => e.into_iter().for_each(|e| {
// println!("{:#}", ) let msg = match e.reason() {
RichReason::Many(errs) => errs[0].to_string(),
_ => e.to_string(),
};
Report::build(ReportKind::Error, src_file, e.span().start) Report::build(ReportKind::Error, src_file, e.span().start)
.with_code("Parser") .with_code("Parser")
.with_message(e.to_string()) .with_message(e.to_string())
.with_label( .with_label(
Label::new((src_file, e.span().into_range())) Label::new((src_file, e.span().into_range()))
.with_message(e.to_string()) .with_message(&msg)
.with_color(Color::Red), .with_color(Color::Red),
) )
.finish() .finish()

View file

@ -26,44 +26,78 @@ pub fn tag<'tokens, 'src: 'tokens>(
.clone() .clone()
.repeated() .repeated()
.collect() .collect()
.delimited_by(just(Token::LSquare), just(Token::RSquare)); .delimited_by(just(Token::LSquare), just(Token::RSquare))
.labelled("Tags body");
let text_body = select! { let text_body = select! {
Token::TextBody(t) => t.to_owned(), Token::TextBody(t) => t.to_owned(),
Token::MLText(t) => trim_indent(t).to_owned(), Token::MLText(t) => trim_indent(t).to_owned(),
Token::MLMSText(n, t) => set_spaces(t, n).to_owned(), Token::MLMSText(n, t) => set_spaces(t, n).to_owned(),
Token::MLRText(t) => t.to_owned() Token::MLRText(t) => t.to_owned()
}; }
.labelled("Text body");
let nnbody = text_body let nnbody = text_body
.map(NNBody::Text) .map(NNBody::Text)
.or(tags_body.clone().map(NNBody::Tags)); .or(tags_body.clone().map(NNBody::Tags))
.labelled("Not null body");
let body = text_body let body = text_body
.map(Body::Text) .map(Body::Text)
.or(tags_body.clone().map(Body::Tags)) .or(tags_body.clone().map(Body::Tags))
.or_not() .or_not()
.to(Body::Null); .to(Body::Null)
.labelled("Body");
let num_arg = select! { let num_arg = select! {
Token::NumberArgument(n) => n Token::NumberArgument(n) => n
}; }
.labelled("Number argument");
let text_arg = select! { let text_arg = select! {
Token::TextArgument(t) => t.to_owned() Token::TextArgument(t) => t.to_owned()
}; }
.labelled("Text argument");
let nnarg = text_arg.map(NNArg::Text).or(num_arg.map(NNArg::Number)); let nnarg = text_arg
.map(NNArg::Text)
.or(num_arg.map(NNArg::Number))
.labelled("Not null argument");
let tnullarg = text_arg let tnullarg = text_arg
.map(TNullArg::Text) .map(TNullArg::Text)
.or_not() .or_not()
.map(|v| v.unwrap_or(TNullArg::Null)); .map(|v| v.unwrap_or(TNullArg::Null))
let hlarg = num_arg.try_map(|n, s| Hl::try_from(n).map_err(|e| Rich::custom(s, e))); .labelled("Text or null argument");
let alignarg = choice((
just(Token::TextArgument("start")).to(AlignArg::Start), let hlarg = num_arg
just(Token::TextArgument("center")).to(AlignArg::Start), .validate(|n, e, emmiter| match Hl::try_from(n) {
just(Token::TextArgument("end")).to(AlignArg::Start), Ok(l) => l,
Err(_) => {
emmiter.emit(Rich::custom(
e.span(),
"Heading level must be between 1 and 6",
)); ));
Hl::One
}
})
.labelled("Heading level argument");
let alignarg = text_arg
.clone()
.validate(|t, e, emmiter| match t.as_str() {
"start" => AlignArg::Start,
"center" => AlignArg::Center,
"end" => AlignArg::End,
_ => {
emmiter.emit(Rich::custom(
e.span(),
"Expected 'start', 'center' or 'end'",
));
AlignArg::Start
}
})
.labelled("Align argument");
let el = just(Token::El).ignore_then(nnbody.clone()).map(El); let el = just(Token::El).ignore_then(nnbody.clone()).map(El);
let h = just(Token::H) let h = just(Token::H)
@ -140,8 +174,9 @@ pub fn tag<'tokens, 'src: 'tokens>(
.map(|v| El(NNBody::Tags(v))); .map(|v| El(NNBody::Tags(v)));
let paragraph = select! { let paragraph = select! {
Token::Paragraph(t) => P(NNBody::Text(t.replace("\n"," ").trim().to_owned())) Token::Paragraph(t) => P(NNBody::Text(t.replace("\n", " ").trim().to_owned()))
}; }
.labelled("Paragraph");
choice(( choice((
el, h, p, br, ul, ol, row, link, navlink, btn, navbtn, img, table, tcol, tpcol, hr, b, el, h, p, br, ul, ol, row, link, navlink, btn, navbtn, img, table, tcol, tpcol, hr, b,