-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
171 lines (165 loc) · 3.9 KB
/
lexer.cpp
File metadata and controls
171 lines (165 loc) · 3.9 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "lexer.h"
wstring token_type(const wstring& str)
{
if (isspecsymbol(str)) return L"spec symbol";
else if (isnumber(str)) return L"number";
else if (isstring(str)) return L"string literal";
else if (isRAWstring(str)) return L"raw string literal";
else if (isFORMATstring(str)) return L"format string literal";
else if (isoperator(str)) return L"operator";
else if (isspace(str)) return L"space";
else if (isnew_line(str)) return L"new line";
else if (iscomment(str)) return L"comment";
else if (iskeyword(str)) return L"keyword";
else return L"id";
}
/*
Ïñåâäîêîä äëÿ òîêåíîâ
ñîçäà¸ì ïåðåìåííó, â êîòîðîé áóäåò âðåìåííî õðàíèòñÿ ñèìâîëû.
ïðîõîäèìñÿ ïî âñåé ñòðîêå è ñìîòðèì ñèìâîëû.
åñëè âñòðå÷àåòñÿ ïðîáåë, òî
åñëè äî ýòîãî áûëà ñòðîêà, òî
ìû å¸ ñèìâîë ñó¸ì â ñòðîêó
îáðàáàòûâàåì ñòðîêó íà òèïû
î÷èùàåì ïåðåìåííóþ
åñëè âñòðå÷àåòñÿ ðàçäåëèòåëü (ñêîáêè, çàïÿòûå), òî
åñëè äî ýòîãî áûëà ñòðîêà, òî
ìû å¸ ñèìâîë ñó¸ì â ñòðîêó
òî îáðàáàòûâàåì ñòðîêó, î÷èùàåì ïåðåìåííóþ è çàòåì ñàì ðàçäåëèòåëü îáðàáàòûâàåì
èä¸ì äàëüøå
åñëè âñòðå÷àåò çíàê îïåðàòîð, òî
åñëè äî ýòîãî áûëà ñòðîêà, òî
ìû å¸ ñèìâîë ñó¸ì â ñòðîêó
ïðîâåðÿåì êàêàÿ ýòî ñòðîêà,
åñëè ýòî íå îïåðàòîð, òî
îáðàáàòûâàåì ñòðîêó è ñìîòðèì, êàêîé ñëåäóþùèé ñèìâîë.
Åñëè ýòî îïåðàòîð, òî ñó¸ì äâà ýòè ñèìâîëà è îáðàáàòûâàåì èõ. èòåðàòîð óâåëè÷èâàåì íà äâà
*/
vector<Token> tokenize(const wstring& str)
{
vector<Token> tokens;
wstring temp = L"";
Token temp_token;
for (wstring::const_iterator i = str.begin(); i != str.end(); ++i)
{
switch (*i)
{
case L' ':
case L'\t':
if (isbeginofstring(temp) && (!isstring(temp) && !isRAWstring(temp) && !isFORMATstring(temp)))
{
temp += *i;
break;
}
if (token_type(temp) != L"space")
{
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
temp = L"";
}
break;
case L'(':
case L')':
case L'[':
case L']':
case L'{':
case L'}':
case L',':
case L'\n':
if (isbeginofstring(temp) && (!isstring(temp) && !isRAWstring(temp) && !isFORMATstring(temp)))
{
temp += *i;
break;
}
if (token_type(temp) != L"space")
{
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
}
temp = wstring(1, *i);
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
temp = L"";
break;
case L'.':
if ((isbeginofstring(temp) &&
!isstring(temp) &&
!isRAWstring(temp) &&
!isFORMATstring(temp)) || isnumber_without_dot(temp) ||
temp.empty())
{
temp += *i;
break;
}
if (token_type(temp) != L"space")
{
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
}
temp = wstring(1, *i);
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
temp = L"";
break;
case L'+':
case L'-':
case L'*':
case L'/':
case L'%':
case L'=':
case L'<':
case L'>':
if (isbeginofstring(temp) && (!isstring(temp) && !isRAWstring(temp) && !isFORMATstring(temp)))
{
temp += *i;
break;
}
if (token_type(temp) != L"space")
{
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
}
temp = wstring(1, *i);
switch (*(i + 1))
{
case L'+':
case L'-':
case L'*':
case L'/':
case L'%':
case L'=':
case L'<':
case L'>':
temp += *(i + 1);
++i;
default:
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
temp = L"";
break;
}
break;
default:
temp += *i;
break;
}
}
if (!(temp.empty()))
{
temp_token.type = token_type(temp);
temp_token.value = temp;
tokens.push_back(temp_token);
temp = L"";
}
temp_token.type = L"EOT";
temp_token.value = L"EOT";
tokens.push_back(temp_token);
return tokens;
}