Skip to main content

shape_ast/error/parse_error/
tokens.rs

1//! Token information types for error messages
2
3/// Information about a found token
4#[derive(Debug, Clone, PartialEq)]
5pub struct TokenInfo {
6    /// The token text (truncated if very long)
7    pub text: String,
8    /// The token kind (if identifiable)
9    pub kind: Option<TokenKind>,
10}
11
12impl TokenInfo {
13    pub fn new(text: impl Into<String>) -> Self {
14        Self {
15            text: text.into(),
16            kind: None,
17        }
18    }
19
20    pub fn with_kind(mut self, kind: TokenKind) -> Self {
21        self.kind = Some(kind);
22        self
23    }
24
25    pub fn end_of_input() -> Self {
26        Self {
27            text: String::new(),
28            kind: Some(TokenKind::EndOfInput),
29        }
30    }
31}
32
33/// Token kinds for better error messages
34#[derive(Debug, Clone, PartialEq)]
35pub enum TokenKind {
36    Identifier,
37    Keyword(String),
38    Number,
39    String,
40    Operator,
41    Punctuation,
42    EndOfInput,
43    Whitespace,
44    Comment,
45    Unknown,
46}
47
48/// What token/construct was expected
49#[derive(Debug, Clone, PartialEq)]
50pub enum ExpectedToken {
51    /// A specific literal token
52    Literal(String),
53    /// A grammar rule (will be converted to user-friendly name)
54    Rule(String),
55    /// A category of tokens
56    Category(TokenCategory),
57}
58
59impl ExpectedToken {
60    pub fn literal(s: impl Into<String>) -> Self {
61        Self::Literal(s.into())
62    }
63
64    pub fn rule(s: impl Into<String>) -> Self {
65        Self::Rule(s.into())
66    }
67
68    pub fn category(c: TokenCategory) -> Self {
69        Self::Category(c)
70    }
71}
72
73/// Categories of expected tokens for grouping
74#[derive(Debug, Clone, Copy, PartialEq)]
75pub enum TokenCategory {
76    Expression,
77    Statement,
78    Type,
79    Pattern,
80    Identifier,
81    Literal,
82    Operator,
83    Delimiter,
84}