-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
101 lines (86 loc) · 1.24 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
#pragma once
#include <experimental/string_view>
#include <vector>
#include <array>
#include <map>
#include <experimental/optional>
using namespace std::experimental;
enum class TokenType
{
BadToken,
// Keywords
Let,
For,
While,
Return,
If,
Else,
True,
False,
Underscore,
// Symbols
Colon,
LCurly,
RCurly,
LParen,
RParen,
Semicolon,
LSquare,
RSquare,
// Operators
// 1 char
Assign,
//Negate,
Not,
Mult,
Div,
Mod,
Add,
Sub,
Less,
Greater,
Comma,
Call,
// 2 char
Increment,
Decrement,
Equal,
NotEqual,
LessEq,
GreaterEq,
And,
Or,
// Literals
Ident,
Number,
String,
};
std::string& TokenName(TokenType type);
struct TTDetail
{
TokenType type;
std::string str;
};
struct Token
{
std::experimental::string_view text;
TokenType type;
};
class Lexer
{
public:
Lexer(std::string fileContents);
Lexer() = delete;
std::vector<Token> Process();
Token Current();
optional<Token> Advance();
private:
std::experimental::optional<Token> TrySymbolOrOperator();
optional<Token> TryLiteral();
optional<Token> TryIdentOrKeyword();
bool ConsumeWhitespace();
std::string m_fileContents;
std::experimental::string_view v;
int index;
Token current;
};