-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStepFactory.cs
95 lines (79 loc) · 3.27 KB
/
StepFactory.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
using System;
using System.Collections.Generic;
namespace Ara3D.StepParser
{
public static unsafe class StepFactory
{
public static StepList GetAttributes(this StepRawInstance inst, byte* lineEnd)
{
if (!inst.IsValid())
return default;
var ptr = inst.Type.End();
var token = StepTokenizer.ParseToken(ptr, lineEnd);
// TODO: there is a potential bug here when the line is split across multiple line
return CreateAggregate(ref token, lineEnd);
}
public static StepValue Create(ref StepToken token, byte* end)
{
switch (token.Type)
{
case StepTokenType.String:
return StepString.Create(token);
case StepTokenType.Symbol:
return StepSymbol.Create(token);
case StepTokenType.Id:
return StepId.Create(token);
case StepTokenType.Redeclared:
return StepRedeclared.Create(token);
case StepTokenType.Unassigned:
return StepUnassigned.Create(token);
case StepTokenType.Number:
return StepNumber.Create(token);
case StepTokenType.Ident:
var span = token.Span;
StepTokenizer.ParseNextToken(ref token, end);
var attr = CreateAggregate(ref token, end);
return new StepEntity(span, attr);
case StepTokenType.BeginGroup:
return CreateAggregate(ref token, end);
case StepTokenType.None:
case StepTokenType.Whitespace:
case StepTokenType.Comment:
case StepTokenType.Unknown:
case StepTokenType.LineBreak:
case StepTokenType.EndOfLine:
case StepTokenType.Definition:
case StepTokenType.Separator:
case StepTokenType.EndGroup:
default:
throw new Exception($"Cannot convert token type {token.Type} to a StepValue");
}
}
public static StepList CreateAggregate(ref StepToken token, byte* end)
{
var values = new List<StepValue>();
StepTokenizer.EatWSpace(ref token, end);
if (token.Type != StepTokenType.BeginGroup)
throw new Exception("Expected '('");
while (StepTokenizer.ParseNextToken(ref token, end))
{
switch (token.Type)
{
// Advance past comments, whitespace, and commas
case StepTokenType.Comment:
case StepTokenType.Whitespace:
case StepTokenType.LineBreak:
case StepTokenType.Separator:
case StepTokenType.None:
continue;
// Expected end of group
case StepTokenType.EndGroup:
return new StepList(values);
}
var curValue = Create(ref token, end);
values.Add(curValue);
}
throw new Exception("Unexpected end of input");
}
}
}