-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
165 lines (136 loc) · 3.18 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
#pragma once
#include "variant.hpp"
#include <deque>
#include <istream>
#include <optional>
#include <ostream>
#include <string>
class Lexer;
struct Token {
virtual ~Token() = default;
enum Tag {
Double,
Integer,
String,
EscapedString,
Newline,
Eof,
Label,
Extern,
Ellipsis,
As,
Sub,
Function,
Variable,
Dim,
IntegerType,
StringType,
DoubleType,
Not,
While,
Wend,
Do,
Loop,
Until,
For,
To,
Step,
Next,
If,
Then,
Else,
Print,
Goto,
Input,
Let,
Gosub,
Return,
End,
Rem,
And,
Or,
Lte,
Gte,
Neq,
Lt,
Gt,
Eq,
Plus,
Minus,
Divide,
Multiply,
Exp,
Comma,
LParens,
RParens,
MaxTag
};
Token() = default;
explicit Token(Lexer* lexer, Tag tag) : Token(lexer, tag, std::string(), NumValue()) {}
explicit Token(Lexer* lexer, Tag tag, std::string extra)
: Token(lexer, tag, std::move(extra), NumValue()) {}
template <typename T,
typename = std::enable_if_t<!std::is_same<T, std::string>::value &&
!std::is_same<T, const char*>::value>>
explicit Token(Lexer* lexer, Tag tag, T extra)
: Token(lexer, tag, std::string(), std::move(extra)) {}
bool isEnding() const {
return tag == Newline || tag == Eof;
}
bool isFunctionDef() const {
return tag == Sub || tag == Extern || tag == Function;
}
Tag tag = MaxTag;
using NumValue = mpark::variant<int64_t, double, char>;
std::optional<NumValue> value;
std::string strValue;
std::shared_ptr<std::string> line;
int startPos;
int endPos;
int lineNumber;
private:
explicit Token(Lexer* lexer, Tag value, std::string strValue, NumValue numValue);
};
std::ostream& operator<<(std::ostream& stream, const Token& token);
class Lexer;
class LexerError : public std::exception {
public:
LexerError(Lexer* lexer, const char* message);
const char* what() const noexcept override {
return _message.c_str();
}
private:
std::string _message;
};
class Lexer {
public:
explicit Lexer(std::istream* stream) : _line(std::make_shared<std::string>()), _stream(stream) {
stream->exceptions(std::ios_base::badbit);
}
Token lex();
int lineCount() const {
return _lineCount;
}
void putBack(Token tok) {
_putBackTokens.push_back(std::move(tok));
}
Token peek() {
auto ret = _lex();
putBack(ret);
return ret;
}
private:
Token _lex();
friend class LexerError;
friend struct Token;
Token _extractNumber();
long _extractInt(int base = 10);
std::string _extractEscapedString(char endAt);
std::shared_ptr<std::string> _line;
std::istream* _stream;
int _lineCount = 0;
bool _seenToken = false;
size_t _lastPos;
size_t _pos;
std::deque<Token> _putBackTokens;
};