Skip to main content

mysz_core/lex/
lexing.rs

1use crate::utils::location::Location;
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum TokenType {
5    // boolish
6    Equals,
7    NotEquals,
8    LessThan,
9    GreaterThan,
10    LessThanEquals,
11    GreaterThanEquals,
12    Or,
13    And,
14
15    // generic signs
16    Assign,
17    LParen,
18    RParen,
19    LBrace,
20    RBrace,
21    SemiColon,
22    Colon,
23    DoubleColon,
24    Period,
25    Comma,
26    LBracket,
27    RBracket,
28
29    // pointers
30    Ampersand,
31    Star,
32
33    // maths signs
34    Add,      // +
35    Minus,    // -
36    Divide,   // /
37    Multiply, // *
38    Modulo,   // %
39    Not,      // !
40
41    // boolean values
42    True,
43    False,
44
45    // keywords
46    VarKeyword,
47    IfKeyword,
48    ElseKeyword,
49    WhileKeyword,
50    FnKeyword,
51    PubKeyword,
52    ReturnKeyword,
53    ExternKeyword,
54    ForKeyword,
55    UseKeyword,
56    StructKeyword,
57    SizeOfKeyword,
58    BreakKeyword,
59    ConstKeyword,
60    AsKeyword,
61    ElseIfKeyword,
62
63    // identifier
64    Identifier,
65
66    // literals
67    IntLiteral,
68    CharLiteral,
69    StringLiteral,
70
71    // when lexing, parsing, etc fails.
72    Niltoken,
73}
74
75#[derive(Clone, Debug)]
76pub struct Token {
77    pub ttype: TokenType,
78    pub location: Location,
79    pub value: String,
80}
81impl std::fmt::Display for Token {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(
84            f,
85            "Token {{ Type: {:?}, Location: ({}, {}), Value: {} }}",
86            self.ttype, self.location.line, self.location.col, self.value
87        )
88    }
89}