-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtoken.h
More file actions
418 lines (338 loc) · 9.5 KB
/
token.h
File metadata and controls
418 lines (338 loc) · 9.5 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
The One Programming Language
File: parser/lexer/token.h
_ _
/ \ |\ | |_ Max Base
\_/ | \| |_ Copyright 2021; One Language Contributors
**/
#ifndef _ONE_TOKENIZER_TOKEN_H_
#define _ONE_TOKENIZER_TOKEN_H_
#include <stdlib.h>
#include <stdbool.h>
typedef enum _token_type
{
TOKEN_EOF = 0,
TOKEN_ERROR,
TOKEN_SKIP_WHITESPACE,
TOKEN_SKIP_WHITESPACE_LINE,
TOKEN_SKIP_COMMENT_SINGLE,
TOKEN_SKIP_COMMENT_MULTI,
TOKEN_VALUE_NUMBER,
TOKEN_VALUE_STRING,
TOKEN_VALUE_CHAR,
TOKEN_VALUE_IDENTIFIER,
TOKEN_TYPE_I8,
TOKEN_TYPE_I16,
TOKEN_TYPE_I32,
TOKEN_TYPE_I64,
TOKEN_TYPE_I128,
TOKEN_TYPE_U8,
TOKEN_TYPE_U16,
TOKEN_TYPE_U32,
TOKEN_TYPE_U64,
TOKEN_TYPE_U128,
TOKEN_TYPE_F32,
TOKEN_TYPE_F64,
TOKEN_TYPE_BOOL,
TOKEN_TYPE_STRING,
TOKEN_TYPE_CHAR,
TOKEN_VALUE_BOOL_TRUE,
TOKEN_VALUE_BOOL_FALSE,
TOKEN_OPERATOR_GREATER, // >
TOKEN_OPERATOR_GREATER_EQUAL, // >=
TOKEN_OPERATOR_LESS, // <
TOKEN_OPERATOR_LESS_EQUAL, // <=
TOKEN_OPERATOR_PLUS, // +
TOKEN_OPERATOR_PLUSPLUS, // ++
TOKEN_OPERATOR_MINUS, // -
TOKEN_OPERATOR_MINUSMINUS, // --
TOKEN_OPERATOR_STAR, // *
TOKEN_OPERATOR_POWER, // **
TOKEN_OPERATOR_SLASH, // /
TOKEN_OPERATOR_SLASH_INT, // //
TOKEN_OPERATOR_REMAINDER, // %
TOKEN_OPERATOR_COMMA, // ,
TOKEN_OPERATOR_COLON, // :
TOKEN_OPERATOR_COLONCOLON, // ::
TOKEN_OPERATOR_QUESTION, // ?
TOKEN_OPERATOR_DOT, // .
TOKEN_OPERATOR_DOTDOT, // ..
TOKEN_OPERATOR_DOTDOTDOT, // ...
TOKEN_OPERATOR_SHIFT_LEFT, // <<
TOKEN_OPERATOR_SHIFT_RIGHT, // >>
TOKEN_OPERATOR_AND, // &&
TOKEN_OPERATOR_BITWISE_AND, //
TOKEN_OPERATOR_OR, // ||
TOKEN_OPERATOR_BITWISE_OR, // |
TOKEN_OPERATOR_BITWISE_XOR, // ^
TOKEN_OPERATOR_EQUAL_REMAINDER, // %=
TOKEN_OPERATOR_EQUAL_PLUS, // +=
TOKEN_OPERATOR_EQUAL_MINUS, // -=
TOKEN_OPERATOR_EQUAL_STAR, // *=
TOKEN_OPERATOR_EQUAL_POWER, // **=
TOKEN_OPERATOR_EQUAL_SLASH, // /=
TOKEN_OPERATOR_EQUAL_SLASH_INT, // //=
TOKEN_OPERATOR_EQUAL_SHIFT_LEFT, // <<=
TOKEN_OPERATOR_EQUAL_SHIFT_RIGHT, // >>=
TOKEN_OPERATOR_EQUAL_THREE, // <=>
TOKEN_OPERATOR_EQUAL_AND, // &&=
TOKEN_OPERATOR_EQUAL_BITWISE_AND, // &=
TOKEN_OPERATOR_EQUAL_OR, // ||=
TOKEN_OPERATOR_EQUAL_BITWISE_OR, // |=
TOKEN_OPERATOR_BRACKET_ROUND_LEFT, // (
TOKEN_OPERATOR_BRACKET_ROUND_RIGHT, // )
TOKEN_OPERATOR_NONE, // none
TOKEN_OPERATOR_BRACKET_SQUARE_LEFT, // [
TOKEN_OPERATOR_BRACKET_SQUARE_RIGHT, // ]
TOKEN_OPERATOR_BRACKET_CURLY_LEFT, // {
TOKEN_OPERATOR_BRACKET_CURLY_RIGHT, // }
TOKEN_OPERATOR_BANG, // !
TOKEN_OPERATOR_EQUAL_BANG, // !=
TOKEN_OPERATOR_EQUAL, // =
TOKEN_OPERATOR_EQUAL_EQUAL, // ==
TOKEN_OPERATOR_EQUAL_EQUAL_EQUAL, // ===
TOKEN_SEMICOLON, // ;
TOKEN_PRINT, // _
TOKEN_PRINTNL, // __
TOKEN_PRINTDB, // !_
TOKEN_PRINTDBNL, // !__
TOKEN_IF, // if
TOKEN_ELSE, // else
// TOKEN_WHILE, // while
TOKEN_FOR, // for
// TOKEN_DO, // do
TOKEN_MATCH, // match
TOKEN_RET, // ret
TOKEN_FN, // fn
TOKEN_CONST, // const
TOKEN_FINAL, // final
TOKEN_STATIC, // static
TOKEN_PACKAGE, // package
TOKEN_IMPORT, // import
TOKEN_IN, // in
TOKEN_IS, // is
TOKEN_AS, // as
TOKEN_ASSERT,
TOKEN_STRUCT,
TOKEN_TYPE,
TOKEN_INTERFACE,
TOKEN_SWITCH, // switch
TOKEN_CASE, // case
TOKEN_BREAK, // break
TOKEN_CONTINUE, // continue
TOKEN_GO, // go
// TOKEN_CLASS, // class
// TOKEN_THIS, // this
// TOKEN_SUPER, // super
} TokenType;
typedef struct _location
{
size_t tokens; // index of current token: default is 0
size_t index; // file source index' character: default is 0
size_t line; // line number: default is 1
size_t column; // column number: default is 0
} Location;
typedef struct _token
{
TokenType type;
const char* value;
size_t length;
Location pos;
Location pos_end;
} Token;
typedef struct _keyword
{
const char* identifier;
size_t length;
TokenType type;
bool caseSensitive; // true = lowercase or uppercase not matter, false means full equal `===`
} Keyword;
static Keyword keywords[] = {
{"_", 1, TOKEN_PRINT, false},
{"__", 2, TOKEN_PRINTNL, false},
{"switch", 6, TOKEN_SWITCH, false},
{"break", 5, TOKEN_BREAK, false},
{"continue", 8, TOKEN_CONTINUE, false},
{"case", 4, TOKEN_CASE, false},
{"package", 7, TOKEN_PACKAGE, false},
{"import", 6, TOKEN_IMPORT, false},
{"fn", 2, TOKEN_FN, false},
{"if", 2, TOKEN_IF, false},
{"else", 4, TOKEN_ELSE, false},
// {"do", 2, TOKEN_DO, false},
// {"while", 5, TOKEN_WHILE, false},
{"for", 3, TOKEN_FOR, false},
{"match", 5, TOKEN_MATCH, false},
{"in", 2, TOKEN_IN, false},
{"as", 2, TOKEN_AS, false},
{"is", 2, TOKEN_IS, false},
{"ret", 3, TOKEN_RET, false},
// {"return", 6, TOKEN_RETURN, false},
{"assert", 6, TOKEN_ASSERT, false},
{"struct", 6, TOKEN_STRUCT, false},
{"type", 4, TOKEN_TYPE, false},
{"interface", 9, TOKEN_INTERFACE, false},
{"static", 6, TOKEN_STATIC, false},
{"final", 5, TOKEN_FINAL, false},
{"const", 5, TOKEN_CONST, false},
{"false", 5, TOKEN_VALUE_BOOL_FALSE, false},
{"true", 4, TOKEN_VALUE_BOOL_TRUE, false},
// data types
{"int", 3, TOKEN_TYPE_I32, false},
{"i8", 2, TOKEN_TYPE_I8, false},
{"i16", 3, TOKEN_TYPE_I16, false},
{"i32", 3, TOKEN_TYPE_I32, false},
{"i64", 3, TOKEN_TYPE_I64, false},
{"uint", 3, TOKEN_TYPE_U32, false},
{"u8", 2, TOKEN_TYPE_U8, false},
{"u16", 3, TOKEN_TYPE_U16, false},
{"u32", 3, TOKEN_TYPE_U32, false},
{"u64", 3, TOKEN_TYPE_U64, false},
{"float", 5, TOKEN_TYPE_F32, false},
{"f32", 3, TOKEN_TYPE_F32, false},
{"f64", 3, TOKEN_TYPE_F64, false},
// {"!_", 2, TOKEN_PRINTDB, false},
// {"!__", 3, TOKEN_PRINTDBNL, false},
{NULL, 0, TOKEN_VALUE_IDENTIFIER, false} // Sentinel to mark the end of the array.
};
/*
* @function: token_is_alpha
* @description: check a char is a-z or A-Z or _
* @arguments: char c is a character input
* @return: bool, true or false
*/
bool token_is_alpha(char c);
/*
* @function: token_is_ident
* @description: check a char is a-z or A-Z or _ or 0-9
* @arguments: char c is a character input
* @return: bool, true or false
*/
bool token_is_ident(char c);
/*
* @function: token_is_digit
* @description: check a char is 0-9 (Not - +)
* @arguments: char c is a character input
* @return: bool, true or false
*/
bool token_is_digit(char c);
/*
* @function: token_is_end
* @description: check a char is \0 EOF
* @arguments: char c is a character input
* @return: bool, true or false
*/
bool token_is_end();
/*
* @function: token_recede
* @description: return -1th character of current source code
* @arguments: nothing
* @return: char, if not exists will be ... (TODO)
*/
char token_recede();
/*
* @function: token_recede_next
* @description: go to back character of current source code and return it
* @arguments: nothing
* @return: char, if not exists will be ... (TODO)
*/
char token_recede_next();
/*
* @function: token_recede_next_next
* @description: return current character of current source code and go to back
* @arguments: nothing
* @return: char, if not exists will be ... (TODO)
*/
char token_recede_next_next();
/*
* @function: token_advance
* @description: return current character and go to next position of the source code
* @arguments: nothing
* @return: char
*/
char token_advance();
/*
* @function: token_advance_next
* @description: go to next position of the source code and return it
* @arguments: nothing
* @return: char
*/
char token_advance_next();
/*
* @function: token_peek
* @description: return current character of current source code
* @arguments: nothing
* @return: char
*/
char token_peek();
/*
* @function: token_peek_next
* @description: return next character of current source code
* @arguments: nothing
* @return: char
*/
char token_peek_next();
/*
* @function: token_peek_prev
* @description: return prev character of current source code
* @arguments: nothing
* @return: char
*/
char token_peek_prev();
/*
* @function: token_match
* @description: check current character of lexer is equal to `expected`, it's yes or no
* @arguments: char expected
* @return: bool, true or false
*/
bool token_match(char expected);
/*
* @function: token_make
* @description: create a pointer to Token struct without value
* @arguments: TokenType
* @return: A pointer to Token struct
*/
Token* token_make(TokenType type);
/*
* @function: token_make_value
* @description: create a pointer to Token struct with a value
* @arguments: TokenType and a char* value
* @return: A pointer to Token struct
*/
Token* token_make_value(TokenType type, char* value);
/*
* @function: token_make_error
* @description: create a pointer to a Error Token struct with a error message
* @arguments: char* error message
* @return: A pointer to Token struct
*/
Token* token_make_error(char* message);
/*
* @function: token_name
* @description: convert TokenType to token name as char*
* @arguments: TokenType type
* @return: char*: token name
*/
char* token_name(TokenType type);
/*
* @function: token_utf8_string_length
* @description: strlen while counting the character in utf8 mode not asci mode
* @arguments: char* s
* @return: size_t: number it will be >= 0
*/
size_t token_utf8_string_length(char* s);
/*
* @function: token_is_skip
* @description: check this tokentype is whitespace, comment-inline and multiline comment etc.
* @arguments: TokenType
* @return: bool: 0 or 1, true or false
*/
bool token_is_skip(TokenType type);
/*
* @function: tokenizer_string
* @description: Create a array of tokens from a one program source-code char*
* @inputs: char* of a One program source-code
* @return: Array of Token
*/
Token** tokenizer_string(char* data);
#endif // _ONE_TOKENIZER_TOKEN_H_