-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
59 lines (50 loc) · 1.07 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
#ifndef LEXER_H
#define LEXER_H
#include <string>
#include <vector>
#include <unordered_set>
// Token type enumeration
enum class TokenType {
Keyword,
Identifier,
Number,
String,
Operator,
Symbol,
EndOfFile,
Unknown
};
// Token structure
struct Token {
TokenType type;
std::string value;
int line;
int column;
Token(TokenType t, const std::string& val, int ln, int col)
: type(t), value(val), line(ln), column(col) {}
void print() const;
};
// Lexer class
class Lexer {
private:
std::string source;
size_t position = 0;
int line = 1;
int column = 1;
std::unordered_set<std::string> keywords = {
"let", "if", "else", "for", "while", "func", "struct", "return", "display"
};
public:
Lexer(const std::string& input);
std::vector<Token> tokenize();
Token nextToken();
private:
bool isAtEnd() const;
char advance();
char peek() const;
void skipWhitespace();
Token parseNumber();
Token parseIdentifierOrKeyword();
Token parseString();
};
#endif // LEXER_H