forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserValueContext.cpp
343 lines (303 loc) · 9.1 KB
/
parserValueContext.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "pxr/pxr.h"
#include "pxr/usd/sdf/parserValueContext.h"
#include "pxr/base/tf/iterator.h"
#include "pxr/base/work/utils.h"
#include "pxr/usd/sdf/fileIO_Common.h"
PXR_NAMESPACE_OPEN_SCOPE
struct Sdf_ToStringVisitor
{
template <typename T>
std::string operator () (const T &value)
{
return TfStringify(value);
}
std::string operator () (const std::string &value)
{
return Sdf_FileIOUtility::Quote(value);
}
};
static void ReportCodingError(const std::string &text)
{
TF_CODING_ERROR(text);
}
Sdf_ParserValueContext::Sdf_ParserValueContext() :
valueTypeIsValid(false),
valueIsShaped(false),
errorReporter(ReportCodingError)
{
Clear();
}
bool
Sdf_ParserValueContext::SetupFactory(const std::string &typeName)
{
if (typeName != lastTypeName) {
const Sdf_ParserHelpers::ValueFactory &factory =
Sdf_ParserHelpers::GetValueFactoryForMenvaName(
typeName, &valueTypeIsValid);
valueTypeName = typeName;
if (!valueTypeIsValid) {
valueFunc = Sdf_ParserHelpers::ValueFactoryFunc();
valueIsShaped = false;
valueTupleDimensions = SdfTupleDimensions();
}
else {
valueFunc = factory.func;
valueIsShaped = factory.isShaped;
valueTupleDimensions = factory.dimensions;
}
lastTypeName = typeName;
}
return valueTypeIsValid;
}
VtValue
Sdf_ParserValueContext::ProduceValue(std::string *errStrPtr)
{
VtValue ret;
if (_isRecordingString) {
ret = SdfUnregisteredValue(GetRecordedString());
}
else {
if (!valueFunc) {
// CODE_COVERAGE_OFF
// We will already have detected a bad typename as we tried to
// create the attribute for this value. We should not hit this
// here.
errorReporter(TfStringPrintf("Unrecognized type name '%s'",
valueTypeName.c_str()).c_str());
return VtValue();
// CODE_COVERAGE_ON
}
size_t index = 0;
ret = valueFunc(shape, vars, index, errStrPtr);
}
Clear();
return ret;
}
void
Sdf_ParserValueContext::Clear()
{
dim = 0;
pushDim = -1;
shape.clear();
// Every time we parse a value, we call ProduceValue() which, in turn,
// calls Clear().
//
// Note that we're NOT resetting the following variables here:
//
// valueTypeName
// valueTypeIsValid
// valueFunc
// valueIsShaped
// valueTupleDimensions
//
// This is because we often parse several values in a row
// (e.g. AnimSpline keyframes), and we don't want the extra overhead of
// resetting the above variables just so that we can set them again
// before parsing the next value. Instead, whenever we parse a new
// attribute type, we call SetupFactory() which caches these variables.
// This allows us to skip over them here.
tupleDepth = 0;
vars.clear();
workingShape.clear();
_isRecordingString = false;
_needComma = false;
}
void
Sdf_ParserValueContext::AppendValue(const Value& value)
{
if (_isRecordingString) {
if (_needComma) {
_recordedString += ", ";
}
Sdf_ToStringVisitor v;
_recordedString += value.ApplyVisitor(v);
_needComma = true;
}
else {
vars.push_back(value);
}
if (pushDim == -1) {
pushDim = dim;
} else {
if (pushDim != dim) {
errorReporter("Non-square shaped value");
return;
}
}
// if inside a list (dim>0) and not inside a tuple (tupleDepth==0)
if (tupleDepth == 0 && dim)
workingShape[dim-1] += 1;
// If we're at the deepest level of the tuple, keep track of the number of
// elements added along the current dimension so that EndTuple() can
// validate the completed tuple dimensions with the correct tuple
// dimensions from the factory.
if (tupleDepth && static_cast<size_t>(tupleDepth) == valueTupleDimensions.size) {
--tupleDimensions.d[tupleDepth-1];
}
}
void
Sdf_ParserValueContext::BeginList()
{
if (_isRecordingString) {
if (_needComma) {
_needComma = false;
_recordedString += ", ";
}
_recordedString += '[';
}
// Dim starts at 1, so the current shape index is dim - 1.
++dim;
// Check if the shape is big enough for dim values
if (static_cast<size_t>(dim) > shape.size()) {
shape.push_back(0);
workingShape.push_back(0);
}
}
void
Sdf_ParserValueContext::EndList()
{
if (_isRecordingString) {
_recordedString += ']';
_needComma = true;
}
if (dim == 0) {
// CODE_COVERAGE_OFF
// This can't happen unless there's a bug in the parser
errorReporter("Mismatched [ ] in shaped value");
return;
// CODE_COVERAGE_ON
}
if(shape[dim-1] == 0) {
// This is the first time we've completed a run in this
// dimension, so store the size of this dimension into
// our discovered shape vector.
shape[dim-1] =
workingShape[dim-1];
if (shape[dim-1] == 0) {
// CODE_COVERAGE_OFF
// This can't happen unless there's a bug in the parser
errorReporter("Shaped value with a zero dimension");
return;
// CODE_COVERAGE_ON
}
} else {
// We've seen a run in this dimension before, so check
// that the size is the same as before.
if (shape[dim-1] !=
workingShape[dim-1]) {
errorReporter("Non-square shaped value");
return;
}
}
// XXX Dim should always be valid here due to check above?
if (dim) {
// Reset our counter for the dimension we just finished
// parsing
workingShape[dim-1] = 0;
--dim;
// And increment the tally for the containing dimension.
if (dim > 0)
++workingShape[dim-1];
}
}
void
Sdf_ParserValueContext::BeginTuple()
{
if (_isRecordingString) {
if (_needComma) {
_needComma = false;
_recordedString += ", ";
}
_recordedString += '(';
}
if (static_cast<size_t>(tupleDepth) >= valueTupleDimensions.size) {
errorReporter(TfStringPrintf("Tuple nesting too deep! Should not be "
"deeper than %d for attribute of type %s.",
tupleDepth, valueTypeName.c_str()));
return;
}
tupleDimensions.d[tupleDepth] =
valueTupleDimensions.d[tupleDepth];
++tupleDepth;
}
void
Sdf_ParserValueContext::EndTuple()
{
if (_isRecordingString) {
_recordedString += ')';
_needComma = true;
}
if (tupleDepth == 0) {
// CODE_COVERAGE_OFF
// This can't happen unless there's a bug in the parser
errorReporter(TfStringPrintf("Mismatched ( ) for attribute "
"of type %s.", valueTypeName.c_str()));
return;
// CODE_COVERAGE_ON
}
--tupleDepth;
if (tupleDimensions.d[tupleDepth] != 0) {
errorReporter(TfStringPrintf("Tuple dimensions error for "
"attribute of type %s.",
valueTypeName.c_str()));
return;
}
if (tupleDepth > 0) {
--tupleDimensions.d[tupleDepth-1];
}
// If we're working on a shaped type and we popped out of a tuple,
// add another element to the working shape here.
if (tupleDepth == 0 && dim)
++workingShape[dim-1];
}
void
Sdf_ParserValueContext::StartRecordingString()
{
_needComma = false;
_isRecordingString = true;
_recordedString.clear();
}
void
Sdf_ParserValueContext::StopRecordingString()
{
_isRecordingString = false;
}
bool
Sdf_ParserValueContext::IsRecordingString() const
{
return _isRecordingString;
}
std::string
Sdf_ParserValueContext::GetRecordedString() const
{
return _recordedString;
}
void
Sdf_ParserValueContext::SetRecordedString(const std::string &text)
{
_recordedString = text;
}
PXR_NAMESPACE_CLOSE_SCOPE