Skip to main content

xur_parser/
token.rs

1use std::fmt;
2
3use strum::{Display, EnumString};
4
5use xur_span::Span;
6
7/// All reserved keywords in Xur.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumString)]
9#[strum(serialize_all = "lowercase")]
10pub enum Keyword {
11    Use,
12    Pub,
13    Fn,
14    Let,
15    Const,
16    Return,
17    If,
18    Else,
19    While,
20    For,
21    In,
22    Loop,
23    Break,
24    Continue,
25    Struct,
26    Enum,
27    Impl,
28    Match,
29    True,
30    False,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum TokenKind<'src> {
35    // --- Literals (zero-copy slices of the source) ---
36    /// The raw text between the quotes; escape sequences are not yet resolved.
37    StringLit(&'src str),
38    /// The digits as written, including `_` separators. Always non-negative:
39    /// a leading `-` is lexed as a separate `Minus`.
40    IntLit(&'src str),
41    /// The literal as written, e.g. `1_000.5`.
42    FloatLit(&'src str),
43
44    // --- Identifiers & keywords ---
45    Ident(&'src str),
46    Keyword(Keyword),
47    /// A lone `_`, used as a wildcard pattern. `_name` is an `Ident`.
48    Underscore,
49
50    // --- Delimiters ---
51    LParen,
52    RParen,
53    LBrace,
54    RBrace,
55    LBracket,
56    RBracket,
57
58    // --- Punctuation ---
59    Semi,
60    Comma,
61    Dot,
62    DotDot,
63    DotDotEq,
64    ColonColon,
65    Colon,
66    Hash,
67    Arrow,
68    FatArrow,
69    Question,
70
71    // --- Operators ---
72    Eq,
73    EqEq,
74    BangEq,
75    Bang,
76    Lt,
77    LtEq,
78    Gt,
79    GtEq,
80    Plus,
81    Minus,
82    Star,
83    Slash,
84    Percent,
85    AmpAmp,
86    Pipe,
87    PipePipe,
88
89    Eof,
90}
91
92impl fmt::Display for TokenKind<'_> {
93    /// Shows the token's source text for fixed tokens and a `<placeholder>`
94    /// for tokens whose text varies, e.g. in "expected `;`, found <ident>".
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        let text = match self {
97            Self::StringLit(_) => "<string>",
98            Self::IntLit(_) => "<int>",
99            Self::FloatLit(_) => "<float>",
100            Self::Ident(_) => "<ident>",
101            Self::Keyword(keyword) => return keyword.fmt(f),
102            Self::Underscore => "_",
103            Self::LParen => "(",
104            Self::RParen => ")",
105            Self::LBrace => "{",
106            Self::RBrace => "}",
107            Self::LBracket => "[",
108            Self::RBracket => "]",
109            Self::Semi => ";",
110            Self::Comma => ",",
111            Self::Dot => ".",
112            Self::DotDot => "..",
113            Self::DotDotEq => "..=",
114            Self::ColonColon => "::",
115            Self::Colon => ":",
116            Self::Hash => "#",
117            Self::Arrow => "->",
118            Self::FatArrow => "=>",
119            Self::Question => "?",
120            Self::Eq => "=",
121            Self::EqEq => "==",
122            Self::BangEq => "!=",
123            Self::Bang => "!",
124            Self::Lt => "<",
125            Self::LtEq => "<=",
126            Self::Gt => ">",
127            Self::GtEq => ">=",
128            Self::Plus => "+",
129            Self::Minus => "-",
130            Self::Star => "*",
131            Self::Slash => "/",
132            Self::Percent => "%",
133            Self::AmpAmp => "&&",
134            Self::Pipe => "|",
135            Self::PipePipe => "||",
136            Self::Eof => "<eof>",
137        };
138        f.write_str(text)
139    }
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub struct Token<'src> {
144    pub kind: TokenKind<'src>,
145    pub span: Span,
146}
147
148impl<'src> Token<'src> {
149    pub(crate) fn new(kind: TokenKind<'src>, span: Span) -> Self {
150        Self { kind, span }
151    }
152}