-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
86 lines (68 loc) · 1.6 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
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
static std::string input;
static char peek;
static size_t idx;
enum class TokenType {
INTEGER,
REAL,
PAREN,
OPERRATOR
};
class Token {
public:
Token() = default;
Token(const std::string lexme, const TokenType type): lexme_(lexme), type_(type) {}
Token(const Token &other): lexme_(other.lexme_), type_(other.type_) {}
inline std::string GetLexme() { return lexme_; }
inline TokenType GetType() { return type_; }
Token &operator=(const Token &rhs) {
lexme_ = rhs.lexme_;
type_ = rhs.type_;
return *this;
}
inline std::string ToString() {
std::ostringstream os;
os << "[lexme: " << lexme_ << ", "
<< "type: ";
if (type_ == TokenType::INTEGER) {
os << "INTEGER";
} else if (type_ == TokenType::REAL) {
os << "REAL";
} else if (type_ == TokenType::PAREN) {
os << "PAREN";
} else if (type_ == TokenType::OPERRATOR) {
os << "OPERATOR";
}
os << "]";
return os.str();
}
private:
std::string lexme_;
TokenType type_;
};
class Lexer {
public:
Lexer() = default;
Lexer(std::string filename) {
ifs.open(filename, std::ios::binary | std::ios::in);
if (!ifs.is_open()) {
fprintf(stderr, "can not open file: \"%s\" \n", filename.c_str());
}
}
inline void ReadCh() { peek = input[idx++]; }
inline bool ReadCh(char c) {
ReadCh();
if (peek != c) return false;
peek = ' ';
return true;
}
std::istream &ReadInput();
Token Scan();
private:
std::ifstream ifs;
// std::vector<Token> tokens_;
};