-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cs
261 lines (232 loc) · 8.78 KB
/
lexer.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace pineapple
{
class lexer
{
//token const
public const int TOKEN_EOF = 0; // end-of-file
public const int TOKEN_VAR_PREFIX = 1; // $
public const int TOKEN_LEFT_PAREN = 2; // (
public const int TOKEN_RIGHT_PAREN = 3; // )
public const int TOKEN_EQUAL = 4; // =
public const int TOKEN_QUOTE = 5; // "
public const int TOKEN_DUOQUOTE = 6; // ""
public const int TOKEN_NAME = 7; // Name ::= [_A-Za-z][_0-9A-Za-z]*
public const int TOKEN_PRINT = 8; // print
public const int TOKEN_IGNORED = 9; // Ignored
static public Dictionary<int, string> tokenNameMap = new Dictionary<int, string>()
{
{TOKEN_EOF,"EOF"},
{TOKEN_VAR_PREFIX,"$"},
{TOKEN_LEFT_PAREN,"("},
{TOKEN_RIGHT_PAREN,")"},
{TOKEN_EQUAL,"="},
{TOKEN_QUOTE,"\""},
{TOKEN_DUOQUOTE,"\"\""},
{TOKEN_NAME,"Name"},
{TOKEN_PRINT,"print"},
{TOKEN_IGNORED,"Ignored"}
};
static Dictionary<string, int> keywords = new Dictionary<string, int>()
{
{"print",TOKEN_PRINT}
};
public struct Lexer
{
public string sourceCode;
public int lineNum;
public string nextToken;
public int nextTokenType;
public int nextTokenLineNum;
public (int, string) NextTokenIs(int tokenType)
{
var (nowLineNum, nowTokenType, nowToken) = GetNextToken();
//syntax error
if (tokenType != nowTokenType)
{
Console.WriteLine("NextTokenIs(): syntax error near \"" + tokenNameMap[nowTokenType] + "\", expected token: {" + tokenNameMap[tokenType] + "} but got {" + tokenNameMap[nowTokenType] + "}.");
throw new System.Exception();
}
return (nowLineNum, nowToken);
}
public int LookAhead()
{
//lexer.nextToken* already setted
if (nextTokenLineNum > 0)
return nextTokenType;
//set it
int nowLineNum = lineNum;
var (ln, tokenType, token) = GetNextToken();
lineNum = nowLineNum;
nextTokenLineNum = ln;
nextTokenType = tokenType;
nextToken = token;
return tokenType;
}
public void LookAheadAndSkip(int expectedType)
{
//get next token
int nowLineNum = lineNum;
var (ln, tokenType, token) = GetNextToken();
//not is expected type, reverse cursor
if (tokenType != expectedType)
{
lineNum = nowLineNum;
nextTokenLineNum = ln;
nextTokenType = tokenType;
nextToken = token;
}
}
private (int, int, string) GetNextToken()
{
if (nextTokenLineNum > 0)
{
int ln = nextTokenLineNum;
lineNum = nextTokenLineNum;
nextTokenLineNum = 0;
return (ln, nextTokenType, nextToken);
}
return MatchToken();
}
public int GetLineNum()
{
return lineNum;
}
public (int, int, string) MatchToken()
{
//check ignored
if (isIgnored())
return (lineNum, TOKEN_IGNORED, "Ignored");
//finish
if (sourceCode.Length == 0)
return (lineNum, TOKEN_EOF, tokenNameMap[TOKEN_EOF]);
//check token
switch (sourceCode[0])
{
case '$':
skipSourceCode(1);
return (lineNum, TOKEN_VAR_PREFIX, "$");
case '(':
skipSourceCode(1);
return (lineNum, TOKEN_LEFT_PAREN, "(");
case ')':
skipSourceCode(1);
return (lineNum, TOKEN_RIGHT_PAREN, ")");
case '=':
skipSourceCode(1);
return (lineNum, TOKEN_EQUAL, "=");
case '"':
if (nextSourceCodeIs("\"\""))
{
skipSourceCode(2);
return (lineNum, TOKEN_DUOQUOTE, "\"\"");
}
skipSourceCode(1);
return (lineNum, TOKEN_QUOTE, "\"");
}
// check multiple character token
if (sourceCode[0] == '_' || char.IsLetter(sourceCode[0]))
{
string token = scanName();
if (keywords.TryGetValue(token, out int tokenType))
return (lineNum, tokenType, token);
return (lineNum, TOKEN_NAME, token);
}
//unexpected symbol
Console.WriteLine("MatchToken(): unexpected symbol near \"" + sourceCode[0] + "\".");
throw new System.Exception();
}
public void skipSourceCode(int n)
{
sourceCode = sourceCode.Substring(n);
}
public bool nextSourceCodeIs(string s)
{
if (s.Length < sourceCode.Length && s.CompareTo(sourceCode.Remove(s.Length)) == 0)
return true;
else if (s == sourceCode)
return true;
return false;
}
public string scanName()
{
return scan(new Regex(@"^[_\d\w]+"));
}
public string scan(Regex regexp)
{
string token = regexp.Match(sourceCode).Groups[0].Value;
if (token != "")
{
skipSourceCode(token.Length);
return token;
}
Console.WriteLine("unreachable!");
throw new System.Exception();
}
//return content before token
public string scanBeforeToken(string token)
{
string[] s = sourceCode.Split(token);
if (s.Length < 2)
{
Console.WriteLine("unreachable!");
throw new System.Exception();
}
skipSourceCode(s[0].Length);
return s[0];
}
public bool isIgnored()
{
bool isIgnored = false;
bool isWhiteSpace(char c)
{
switch (c)
{
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
case ' ':
return true;
}
return false;
}
//matching
while (sourceCode.Length > 0)
{
//isNewLine
if (nextSourceCodeIs("\r\n") || nextSourceCodeIs("\n\r"))
{
skipSourceCode(2);
lineNum++;
isIgnored = true;
}
else if (sourceCode[0] == '\r' || sourceCode[0] == '\n')
{
skipSourceCode(1);
lineNum++;
isIgnored = true;
}
//isWhiteSpace
else if (isWhiteSpace(sourceCode[0]))
{
skipSourceCode(1);
isIgnored = true;
}
else
{
break;
}
}
return isIgnored;
}
}
static public Lexer NewLexer(string sourceCode)
{
return new Lexer() { sourceCode = sourceCode, lineNum = 1, nextToken = "", nextTokenType = 0, nextTokenLineNum = 0 };
}
}
}