-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStepValue.cs
170 lines (140 loc) · 4.56 KB
/
StepValue.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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Ara3D.Buffers;
using Ara3D.Utils;
namespace Ara3D.StepParser
{
/// <summary>
/// The base class of the different type of value items that can be found in a STEP file.
/// * Entity
/// * List
/// * String
/// * Symbol
/// * Unassigned token
/// * Redeclared token
/// * Number
/// </summary>
public class StepValue;
public class StepEntity : StepValue
{
public readonly ByteSpan EntityType;
public readonly StepList Attributes;
public StepEntity(ByteSpan entityType, StepList attributes)
{
Debug.Assert(!entityType.IsNull());
EntityType = entityType;
Attributes = attributes;
}
public override string ToString()
=> $"{EntityType}{Attributes}";
}
public class StepList : StepValue
{
public readonly List<StepValue> Values;
public StepList(List<StepValue> values)
=> Values = values;
public override string ToString()
=> $"({Values.JoinStringsWithComma()})";
public static StepList Default = new(new List<StepValue>());
}
public class StepString : StepValue
{
public readonly ByteSpan Value;
public static StepString Create(StepToken token)
{
var span = token.Span;
Debug.Assert(token.Type == StepTokenType.String);
Debug.Assert(span.Length >= 2);
Debug.Assert(span.First() == '\'' || span.First() == '"');
Debug.Assert(span.Last() == '\'' || span.Last() == '"');
return new StepString(span.Trim(1, 1));
}
public StepString(ByteSpan value)
=> Value = value;
public override string ToString()
=> $"'{Value}'";
}
public class StepSymbol : StepValue
{
public readonly ByteSpan Name;
public StepSymbol(ByteSpan name)
=> Name = name;
public override string ToString()
=> $".{Name}.";
public static StepSymbol Create(StepToken token)
{
Debug.Assert(token.Type == StepTokenType.Symbol);
var span = token.Span;
Debug.Assert(span.Length >= 2);
Debug.Assert(span.First() == '.');
Debug.Assert(span.Last() == '.');
return new StepSymbol(span.Trim(1, 1));
}
}
public class StepNumber : StepValue
{
public readonly ByteSpan Span;
public double Value => Span.ToDouble();
public StepNumber(ByteSpan span)
=> Span = span;
public override string ToString()
=> $"{Value}";
public static StepNumber Create(StepToken token)
{
Debug.Assert(token.Type == StepTokenType.Number);
var span = token.Span;
return new(span);
}
}
public class StepId : StepValue
{
public readonly uint Id;
public StepId(uint id)
=> Id = id;
public override string ToString()
=> $"#{Id}";
public static unsafe StepId Create(StepToken token)
{
Debug.Assert(token.Type == StepTokenType.Id);
var span = token.Span;
Debug.Assert(span.Length >= 2);
Debug.Assert(span.First() == '#');
var id = 0u;
for (var i = 1; i < span.Length; ++i)
{
Debug.Assert(span.Ptr[i] >= '0' && span.Ptr[i] <= '9');
id = id * 10 + span.Ptr[i] - '0';
}
return new StepId(id);
}
}
public class StepUnassigned : StepValue
{
public static readonly StepUnassigned Default = new();
public override string ToString()
=> "$";
public static StepUnassigned Create(StepToken token)
{
Debug.Assert(token.Type == StepTokenType.Unassigned);
var span = token.Span;
Debug.Assert(span.Length == 1);
Debug.Assert(span.First() == '$');
return Default;
}
}
public class StepRedeclared : StepValue
{
public static readonly StepRedeclared Default = new();
public override string ToString()
=> "*";
public static StepRedeclared Create(StepToken token)
{
Debug.Assert(token.Type == StepTokenType.Redeclared);
var span = token.Span;
Debug.Assert(span.Length == 1);
Debug.Assert(span.First() == '*');
return Default;
}
}
}