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
|
/*
* Copyright (C) 2023 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LEXER_H
#define LEXER_H
#include "string_view.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum
{
// Non-Literal Tokens
TOKEN_NAME,
TOKEN_NUMBER,
TOKEN_EOF,
// Literal Tokens
TOKEN_OPAREN,
TOKEN_CPAREN,
TOKEN_COMMA,
TOKEN_COLON,
TOKEN_SEMICOLON,
TOKEN_OCURLY,
TOKEN_CCURLY,
TOKEN_PLUS,
TOKEN_MINUS,
TOKEN_STAR,
TOKEN_SLASH,
TOKEN_ASSIGN,
TOKEN_EQUAL,
TOKEN_NOT,
TOKEN_NOT_EQUAL,
TOKEN_GT,
TOKEN_GT_EQUAL,
TOKEN_LT,
TOKEN_LT_EQUAL,
TOKEN_AND,
TOKEN_OR,
TOKEN_BITWISE_AND,
TOKEN_BITWISE_OR,
TOKEN_BITWISE_SHIFT_LEFT,
TOKEN_BITWISE_SHIFT_RIGHT,
TOKEN_BITWISE_XOR,
TOKEN_BITWISE_NOT,
TOKEN_KEYWORD_RETURN,
TOKEN_KEYWORD_FN,
TOKEN_KEYWORD_LET,
TOKEN_KEYWORD_IF,
TOKEN_TRUE,
TOKEN_FALSE,
TOKEN_UNKNOWN
} token_kind_t;
typedef struct token_t
{
token_kind_t kind;
string_view_t value;
char *filepath;
uint32_t row;
uint32_t col;
uint32_t bol;
} token_t;
typedef struct lexer_t
{
char *filepath;
char *src;
size_t srclen;
uint32_t cur;
uint32_t row;
uint32_t bol;
} lexer_t;
void
lexer_init(lexer_t *lexer, char *filepath);
void
lexer_next_token(lexer_t *lexer, token_t *token);
void
lexer_load_file_contents(lexer_t *lexer);
char
lexer_current_char(lexer_t *lexer);
bool
lexer_is_eof(lexer_t *lexer);
bool
lexer_is_not_eof(lexer_t *lexer);
void
lexer_drop_char(lexer_t *lexer);
void
lexer_drop_next_token(lexer_t *lexer);
void
lexer_peek_next_token(lexer_t *lexer, token_t *token);
void
lexer_step_back_to(lexer_t *lexer, token_t *token);
void
lexer_lookahead(lexer_t *lexer, token_t *token, size_t level);
char *
token_kind_to_str(token_kind_t kind);
#endif /* LEXER_H */
|