Pretty much everything Label wise is working
This commit is contained in:
155
src/dawnshared/util/parser/TypeParsers.hpp
Normal file
155
src/dawnshared/util/parser/TypeParsers.hpp
Normal file
@ -0,0 +1,155 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "util/string.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
static inline std::string rawParser(std::string v, std::string *error) {
|
||||
return v;
|
||||
};
|
||||
|
||||
static inline std::string stringParser(std::string v, std::string *error) {
|
||||
return "\"" + v + "\"";
|
||||
};
|
||||
|
||||
static inline std::string floatParser(std::string v, std::string *error) {
|
||||
v = stringTrim(v);
|
||||
|
||||
// Make sure number contains decimal
|
||||
if(v.find(".") == std::string::npos) {
|
||||
v += ".0";
|
||||
}
|
||||
// Make sure number contains a number before the decimal
|
||||
if(v.find(".") == 0) {
|
||||
v = "0" + v;
|
||||
}
|
||||
// Make sure ends with f
|
||||
if(v.find("f") == std::string::npos) {
|
||||
v += "f";
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
static inline std::string intParser(std::string v, std::string *error) {
|
||||
v = stringTrim(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
static inline std::string boolParser(std::string v, std::string *error) {
|
||||
v = stringTrim(v);
|
||||
if(v == "true") return "true";
|
||||
if(v == "false") return "false";
|
||||
*error = "Invalid bool value: " + v;
|
||||
return std::string("");
|
||||
}
|
||||
|
||||
static inline std::string vec2Parser(std::string v, std::string *error) {
|
||||
// Split string by comma into two strings that we pass into float
|
||||
auto split = stringSplit(v, ",");
|
||||
if(split.size() != 2) {
|
||||
*error = "Invalid vec2 value: " + v;
|
||||
return std::string("");
|
||||
}
|
||||
return std::string(
|
||||
"glm::vec2(" +
|
||||
floatParser(split[0], error) + ", " +
|
||||
floatParser(split[1], error) +
|
||||
")"
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
static inline std::string vec3Parser(std::string v, std::string *error) {
|
||||
// Split string by comma into two strings that we pass into float
|
||||
auto split = stringSplit(v, ",");
|
||||
if(split.size() != 3) {
|
||||
*error = "Invalid vec3 value: " + v;
|
||||
return std::string("");
|
||||
}
|
||||
return std::string(
|
||||
"glm::vec3(" +
|
||||
floatParser(split[0], error) + ", " +
|
||||
floatParser(split[1], error) + ", " +
|
||||
floatParser(split[2], error) +
|
||||
")"
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
static inline std::string vec6Parser(std::string v, std::string *error) {
|
||||
// Split string by comma into two strings that we pass into float
|
||||
auto split = stringSplit(v, ",");
|
||||
if(split.size() != 6) {
|
||||
*error = "Invalid vec6 value: " + v;
|
||||
return std::string("");
|
||||
}
|
||||
return std::string(
|
||||
"glm::vec6(" +
|
||||
floatParser(split[0], error) + ", " +
|
||||
floatParser(split[1], error) + ", " +
|
||||
floatParser(split[2], error) + ", " +
|
||||
floatParser(split[3], error) + ", " +
|
||||
floatParser(split[4], error) + ", " +
|
||||
floatParser(split[5], error) +
|
||||
")"
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
static inline std::string vec4Parser(std::string v, std::string *error) {
|
||||
// Split string by comma into two strings that we pass into float
|
||||
auto split = stringSplit(v, ",");
|
||||
if(split.size() != 4) {
|
||||
*error = "Invalid vec4 value: " + v;
|
||||
return std::string("");
|
||||
}
|
||||
return std::string(
|
||||
"glm::vec4(" +
|
||||
floatParser(split[0], error) + ", " +
|
||||
floatParser(split[1], error) + ", " +
|
||||
floatParser(split[2], error) + ", " +
|
||||
floatParser(split[3], error) +
|
||||
")"
|
||||
);
|
||||
};
|
||||
|
||||
static inline std::string colorParser(std::string v, std::string *error) {
|
||||
return rawParser(v, error);
|
||||
}
|
||||
|
||||
static inline std::function<std::string(std::string, std::string*)> parserFromTypeName(std::string type) {
|
||||
std::function<std::string(std::string, std::string*)> parser = rawParser;
|
||||
|
||||
if(type.find("string") != std::string::npos) {
|
||||
parser = stringParser;
|
||||
} else if(type.find("float") != std::string::npos) {
|
||||
parser = floatParser;
|
||||
} else if(type.find("Color") != std::string::npos) {
|
||||
parser = colorParser;
|
||||
} else if(type.find("vec2") != std::string::npos) {
|
||||
parser = vec2Parser;
|
||||
} else if(type.find("vec3") != std::string::npos) {
|
||||
parser = vec3Parser;
|
||||
} else if(type.find("vec4") != std::string::npos) {
|
||||
parser = vec4Parser;
|
||||
} else if(type == "int32_t" || type == "int") {
|
||||
parser = intParser;
|
||||
} else if(type == "bool_t") {
|
||||
parser = boolParser;
|
||||
} else if(type == "flag_t") {
|
||||
parser = rawParser;
|
||||
} else if(type.starts_with("enum")) {
|
||||
parser = rawParser;
|
||||
} else if(type.find("*") == (type.size() - 1)) {
|
||||
type = type.substr(0, type.size() - 1);
|
||||
parser = rawParser;
|
||||
} else {
|
||||
throw std::string("Invalid parser type");
|
||||
}
|
||||
|
||||
return parser;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user