|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2017 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "parser.h" |
|
20 #include "lddocument.h" |
|
21 #include "linetypes/comment.h" |
|
22 #include "linetypes/conditionaledge.h" |
|
23 #include "linetypes/edgeline.h" |
|
24 #include "linetypes/empty.h" |
|
25 #include "linetypes/quadrilateral.h" |
|
26 #include "linetypes/triangle.h" |
|
27 |
|
28 Parser::Parser(QIODevice& device, QObject* parent) : |
|
29 QObject {parent}, |
|
30 device {device} {} |
|
31 |
|
32 QString Parser::readLine() |
|
33 { |
|
34 return QString::fromUtf8(this->device.readLine()).simplified(); |
|
35 } |
|
36 |
|
37 Parser::HeaderParseResult Parser::parseHeaderLine(LDHeader& header, const QString& line) |
|
38 { |
|
39 if (line.isEmpty()) |
|
40 { |
|
41 return ParseSuccess; |
|
42 } |
|
43 else if (not line.startsWith("0") or line.startsWith("0 //")) |
|
44 { |
|
45 return StopParsing; |
|
46 } |
|
47 else if (line.startsWith("0 !LDRAW_ORG ")) |
|
48 { |
|
49 QStringList tokens = line |
|
50 .mid(strlen("0 !LDRAW_ORG ")) |
|
51 .split(" ", QString::SkipEmptyParts); |
|
52 |
|
53 if (not tokens.isEmpty()) |
|
54 { |
|
55 static const QMap<QString, decltype(LDHeader::type)> typeStrings { |
|
56 {"Part", LDHeader::Part}, |
|
57 {"Subpart", LDHeader::Subpart}, |
|
58 {"Shortcut", LDHeader::Shortcut}, |
|
59 {"Primitive", LDHeader::Primitive}, |
|
60 {"8_Primitive", LDHeader::Primitive_8}, |
|
61 {"48_Primitive", LDHeader::Primitive_48}, |
|
62 {"Configuration", LDHeader::Configuration}, |
|
63 }; |
|
64 QString partTypeString = tokens[0]; |
|
65 // Anything that enters LDForge becomes unofficial in any case if saved. |
|
66 // Therefore we don't need to give the Unofficial type any special |
|
67 // consideration. |
|
68 if (partTypeString.startsWith("Unofficial_")) |
|
69 partTypeString = partTypeString.mid(strlen("Unofficial_")); |
|
70 header.type = typeStrings.value(partTypeString, LDHeader::Part); |
|
71 header.qualfiers = 0; |
|
72 if (tokens.contains("Alias")) |
|
73 header.qualfiers |= LDHeader::Alias; |
|
74 if (tokens.contains("Physical_Color")) |
|
75 header.qualfiers |= LDHeader::Physical_Color; |
|
76 if (tokens.contains("Flexible_Section")) |
|
77 header.qualfiers |= LDHeader::Flexible_Section; |
|
78 return ParseSuccess; |
|
79 } |
|
80 else |
|
81 { |
|
82 return ParseFailure; |
|
83 } |
|
84 } |
|
85 else if (line == "0 BFC CERTIFY CCW") |
|
86 { |
|
87 header.winding = LDHeader::CounterClockwise; |
|
88 return ParseSuccess; |
|
89 } |
|
90 else if (line == "0 BFC CERTIFY CW") |
|
91 { |
|
92 header.winding = LDHeader::Clockwise; |
|
93 return ParseSuccess; |
|
94 } |
|
95 else if (line == "0 BFC NOCERTIFY") |
|
96 { |
|
97 header.winding = LDHeader::NoWinding; |
|
98 return ParseSuccess; |
|
99 } |
|
100 else if (line.startsWith("0 !HISTORY ")) |
|
101 { |
|
102 static const QRegExp historyRegexp { |
|
103 R"(0 !HISTORY\s+(\d{4}-\d{2}-\d{2})\s+)" |
|
104 R"((\{[^}]+|\[[^]]+)[\]}]\s+(.+))" |
|
105 }; |
|
106 if (historyRegexp.exactMatch(line)) |
|
107 { |
|
108 QString dateString = historyRegexp.capturedTexts().value(0); |
|
109 QString authorWithPrefix = historyRegexp.capturedTexts().value(1); |
|
110 QString description = historyRegexp.capturedTexts().value(2); |
|
111 LDHeader::HistoryEntry historyEntry; |
|
112 historyEntry.date = QDate::fromString(dateString, Qt::ISODate); |
|
113 historyEntry.author = authorWithPrefix.mid(1); |
|
114 historyEntry.description = description; |
|
115 |
|
116 if (authorWithPrefix[0] == '{') |
|
117 historyEntry.authorType = LDHeader::HistoryEntry::RealName; |
|
118 else |
|
119 historyEntry.authorType = LDHeader::HistoryEntry::UserName; |
|
120 |
|
121 header.history.append(historyEntry); |
|
122 return ParseSuccess; |
|
123 } |
|
124 else |
|
125 { |
|
126 return ParseFailure; |
|
127 } |
|
128 } |
|
129 else if (line.startsWith("0 Author: ")) |
|
130 { |
|
131 static const QRegExp authorRegexp {R"(0 Author: ([^[]+)(?: \[([^]]+)\])?)"}; |
|
132 if (authorRegexp.exactMatch(line)) |
|
133 { |
|
134 QStringList tokens = authorRegexp.capturedTexts(); |
|
135 header.author.realName = tokens.value(0); |
|
136 header.author.userName = tokens.value(1); |
|
137 return ParseSuccess; |
|
138 } |
|
139 else |
|
140 { |
|
141 return ParseFailure; |
|
142 } |
|
143 } |
|
144 else if (line.startsWith("0 Name: ")) |
|
145 { |
|
146 header.name = line.mid(strlen("0 Name: ")); |
|
147 return ParseSuccess; |
|
148 } |
|
149 else if (line.startsWith("0 !HELP ")) |
|
150 { |
|
151 header.help.append(line.mid(strlen("0 !HELP "))); |
|
152 return ParseSuccess; |
|
153 } |
|
154 else if (line.startsWith("0 !KEYWORDS ")) |
|
155 { |
|
156 header.keywords.append(line.mid(strlen("0 !KEYWORDS "))); |
|
157 return ParseSuccess; |
|
158 } |
|
159 else if (line.startsWith("0 !CATEGORY ")) |
|
160 { |
|
161 header.category = line.mid(strlen("0 !CATEGORY ")); |
|
162 return ParseSuccess; |
|
163 } |
|
164 else if (line.startsWith("0 !CMDLINE ")) |
|
165 { |
|
166 header.cmdline = line.mid(strlen("0 !CMDLINE ")); |
|
167 return ParseSuccess; |
|
168 } |
|
169 else if (line.startsWith("0 !LICENSE Redistributable under CCAL version 2.0")) |
|
170 { |
|
171 header.license = LDHeader::CaLicense; |
|
172 return ParseSuccess; |
|
173 } |
|
174 else if (line.startsWith("0 !LICENSE Not redistributable")) |
|
175 { |
|
176 header.license = LDHeader::NonCaLicense; |
|
177 return ParseSuccess; |
|
178 } |
|
179 else |
|
180 { |
|
181 return ParseFailure; |
|
182 } |
|
183 } |
|
184 |
|
185 LDHeader Parser::parseHeader() |
|
186 { |
|
187 LDHeader header = {}; |
|
188 |
|
189 if (not this->device.atEnd()) |
|
190 { |
|
191 // Parse the description |
|
192 QString descriptionLine = this->readLine(); |
|
193 if (descriptionLine.startsWith("0 ")) |
|
194 { |
|
195 header.description = descriptionLine.mid(strlen("0 ")).simplified(); |
|
196 |
|
197 // Parse the rest of the header |
|
198 while (not this->device.atEnd()) |
|
199 { |
|
200 const QString& line = this->readLine(); |
|
201 auto result = parseHeaderLine(header, line); |
|
202 |
|
203 if (result == ParseFailure) |
|
204 { |
|
205 this->bag.append(line); |
|
206 } |
|
207 else if (result == StopParsing) |
|
208 { |
|
209 this->bag.append(line); |
|
210 break; |
|
211 } |
|
212 } |
|
213 } |
|
214 else |
|
215 { |
|
216 this->bag.append(descriptionLine); |
|
217 } |
|
218 } |
|
219 |
|
220 return header; |
|
221 } |
|
222 |
|
223 void Parser::parseBody(Model& model) |
|
224 { |
|
225 bool invertNext = false; |
|
226 |
|
227 while (not this->device.atEnd()) |
|
228 this->bag.append(this->readLine()); |
|
229 |
|
230 for (const QString& line : this->bag) |
|
231 { |
|
232 if (line == "0 BFC INVERTNEXT" or line == "0 BFC CERTIFY INVERTNEXT") |
|
233 { |
|
234 invertNext = true; |
|
235 continue; |
|
236 } |
|
237 |
|
238 LDObject* object = parseFromString(model, model.size(), line); |
|
239 |
|
240 /* |
|
241 // Check for parse errors and warn about them |
|
242 if (obj->type() == LDObjectType::Error) |
|
243 { |
|
244 emit parseErrorMessage(format( |
|
245 tr("Couldn't parse line #%1: %2"), |
|
246 progress() + 1, static_cast<LDError*> (obj)->reason())); |
|
247 ++m_warningCount; |
|
248 } |
|
249 */ |
|
250 |
|
251 if (invertNext and object->type() == LDObjectType::SubfileReference) |
|
252 object->setInverted(true); |
|
253 |
|
254 invertNext = false; |
|
255 } |
|
256 } |
|
257 |
|
258 // ============================================================================= |
|
259 // |
|
260 static void CheckTokenCount (const QStringList& tokens, int num) |
|
261 { |
|
262 if (countof(tokens) != num) |
|
263 throw QString (format ("Bad amount of tokens, expected %1, got %2", num, countof(tokens))); |
|
264 } |
|
265 |
|
266 // ============================================================================= |
|
267 // |
|
268 static void CheckTokenNumbers (const QStringList& tokens, int min, int max) |
|
269 { |
|
270 bool ok; |
|
271 QRegExp scientificRegex ("\\-?[0-9]+\\.[0-9]+e\\-[0-9]+"); |
|
272 |
|
273 for (int i = min; i <= max; ++i) |
|
274 { |
|
275 // Check for floating point |
|
276 tokens[i].toDouble (&ok); |
|
277 if (ok) |
|
278 return; |
|
279 |
|
280 // Check hex |
|
281 if (tokens[i].startsWith ("0x")) |
|
282 { |
|
283 tokens[i].mid (2).toInt (&ok, 16); |
|
284 |
|
285 if (ok) |
|
286 return; |
|
287 } |
|
288 |
|
289 // Check scientific notation, e.g. 7.99361e-15 |
|
290 if (scientificRegex.exactMatch (tokens[i])) |
|
291 return; |
|
292 |
|
293 throw QString (format ("Token #%1 was `%2`, expected a number (matched length: %3)", |
|
294 (i + 1), tokens[i], scientificRegex.matchedLength())); |
|
295 } |
|
296 } |
|
297 |
|
298 static Vertex parseVertex(QStringList& tokens, const int n) |
|
299 { |
|
300 return {tokens[n].toDouble(), tokens[n + 1].toDouble(), tokens[n + 2].toDouble()}; |
|
301 } |
|
302 |
|
303 // TODO: rewrite this using regular expressions |
|
304 LDObject* Parser::parseFromString(Model& model, int position, QString line) |
|
305 { |
|
306 if (position == EndOfModel) |
|
307 position = model.size(); |
|
308 |
|
309 try |
|
310 { |
|
311 QStringList tokens = line.split(" ", QString::SkipEmptyParts); |
|
312 |
|
313 if (tokens.isEmpty()) |
|
314 { |
|
315 // Line was empty, or only consisted of whitespace |
|
316 return model.emplaceAt<LDEmpty>(position); |
|
317 } |
|
318 |
|
319 if (countof(tokens[0]) != 1 or not tokens[0][0].isDigit()) |
|
320 throw QString ("Illogical line code"); |
|
321 |
|
322 int num = tokens[0][0].digitValue(); |
|
323 |
|
324 switch (num) |
|
325 { |
|
326 case 0: |
|
327 { |
|
328 // Comment |
|
329 QString commentText = line.mid(line.indexOf("0") + 2); |
|
330 QString commentTextSimplified = commentText.simplified(); |
|
331 |
|
332 // Handle BFC statements |
|
333 if (countof(tokens) > 2 and tokens[1] == "BFC") |
|
334 { |
|
335 for (BfcStatement statement : iterateEnum<BfcStatement>()) |
|
336 { |
|
337 if (commentTextSimplified == format("BFC %1", LDBfc::statementToString(statement))) |
|
338 return model.emplaceAt<LDBfc>(position, statement); |
|
339 } |
|
340 |
|
341 // handle MLCAD nonsense |
|
342 if (commentTextSimplified == "BFC CERTIFY CLIP") |
|
343 return model.emplaceAt<LDBfc>(position, BfcStatement::Clip); |
|
344 else if (commentTextSimplified == "BFC CERTIFY NOCLIP") |
|
345 return model.emplaceAt<LDBfc>(position, BfcStatement::NoClip); |
|
346 } |
|
347 |
|
348 if (countof(tokens) > 2 and tokens[1] == "!LDFORGE") |
|
349 { |
|
350 // Handle LDForge-specific types, they're embedded into comments too |
|
351 if (tokens[2] == "BEZIER_CURVE") |
|
352 { |
|
353 CheckTokenCount (tokens, 16); |
|
354 CheckTokenNumbers (tokens, 3, 15); |
|
355 LDBezierCurve* obj = model.emplaceAt<LDBezierCurve>(position); |
|
356 obj->setColor(tokens[3].toInt(nullptr, 0)); |
|
357 |
|
358 for (int i = 0; i < 4; ++i) |
|
359 obj->setVertex (i, parseVertex (tokens, 4 + (i * 3))); |
|
360 |
|
361 return obj; |
|
362 } |
|
363 } |
|
364 |
|
365 // Just a regular comment: |
|
366 return model.emplaceAt<LDComment>(position, commentText); |
|
367 } |
|
368 |
|
369 case 1: |
|
370 { |
|
371 // Subfile |
|
372 CheckTokenCount (tokens, 15); |
|
373 CheckTokenNumbers (tokens, 1, 13); |
|
374 |
|
375 Vertex referncePosition = parseVertex (tokens, 2); // 2 - 4 |
|
376 Matrix transform; |
|
377 |
|
378 for (int i = 0; i < 9; ++i) |
|
379 transform.value(i) = tokens[i + 5].toDouble(); // 5 - 13 |
|
380 |
|
381 LDSubfileReference* obj = model.emplaceAt<LDSubfileReference>(position, tokens[14], transform, referncePosition); |
|
382 obj->setColor (tokens[1].toInt(nullptr, 0)); |
|
383 return obj; |
|
384 } |
|
385 |
|
386 case 2: |
|
387 { |
|
388 CheckTokenCount (tokens, 8); |
|
389 CheckTokenNumbers (tokens, 1, 7); |
|
390 |
|
391 // Line |
|
392 LDEdgeLine* obj = model.emplaceAt<LDEdgeLine>(position); |
|
393 obj->setColor (tokens[1].toInt(nullptr, 0)); |
|
394 |
|
395 for (int i = 0; i < 2; ++i) |
|
396 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 7 |
|
397 |
|
398 return obj; |
|
399 } |
|
400 |
|
401 case 3: |
|
402 { |
|
403 CheckTokenCount (tokens, 11); |
|
404 CheckTokenNumbers (tokens, 1, 10); |
|
405 |
|
406 // Triangle |
|
407 LDTriangle* obj = model.emplaceAt<LDTriangle>(position); |
|
408 obj->setColor (tokens[1].toInt(nullptr, 0)); |
|
409 |
|
410 for (int i = 0; i < 3; ++i) |
|
411 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 10 |
|
412 |
|
413 return obj; |
|
414 } |
|
415 |
|
416 case 4: |
|
417 case 5: |
|
418 { |
|
419 CheckTokenCount (tokens, 14); |
|
420 CheckTokenNumbers (tokens, 1, 13); |
|
421 |
|
422 // Quadrilateral / Conditional line |
|
423 LDObject* obj; |
|
424 |
|
425 if (num == 4) |
|
426 obj = model.emplaceAt<LDQuadrilateral>(position); |
|
427 else |
|
428 obj = model.emplaceAt<LDConditionalEdge>(position); |
|
429 |
|
430 obj->setColor (tokens[1].toInt(nullptr, 0)); |
|
431 |
|
432 for (int i = 0; i < 4; ++i) |
|
433 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 13 |
|
434 |
|
435 return obj; |
|
436 } |
|
437 |
|
438 default: |
|
439 throw QString {"Unknown line code number"}; |
|
440 } |
|
441 } |
|
442 catch (QString& errorMessage) |
|
443 { |
|
444 // Strange line we couldn't parse |
|
445 return model.emplaceAt<LDError>(position, line, errorMessage); |
|
446 } |
|
447 } |