-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsTextBuffer.cs
executable file
·282 lines (237 loc) · 6.16 KB
/
CsTextBuffer.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Utility;
using System.Text;
using System;
namespace CSharpCodeParser
{
public class CsTextBuffer
{
public enum BlockState : byte
{
None = 0,
CommentBlock = 1,
StringBlock = 2,
}
public class FormatedLine
{
public BlockState blockState;
public List<SyntaxToken> tokens;
[System.NonSerialized]
public int index;
public override string ToString()
{
return ExtensionUtility.ToString (tokens);
}
}
public FormatedLine[] formatedLines = new FormatedLine[0];
public List<string> lines = new List<string>();
public CsParser parser = new CsParser();
private string lineEnding = "\n";
public void LoadCsCode(string content)
{
lines = new List<string>(content.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'));
formatedLines = new FormatedLine[lines.Count-1];
for (int i=0; i<lines.Count-1; i++)
{
FormatLine(i);
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder ();
foreach(var formatedLine in this.formatedLines)
{
sb.AppendLine(formatedLine.ToString());
}
return sb.ToString();
}
private void FormatLine(int lineIndex)
{
var formateLine = this.formatedLines [lineIndex];
if (formateLine == null)
{
formateLine = new FormatedLine();
this.formatedLines[lineIndex] = formateLine;
formateLine.index = lineIndex;
}
if(lineIndex > 0)
{
formateLine.blockState = this.formatedLines[lineIndex -1].blockState;
}
parser.Tokenize (this.lines[lineIndex], formateLine);
}
}
public class Scanner: IEnumerator<SyntaxToken>
{
public readonly string fileName;
readonly CsGrammar grammar;
readonly CsTextBuffer.FormatedLine[] lines;
List<SyntaxToken> tokens;
int currentLine = -1;
int currentTokenIndex = -1;
private static SyntaxToken EOF;
public CsGrammar.Node CurrentGrammarNode { get; set; }
private ParseTree.Node _currentPTN;
public ParseTree.Node CurrentParseTreeNode {
get { return _currentPTN; }
set { _currentPTN = value; }
}
private int maxScanDistance;
public bool KeepScanning { get { return maxScanDistance > 0; } }
public int CurrentLine() { return currentLine + 1; }
public int CurrentTokenIndex() { return currentTokenIndex; }
public Scanner(CsGrammar grammar, CsTextBuffer.FormatedLine[] formatedLines, string fileName)
{
this.grammar = grammar;
this.fileName = fileName;
lines = formatedLines;
if (EOF == null)
EOF = new SyntaxToken(SyntaxToken.Kind.EOF, string.Empty) { tokenId = grammar.tokenEOF };
}
public bool Lookahead(CsGrammar.Node node, int maxDistance = int.MaxValue)
{
if (tokens == null && currentLine > 0)
return false;
var line = currentLine;
var index = currentTokenIndex;
// var realIndex = nonTriviaTokenIndex;
var temp = maxScanDistance;
maxScanDistance = maxDistance;
var match = node.Scan(this);
maxScanDistance = temp;
currentLine = line;
currentTokenIndex = index;
// nonTriviaTokenIndex = realIndex;
tokens = currentLine < lines.Length ? lines[currentLine].tokens : null;
return match;
}
public SyntaxToken Lookahead(int offset, bool skipWhitespace = true)
{
if (!skipWhitespace)
{
return currentTokenIndex + 1 < tokens.Count ? tokens[currentTokenIndex + 1] : EOF;
}
var t = tokens;
var cl = currentLine;
var cti = currentTokenIndex;
while (offset --> 0)
{
if (!MoveNext())
{
tokens = t;
currentLine = cl;
currentTokenIndex = cti;
return EOF;
}
}
var token = tokens[currentTokenIndex];
tokens = t;
currentLine = cl;
currentTokenIndex = cti;
return token;
}
public SyntaxToken CurrentToken()
{
return null;
}
public bool Seeking { get; set; }
public SyntaxToken Current
{
get
{
return tokens != null ? tokens[currentTokenIndex] : EOF;
}
}
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
if (maxScanDistance > 0)
--maxScanDistance;
while (MoveNextSingle())
{
if (tokens[currentTokenIndex].tokenId == -1)
{
var token = tokens[currentTokenIndex];
switch (token.tokenKind)
{
case SyntaxToken.Kind.Missing:
case SyntaxToken.Kind.Whitespace:
case SyntaxToken.Kind.Comment:
case SyntaxToken.Kind.EOF:
case SyntaxToken.Kind.Preprocessor:
case SyntaxToken.Kind.PreprocessorSymbol:
case SyntaxToken.Kind.PreprocessorArguments:
case SyntaxToken.Kind.VerbatimStringLiteral:
break;
case SyntaxToken.Kind.Punctuator:
case SyntaxToken.Kind.Keyword:
case SyntaxToken.Kind.BuiltInLiteral:
token.tokenId = grammar.TokenToId(token.text);
break;
case SyntaxToken.Kind.Identifier:
case SyntaxToken.Kind.ContextualKeyword:
token.tokenId = grammar.tokenIdentifier;
break;
case SyntaxToken.Kind.IntegerLiteral:
case SyntaxToken.Kind.RealLiteral:
case SyntaxToken.Kind.CharLiteral:
case SyntaxToken.Kind.StringLiteral:
case SyntaxToken.Kind.VerbatimStringBegin:
token.tokenId = grammar.tokenLiteral;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
if (tokens[currentTokenIndex].tokenKind > SyntaxToken.Kind.VerbatimStringLiteral)
{
return true;
}
}
tokens = null;
++currentLine;
currentTokenIndex = -1;
return false;
}
public bool MoveNextSingle()
{
while (tokens == null)
{
if (currentLine + 1 >= lines.Length)
return false;
currentTokenIndex = -1;
tokens = lines[++currentLine].tokens;
}
while (currentTokenIndex + 1 >= tokens.Count)
{
if (currentLine + 1 >= lines.Length)
{
tokens = null;
return false;
}
currentTokenIndex = -1;
tokens = lines[++currentLine].tokens;
while (tokens == null)
{
if (currentLine + 1 >= lines.Length)
return false;
tokens = lines[++currentLine].tokens;
}
}
++currentTokenIndex;
return true;
}
public void Reset()
{
}
public void Dispose()
{
}
}
}