Simplify parsing (removed header parsing)

Wed, 09 Mar 2022 14:07:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 09 Mar 2022 14:07:58 +0200
changeset 176
cd9d6bf6f649
parent 174
3016b494685c
child 177
f69d53c053df

Simplify parsing (removed header parsing)
Removed ldraw::Comment (MetaCommand handles line type 0)

CMakeLists.txt file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/linetypes/comment.cpp file | annotate | diff | comparison | revisions
src/linetypes/comment.h file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
src/parser.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Wed Mar 09 13:14:40 2022 +0200
+++ b/CMakeLists.txt	Wed Mar 09 14:07:58 2022 +0200
@@ -54,7 +54,6 @@
 	src/gl/gridprogram.cpp
 	src/gl/partrenderer.cpp
 	src/gl/vertexprogram.cpp
-	src/linetypes/comment.cpp
 	src/linetypes/conditionaledge.cpp
 	src/linetypes/edge.cpp
 	src/linetypes/errorline.cpp
@@ -112,7 +111,6 @@
 	src/gl/gridprogram.h
 	src/gl/partrenderer.h
 	src/gl/vertexprogram.h
-	src/linetypes/comment.h
 	src/linetypes/conditionaledge.h
 	src/linetypes/edge.h
 	src/linetypes/errorline.h
--- a/src/documentmanager.cpp	Wed Mar 09 13:14:40 2022 +0200
+++ b/src/documentmanager.cpp	Wed Mar 09 14:07:58 2022 +0200
@@ -22,7 +22,6 @@
 #include <QSaveFile>
 #include "documentmanager.h"
 #include "modeleditor.h"
-#include "linetypes/comment.h"
 #include "linetypes/subfilereference.h"
 #include "parser.h"
 
--- a/src/linetypes/comment.cpp	Wed Mar 09 13:14:40 2022 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-#include <QFont>
-#include "comment.h"
-
-QFont ldraw::Comment::textRepresentationFont() const
-{
-	QFont font;
-	font.setItalic(true);
-	return font;
-}
-
-ldraw::Object::Type ldraw::Comment::typeIdentifier() const
-{
-	return Type::Comment;
-}
-
-QString ldraw::Comment::toLDrawCode() const
-{
-	return ("0 // " + this->storedText).trimmed();
-}
-
--- a/src/linetypes/comment.h	Wed Mar 09 13:14:40 2022 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-#pragma once
-#include "object.h"
-#include "metacommand.h"
-
-namespace ldraw
-{
-	class Comment;
-}
-
-class ldraw::Comment : public MetaCommand
-{
-	using MetaCommand::MetaCommand;
-	QFont textRepresentationFont() const override;
-	Type typeIdentifier() const override;
-	QString toLDrawCode() const override;
-};
--- a/src/parser.cpp	Wed Mar 09 13:14:40 2022 +0200
+++ b/src/parser.cpp	Wed Mar 09 14:07:58 2022 +0200
@@ -18,7 +18,6 @@
 
 #include "model.h"
 #include "parser.h"
-#include "linetypes/comment.h"
 #include "linetypes/conditionaledge.h"
 #include "linetypes/edge.h"
 #include "linetypes/errorline.h"
@@ -48,187 +47,6 @@
 	return QString::fromUtf8(this->device.readLine()).trimmed();
 }
 
-/*
- * Parses a single line of the header.
- * Possible parse results:
- *   · ParseSuccess: the header line was parsed successfully.
- *   · ParseFailure: the header line was parsed incorrectly and needs to be handled otherwise.
- *   · StopParsing: the line does not belong in the header and header parsing needs to stop.
- */
-Parser::HeaderParseResult Parser::parseHeaderLine(
-	LDHeader& header,
-	Winding& winding,
-	const QString& line
-) {
-	if (line.isEmpty())
-	{
-		return ParseSuccess;
-	}
-	else if (not line.startsWith("0") or line.startsWith("0 //"))
-	{
-		return StopParsing;
-	}
-	else if (line.startsWith("0 !LDRAW_ORG "))
-	{
-		QStringList tokens = line
-			.mid(strlen("0 !LDRAW_ORG "))
-			.split(" ", Qt::SkipEmptyParts);
-		if (not tokens.isEmpty())
-		{
-			QString partTypeString = tokens[0];
-			// Anything that enters LDForge becomes unofficial in any case if saved.
-			// Therefore we don't need to give the Unofficial type any special
-			// consideration.
-			if (partTypeString.startsWith("Unofficial_"))
-				partTypeString = partTypeString.mid(strlen("Unofficial_"));
-			header.type = headerTypeFromString(partTypeString);
-			header.qualfiers = {};
-			if (tokens.contains("Alias"))
-				header.qualfiers |= LDHeader::Alias;
-			if (tokens.contains("Physical_Color"))
-				header.qualfiers |= LDHeader::PhysicalColour;
-			if (tokens.contains("Flexible_Section"))
-				header.qualfiers |= LDHeader::FlexibleSection;
-			return ParseSuccess;
-		}
-		else
-		{
-			return ParseFailure;
-		}
-	}
-	else if (line == "0 BFC CERTIFY CCW")
-	{
-		winding = Anticlockwise;
-		return ParseSuccess;
-	}
-	else if (line == "0 BFC CERTIFY CW")
-	{
-		winding = Clockwise;
-		return ParseSuccess;
-	}
-	else if (line == "0 BFC NOCERTIFY")
-	{
-		winding = NoWinding;
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 !HISTORY "))
-	{
-		static const QRegExp historyRegexp {
-			R"(0 !HISTORY\s+(\d{4}-\d{2}-\d{2})\s+)"
-			R"((\{[^}]+|\[[^]]+)[\]}]\s+(.+))"
-		};
-		if (historyRegexp.exactMatch(line))
-		{
-			QString dateString = historyRegexp.capturedTexts().value(1);
-			QString authorWithPrefix = historyRegexp.capturedTexts().value(2);
-			QString description = historyRegexp.capturedTexts().value(3);
-			LDHeader::HistoryEntry historyEntry;
-			historyEntry.date = QDate::fromString(dateString, Qt::ISODate);
-			historyEntry.description = description;
-
-			if (authorWithPrefix[0] == '{')
-				historyEntry.author = authorWithPrefix + "}";
-			else
-				historyEntry.author = authorWithPrefix.mid(1);
-
-			header.history.append(historyEntry);
-			return ParseSuccess;
-		}
-		else
-		{
-			return ParseFailure;
-		}
-	}
-	else if (line.startsWith("0 Author: "))
-	{
-		header.author = line.mid(strlen("0 Author: "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 Name: "))
-	{
-		header.name = line.mid(strlen("0 Name: "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 !HELP "))
-	{
-		if (not header.help.isEmpty())
-			header.help += "\n";
-		header.help += line.mid(strlen("0 !HELP "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 !KEYWORDS "))
-	{
-		if (not header.keywords.isEmpty())
-			header.keywords += "\n";
-		header.keywords += line.mid(strlen("0 !KEYWORDS "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 !CATEGORY "))
-	{
-		header.category = line.mid(strlen("0 !CATEGORY "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith("0 !CMDLINE "))
-	{
-		header.cmdline = line.mid(strlen("0 !CMDLINE "));
-		return ParseSuccess;
-	}
-	else if (line.startsWith(LDHeader::caLicenseString))
-	{
-		header.license = LDHeader::CaLicense;
-		return ParseSuccess;
-	}
-	else if (line.startsWith(LDHeader::nonCaLicenseString))
-	{
-		header.license = LDHeader::NonCaLicense;
-		return ParseSuccess;
-	}
-	else
-	{
-		return ParseFailure;
-	}
-}
-
-/*
- * Parses the header from the device given at construction and returns
- * the resulting header structure.
- */
-LDHeader Parser::parseHeader(Winding& winding)
-{
-	LDHeader header = {};
-	if (not this->device.atEnd())
-	{
-		// Parse the description
-		QString descriptionLine = this->readLine();
-		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.
-					this->bag.append(line);
-				}
-				else if (result == StopParsing)
-				{
-					// Header parsing stops, add this line to the body.
-					this->bag.append(line);
-					break;
-				}
-			}
-		}
-		else
-		{
-			this->bag.append(descriptionLine);
-		}
-	}
-	return header;
-}
-
 /**
  * @brief Parses the model body into the given model.
  * @param editor Handle to model edit context
@@ -237,42 +55,22 @@
 {
 	bool invertNext = false;
 	while (not this->device.atEnd())
-		this->bag.append(this->readLine());
-	for (const QString& line : this->bag)
 	{
 		// Some LDraw parts such as 53588.dat can contain "BFC  INVERTNEXT" with multiple inner whitespaces.
 		// So we need to pass the string through QString::simplified to catch these cases.
-		const QString simplifiedLine = line.simplified();
-		if (simplifiedLine == "0 BFC INVERTNEXT" or simplifiedLine == "0 BFC CERTIFY INVERTNEXT")
+		const QString line = this->readLine().simplified();
+		if (line == "0 BFC INVERTNEXT" or line == "0 BFC CERTIFY INVERTNEXT")
 		{
 			invertNext = true;
 			continue;
 		}
-		std::unique_ptr<ldraw::Object> object = parseFromString(line);
-		model.append(std::move(object));
+		model.append(parseFromString(line));
 		if (invertNext)
 		{
 			model[model.size() - 1]->invert();
 		}
 		invertNext = false;
 	}
-	/*
-	// Test quadrilateral splitting by splitting all the quadrilaterals
-	QVector<ldraw::quadrilateralid_t> quadrilateral_ids;
-	for (int i = 0; i < editor.model().size(); i += 1)
-	{
-		const ldraw::id_t id = editor.model().resolve(editor.model().index(i));
-		const ldraw::quadrilateralid_t quad_id = editor.model().checkType<ldraw::Quadrilateral>(id);
-		if (not(quad_id == ldraw::NULL_ID))
-		{
-			quadrilateral_ids.push_back(quad_id);
-		}
-	}
-	for (const ldraw::quadrilateralid_t id : quadrilateral_ids)
-	{
-		ldraw::splitQuadrilateral(editor, id);
-	}
-	*/
 }
 
 static ldraw::Color colorFromString(const QString& colorString)
@@ -347,15 +145,7 @@
 	const QStringList& tokens)
 {
 	Q_UNUSED(tokens)
-	if (line.startsWith("0 //"))
-	{
-		// lol wut
-		return std::make_unique<ldraw::Comment>(line.mid(std::strlen("0 //")).trimmed());
-	}
-	else
-	{
-		return std::make_unique<ldraw::MetaCommand>(line.mid(1).trimmed());
-	}
+	return std::make_unique<ldraw::MetaCommand>(line.mid(1).trimmed());
 }
 
 static std::unique_ptr<ldraw::SubfileReference> parseType1Line(
--- a/src/parser.h	Wed Mar 09 13:14:40 2022 +0200
+++ b/src/parser.h	Wed Mar 09 14:07:58 2022 +0200
@@ -29,13 +29,9 @@
 public:
 	enum { EndOfModel = -1 };
 	Parser(QIODevice& device, QObject* parent = nullptr);
-	LDHeader parseHeader(Winding& winding);
 	void parseBody(Model &model);
 	static std::unique_ptr<ldraw::Object> parseFromString(QString line);
 private:
-	enum HeaderParseResult {ParseSuccess, ParseFailure, StopParsing};
 	QString readLine();
-	HeaderParseResult parseHeaderLine(LDHeader& header, Winding& winding, const QString& line);
 	QIODevice& device;
-	QStringList bag;
 };

mercurial