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