-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.hpp
79 lines (63 loc) · 1.3 KB
/
lexer.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
#pragma once
#include <string>
#include <functional>
#include <optional>
#include <fstream>
struct TextSpan {
unsigned long long begin;
unsigned long long end;
TextSpan(unsigned long long begin, unsigned long long end);
};
enum TokenKind {
Dot,
Comma,
LeftAngledBracket,
RightAngledBracket,
LeftBracket,
RightBracket,
Plus,
Minus,
Comment,
EndOfFile
};
class Token {
public:
const TextSpan span;
const TokenKind type;
const char value;
Token(const TextSpan& span, TokenKind type, char value);
};
class TokenStream {
public:
auto lookahead() -> Token;
auto next() -> Token;
explicit TokenStream(std::function<Token()> supplier);
private:
const std::function<Token()> supplier;
std::optional<Token> stash;
};
class Lexer {
public:
auto lex() -> TokenStream;
protected:
unsigned long long position{};
virtual auto supply() -> char = 0;
};
class TextLexer : public Lexer {
public:
explicit TextLexer(std::string text);
private:
const std::string text;
auto supply() -> char override;
};
class FileLexer : public Lexer {
public:
explicit FileLexer(std::ifstream stream);
~FileLexer() {
stream.close();
}
private:
std::ifstream stream;
bool done;
auto supply() -> char override;
};