Skip to main content

shape_ast/error/parse_error/
kinds.rs

1//! Parse error kinds and related enum types
2
3use super::{ExpectedToken, TokenInfo};
4use crate::error::SourceLocation;
5
6/// Specific parse error variants with structured context
7#[derive(Debug, Clone, PartialEq)]
8pub enum ParseErrorKind {
9    /// Parser expected specific tokens but found something else
10    UnexpectedToken {
11        /// What was found at this position
12        found: TokenInfo,
13        /// What tokens/rules were expected
14        expected: Vec<ExpectedToken>,
15    },
16
17    /// End of input reached unexpectedly
18    UnexpectedEof {
19        /// What tokens/rules were expected
20        expected: Vec<ExpectedToken>,
21    },
22
23    /// Unterminated string literal
24    UnterminatedString {
25        /// Where the string started
26        start_location: SourceLocation,
27        /// The delimiter used
28        delimiter: StringDelimiter,
29    },
30
31    /// Unterminated block comment
32    UnterminatedComment {
33        /// Where the comment started
34        start_location: SourceLocation,
35    },
36
37    /// Invalid number literal
38    InvalidNumber {
39        /// The invalid number text
40        text: String,
41        /// Why it's invalid
42        reason: NumberError,
43    },
44
45    /// Unbalanced delimiter (brackets, braces, parentheses)
46    UnbalancedDelimiter {
47        /// The opening delimiter
48        opener: char,
49        /// Where it was opened
50        open_location: SourceLocation,
51        /// What was found instead of closing (if any)
52        found: Option<char>,
53    },
54
55    /// Reserved keyword used as identifier
56    ReservedKeyword {
57        /// The keyword that was used
58        keyword: String,
59        /// Context where it was used
60        context: IdentifierContext,
61    },
62
63    /// Invalid escape sequence in string
64    InvalidEscape {
65        /// The escape sequence text
66        sequence: String,
67        /// Valid escapes for reference
68        valid_escapes: Vec<String>,
69    },
70
71    /// Invalid character in source
72    InvalidCharacter {
73        /// The invalid character
74        char: char,
75        /// Unicode codepoint for non-printable chars
76        codepoint: u32,
77    },
78
79    /// Missing required component
80    MissingComponent {
81        /// What's missing
82        component: MissingComponentKind,
83        /// Where it should appear
84        after: Option<String>,
85    },
86
87    /// Custom parse error from semantic validation during parsing
88    Custom {
89        /// Error message
90        message: String,
91    },
92}
93
94/// String delimiter types
95#[derive(Debug, Clone, Copy, PartialEq)]
96pub enum StringDelimiter {
97    DoubleQuote,
98    SingleQuote,
99    Backtick,
100}
101
102/// Why a number is invalid
103#[derive(Debug, Clone, PartialEq)]
104pub enum NumberError {
105    MultipleDecimalPoints,
106    InvalidExponent,
107    TrailingDecimalPoint,
108    LeadingZeros,
109    InvalidDigit(char),
110    TooLarge,
111    Empty,
112}
113
114/// What component is missing
115#[derive(Debug, Clone, PartialEq)]
116pub enum MissingComponentKind {
117    Semicolon,
118    ClosingBrace,
119    ClosingBracket,
120    ClosingParen,
121    FunctionBody,
122    Expression,
123    TypeAnnotation,
124    Identifier,
125    Arrow,
126    Colon,
127}
128
129/// Context where an identifier was expected
130#[derive(Debug, Clone, PartialEq)]
131pub enum IdentifierContext {
132    VariableName,
133    FunctionName,
134    ParameterName,
135    PatternName,
136    TypeName,
137    PropertyName,
138}
139
140/// Error severity levels
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
142pub enum ErrorSeverity {
143    #[default]
144    Error,
145    Warning,
146    Info,
147    Hint,
148}