-
Notifications
You must be signed in to change notification settings - Fork 198
/
TinymoeLexicalAnalyzer.h
126 lines (102 loc) · 2.87 KB
/
TinymoeLexicalAnalyzer.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
#ifndef VCZH_COMPILER_TINYMOELEXICALANALYZER
#define VCZH_COMPILER_TINYMOELEXICALANALYZER
#include "../TinymoeSTL.h"
namespace tinymoe
{
namespace compiler
{
enum class CodeTokenType
{
Integer,
Float,
String,
Identifier,
Module, // module declaration
Using, // import module declaration
Phrase, // phrase declaration
Sentence, // sentence declaration
Block, // block declaration
Symbol, // symbol declaration
Type, // type declaration
CPS, // continuation decoration
Category, // block category definition
Expression, // expression argument
Argument, // argument argument
Assignable, // assignable argument
List, // list
End, // end of an entity
OpenBracket, // (
CloseBracket, // )
Comma, // ,
Colon, // :
Concat, // &
Add, // +
Sub, // -
Mul, // *
Div, // /
IntDiv, // \ (can apply on floats)
Mod, // %
LT, // <
GT, // >
LE, // <=
GE, // >=
EQ, // =
NE, // <>
And, // and
Or, // or
Not, // not
Comment, // -- xxxx
Unknown,
};
struct CodeToken
{
typedef vector<CodeToken> List;
CodeTokenType type = CodeTokenType::Unknown;
int row = -1;
int column = -1;
string_t value;
int codeIndex = -1;
bool IsNameFragmentToken();
static string_t EscapeString(string_t value);
static string_t UnescapeString(string_t str);
};
struct CodeError
{
typedef vector<CodeError> List;
CodeToken position;
string_t message;
};
struct CodeLine
{
typedef shared_ptr<CodeLine> Ptr;
typedef vector<Ptr> List;
CodeToken::List tokens;
};
struct CodeFile
{
typedef shared_ptr<CodeFile> Ptr;
typedef vector<Ptr> List;
CodeLine::List lines;
static CodeFile::Ptr Parse(const string_t& code, int codeIndex, CodeError::List& errors);
};
class CodeFragment
{
public:
CodeFragment();
virtual ~CodeFragment();
};
class SymbolName : public CodeFragment
{
public:
typedef shared_ptr<SymbolName> Ptr;
typedef vector<Ptr> List;
vector<CodeToken> identifiers;
string_t GetName();
string_t GetComposedName();
static bool ConsumeToken(CodeToken::List::iterator& it, CodeToken::List::iterator end, CodeTokenType tokenType, const string_t& content, CodeToken ownerToken, CodeError::List& errors);
static SymbolName::Ptr ParseToEnd(CodeToken::List::iterator it, CodeToken::List::iterator end, const string_t& ownerName, CodeToken ownerToken, CodeError::List& errors);
static SymbolName::Ptr ParseToFarest(CodeToken::List::iterator& it, CodeToken::List::iterator end, const string_t& ownerName, CodeToken ownerToken, CodeError::List& errors);
};
}
}
#endif