src/parser.h

Sat, 08 Apr 2023 12:55:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 12:55:11 +0300
changeset 343
4a82990affd5
parent 333
07e65a4c6611
child 358
ef90ed0a5720
permissions
-rw-r--r--

Fix BFC formatting not working due to being evaluated after comment format

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include "src/basics.h"
#include "src/model.h"

struct TextRange
{
	int start;
	int length;
};


enum class Attribute {
	LineType,
	Color,
	X1,
	Y1,
	Z1,
	X2,
	Y2,
	Z2,
	X3,
	Y3,
	Z3,
	X4,
	Y4,
	Z4,
	Name,
	Text
};

template<typename T, Attribute... Attribs>
struct LineType
{
	static constexpr Attribute attributes[] = {Attribs...};
	QString content;
	TextRange positions[countof(attributes)];
	T value;
};

using LineType0 = LineType<Comment, Attribute::Text>;
using LineType1 = LineType<Colored<SubfileReference>,
	Attribute::LineType,
	Attribute::Color,
	Attribute::X1,
	Attribute::Y1,
	Attribute::Z1,
	Attribute::X2,
	Attribute::Y2,
	Attribute::Z2,
	Attribute::X3,
	Attribute::Y3,
	Attribute::Z3,
	Attribute::X4,
	Attribute::Y4,
	Attribute::Z4,
	Attribute::Name>;
using LineType2 = LineType<Colored<LineSegment>,
	Attribute::LineType,
	Attribute::Color,
	Attribute::X1,
	Attribute::Y1,
	Attribute::Z1,
	Attribute::X2,
	Attribute::Y2,
	Attribute::Z2>;
using LineType3 = LineType<Colored<Triangle>,
	Attribute::LineType,
	Attribute::Color,
	Attribute::X1,
	Attribute::Y1,
	Attribute::Z1,
	Attribute::X2,
	Attribute::Y2,
	Attribute::Z2,
	Attribute::X3,
	Attribute::Y3,
	Attribute::Z3>;
using LineType4 = LineType<Colored<Quadrilateral>,
	Attribute::LineType,
	Attribute::Color,
	Attribute::X1,
	Attribute::Y1,
	Attribute::Z1,
	Attribute::X2,
	Attribute::Y2,
	Attribute::Z2,
	Attribute::X3,
	Attribute::Y3,
	Attribute::Z3,
	Attribute::X4,
	Attribute::Y4,
	Attribute::Z4>;
using LineType5 = LineType<Colored<ConditionalEdge>,
	Attribute::LineType,
	Attribute::Color,
	Attribute::X1,
	Attribute::Y1,
	Attribute::Z1,
	Attribute::X2,
	Attribute::Y2,
	Attribute::Z2,
	Attribute::X3,
	Attribute::Y3,
	Attribute::Z3,
	Attribute::X4,
	Attribute::Y4,
	Attribute::Z4>;

template<typename T, Attribute Attrib>
constexpr int findAttributeIndex()
{
	constexpr auto it = std::find(
		std::begin(T::attributes),
		std::end(T::attributes),
		Attrib
	);
	static_assert(it != std::end(T::attributes), "Attribute not found");
	return it - std::begin(T::attributes);
}

template<typename T, Attribute Attrib>
constexpr int attribIndex = findAttributeIndex<T, Attrib>();
static_assert(attribIndex<LineType3, Attribute::X1> == 2);
using ParsedLine = std::variant<LineType0, LineType1, LineType2, LineType3, LineType4, LineType5>;
opt<ParsedLine> parse(const QString& line);

mercurial