This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JassTokenizer.cs
192 lines (162 loc) · 6.95 KB
/
JassTokenizer.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
// ------------------------------------------------------------------------------
// <copyright file="JassTokenizer.cs" company="Drake53">
// Licensed under the MIT license.
// See the LICENSE file in the project root for more information.
// </copyright>
// ------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace War3Net.CodeAnalysis.Jass
{
internal class JassTokenizer
{
private const int BufferCapacity = 120;
private readonly string _text;
private readonly bool _appendEOF;
private TokenizerMode _mode;
public JassTokenizer(string text, bool appendEOF = true)
{
_text = text;
_appendEOF = appendEOF;
}
public IEnumerable<SyntaxToken> Tokenize()
{
_mode = TokenizerMode.Content;
var buffer = new StringBuilder(BufferCapacity);
const char EscapeCharacter = '\\';
var encounteredEscapeCharacter = false;
using (var reader = new StringReader(_text))
{
while (true)
{
var peek = reader.Peek();
if (peek == EscapeCharacter)
{
if (_mode == TokenizerMode.Content)
{
throw new Exception($"Unexpectedly encountered escape character '{EscapeCharacter}'");
}
if (!encounteredEscapeCharacter)
{
encounteredEscapeCharacter = true;
buffer.Append((char)reader.Read());
continue;
}
}
var ignoreDelimiter = encounteredEscapeCharacter;
encounteredEscapeCharacter = false;
if (!ignoreDelimiter && IsCharacterDelimiter(peek))
{
if (buffer.Length == 0)
{
if (peek == -1)
{
break;
}
var peekChar = (char)peek;
if (SyntaxToken.TryTokenizeSingleSymbol(peekChar, out var token))
{
reader.Read();
peek = reader.Peek();
if (peek != -1 && SyntaxToken.TryTokenizeKeyword($"{peekChar}{(char)peek}", out var token2))
{
reader.Read();
// Assumption: non-alphanumeric tokens are at most 2 characters (==, !=, <=, >=, //).
yield return TryUpdateMode(token2);
}
else
{
yield return TryUpdateMode(token);
}
continue;
}
else if (char.IsWhiteSpace(peekChar))
{
reader.Read();
continue;
}
else
{
// Handle 2-char symbols for which the first symbol on its own is not a token.
reader.Read();
peek = reader.Read();
if (SyntaxToken.TryTokenizeKeyword($"{peekChar}{(char)peek}", out var token2))
{
yield return TryUpdateMode(token2);
continue;
}
throw new InvalidDataException($"Invalid sequence of symbols: {peekChar}{(char)peek}");
}
}
else
{
var tokenText = buffer.ToString();
buffer.Length = 0;
if (_mode == TokenizerMode.Content && SyntaxToken.TryTokenizeKeyword(tokenText, out var token))
{
yield return TryUpdateMode(token);
}
else
{
yield return TryUpdateMode(new SyntaxToken(SyntaxToken.GetAlphanumericalTokenType(tokenText, _mode), tokenText));
}
continue;
}
}
buffer.Append((char)reader.Read());
}
if (_appendEOF)
{
yield return new SyntaxToken(SyntaxTokenType.NewlineSymbol);
yield return new SyntaxToken(SyntaxTokenType.EndOfFile);
}
}
}
private bool IsCharacterDelimiter(int c)
{
if (c == -1)
{
return true;
}
var character = (char)c;
switch (_mode)
{
case TokenizerMode.Content: return !char.IsLetterOrDigit(character) && character != '_' && character != '.' && character != '$';
case TokenizerMode.String: return character == '"';
case TokenizerMode.FourCC: return character == '\'';
case TokenizerMode.SingleLineComment: return character == '\n' || character == '\r';
default: throw new InvalidOperationException();
}
}
private SyntaxToken TryUpdateMode(SyntaxToken lastToken)
{
if (_mode == TokenizerMode.SingleLineComment && lastToken.TokenType == SyntaxTokenType.NewlineSymbol)
{
_mode = TokenizerMode.Content;
}
else if (_mode == TokenizerMode.FourCC && lastToken.TokenType == SyntaxTokenType.SingleQuote)
{
_mode = TokenizerMode.Content;
}
else if (_mode == TokenizerMode.String && lastToken.TokenType == SyntaxTokenType.DoubleQuotes)
{
_mode = TokenizerMode.Content;
}
else if (lastToken.TokenType == SyntaxTokenType.DoubleQuotes)
{
_mode = TokenizerMode.String;
}
else if (lastToken.TokenType == SyntaxTokenType.SingleQuote)
{
_mode = TokenizerMode.FourCC;
}
else if (lastToken.TokenType == SyntaxTokenType.DoubleForwardSlash)
{
_mode = TokenizerMode.SingleLineComment;
}
return lastToken;
}
}
}