rusty_zig/tokenizer/
token.rs

1#[derive(Debug)]
2pub struct Pos {
3    start: usize,
4    end: usize,
5    line: usize,
6    // lexeme: String,
7}
8
9impl Pos {
10    pub fn new(start: usize, end: usize, line: usize) -> Self {
11        Self { start, end, line }
12    }
13}
14
15#[derive(Debug)]
16pub enum TokenType {
17    /// +
18    Plus,
19    
20    /// -
21    Minus,
22    
23    /// *
24    Star,
25
26    /// /
27    Slash,
28    
29    /// %
30    Percent,
31
32    /// (
33    LParen,
34    
35    /// )
36    RParen,
37    
38    /// [
39    LSquare,
40    
41    /// ]
42    RSquare,
43    
44    /// {
45    LCurly,
46    
47    /// }
48    RCurly,
49
50
51    Identifier(String),
52    Integer(usize),
53    String(String),
54    Float(f64),
55    Builtin(String),
56    Keyword(KeywordType),
57    EOF,
58}
59
60#[derive(Debug)]
61pub enum KeywordType {
62    AddrSpace,
63    Align,
64    AllowZero,
65    And,
66    AnyFrame,
67    AnyType,
68    Asm,
69    Async,
70    Await,
71    Break,
72    CallConv,
73    Catch,
74    Comptime,
75    Const,
76    Continue,
77    Defer,
78    Else,
79    Enum,
80    Errdefer,
81    Error,
82    Export,
83    Extern,
84    Fn,
85    For,
86    If,
87    Inline,
88    LinkSection,
89    NoAlias,
90    NoSuspend,
91    Opaque,
92    Or,
93    OrElse,
94    Packed,
95    Pub,
96    Resume,
97    Return,
98    Struct,
99    Suspend,
100    Switch,
101    Test,
102    ThreadLocal,
103    Try,
104    Union,
105    Unreachable,
106    UsingNamespace,
107    Var,
108    Volatile,
109    While,
110}
111
112#[derive(Debug)]
113pub struct Token(Pos, TokenType);
114
115impl Token {
116    pub fn new(diag: Pos, r#type: TokenType) -> Self {
117        Self(diag, r#type)
118    }
119
120    pub fn is_keyword(&self) -> bool {
121        matches!(self.1, TokenType::Keyword(_))
122    }
123
124    pub fn is_int(&self) -> bool {
125        matches!(self.1, TokenType::Integer(_))
126    }
127}