-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaUnicodeStream.cs
More file actions
227 lines (188 loc) · 5.54 KB
/
Copy pathJavaUnicodeStream.cs
File metadata and controls
227 lines (188 loc) · 5.54 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
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
namespace Tvl.VisualStudio.Language.Java
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Antlr.Runtime;
public class JavaUnicodeStream : ICharStream
{
private readonly ICharStream _source;
private readonly List<CharInfo> _next = new List<CharInfo>();
private int _slashCount;
public JavaUnicodeStream(ICharStream source)
{
Contract.Requires<ArgumentNullException>(source != null, "source");
_source = source;
}
public int CharPositionInLine
{
get
{
return _source.CharPositionInLine;
}
set
{
_source.CharPositionInLine = value;
}
}
public int Line
{
get
{
return _source.Line;
}
set
{
_source.Line = value;
}
}
public int Count
{
get
{
return _source.Count;
}
}
public int Index
{
get
{
return _source.Index;
}
}
public string SourceName
{
get
{
return _source.SourceName;
}
}
int ICharStream.LT(int i)
{
return LA(i);
}
public string Substring(int start, int length)
{
return _source.Substring(start, length);
}
public void Consume()
{
if (_next.Count == 0)
{
if (LA(1) == CharStreamConstants.EndOfFile)
return;
}
if (_next.Count > 0)
{
int count = _next[0].Index - Index;
for (int i = 0; i < count; i++)
_source.Consume();
if (_next[0].Char == '\\' && count == 1)
_slashCount++;
else
_slashCount = 0;
_next.RemoveAt(0);
}
}
public int LA(int i)
{
if (i <= 0)
return _source.LA(i);
if (i - 1 >= _next.Count)
{
int nextIndex = Index;
if (_next.Count > 0)
nextIndex = _next[_next.Count - 1].Index;
int slashCount = _slashCount;
if (_next.Count > 0)
slashCount = _next[_next.Count - 1].SlashCount;
for (int j = _next.Count; j < i; j++)
{
int c = ReadCharAt(ref nextIndex, ref slashCount);
_next.Add(new CharInfo(c, nextIndex, slashCount));
}
}
return _next[i - 1].Char;
}
private int ReadCharAt(ref int nextIndex, ref int slashCount)
{
bool blockUnicodeEscape = (slashCount % 2) != 0;
int c0 = _source.LA(nextIndex - Index + 1);
if (c0 == '\\')
{
slashCount++;
if (!blockUnicodeEscape)
{
int c1 = _source.LA(nextIndex - Index + 2);
if (c1 == 'u')
{
int c2 = _source.LA(nextIndex - Index + 3);
int c3 = _source.LA(nextIndex - Index + 4);
int c4 = _source.LA(nextIndex - Index + 5);
int c5 = _source.LA(nextIndex - Index + 6);
if (IsHexDigit(c2) && IsHexDigit(c3) && IsHexDigit(c4) && IsHexDigit(c5))
{
int value = HexValue(c2);
value = (value << 4) + HexValue(c3);
value = (value << 4) + HexValue(c4);
value = (value << 4) + HexValue(c5);
nextIndex += 6;
slashCount = 0;
return value;
}
}
}
}
nextIndex++;
return c0;
}
private static bool IsHexDigit(int c)
{
return (c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F');
}
private static int HexValue(int c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
throw new ArgumentOutOfRangeException("c");
}
public int Mark()
{
return _source.Mark();
}
public void Release(int marker)
{
_source.Release(marker);
}
public void Rewind()
{
_source.Rewind();
}
public void Rewind(int marker)
{
_source.Rewind(marker);
}
public void Seek(int index)
{
_source.Seek(index);
}
private struct CharInfo
{
public readonly int Char;
public readonly int Index;
public readonly int SlashCount;
public CharInfo(int c, int index, int slashCount)
{
Char = c;
Index = index;
SlashCount = slashCount;
}
}
}
}