1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
use core::fmt;
use internment::Intern;
use crate::utils::span::{Span, Spanned};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Token {
span: Span,
kind: TokenKind,
}
impl Spanned for Token {
#[inline(always)]
fn span(&self) -> Span {
self.span
}
}
impl Token {
pub fn new(span: Span, kind: TokenKind) -> Self {
Self { span, kind }
}
#[inline(always)]
pub fn kind(&self) -> TokenKind {
self.kind
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Keyword {
If,
Else,
Then,
As,
Let,
Struct,
Enum,
Do,
End,
Match,
With,
Or,
And,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Literal {
///Default int literal, may change in the parser based on the type of the variable
Int(i64),
///Default float literal, may change in the parser based on the type of the variable
Float(f64),
Bool(bool),
//At this point, types don't exist in the parser, it's just Identifier
Identifier(Intern<String>),
StringLiteral(Intern<String>),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TokenKind {
/// A literal see [Literal] for more information
Literal(Literal),
/// A keyword see [Keyword] for more information
Keyword(Keyword),
//One character tokens
/// ";"
SemiColon,
/// ","
Comma,
/// "."
Dot,
/// "("
LParen,
/// ")"
RParen,
/// "{"
LBrace,
/// "}"
RBrace,
/// "["
LBracket,
/// "]"
RBracket,
/// "@"
At,
/// "#"
HashTag,
/// "~"
Tilde,
/// "?"
Question,
/// ":"
Colon,
/// "$"
Dollar,
/// "="
Eq,
/// "!"
Bang,
/// "<"
LAngle,
/// ">"
RAngle,
/// "-"
Minus,
/// "&"
Ampersand,
/// "|"
Pipe,
/// "+"
Plus,
/// "*"
Star,
/// "/"
Slash,
/// "^"
Caret,
/// "%"
Percent,
/// "_"
Underscore,
/// "\"
Backslash,
//Two character tokens
/// "=>"
FatArrow,
/// "<-"
LArrow,
/// "->"
RArrow,
/// "::"
DoubleColon,
/// ".."
DoubleDot,
/// "!="
NEq,
/// "<="
LtEq,
/// ">="
GtEq,
/// "=="
DoubleEq,
//Others
/// Represents an unknown character (not supported by the current Lexer)
UnknownChar(char),
/// Start Of File
SOI,
/// End Of File
EOI,
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Keyword::*;
match self {
If => write!(f, "if"),
Else => write!(f, "else"),
Then => write!(f, "then"),
As => write!(f, "as"),
Let => write!(f, "let"),
Struct => write!(f, "struct"),
Enum => write!(f, "enum"),
Do => write!(f, "do"),
End => write!(f, "end"),
Match => write!(f, "match"),
With => write!(f, "with"),
Or => write!(f, "or"),
And => write!(f, "and"),
}
}
}
impl fmt::Display for Literal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Literal::*;
match self {
Int(i) => write!(f, "{}", i),
Float(i) => write!(f, "{}", i),
Bool(i) => write!(f, "{}", i),
Identifier(i) => write!(f, "{}", i),
StringLiteral(i) => write!(f, "{}", i),
}
}
}
impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use TokenKind::*;
match self {
Keyword(k) => write!(f, "Keyword: {}", k),
Literal(l) => write!(f, "Literal: {}", l),
UnknownChar(c) => write!(f, "UnknownChar: {}", c),
_ => write!(f, "{:?}", self),
}
}
}