-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenizer.hpp
80 lines (59 loc) · 1.73 KB
/
tokenizer.hpp
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
#ifndef _TOKENIZER_HPP_
#define _TOKENIZER_HPP_
#include "token.hpp"
#include <vector>
#include <cstdint>
#include <string_view>
#include <unordered_map>
namespace pl0::tokenizer {
using namespace token;
class Tokenizer final {
public:
explicit Tokenizer(std::string_view source)
: m_source(source) {}
auto tokenize() -> std::vector<Token>;
private:
auto scanToken() -> void;
inline auto advance() -> char {
return !isAtEnd()
? m_source[m_curr++]
: '\0';
}
inline auto match(char c) -> bool {
if(peek() == c) {
return advance(), true;
}
return false;
}
constexpr auto isAtEnd() const -> bool {
return m_curr >= m_source.length();
}
constexpr auto peek() const -> char {
return m_source[m_curr];
}
inline auto makeToken(TokenType type) -> void {
const auto lexemeLength = m_curr - m_start;
m_tokens.emplace_back(type, m_source.substr(m_start, lexemeLength), m_line);
}
private:
std::string_view m_source;
std::vector<Token> m_tokens;
const std::unordered_map<std::string_view, TokenType> m_keywords = {
{"const", TokenType::ConstKeyword},
{"var", TokenType::VarKeyword},
{"procedure", TokenType::ProcedureKeyword},
{"call", TokenType::CallKeyword},
{"begin", TokenType::BeginKeyword},
{"end", TokenType::EndKeyword},
{"if", TokenType::IfKeyword},
{"then", TokenType::ThenKeyword},
{"while", TokenType::WhileKeyword},
{"do", TokenType::DoKeyword},
{"odd", TokenType::OddKeyword}
};
std::uint32_t m_curr = 0;
std::uint32_t m_start = 0;
std::uint32_t m_line = 1;
};
}
#endif