src/parser.cpp

changeset 5
593a658cba8e
parent 4
68988ebc2a68
child 8
44679e468ba9
--- a/src/parser.cpp	Mon Sep 23 14:06:36 2019 +0300
+++ b/src/parser.cpp	Thu Oct 03 11:45:44 2019 +0300
@@ -41,7 +41,7 @@
 	return QString::fromUtf8(this->device.readLine()).trimmed();
 }
 
-const QMap<QString, decltype(LDHeader::type)> Parser::typeStrings {
+static const QMap<QString, decltype(LDHeader::type)> typeStrings {
 	{"Part", LDHeader::Part},
 	{"Subpart", LDHeader::Subpart},
 	{"Shortcut", LDHeader::Shortcut},
@@ -76,7 +76,6 @@
 		QStringList tokens = line
 			.mid(strlen("0 !LDRAW_ORG "))
 			.split(" ", QString::SkipEmptyParts);
-
 		if (not tokens.isEmpty())
 		{
 			QString partTypeString = tokens[0];
@@ -85,7 +84,7 @@
 			// consideration.
 			if (partTypeString.startsWith("Unofficial_"))
 				partTypeString = partTypeString.mid(strlen("Unofficial_"));
-			header.type = Parser::typeStrings.value(partTypeString, LDHeader::Part);
+			header.type = typeStrings.value(partTypeString, LDHeader::Part);
 			header.qualfiers = 0;
 			if (tokens.contains("Alias"))
 				header.qualfiers |= LDHeader::Alias;
@@ -200,7 +199,6 @@
 LDHeader Parser::parseHeader(Winding& winding)
 {
 	LDHeader header = {};
-
 	if (not this->device.atEnd())
 	{
 		// Parse the description
@@ -208,13 +206,11 @@
 		if (descriptionLine.startsWith("0 "))
 		{
 			header.description = descriptionLine.mid(strlen("0 ")).trimmed();
-
 			// Parse the rest of the header
 			while (not this->device.atEnd())
 			{
 				const QString& line = this->readLine();
 				auto result = parseHeaderLine(header, winding, line);
-
 				if (result == ParseFailure)
 				{
 					// Failed to parse this header line, add it as a comment into the body later.
@@ -233,7 +229,6 @@
 			this->bag.append(descriptionLine);
 		}
 	}
-
 	return header;
 }
 
@@ -308,9 +303,49 @@
 	}
 }
 
+static Vertex vertexFromString(const QString& vertex_string)
+{
+	static const QRegExp pattern {R"(^\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*$)"};
+	const bool succeeded = pattern.exactMatch(vertex_string);
+	if (succeeded)
+	{
+		const float x = pattern.cap(1).toFloat(nullptr);
+		const float y = pattern.cap(2).toFloat(nullptr);
+		const float z = pattern.cap(3).toFloat(nullptr);
+		return {x, y, z};
+	}
+	else
+	{
+		return {};
+	}
+}
+
+static modelobjects::Edge* parseEdgeline(
+	Model::EditContext& editor,
+	const QString& line)
+{
+	const bool succeeded = regexes::edgeline.exactMatch(line);
+	if (succeeded)
+	{
+		const Color colour = {regexes::edgeline.cap(1).toInt(nullptr)};
+		const Vertex v_1 = vertexFromString(regexes::edgeline.cap(2));
+		const Vertex v_2 = vertexFromString(regexes::edgeline.cap(3));
+		return editor.append<modelobjects::Edge>(v_1, v_2, colour);
+	}
+	else
+	{
+		return nullptr;
+	}
+}
+
 modelobjects::BaseObject* Parser::parseFromString(
 	Model::EditContext& editor,
 	const QString& line)
 {
-	return editor.append<modelobjects::Comment>(line);
+	modelobjects::Edge* edge = parseEdgeline(editor, line);
+	if (edge)
+	{
+		return edge;
+	}
+	return editor.append<modelobjects::ErrorLine>(line);
 }

mercurial