This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
forked from ocen-lang/ocen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.oc
197 lines (181 loc) · 3.73 KB
/
tokens.oc
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
//* Definitions for Tokens
import std::span::{ Span, Location }
import std::libc::calloc
struct Token {
type: TokenType
span: Span
text: str
suffix: &Token
seen_newline: bool
//* Comment occuring *before* this token
comment: str
comment_loc: Location
}
def Token::new(type: TokenType, span: Span, text: str): &Token {
let tok = calloc(1, sizeof(Token)) as &Token
*tok = Token(type, span, text, suffix: null, seen_newline: false, comment: null, comment_loc: Location::default())
return tok
}
def Token::from_type(type: TokenType, span: Span): &Token => Token::new(type, span, "")
def Token::from_ident(text: str, span: Span): &Token {
let type = TokenType::from_text(text)
return Token::new(type, span, text)
}
def Token::str(&this): str => `{.span.str()}: {.type.str()}`
def Token::is_word(this): bool => match .type {
TokenType::Identifier => true
else => .type as u64 > TokenType::BEGIN_KEYWORDS as u64
}
enum TokenType {
// Other tokens
AtSign
Ampersand
Backtick
Caret
CharLiteral
CloseCurly
CloseParen
CloseSquare
Colon
ColonColon
Comma
Dot
EOF
Equals
EqualEquals
Exclamation
FatArrow
FloatLiteral
FormatStringLiteral
GreaterThan
GreaterThanEquals
Identifier
IntLiteral
LessThan
LessThanEquals
Line
Minus
MinusEquals
MinusMinus
NotEquals
OpenCurly
OpenParen
OpenSquare
Percent
Plus
PlusEquals
PlusPlus
Question
Semicolon
Slash
SlashEquals
Star
StarEquals
StringLiteral
Tilde
//* Not an actual token, just here for convenience
Newline
BEGIN_KEYWORDS
// Keywords
And
As
Assert
Break
Const
Continue
Def
Defer
Else
Enum
Extern
False
For
Fn
If
Let
Match
Namespace
Null
Not
Or
Return
SizeOf
Struct
True
Then
Union
Import
Void
Yield
While
}
def TokenType::from_text(text: str): TokenType => match text {
"and" => TokenType::And
"as" => TokenType::As
"assert" => TokenType::Assert
"break" => TokenType::Break
"const" => TokenType::Const
"continue" => TokenType::Continue
"def" => TokenType::Def
"defer" => TokenType::Defer
"else" => TokenType::Else
"enum" => TokenType::Enum
"extern" => TokenType::Extern
"false" => TokenType::False
"for" => TokenType::For
"fn" => TokenType::Fn
"if" => TokenType::If
"let" => TokenType::Let
"match" => TokenType::Match
"namespace" => TokenType::Namespace
"not" => TokenType::Not
"null" => TokenType::Null
"or" => TokenType::Or
"return" => TokenType::Return
"sizeof" => TokenType::SizeOf
"struct" => TokenType::Struct
"true" => TokenType::True
"then" => TokenType::Then
"union" => TokenType::Union
"import" => TokenType::Import
"void" => TokenType::Void
"yield" => TokenType::Yield
"while" => TokenType::While
else => TokenType::Identifier
}
def TokenType::str(this): str => match this {
// Keywords
And => "and"
As => "as"
Assert => "assert"
Break => "break"
Const => "const"
Continue => "continue"
Def => "def"
Defer => "defer"
Else => "else"
Enum => "enum"
Extern => "extern"
False => "false"
For => "for"
Fn => "fn"
If => "if"
Let => "let"
Match => "match"
Namespace => "namespace"
Not => "not"
Null => "null"
Or => "or"
Return => "return"
SizeOf => "sizeof"
Struct => "struct"
True => "true"
Then => "then"
Union => "union"
Import => "import"
Void => "void"
Yield => "yield"
While => "while"
// Others
else => .dbg()
}