Thu, 03 Oct 2019 23:44:28 +0300
language support
3 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 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 "model.h" | |
20 | #include "parser.h" | |
21 | #include "objecttypes/comment.h" | |
22 | #include "objecttypes/conditionaledge.h" | |
23 | #include "objecttypes/edge.h" | |
24 | #include "objecttypes/errorline.h" | |
25 | #include "objecttypes/modelobject.h" | |
26 | #include "objecttypes/polygon.h" | |
27 | #include "objecttypes/subfilereference.h" | |
28 | ||
29 | /* | |
30 | * Constructs an LDraw parser | |
31 | */ | |
32 | Parser::Parser(QIODevice& device, QObject* parent) : | |
33 | QObject {parent}, | |
34 | device {device} {} | |
35 | ||
36 | /* | |
37 | * Reads a single line from the device. | |
38 | */ | |
39 | QString Parser::readLine() | |
40 | { | |
41 | return QString::fromUtf8(this->device.readLine()).trimmed(); | |
42 | } | |
43 | ||
5 | 44 | static const QMap<QString, decltype(LDHeader::type)> typeStrings { |
3 | 45 | {"Part", LDHeader::Part}, |
46 | {"Subpart", LDHeader::Subpart}, | |
47 | {"Shortcut", LDHeader::Shortcut}, | |
48 | {"Primitive", LDHeader::Primitive}, | |
49 | {"8_Primitive", LDHeader::Primitive_8}, | |
50 | {"48_Primitive", LDHeader::Primitive_48}, | |
51 | {"Configuration", LDHeader::Configuration}, | |
52 | }; | |
53 | ||
54 | /* | |
55 | * Parses a single line of the header. | |
56 | * Possible parse results: | |
57 | * · ParseSuccess: the header line was parsed successfully. | |
58 | * · ParseFailure: the header line was parsed incorrectly and needs to be handled otherwise. | |
59 | * · StopParsing: the line does not belong in the header and header parsing needs to stop. | |
60 | */ | |
61 | Parser::HeaderParseResult Parser::parseHeaderLine( | |
62 | LDHeader& header, | |
63 | Winding& winding, | |
64 | const QString& line | |
65 | ) { | |
66 | if (line.isEmpty()) | |
67 | { | |
68 | return ParseSuccess; | |
69 | } | |
70 | else if (not line.startsWith("0") or line.startsWith("0 //")) | |
71 | { | |
72 | return StopParsing; | |
73 | } | |
74 | else if (line.startsWith("0 !LDRAW_ORG ")) | |
75 | { | |
76 | QStringList tokens = line | |
77 | .mid(strlen("0 !LDRAW_ORG ")) | |
78 | .split(" ", QString::SkipEmptyParts); | |
79 | if (not tokens.isEmpty()) | |
80 | { | |
81 | QString partTypeString = tokens[0]; | |
82 | // Anything that enters LDForge becomes unofficial in any case if saved. | |
83 | // Therefore we don't need to give the Unofficial type any special | |
84 | // consideration. | |
85 | if (partTypeString.startsWith("Unofficial_")) | |
86 | partTypeString = partTypeString.mid(strlen("Unofficial_")); | |
5 | 87 | header.type = typeStrings.value(partTypeString, LDHeader::Part); |
3 | 88 | header.qualfiers = 0; |
89 | if (tokens.contains("Alias")) | |
90 | header.qualfiers |= LDHeader::Alias; | |
91 | if (tokens.contains("Physical_Color")) | |
92 | header.qualfiers |= LDHeader::Physical_Color; | |
93 | if (tokens.contains("Flexible_Section")) | |
94 | header.qualfiers |= LDHeader::Flexible_Section; | |
95 | return ParseSuccess; | |
96 | } | |
97 | else | |
98 | { | |
99 | return ParseFailure; | |
100 | } | |
101 | } | |
102 | else if (line == "0 BFC CERTIFY CCW") | |
103 | { | |
104 | winding = CounterClockwise; | |
105 | return ParseSuccess; | |
106 | } | |
107 | else if (line == "0 BFC CERTIFY CW") | |
108 | { | |
109 | winding = Clockwise; | |
110 | return ParseSuccess; | |
111 | } | |
112 | else if (line == "0 BFC NOCERTIFY") | |
113 | { | |
114 | winding = NoWinding; | |
115 | return ParseSuccess; | |
116 | } | |
117 | else if (line.startsWith("0 !HISTORY ")) | |
118 | { | |
119 | static const QRegExp historyRegexp { | |
120 | R"(0 !HISTORY\s+(\d{4}-\d{2}-\d{2})\s+)" | |
121 | R"((\{[^}]+|\[[^]]+)[\]}]\s+(.+))" | |
122 | }; | |
123 | if (historyRegexp.exactMatch(line)) | |
124 | { | |
125 | QString dateString = historyRegexp.capturedTexts().value(1); | |
126 | QString authorWithPrefix = historyRegexp.capturedTexts().value(2); | |
127 | QString description = historyRegexp.capturedTexts().value(3); | |
128 | LDHeader::HistoryEntry historyEntry; | |
129 | historyEntry.date = QDate::fromString(dateString, Qt::ISODate); | |
130 | historyEntry.description = description; | |
131 | ||
132 | if (authorWithPrefix[0] == '{') | |
133 | historyEntry.author = authorWithPrefix + "}"; | |
134 | else | |
135 | historyEntry.author = authorWithPrefix.mid(1); | |
136 | ||
137 | header.history.append(historyEntry); | |
138 | return ParseSuccess; | |
139 | } | |
140 | else | |
141 | { | |
142 | return ParseFailure; | |
143 | } | |
144 | } | |
145 | else if (line.startsWith("0 Author: ")) | |
146 | { | |
147 | header.author = line.mid(strlen("0 Author: ")); | |
148 | return ParseSuccess; | |
149 | } | |
150 | else if (line.startsWith("0 Name: ")) | |
151 | { | |
152 | header.name = line.mid(strlen("0 Name: ")); | |
153 | return ParseSuccess; | |
154 | } | |
155 | else if (line.startsWith("0 !HELP ")) | |
156 | { | |
157 | if (not header.help.isEmpty()) | |
158 | header.help += "\n"; | |
159 | header.help += line.mid(strlen("0 !HELP ")); | |
160 | return ParseSuccess; | |
161 | } | |
162 | else if (line.startsWith("0 !KEYWORDS ")) | |
163 | { | |
164 | if (not header.keywords.isEmpty()) | |
165 | header.keywords += "\n"; | |
166 | header.keywords += line.mid(strlen("0 !KEYWORDS ")); | |
167 | return ParseSuccess; | |
168 | } | |
169 | else if (line.startsWith("0 !CATEGORY ")) | |
170 | { | |
171 | header.category = line.mid(strlen("0 !CATEGORY ")); | |
172 | return ParseSuccess; | |
173 | } | |
174 | else if (line.startsWith("0 !CMDLINE ")) | |
175 | { | |
176 | header.cmdline = line.mid(strlen("0 !CMDLINE ")); | |
177 | return ParseSuccess; | |
178 | } | |
179 | else if (line.startsWith("0 !LICENSE Redistributable under CCAL version 2.0")) | |
180 | { | |
181 | header.license = LDHeader::CaLicense; | |
182 | return ParseSuccess; | |
183 | } | |
184 | else if (line.startsWith("0 !LICENSE Not redistributable")) | |
185 | { | |
186 | header.license = LDHeader::NonCaLicense; | |
187 | return ParseSuccess; | |
188 | } | |
189 | else | |
190 | { | |
191 | return ParseFailure; | |
192 | } | |
193 | } | |
194 | ||
195 | /* | |
196 | * Parses the header from the device given at construction and returns | |
197 | * the resulting header structure. | |
198 | */ | |
199 | LDHeader Parser::parseHeader(Winding& winding) | |
200 | { | |
201 | LDHeader header = {}; | |
202 | if (not this->device.atEnd()) | |
203 | { | |
204 | // Parse the description | |
205 | QString descriptionLine = this->readLine(); | |
206 | if (descriptionLine.startsWith("0 ")) | |
207 | { | |
208 | header.description = descriptionLine.mid(strlen("0 ")).trimmed(); | |
209 | // Parse the rest of the header | |
210 | while (not this->device.atEnd()) | |
211 | { | |
212 | const QString& line = this->readLine(); | |
213 | auto result = parseHeaderLine(header, winding, line); | |
214 | if (result == ParseFailure) | |
215 | { | |
216 | // Failed to parse this header line, add it as a comment into the body later. | |
217 | this->bag.append(line); | |
218 | } | |
219 | else if (result == StopParsing) | |
220 | { | |
221 | // Header parsing stops, add this line to the body. | |
222 | this->bag.append(line); | |
223 | break; | |
224 | } | |
225 | } | |
226 | } | |
227 | else | |
228 | { | |
229 | this->bag.append(descriptionLine); | |
230 | } | |
231 | } | |
232 | return header; | |
233 | } | |
234 | ||
235 | /** | |
236 | * @brief Parses the model body into the given model. | |
237 | * @param editor Handle to model edit context | |
238 | */ | |
239 | void Parser::parseBody(Model::EditContext& editor) | |
240 | { | |
241 | bool invertNext = false; | |
242 | while (not this->device.atEnd()) | |
243 | this->bag.append(this->readLine()); | |
244 | for (const QString& line : this->bag) | |
245 | { | |
246 | if (line == "0 BFC INVERTNEXT" or line == "0 BFC CERTIFY INVERTNEXT") | |
247 | { | |
248 | invertNext = true; | |
249 | continue; | |
250 | } | |
251 | modelobjects::BaseObject* object = parseFromString(editor, line); | |
252 | if (invertNext) | |
253 | { | |
254 | editor.setObjectProperty(object, modelobjects::Property::IsInverted, true); | |
255 | } | |
256 | invertNext = false; | |
257 | } | |
258 | } | |
259 | ||
4
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
260 | namespace |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
261 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
262 | namespace regexes |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
263 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
264 | static const QRegExp comment {R"(^\s*0\s*\/\/\s*(.+)$)"}; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
265 | static const QRegExp metacommand {R"(^\s*0\s*(.+)$)"}; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
266 | static const QRegExp edgeline |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
267 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
268 | R"(^\s*2)" // starting 2-token |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
269 | R"(\s+(\d+))" // colour |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
270 | R"(((?:\s+[^\s]+){3}))" // 1st vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
271 | R"(((?:\s+[^\s]+){3}))" // 2nd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
272 | R"(\s*$)" // end |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
273 | }; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
274 | static const QRegExp triangle |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
275 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
276 | R"(^\s*3)" // starting 3-token |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
277 | R"(\s+(\d+))" // colour |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
278 | R"(((?:\s+[^\s]+){3}))" // 1st vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
279 | R"(((?:\s+[^\s]+){3}))" // 2nd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
280 | R"(((?:\s+[^\s]+){3}))" // 3rd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
281 | R"(\s*$)" // end |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
282 | }; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
283 | static const QRegExp quadrilateral |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
284 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
285 | R"(^\s*4)" // starting 4-token |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
286 | R"(\s+(\d+))" // colour |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
287 | R"(((?:\s+[^\s]+){3}))" // 1st vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
288 | R"(((?:\s+[^\s]+){3}))" // 2nd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
289 | R"(((?:\s+[^\s]+){3}))" // 3rd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
290 | R"(((?:\s+[^\s]+){3}))" // 4th vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
291 | R"(\s*$)" // end |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
292 | }; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
293 | static const QRegExp conditionaledge |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
294 | { |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
295 | R"(^\s*5)" // starting 5-token |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
296 | R"(\s+(\d+))" // colour |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
297 | R"(((?:\s+[^\s]+){3}))" // 1st vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
298 | R"(((?:\s+[^\s]+){3}))" // 2nd vertex |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
299 | R"(((?:\s+[^\s]+){3}))" // 1st control point |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
300 | R"(((?:\s+[^\s]+){3}))" // 2nd control point |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
301 | R"(\s*$)" // end |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
302 | }; |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
303 | } |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
304 | } |
68988ebc2a68
added regular expressions for the parser
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
305 | |
5 | 306 | static Vertex vertexFromString(const QString& vertex_string) |
307 | { | |
308 | static const QRegExp pattern {R"(^\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*$)"}; | |
309 | const bool succeeded = pattern.exactMatch(vertex_string); | |
310 | if (succeeded) | |
311 | { | |
312 | const float x = pattern.cap(1).toFloat(nullptr); | |
313 | const float y = pattern.cap(2).toFloat(nullptr); | |
314 | const float z = pattern.cap(3).toFloat(nullptr); | |
315 | return {x, y, z}; | |
316 | } | |
317 | else | |
318 | { | |
319 | return {}; | |
320 | } | |
321 | } | |
322 | ||
323 | static modelobjects::Edge* parseEdgeline( | |
324 | Model::EditContext& editor, | |
325 | const QString& line) | |
326 | { | |
327 | const bool succeeded = regexes::edgeline.exactMatch(line); | |
328 | if (succeeded) | |
329 | { | |
330 | const Color colour = {regexes::edgeline.cap(1).toInt(nullptr)}; | |
331 | const Vertex v_1 = vertexFromString(regexes::edgeline.cap(2)); | |
332 | const Vertex v_2 = vertexFromString(regexes::edgeline.cap(3)); | |
333 | return editor.append<modelobjects::Edge>(v_1, v_2, colour); | |
334 | } | |
335 | else | |
336 | { | |
337 | return nullptr; | |
338 | } | |
339 | } | |
340 | ||
3 | 341 | modelobjects::BaseObject* Parser::parseFromString( |
342 | Model::EditContext& editor, | |
343 | const QString& line) | |
344 | { | |
5 | 345 | modelobjects::Edge* edge = parseEdgeline(editor, line); |
346 | if (edge) | |
347 | { | |
348 | return edge; | |
349 | } | |
350 | return editor.append<modelobjects::ErrorLine>(line); | |
3 | 351 | } |