diff -r 6988973515d2 -r ca23936b455b src/parser.cpp --- a/src/parser.cpp Wed May 25 20:36:34 2022 +0300 +++ b/src/parser.cpp Mon Jun 06 22:01:22 2022 +0300 @@ -18,14 +18,7 @@ #include "model.h" #include "parser.h" -#include "linetypes/conditionaledge.h" -#include "linetypes/edge.h" -#include "linetypes/errorline.h" -#include "linetypes/metacommand.h" -#include "linetypes/object.h" -#include "linetypes/quadrilateral.h" -#include "linetypes/subfilereference.h" -#include "linetypes/triangle.h" +#include "ldrawalgorithm.h" struct BodyParseError { @@ -64,11 +57,12 @@ invertNext = true; continue; } - model.append(parseFromString(line)); + ModelElement element = parseLDrawLine(line); if (invertNext) { - model[model.size() - 1]->invert(nullptr); + element = inverted(element); } + model.append(element); invertNext = false; } } @@ -140,19 +134,13 @@ return result; } -static std::unique_ptr parseType0Line( - const QString& line, - const QStringList& tokens) +static Comment parseType0Line(const QString& line) { - Q_UNUSED(tokens) - return std::make_unique(line.mid(1).trimmed()); + return {line.mid(1).trimmed()}; } -static std::unique_ptr parseType1Line( - const QString& line, - const QStringList& tokens) +static Colored parseType1Line(const QStringList& tokens) { - Q_UNUSED(line) constexpr int colorPosition = 1; constexpr int positionPosition = 2; // 2..4 constexpr int transformPosition = 5; // 5..13 @@ -164,15 +152,18 @@ const ldraw::Color color = colorFromString(tokens[colorPosition]); const glm::mat4 transform = matrixFromStrings(tokens, transformPosition, positionPosition); const QString& name = tokens[namePosition]; - return std::make_unique(transform, name, color); + return Colored{ + { + .name = name, + .transformation = transform, + }, + color, + }; } -template -static std::unique_ptr parsePolygon( - const QString& line, - const QStringList& tokens) +template +static auto parsePolygon(const QStringList& tokens) { - Q_UNUSED(line) constexpr int colorPosition = 1; auto vertexPosition = [](int n) { return 2 + 3*n; }; if (tokens.size() != 2 + 3 * NumVertices) @@ -185,10 +176,10 @@ { vertices[unsigned_cast(i)] = vertexFromStrings(tokens, vertexPosition(i)); } - return std::make_unique(vertices, color); + return std::make_pair(vertices, color); } -std::unique_ptr Parser::parseFromString(QString line) +ModelElement parseLDrawLine(QString line) { line = line.trimmed(); try @@ -196,7 +187,7 @@ const QStringList tokens = line.split(QRegExp{R"(\s+)"}); if (tokens.empty() or tokens == QStringList{{""}}) { - return std::make_unique(); + return Empty{}; } bool ok_code; const int code = tokens[0].toInt(&ok_code); @@ -207,23 +198,38 @@ switch (code) { case 0: - return parseType0Line(line, tokens); + return parseType0Line(line); case 1: - return parseType1Line(line, tokens); + return parseType1Line(tokens); case 2: - return parsePolygon(line, tokens); + { + const auto pair = parsePolygon<2>(tokens); + return Colored{{pair.first[0], pair.first[1]}, pair.second}; + } case 3: - return parsePolygon(line, tokens); + { + const auto pair = parsePolygon<3>(tokens); + return Colored{{pair.first[0], pair.first[1], pair.first[2]}, pair.second + }; + } case 4: - return parsePolygon(line, tokens); + { + const auto pair = parsePolygon<4>(tokens); + const Quadrilateral quad{pair.first[0], pair.first[1], pair.first[2], pair.first[3]}; + return Colored{quad, pair.second}; + } case 5: - return parsePolygon(line, tokens); + { + const auto pair = parsePolygon<4>(tokens); + const ConditionalEdge cedge{pair.first[0], pair.first[1], pair.first[2], pair.first[3]}; + return Colored{cedge, pair.second}; + } default: throw BodyParseError{utility::format("bad line type '%1'", code)}; } } catch(const BodyParseError& error) { - return std::make_unique(line, error.message); + return ParseError{line}; } }