rowan_nom/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::ops::Range;

pub trait RowanNomError<Lang: super::Language> {
    /// Generic error, should probably be ultimately removed
    fn from_message(message: &str) -> Self;

    fn from_expected(position: usize, message: &str) -> Self;

    fn from_expected_eof(range: Range<usize>) -> Self;

    /// Creates error: Attempted to read a token when there are none remaining
    fn from_unexpected_eof(position: usize) -> Self;

    fn from_unexpected_token(span: Range<usize>, expected: Lang::Kind, found: Lang::Kind) -> Self;

    fn with_context(self, ctx: &'static str) -> Self;

    // TODO add much more errors, include location information in constructors
}

/// A ZST that implements [`RowanNomError`] when details or context don't matter
#[derive(Debug, Default, Clone, Copy, thiserror::Error)]
#[error("dummy error used, can't provide further information")]
pub struct DummyError;

impl<Lang: super::Language> RowanNomError<Lang> for DummyError {
    fn from_message(_message: &str) -> Self {
        Self
    }

    fn from_expected(_position: usize, _message: &str) -> Self {
        Self
    }

    fn from_expected_eof(_range: Range<usize>) -> Self {
        Self
    }

    fn from_unexpected_eof(_position: usize) -> Self {
        Self
    }

    fn from_unexpected_token(
        _span: Range<usize>,
        _expected: Lang::Kind,
        _found: Lang::Kind,
    ) -> Self {
        Self
    }

    fn with_context(self, _ctx: &'static str) -> Self {
        Self
    }
}