-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
83 lines (73 loc) · 1.49 KB
/
token.h
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
#pragma once
#include <string>
#include <variant>
#include <fmt/core.h>
#include <magic_enum.hpp>
enum class TokenType
{
// Single char tokens
LeftParen,
RightParen,
LeftBrace,
RightBrace,
Comma,
Dot,
Minus,
Plus,
Semicolon,
Slash,
Star,
// One or two char
Bang,
BangEqual,
Equal,
EqualEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
// Literals
Identifier,
String,
Number,
// Keywords
And,
Class,
Else,
False,
True,
Function,
For,
If,
Nil,
Or,
Print,
Return,
Super,
This,
Var,
While,
LEOF,
};
using TokenLiteral = std::variant<std::string, double>;
struct Token
{
TokenType type;
std::string lexeme;
int line;
int start;
int end;
TokenLiteral literal = {};
Token(TokenType type, std::string lexeme, int line, int start, int end, std::string literal)
: type(type), lexeme(lexeme), line(line), start(start), end(end), literal(literal) {}
Token(TokenType type, std::string lexeme, int line, int start, int end, double literal)
: type(type), lexeme(lexeme), line(line), start(start), end(end), literal(literal) {}
Token(TokenType type, std::string lexeme, int line, int start, int end)
: type(type), lexeme(lexeme), line(line), start(start), end(end) {}
Token() {}
std::string to_string()
{
std::string token_string = fmt::format("token: {}\nline: {}\ntype: {}\nstart: {}\nend: {}\n", lexeme, std::to_string(line), magic_enum::enum_name(type), start, end);
return token_string;
}
};