Added dynamic color support + text display item
This commit is contained in:
1 parent
e6bbaabde4
commit
09991904e9
13 files changed
+265
-36
No files matched your search
@@ -20,6 +20,19 @@ NumberNode::NumberNode(const std::string &expression) {
|
||||
}
|
||||
}
|
||||
|
||||
/*************************** COLOR NODE *********************************/
|
||||
|
||||
ColorNode::ColorNode(const std::string &expression) {
|
||||
auto value = expression.substr(1);
|
||||
color.r = std::stoul(value.substr(0, 2), nullptr, 16);
|
||||
color.g = std::stoul(value.substr(2, 4), nullptr, 16);
|
||||
color.b = std::stoul(value.substr(4, 6), nullptr, 16);
|
||||
color.a = 255;
|
||||
if(value.length() == 8) {
|
||||
color.a = std::stoul(value.substr(6,8), nullptr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************** STRING NODE ********************************/
|
||||
|
||||
std::string StringNode::asString() {
|
||||
@@ -49,6 +62,14 @@ std::string ValueNode::asIdentifier() {
|
||||
return "";
|
||||
}
|
||||
|
||||
Color ValueNode::asColor() {
|
||||
if (is(COLOR)) {
|
||||
return colorValue.value();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
bool ValueNode::asBool() {
|
||||
if (is(BOOL)) {
|
||||
return boolValue.value();
|
||||
@@ -94,6 +115,13 @@ ValueNode ValueNode::ofNull() {
|
||||
return value;
|
||||
}
|
||||
|
||||
ValueNode ValueNode::ofColor(Color color) {
|
||||
ValueNode value;
|
||||
value.type = EnumNode(COLOR);
|
||||
value.colorValue = color;
|
||||
return value;
|
||||
}
|
||||
|
||||
ValueNode ValueNode::ofWire(std::optional<std::string> _value) {
|
||||
ValueNode value;
|
||||
value.type = EnumNode(WIRE);
|
||||
@@ -106,4 +134,4 @@ ValueNode ValueNode::ofMemory(std::optional<std::string> _value) {
|
||||
value.type = EnumNode(MEMORY);
|
||||
value.identifierValue = _value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#define AST_NODE_H
|
||||
|
||||
#include "token.h"
|
||||
#include "color.h"
|
||||
#include "source_error.h"
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
@@ -56,6 +57,14 @@ struct NumberNode : public AstNode {
|
||||
NumberNode() : value(0) {}
|
||||
};
|
||||
|
||||
struct ColorNode : public AstNode {
|
||||
Color color;
|
||||
|
||||
explicit ColorNode(const std::string &expression);
|
||||
|
||||
ColorNode() {}
|
||||
};
|
||||
|
||||
|
||||
struct CountNode : public AstNode {
|
||||
NumberNode first;
|
||||
@@ -83,6 +92,7 @@ public:
|
||||
IDENTIFIER,
|
||||
MEMORY,
|
||||
NIL,
|
||||
COLOR,
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -91,6 +101,7 @@ private:
|
||||
std::optional<std::string> stringValue;
|
||||
std::optional<bool> boolValue;
|
||||
std::optional<std::string> identifierValue;
|
||||
std::optional<Color> colorValue;
|
||||
|
||||
public:
|
||||
ValueNode() = default;
|
||||
@@ -105,6 +116,8 @@ public:
|
||||
|
||||
std::string asIdentifier();
|
||||
|
||||
Color asColor();
|
||||
|
||||
bool asBool();
|
||||
|
||||
bool is(ValueType valueType);
|
||||
@@ -121,7 +134,10 @@ public:
|
||||
|
||||
static ValueNode ofNull();
|
||||
|
||||
static ValueNode ofColor(Color color);
|
||||
|
||||
static ValueNode ofWire(std::optional<std::string> _value);
|
||||
|
||||
};
|
||||
|
||||
struct ConditionNode {
|
||||
@@ -196,6 +212,21 @@ struct DisplayItemNode : public AstNode {
|
||||
return _default;
|
||||
}
|
||||
|
||||
Color asColor(std::vector<SourceError> *errors, const std::string &property, Color _default = Color(0, 0, 0)) {
|
||||
for (auto &prop: values) {
|
||||
if (prop.key.value == property) {
|
||||
if (prop.value.is(ValueNode::COLOR)) {
|
||||
return prop.value.asColor();
|
||||
} else {
|
||||
if (errors != nullptr) {
|
||||
errors->emplace_back(prop.value.span, "expected number");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _default;
|
||||
}
|
||||
|
||||
std::string asIdentifier(std::vector<SourceError> *errors, const std::string &property, std::string _default = "") {
|
||||
for (auto &prop: values) {
|
||||
if (prop.key.value == property) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by bbr on 12.06.22..
|
||||
//
|
||||
|
||||
#include "color.h"
|
||||
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by bbr on 12.06.22..
|
||||
//
|
||||
|
||||
#ifndef SCHEMEEDITOR_COLOR_H
|
||||
#define SCHEMEEDITOR_COLOR_H
|
||||
|
||||
|
||||
struct Color {
|
||||
unsigned char r;
|
||||
unsigned char g;
|
||||
unsigned char b;
|
||||
unsigned char a;
|
||||
|
||||
Color(): r(0), g(0), b(0), a(0) {}
|
||||
|
||||
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0): r(r), g(g), b(b), a(a) {}
|
||||
};
|
||||
|
||||
|
||||
#endif //SCHEMEEDITOR_COLOR_H
|
||||
@@ -259,9 +259,7 @@ std::optional<LibraryNode> ComdelParser::parseLibrary() {
|
||||
TokenType::RBRACE,
|
||||
std::nullopt,
|
||||
false,
|
||||
[this] { return parseProperty(
|
||||
std::optional<TokenType>(
|
||||
TokenType::STRING));
|
||||
[this] { return parseProperty(TokenType::STRING);
|
||||
}
|
||||
));
|
||||
if (!err.has_value()) {
|
||||
@@ -302,6 +300,23 @@ PResult<StringNode> ComdelParser::parseString() {
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* ColorNode := #RRGGBB[AA]
|
||||
*
|
||||
****************************************************************************/
|
||||
PResult<ColorNode> ComdelParser::parseColor() {
|
||||
auto spanner = getSpanner();
|
||||
if (check(TokenType::COLOR)) {
|
||||
ColorNode node{current().text};
|
||||
node.span = current().span;
|
||||
bump();
|
||||
return spanner(node);
|
||||
}
|
||||
return unexpected();
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* IdentifierNode := IDENTIFIER
|
||||
@@ -1144,6 +1159,8 @@ PResult<ValueNode> ComdelParser::parseValue() {
|
||||
} else if (check(TokenType::NIL)) {
|
||||
bump();
|
||||
value = ValueNode::ofNull();
|
||||
} else if(check(TokenType::COLOR)) {
|
||||
value = ValueNode::ofColor(parseColor()->color);
|
||||
} else {
|
||||
return unexpected();
|
||||
}
|
||||
|
||||
@@ -66,6 +66,8 @@ private:
|
||||
// used to parse library and schema
|
||||
PResult<StringNode> parseString();
|
||||
|
||||
PResult<ColorNode> parseColor();
|
||||
|
||||
PResult<IdentifierNode> parseIdentifier();
|
||||
|
||||
PResult<NumberNode> parseNumber();
|
||||
|
||||
Reference in new issue
Block a user