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

vcfxb / wright-lang / 9926436334

14 Jul 2024 08:01AM UTC coverage: 47.833% (-1.8%) from 49.647%
9926436334

push

github

vcfxb
cargo fmt

146 of 433 branches covered (33.72%)

Branch coverage included in aggregate %.

3 of 6 new or added lines in 2 files covered. (50.0%)

24 existing lines in 3 files now uncovered.

428 of 767 relevant lines covered (55.8%)

13.74 hits per line

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

1.69
/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::description].
12
#[allow(missing_docs)]
13
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6!
14
pub enum ParserErrorKind {
15
    UnternminatedStringLiteralEncountered,
16
    UnterminatedMultilineCommentEncountered,
17
    ExpectedIdentifier,
18
    ExpectedPath,
19
}
20

21
/// Table of all the definition strings for v
22
pub const ERROR_VARIANT_DESCRIPTION_TABLE: &[(ParserErrorKind, &str)] = &[
23
    (
24
        ParserErrorKind::UnternminatedStringLiteralEncountered,
25
        "encountered unterminated string literal while parsing",
26
    ),
27
    (
28
        ParserErrorKind::UnterminatedMultilineCommentEncountered,
29
        "encountered unterminated multiline comment while parsing",
30
    ),
31
    (ParserErrorKind::ExpectedIdentifier, "expected identifier"),
32
    (ParserErrorKind::ExpectedPath, "expected path or identifier"),
33
];
34

35
impl ParserErrorKind {
36
    /// Check (at compile time) if this [ParserErrorKind] has a descrition in the [ERROR_VARIANT_DESCRIPTION_TABLE].
37
    pub const fn has_descrition(self) -> bool {
×
38
        let mut i = 0;
×
39

40
        while i < ERROR_VARIANT_DESCRIPTION_TABLE.len() {
×
41
            if ERROR_VARIANT_DESCRIPTION_TABLE[i].0 as u64 == self as u64 {
×
42
                return true;
×
43
            }
44

45
            i += 1;
×
46
        }
47

48
        false
×
49
    }
×
50

51
    /// Get the description string of this [ParserErrorKind], if one exists. Calls to this against literals
52
    /// should be zero-cost since all the lookups are done at compile time. You can use a `const { }` block
53
    /// to ensure this.
54
    ///
55
    /// Calls against variables might be a bit more expensive, since this does an iterative lookup against the
56
    /// [ERROR_VARIANT_DESCRIPTION_TABLE].
57
    pub const fn find_description(self) -> Option<&'static str> {
×
58
        let mut i = 0;
×
59

60
        while i < ERROR_VARIANT_DESCRIPTION_TABLE.len() {
×
61
            if ERROR_VARIANT_DESCRIPTION_TABLE[i].0 as u64 == self as u64 {
×
62
                return Some(ERROR_VARIANT_DESCRIPTION_TABLE[i].1);
×
63
            }
64

65
            i += 1;
×
66
        }
67

68
        None
×
69
    }
×
70

71
    /// Return this [ParserErrorKind] cast to a [u64] preceded by the letters "WPE" standing for "Wright Parser Error".
72
    pub fn error_code_string(self) -> String {
×
73
        format!("WPE{}", self as u64)
×
74
    }
×
75
}
76

77
/// An error that occurred while parsing.
78
/// This error structure is pretty simple compared to what can be represented using a diagnostic. That's fine,
79
/// since most of the more complex errors arise when typechecking, rather than checking syntax.
80
#[derive(Debug)]
×
81
pub struct ParserError {
82
    /// What type/cause there is for this error.
83
    pub kind: ParserErrorKind,
×
84

85
    /// Where this error occurred.
86
    pub location: Fragment,
×
87

88
    /// Optionally, a help string that can be printed with this error.
NEW
89
    pub help: Option<Cow<'static, str>>,
×
90
}
91

92
impl ParserError {
93
    /// Turn this parser error into a full blown compiler error.
94
    pub fn as_diagnostic(self) -> Diagnostic {
×
NEW
95
        let description = self
×
96
            .kind
97
            .find_description()
98
            .map(ToOwned::to_owned)
99
            .unwrap_or(format!("parser error ({:?})", self.kind));
×
100

101
        let mut diagnostic = Diagnostic::error()
×
102
            .with_code(self.kind.error_code_string())
×
103
            .with_message(description)
NEW
104
            .with_highlights([Highlight::primary(self.location, "")]);
×
105

106
        if let Some(help) = self.help {
×
107
            diagnostic = diagnostic.with_notes([help]);
×
108
        }
109

110
        diagnostic
×
111
    }
×
112
}
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