-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
169 lines (137 loc) · 3.63 KB
/
lexer.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
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
//
// Created by fathi on 10/19/2024.
//
#ifndef TIGE_LEXER_H
#define TIGE_LEXER_H
#include <stddef.h>
#include <stdint.h>
#include "error.h"
// Token types
typedef enum {
TOKEN_EOF = 0,
TOKEN_IDENTIFIER,
TOKEN_INTEGER,
TOKEN_FLOAT, // Added float token type
TOKEN_STRING,
TOKEN_OPERATOR,
TOKEN_SEMICOLON,
TOKEN_COLON,
TOKEN_COMMA,
TOKEN_DOT,
TOKEN_DOTDOT, // ..
TOKEN_LPAREN, // (
TOKEN_RPAREN, // )
TOKEN_LBRACE, // {
TOKEN_RBRACE, // }
TOKEN_EQUALS, // =
TOKEN_PLUS, // +
TOKEN_MINUS, // -
TOKEN_ASTERISK, // *
TOKEN_SLASH, // /
TOKEN_GT, // >
TOKEN_LT, // <
TOKEN_GTE, // >=
TOKEN_LTE, // <=
TOKEN_EQ, // ==
TOKEN_NEQ, // !=
TOKEN_QUESTION, // ?
TOKEN_BANG,
TOKEN_SCOPE, // ::
TOKEN_AND, // && or AND
TOKEN_OR, // || or OR
TOKEN_TRUE,
TOKEN_FALSE,
// Keywords
TOKEN_CLASS,
TOKEN_PUBLIC,
TOKEN_PRIVATE,
TOKEN_NAMESPACE,
TOKEN_FN,
TOKEN_LET,
TOKEN_IF,
TOKEN_ELSE,
TOKEN_FOR,
TOKEN_IN,
TOKEN_LOOP,
TOKEN_BREAK,
TOKEN_RETURN,
TOKEN_THIS,
TOKEN_ERROR,
TOKEN_NONE,
} TokenType;
// Token structure
typedef struct {
TokenType type;
union {
char *str_value;
int64_t int_value;
double float_value;
bool bool_value;
void *value;
};
int line;
int column;
} Token;
// Lexer structure
typedef struct {
const char *source;
size_t source_len;
size_t position;
size_t line;
size_t column;
char current;
ErrorList* error_list;
} Lexer;
typedef struct TokenList {
Token **tokens;
size_t capacity;
size_t size;
size_t current_index;
} TokenList;
TokenList *token_list_create(size_t initial_capacity);
void token_list_add(TokenList *list, Token *token);
void token_list_free(TokenList *list);
Token* token_list_current(TokenList* list);
Token* token_list_at(TokenList* list, size_t index);
Token* token_list_next(TokenList* list);
Token* token_list_prev(TokenList* list);
void lexer_init(Lexer *lexer, const char *source);
bool lexer_is_initialized(Lexer *lexer);
Token *lex(Lexer *lexer);
const char *token_type_to_string(TokenType type);
void token_free(Token *token);
int token_is_type(const Token *token, TokenType type);
Token *create_token(TokenType type, const char *value, int line, int column);
Token *token_create_string(TokenType type, const char *value, int line, int column);
Token *token_create_int(TokenType type, long long value, int line, int column);
Token *token_create_float(TokenType type, double value, int line, int column);
// Error handling structure
typedef struct {
int line;
int column;
char *message;
} LexerError;
const LexerError *lexer_get_error(const Lexer *lexer);
// Debug functions
#ifdef LEXER_DEBUG
void token_print(const Token *token);
char *lexer_get_state(const Lexer *lexer);
#endif
// Token type categories for easier classification
static inline int token_is_operator(TokenType type) {
return type >= TOKEN_PLUS && type <= TOKEN_NEQ;
}
static inline int token_is_keyword(TokenType type) {
return type >= TOKEN_CLASS && type <= TOKEN_THIS;
}
static inline int token_is_literal(TokenType type) {
return type == TOKEN_INTEGER || type == TOKEN_FLOAT || type == TOKEN_STRING;
}
static inline int token_is_numeric(TokenType type) {
return type == TOKEN_INTEGER || type == TOKEN_FLOAT;
}
// Maximum lengths and limits
#define MAX_IDENTIFIER_LENGTH 255
#define MAX_STRING_LENGTH 4096
#define MAX_NUMBER_LENGTH 100
#endif // TIGE_LEXER_H