-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepLexer.cs
330 lines (287 loc) · 10.5 KB
/
StepLexer.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using StepParser.Syntax;
using StepParser.Tokens;
using System;
using System.Collections.Generic;
using System.Text;
namespace StepParser
{
internal class StepLexer
{
private List<StepToken> _tokens;
private int _offset = 0;
public StepLexer(IEnumerable<StepToken> tokens)
{
_tokens = new List<StepToken>(tokens);
}
private bool TokensRemain()
{
return _offset < _tokens.Count;
}
private void MoveNext()
{
_offset++;
}
private StepToken Current => _tokens[_offset];
public StepFileSyntax LexFileSyntax()
{
_offset = 0;
SwallowKeywordAndSemicolon(StepFile.MagicHeader);
var header = LexHeaderSection();
var data = LexDataSection();
var file = new StepFileSyntax(header, data);
SwallowKeywordAndSemicolon(StepFile.MagicFooter);
return file;
}
private StepHeaderSectionSyntax LexHeaderSection()
{
AssertTokensRemain();
var headerLine = Current.Line;
var headerColumn = Current.Column;
SwallowKeywordAndSemicolon(StepFile.HeaderText);
var macros = new List<StepHeaderMacroSyntax>();
while (TokensRemain() && Current.Kind == StepTokenKind.Keyword && !IsCurrentEndSec())
{
var macro = LexHeaderMacro();
macros.Add(macro);
}
SwallowKeywordAndSemicolon(StepFile.EndSectionText);
return new StepHeaderSectionSyntax(headerLine, headerColumn, macros);
}
private StepHeaderMacroSyntax LexHeaderMacro()
{
AssertNextTokenKind(StepTokenKind.Keyword);
var name = ((StepKeywordToken)Current).Value;
MoveNext();
var syntaxList = LexSyntaxList();
SwallowSemicolon();
return new StepHeaderMacroSyntax(name, syntaxList);
}
private StepSyntax LexIndividualValue()
{
StepSyntax result;
AssertTokensRemain();
switch (Current.Kind)
{
case StepTokenKind.Integer:
result = new StepIntegerSyntax((StepIntegerToken)Current);
MoveNext();
break;
case StepTokenKind.Real:
result = new StepRealSyntax((StepRealToken)Current);
MoveNext();
break;
case StepTokenKind.String:
result = new StepStringSyntax((StepStringToken)Current);
MoveNext();
break;
case StepTokenKind.Asterisk:
result = new StepAutoSyntax((StepAsteriskToken)Current);
MoveNext();
break;
case StepTokenKind.Omitted:
result = new StepOmittedSyntax((StepOmittedToken)Current);
MoveNext();
break;
case StepTokenKind.Enumeration:
result = new StepEnumerationValueSyntax((StepEnumerationToken)Current);
MoveNext();
break;
case StepTokenKind.LeftParen:
result = LexSyntaxList();
break;
case StepTokenKind.Keyword:
result = LexSimpleItem();
break;
case StepTokenKind.EntityInstance:
result = new StepEntityInstanceReferenceSyntax((StepEntityInstanceToken)Current);
MoveNext();
break;
default:
ReportError($"Unexpected syntax token '{Current.Kind}'");
result = null; // unreachable
break;
}
return result;
}
private StepSyntaxList LexSyntaxList()
{
AssertTokensRemain();
var listLine = Current.Line;
var listColumn = Current.Column;
SwallowLeftParen();
var values = new List<StepSyntax>();
bool keepReading = true;
bool expectingValue = true;
while (keepReading)
{
AssertTokensRemain();
if (expectingValue || values.Count == 0)
{
// expect a value or a close paren
switch (Current.Kind)
{
case StepTokenKind.RightParen:
keepReading = false;
MoveNext();
break;
default:
values.Add(LexIndividualValue());
break;
}
}
else
{
// expect a comma or close paren
switch (Current.Kind)
{
case StepTokenKind.RightParen:
keepReading = false;
MoveNext();
break;
case StepTokenKind.Comma:
MoveNext();
break;
default:
ReportError($"Expected right paren or comma but found '{Current.Kind}'");
break;
}
}
expectingValue = !expectingValue;
}
return new StepSyntaxList(listLine, listColumn, values);
}
private StepDataSectionSyntax LexDataSection()
{
AssertTokensRemain();
var dataLine = Current.Line;
var dataColumn = Current.Column;
SwallowKeywordAndSemicolon(StepFile.DataText);
var itemInstsances = new List<StepEntityInstanceSyntax>();
while (TokensRemain() && Current.Kind == StepTokenKind.EntityInstance)
{
var itemInstance = LexItemInstance();
itemInstsances.Add(itemInstance);
}
SwallowKeywordAndSemicolon(StepFile.EndSectionText);
return new StepDataSectionSyntax(dataLine, dataColumn, itemInstsances);
}
private StepEntityInstanceSyntax LexItemInstance()
{
var line = Current.Line;
var column = Current.Column;
AssertNextTokenKind(StepTokenKind.EntityInstance);
var reference = (StepEntityInstanceToken)Current;
MoveNext();
SwallowEquals();
AssertTokensRemain();
StepItemSyntax item = null;
switch (Current.Kind)
{
case StepTokenKind.Keyword:
item = LexSimpleItem();
break;
case StepTokenKind.LeftParen:
item = LexComplexItem();
break;
default:
ReportError($"Expected left paren but found {Current.Kind}");
break; // unreachable
}
SwallowSemicolon();
return new StepEntityInstanceSyntax(reference, item);
}
private StepSimpleItemSyntax LexSimpleItem()
{
AssertNextTokenKind(StepTokenKind.Keyword);
var keyword = (StepKeywordToken)Current;
MoveNext();
var parameters = LexSyntaxList();
return new StepSimpleItemSyntax(keyword, parameters, 0);
}
private StepComplexItemSyntax LexComplexItem()
{
var entities = new List<StepSimpleItemSyntax>();
var itemLine = Current.Line;
var itemColumn = Current.Column;
SwallowLeftParen();
entities.Add(LexSimpleItem()); // there's always at least one
bool keepReading = true;
while (keepReading)
{
AssertTokensRemain();
switch (Current.Kind)
{
case StepTokenKind.RightParen:
SwallowRightParen();
keepReading = false;
break;
case StepTokenKind.Keyword:
entities.Add(LexSimpleItem());
break;
default:
ReportError($"Expected right paren or keyword but found {Current.Kind}");
break; // unreachable
}
}
return new StepComplexItemSyntax(itemLine, itemColumn, entities);
}
private bool IsCurrentEndSec()
{
return Current.Kind == StepTokenKind.Keyword && ((StepKeywordToken)Current).Value == StepFile.EndSectionText;
}
private void SwallowKeyword(string keyword)
{
AssertNextTokenKind(StepTokenKind.Keyword);
if (((StepKeywordToken)Current).Value != keyword)
{
ReportError($"Expected keyword '{keyword}' but found '{((StepKeywordToken)Current).Value}'");
}
MoveNext();
}
private void SwallowKeywordAndSemicolon(string keyword)
{
SwallowKeyword(keyword);
SwallowSemicolon();
}
private void SwallowSemicolon()
{
SwallowToken(StepTokenKind.Semicolon);
}
private void SwallowLeftParen()
{
SwallowToken(StepTokenKind.LeftParen);
}
private void SwallowRightParen()
{
SwallowToken(StepTokenKind.RightParen);
}
private void SwallowEquals()
{
SwallowToken(StepTokenKind.Equals);
}
private void SwallowToken(StepTokenKind kind)
{
AssertNextTokenKind(kind);
MoveNext();
}
private void AssertNextTokenKind(StepTokenKind kind)
{
AssertTokensRemain();
if (Current.Kind != kind)
{
ReportError($"Expected '{kind}' token but found '{Current.Kind}'");
}
}
private void AssertTokensRemain()
{
if (!TokensRemain())
{
ReportError("Unexpected end of token stream", 0, 0);
}
}
private void ReportError(string message, int? line = null, int? column = null)
{
throw new StepReadException(message, line ?? Current.Line, column ?? Current.Column);
}
}
}