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

vcfxb / wright-lang / 15061769677

16 May 2025 06:02AM UTC coverage: 75.523% (+0.7%) from 74.809%
15061769677

push

github

vcfxb
chore: cargo fmt

6 of 13 new or added lines in 4 files covered. (46.15%)

27 existing lines in 2 files now uncovered.

938 of 1242 relevant lines covered (75.52%)

29.83 hits per line

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

55.0
/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)]
14
pub enum ParserErrorKind {
15
    EncounteredUnknownToken,
16
    EncounteredUnterminatedComment,
17
    EncounteredUnterminatedString,
18
    ExpectedAtomicTypeSignature,
19
    ExpectedBooleanLiteral,
20
    ExpectedIdentifier,
21
    ExpectedImportDeclaration,
22
    ExpectedIntegerLiteral,
23
    ExpectedPath,
24
    ExpectedReferenceTypeSignature,
25
    ExpectedTypeSignature,
26
    ExpectedWhitespace,
27
    ImportMustEndWithSemicolon,
28
}
29

30
impl ParserErrorKind {
31
    /// Get a short description of this kind of error.
32
    pub const fn describe(self) -> &'static str {
1✔
33
        use ParserErrorKind::*;
34

35
        match self {
1✔
36
            EncounteredUnknownToken => "encountered unknown token",
×
37
            EncounteredUnterminatedComment => {
UNCOV
38
                "encountered unterminated multiline comment while parsing"
×
39
            }
40
            EncounteredUnterminatedString => {
41
                "encountered unterminated string literal while parsing"
×
42
            }
UNCOV
43
            ExpectedAtomicTypeSignature => "expected atomic primitive type",
×
44
            ExpectedBooleanLiteral => "expected boolean literal",
×
45
            ExpectedIdentifier => "expected identifier",
1✔
46
            ExpectedImportDeclaration => "expected import declaration",
×
47
            ExpectedIntegerLiteral => "expected integer literal",
×
48
            ExpectedPath => "expected path or identifier",
×
UNCOV
49
            ExpectedReferenceTypeSignature => "expected reference type signature",
×
UNCOV
50
            ExpectedTypeSignature => "expected type signature",
×
UNCOV
51
            ExpectedWhitespace => "expected whitespace character(s)",
×
UNCOV
52
            ImportMustEndWithSemicolon => "import declarations must end with a semicolon",
×
53
        }
54
    }
1✔
55

56
    /// Construct a [ParserError] by adding a location [Fragment] to this error variant.
57
    pub const fn at(self, f: Fragment) -> ParserError {
15✔
58
        ParserError {
15✔
59
            kind: self,
15✔
60
            location: f,
15✔
61
            help: Vec::new(),
15✔
62
        }
15✔
63
    }
15✔
64
}
65

66
/// An error that occurred while parsing.
67
/// This error structure is pretty simple compared to what can be represented using a diagnostic. That's fine,
68
/// since most of the more complex errors arise when typechecking, rather than checking syntax.
69
#[derive(Debug)]
70
pub struct ParserError {
71
    /// What type/cause there is for this error.
72
    pub kind: ParserErrorKind,
73

74
    /// Where this error occurred.
75
    pub location: Fragment,
76

77
    /// Optional help strings that can be printed with this error.
78
    pub help: Vec<Cow<'static, str>>,
79
}
80

81
impl ParserError {
82
    /// Builder-style method to add a help string to a [ParserError].
UNCOV
83
    pub fn with_help(mut self, help: impl Into<Cow<'static, str>>) -> Self {
×
UNCOV
84
        self.help.push(help.into());
×
UNCOV
85
        self
×
UNCOV
86
    }
×
87

88
    /// Turn this parser error into a full blown compiler error.
89
    pub fn as_diagnostic(self) -> Diagnostic {
1✔
90
        let description = self.kind.describe();
1✔
91

92
        // Put a little clarification if the parser reached end of source and then produced an error.
93
        let message = if self.location.is_empty_at_end_of_source() {
1✔
UNCOV
94
            Cow::Borrowed("found end of source here")
×
95
        } else {
96
            Cow::Borrowed("")
1✔
97
        };
98

99
        let mut diagnostic = Diagnostic::error()
1✔
100
            .with_message(description)
1✔
101
            .with_highlights([Highlight::primary(self.location.clone(), message)]);
1✔
102

103
        if !self.help.is_empty() {
1✔
UNCOV
104
            diagnostic = diagnostic.with_notes(self.help);
×
105
        }
1✔
106

107
        diagnostic
1✔
108
    }
1✔
109
}
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

© 2025 Coveralls, Inc