-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.h
113 lines (96 loc) · 1.73 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
#pragma once
#include <cstddef>
#include <string>
#include <vector>
#include <unordered_map>
#include <utility>
#include <stdexcept>
struct Token;
enum class CharType;
enum class TokenType;
enum class State;
struct pair_hash;
class Lexer
{
using MapInType = std::pair<State,CharType>;
using MapOUtType = std::pair<State,TokenType>;
public:
void runLexer(const std::string & input);
std::vector<Token> getTokens(void);
Lexer();
~Lexer() = default;
private:
std::vector<Token> mTokens;
static const std::unordered_map< Lexer::MapInType, Lexer::MapOUtType, pair_hash > mLexerTable;
size_t mNumLine;
size_t mColumn;
void clear(void);
CharType charType(char ch);
void error(const std::string & info);
void updatePosition(const State &nowState);
};
enum class TokenType
{
NO_TOKEN, // in lexer table, sometimes there is no token for output
BAD_TOKEN,
NUMBER,
ADD,
MUL,
DIV,
SUB,
LEFT_PAR,
RIGHT_PAR,
ASSIGN,
VAR,
ENDL
};
enum class State
{
INIT,
ADD,
SUB,
MUL,
DIV,
LP,
RP,
NUM_INT,
POINT_FNT, // point at front
POINT_NML, // point after some \d
NUM_DEC,
VAR,
ASN, // =
ENDL,
OVER
};
enum class CharType
{
END,
SPACE,
ENDL,
ADD,
SUB,
MUL,
DIV,
LP,
RP,
NUM,
POINT,
WORD,
ASN
};
struct Token
{
TokenType mTokenType;
std::string mValue;
std::size_t mLine;
std::size_t mColumn;
};
struct pair_hash
{
std::size_t operator () (const std::pair<State,CharType > &p) const
{
auto h1 = std::hash<State>{}(p.first);
auto h2 = std::hash<CharType>{}(p.second);
return h1 ^ h2;
}
};