feat: starting to get pretty error messages

This commit is contained in:
rvcas
2022-10-11 13:55:38 -04:00
committed by Lucas
parent 59d7b54473
commit da89e9902c
8 changed files with 103 additions and 12 deletions

View File

@@ -498,6 +498,12 @@ pub struct Span {
pub end: usize,
}
impl From<Span> for miette::SourceSpan {
fn from(span: Span) -> Self {
Self::new(span.start.into(), span.end.into())
}
}
impl Span {
pub fn empty() -> Self {
use chumsky::Span;

View File

@@ -1,11 +1,15 @@
use std::{collections::HashSet, fmt};
use miette::Diagnostic;
use crate::{ast::Span, token::Token};
#[derive(Debug)]
#[derive(Debug, Diagnostic, thiserror::Error)]
#[error("{}", .kind)]
pub struct ParseError {
kind: ErrorKind,
span: Span,
pub kind: ErrorKind,
#[label]
pub span: Span,
#[allow(dead_code)]
while_parsing: Option<(Span, &'static str)>,
expected: HashSet<Pattern>,
@@ -63,19 +67,24 @@ impl<T: Into<Pattern>> chumsky::Error<T> for ParseError {
}
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Diagnostic, thiserror::Error)]
pub enum ErrorKind {
#[error("unexpected end")]
UnexpectedEnd,
#[error("unexpected {0}")]
#[diagnostic(help("try removing it"))]
Unexpected(Pattern),
#[error("unclosed {start}")]
Unclosed {
start: Pattern,
before_span: Span,
before: Option<Pattern>,
},
#[error("no end branch")]
NoEndBranch,
}
#[derive(Debug, PartialEq, Eq, Hash)]
#[derive(Debug, PartialEq, Eq, Hash, Diagnostic, thiserror::Error)]
pub enum Pattern {
Char(char),
Token(Token),