-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
59 lines (55 loc) · 1.41 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
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "ost/token.h"
struct Lexer
{
Lexer(std::string source) : source(source) {}
~Lexer() {}
std::vector<Token> scan_tokens();
static bool is_digit(char);
static bool is_alpha(char);
static bool is_alphanumeric(char);
private:
std::string source;
std::vector<Token> tokens{};
int start = 0;
int current = 0;
int line = 0;
std::unordered_map<std::string, TokenType> keywords = {
{"and", TokenType::And},
{"class", TokenType::Class},
{"else", TokenType::Else},
{"false", TokenType::False},
{"for", TokenType::For},
{"function", TokenType::Function},
{"if", TokenType::If},
{"nil", TokenType::Nil},
{"or", TokenType::Or},
{"print", TokenType::Print},
{"return", TokenType::Return},
{"super", TokenType::Super},
{"this", TokenType::This},
{"true", TokenType::True},
{"var", TokenType::Var},
{"while", TokenType::While},
};
private:
void scan_token(char c);
void add_token(TokenType);
template <typename T>
void add_token(TokenType, T);
void consume_token(TokenType);
template <typename T>
void consume_token(TokenType, T);
void consume_string();
void consume_number();
void consume_identifier();
void consume_eof();
bool is_at_end();
void advance();
bool match_and_advance(char);
char peek();
char peek_next();
};