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    PlusEq,
86    Minus,
87    MinusEq,
88    Star,
89    StarEq,
90    Slash,
91    Percent,
92    Amp,
93    AmpAmp,
94    Pipe,
95    PipePipe,
96    Caret,
97    Tilde,
98    At,
99
100    Eof,
101}
102
103impl fmt::Display for TokenKind<'_> {
104    /// Shows the token's source text for fixed tokens and a `<placeholder>`
105    /// for tokens whose text varies, e.g. in "expected `;`, found <ident>".
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        let text = match self {
108            Self::StringLit(_) => "<string>",
109            Self::ByteStrLit(_) => "<bytes>",
110            Self::IntLit(_) => "<int>",
111            Self::FloatLit(_) => "<float>",
112            Self::Ident(_) => "<ident>",
113            Self::Keyword(keyword) => return keyword.fmt(f),
114            Self::Underscore => "_",
115            Self::LParen => "(",
116            Self::RParen => ")",
117            Self::LBrace => "{",
118            Self::RBrace => "}",
119            Self::LBracket => "[",
120            Self::RBracket => "]",
121            Self::Semi => ";",
122            Self::Comma => ",",
123            Self::Dot => ".",
124            Self::DotDot => "..",
125            Self::DotDotEq => "..=",
126            Self::ColonColon => "::",
127            Self::Colon => ":",
128            Self::Hash => "#",
129            Self::Arrow => "->",
130            Self::FatArrow => "=>",
131            Self::Question => "?",
132            Self::Eq => "=",
133            Self::EqEq => "==",
134            Self::BangEq => "!=",
135            Self::Bang => "!",
136            Self::Lt => "<",
137            Self::LtEq => "<=",
138            Self::Gt => ">",
139            Self::GtEq => ">=",
140            Self::Plus => "+",
141            Self::PlusEq => "+=",
142            Self::Minus => "-",
143            Self::MinusEq => "-=",
144            Self::Star => "*",
145            Self::StarEq => "*=",
146            Self::Slash => "/",
147            Self::Percent => "%",
148            Self::Amp => "&",
149            Self::AmpAmp => "&&",
150            Self::Pipe => "|",
151            Self::PipePipe => "||",
152            Self::Caret => "^",
153            Self::Tilde => "~",
154            Self::At => "@",
155            Self::Eof => "<eof>",
156        };
157        f.write_str(text)
158    }
159}
160
161#[derive(Debug, Clone, Copy, PartialEq, Eq)]
162pub struct Token<'src> {
163    pub kind: TokenKind<'src>,
164    pub span: Span,
165}
166
167impl<'src> Token<'src> {
168    pub(crate) fn new(kind: TokenKind<'src>, span: Span) -> Self {
169        Self { kind, span }
170    }
171}