-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.h
131 lines (126 loc) · 2.95 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
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
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include <ostream>
#include "Utils.h"
namespace lwscript
{
enum class TokenKind
{
NUMBER = 0, // number literal,like 100
STRING, // string literal,like "AABB"
CHARACTER, // character literal,like 'A'
IDENTIFIER, // variable like A
DOT, // .
ELLIPSIS, // ...
COMMA, // ,
COLON, // :
SEMICOLON, // ;
LBRACKET, // [
RBRACKET, // ]
LBRACE, // {
RBRACE, // }
LPAREN, // (
RPAREN, // )
PLUS, // +
MINUS, // -
ASTERISK, // *
SLASH, // /
PERCENT, // %
EQUAL, // =
LESS, // <
GREATER, // >
AMPERSAND, // &
VBAR, // |
CARET, // ^
TILDE, // ~
QUESTION, // ?
BANG, // !
LPAREN_LBRACE, // ({
RBRACE_RPAREN, // })
LESS_LESS, // <<
GREATER_GREATER, // >>
PLUS_PLUS, // ++
MINUS_MINUS, // --
PLUS_EQUAL, // +=
MINUS_EQUAL, // -=
ASTERISK_EQUAL, // *=
SLASH_EQUAL, // /=
PERCENT_EQUAL, // %=
AMPERSAND_EQUAL, // &=
VBAR_EQUAL, // |=
CARET_EQUAL, // ^=
LESS_LESS_EQUAL, // <<=
GREATER_GREATER_EQUAL, // >>=
EQUAL_EQUAL, // ==
LESS_EQUAL, // <=
GREATER_EQUAL, // >=
BANG_EQUAL, // !=
AMPERSAND_AMPERSAND, // &&
VBAR_VBAR, // ||
LET, // let
IF, // if
ELSE, // else
TRUE, // true
FALSE, // false
NIL, // null
WHILE, // while
FOR, // for
FUNCTION, // fn
CLASS, // class
STRUCT, // struct
THIS, // this
BASE, // base
RETURN, // return
STATIC, // static
CONST, // const
BREAK, // break
CONTINUE, // continue
IMPORT, // import
MODULE, // module
SWITCH, // switch
DEFAULT, // default
MATCH, // match
ENUM, // enum
U8, // u8
U16, // u16
U32, // u32
U64, // u64
I8, // i8
I16, // i16
I32, // i32
I64, // i64
F32, // f32
F64, // f64
BOOL, // bool
CHAR, // char
ANY, // any
VOID, // void
AS, // as
NEW, // new
END,
};
struct SourceLocation
{
uint64_t line{1};
uint64_t column{1};
uint64_t pos{1};
};
struct LWSCRIPT_API Token
{
Token() : kind(TokenKind::END), literal(TEXT("")) {}
Token(TokenKind kind, STD_STRING_VIEW literal, const SourceLocation &srcLoc) : kind(kind), literal(literal), sourceLocation(srcLoc) {}
STD_STRING ToString() const
{
return TEXT("\"") + literal + TEXT("\"(") + TO_STRING(sourceLocation.line) + TEXT(",") + TO_STRING(sourceLocation.line) + TEXT(")");
}
TokenKind kind;
STD_STRING literal;
SourceLocation sourceLocation;
};
inline STD_OSTREAM &operator<<(STD_OSTREAM &stream, const Token &token)
{
return stream << token.ToString();
}
}