-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
93 lines (63 loc) · 1.69 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
#pragma once
#include <string>
#include <string_view>
enum class TokenType {
OpenCurlyBrace, /* { */
CloseCurlyBrace, /* } */
OpenBrace, /* [ */
CloseBrace, /* ] */
Colon, /* : */
Comma, /* , */
StringLiteral,
NumberLiteral,
BooleanTrue, /* true */
BooleanFalse, /* false */
Null, /* null */
EndOfFile,
Garbage,
};
auto token_to_string(TokenType) -> std::string_view;
class Token {
public:
Token() = default;
Token(TokenType type)
: m_type(type) {}
Token(TokenType type, std::string lexeme)
: m_type(type), m_lexeme(std::move(lexeme)) {}
Token(Token&& other)
: m_type(other.m_type), m_lexeme(std::move(other.m_lexeme))
{
other.m_type = TokenType::Garbage;
}
auto operator=(const Token& other) -> Token& = delete;
auto operator=(Token&& other) -> Token& {
m_type = other.m_type;
m_lexeme = std::move(other.m_lexeme);
other.m_type = TokenType::Garbage;
return *this;
}
auto to_string() const -> std::string;
auto type() const -> TokenType;
auto lexeme() const -> const std::string&;
private:
TokenType m_type{TokenType::Garbage};
std::string m_lexeme;
};
class JsonLexer {
public:
JsonLexer(std::string_view input)
: m_input(input) {}
auto is_eof() const -> bool;
auto get_token() -> Token;
private:
auto get_boolean() -> Token;
auto get_number_literal() -> Token;
auto get_string_literal() -> Token;
auto get_null() -> Token;
auto current() const -> char;
auto advance() -> void;
auto skip_whitespaces() -> void;
private:
std::size_t m_cursor{0};
std::string_view m_input;
};