• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vcfxb / wright-lang / 10029090466

21 Jul 2024 02:27PM UTC coverage: 61.486% (+2.6%) from 58.917%
10029090466

push

github

vcfxb
Parser Benchmark

190 of 466 branches covered (40.77%)

Branch coverage included in aggregate %.

629 of 866 relevant lines covered (72.63%)

21.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.19
/wright/src/parser/error.rs
1
//! Representation and implementation relating to errors that may be encountered in parsing.
2

3
use crate::{
4
    reporting::{Diagnostic, Highlight},
5
    source_tracking::fragment::Fragment,
6
};
7
use std::borrow::Cow;
8

9
/// All the different errors that can be produced in the process of parsing.
10
/// The names of these should be self-describing, but in cases when one of these needs to appear in a diagnostic,
11
/// use [ParserErrorKind::describe].
12
#[allow(missing_docs)]
13
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6!
14
pub enum ParserErrorKind {
15
    UnterminatedStringLiteralEncountered,
16
    UnterminatedMultilineCommentEncountered,
17
    ExpectedIdentifier,
18
    ExpectedPath,
19
    ExpectedWhitespace,
20
}
21

22
impl ParserErrorKind {
23
    /// Get a short description of this kind of error.
24
    pub const fn describe(self) -> &'static str {
1✔
25
        use ParserErrorKind::*;
26

27
        match self {
1!
28
            ExpectedIdentifier => "expected identifier",
1✔
29
            ExpectedPath => "expected path or identifier",
×
30
            ExpectedWhitespace => "expected whitespace character(s)",
×
31
            UnterminatedMultilineCommentEncountered => {
32
                "encountered unterminated multiline comment while parsing"
×
33
            }
34
            UnterminatedStringLiteralEncountered => {
35
                "encountered unterminated string literal while parsing"
×
36
            }
37
        }
38
    }
1✔
39

40
    /// Return this [ParserErrorKind] cast to a [u64], adding 1, preceded by the letters "WPE" standing for "Wright Parser Error".
41
    pub fn error_code_string(self) -> String {
1✔
42
        format!("WPE{}", self as u64 + 1)
1✔
43
    }
1✔
44
}
45

46
/// An error that occurred while parsing.
47
/// This error structure is pretty simple compared to what can be represented using a diagnostic. That's fine,
48
/// since most of the more complex errors arise when typechecking, rather than checking syntax.
49
#[derive(Debug)]
×
50
pub struct ParserError {
51
    /// What type/cause there is for this error.
52
    pub kind: ParserErrorKind,
×
53

54
    /// Where this error occurred.
55
    pub location: Fragment,
×
56

57
    /// Optionally, a help string that can be printed with this error.
58
    pub help: Option<Cow<'static, str>>,
×
59
}
60

61
impl ParserError {
62
    /// Turn this parser error into a full blown compiler error.
63
    pub fn as_diagnostic(self) -> Diagnostic {
1✔
64
        let description = self.kind.describe();
1✔
65

66
        let mut diagnostic = Diagnostic::error()
3✔
67
            .with_code(self.kind.error_code_string())
1✔
68
            .with_message(description)
69
            .with_highlights([Highlight::primary(self.location, "")]);
1✔
70

71
        if let Some(help) = self.help {
1!
72
            diagnostic = diagnostic.with_notes([help]);
×
73
        }
74

75
        diagnostic
1✔
76
    }
1!
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc