More string_view tweaks

This commit is contained in:
Jason Turner 2017-08-25 14:49:44 -06:00
parent 04902f8209
commit dce9e17c34
5 changed files with 44 additions and 48 deletions

View File

@ -50,7 +50,7 @@ namespace chaiscript
return opers[static_cast<int>(t_oper)];
}
constexpr static Opers to_operator(const char * const t_str, bool t_is_unary = false) noexcept
constexpr static Opers to_operator(const std::string_view &t_str, bool t_is_unary = false) noexcept
{
#ifdef CHAISCRIPT_MSVC
#pragma warning(push)

View File

@ -32,54 +32,46 @@ struct AST_Node;
namespace chaiscript
{
struct Name_Validator {
static bool is_reserved_word(const std::string &name) noexcept
template<typename T>
static bool is_reserved_word(const T &s) noexcept
{
static const std::unordered_set<std::string> words {
"def", "fun", "while", "for", "if", "else",
"&&", "||", ",", "auto", "return", "break",
"true", "false", "class", "attr", "var", "global",
"GLOBAL", "_",
"__LINE__", "__FILE__", "__FUNC__", "__CLASS__"
};
const static std::unordered_set<std::uint32_t> words{
utility::fnv1a_32("def"),
utility::fnv1a_32("fun"),
utility::fnv1a_32("while"),
utility::fnv1a_32("for"),
utility::fnv1a_32("if"),
utility::fnv1a_32("else"),
utility::fnv1a_32("&&"),
utility::fnv1a_32("||"),
utility::fnv1a_32(","),
utility::fnv1a_32("auto"),
utility::fnv1a_32("return"),
utility::fnv1a_32("break"),
utility::fnv1a_32("true"),
utility::fnv1a_32("false"),
utility::fnv1a_32("class"),
utility::fnv1a_32("attr"),
utility::fnv1a_32("var"),
utility::fnv1a_32("global"),
utility::fnv1a_32("GLOBAL"),
utility::fnv1a_32("_"),
utility::fnv1a_32("__LINE__"),
utility::fnv1a_32("__FILE__"),
utility::fnv1a_32("__FUNC__"),
utility::fnv1a_32("__CLASS__")};
return words.count(name) == 1;
return words.count(utility::fnv1a_32(s)) == 1;
}
static bool is_reserved_word(const std::string_view &name) noexcept
{
static const std::unordered_set<std::string_view> words {
"def", "fun", "while", "for", "if", "else",
"&&", "||", ",", "auto", "return", "break",
"true", "false", "class", "attr", "var", "global",
"GLOBAL", "_",
"__LINE__", "__FILE__", "__FUNC__", "__CLASS__"
};
return words.count(name) == 1;
}
static bool valid_object_name(const std::string &name) noexcept
template<typename T>
static bool valid_object_name(const T &name) noexcept
{
return name.find("::") == std::string::npos && !is_reserved_word(name);
}
static void validate_object_name(const std::string &name)
{
if (is_reserved_word(name)) {
throw exception::reserved_word_error(name);
}
if (name.find("::") != std::string::npos) {
throw exception::illegal_name_error(name);
}
}
static bool valid_object_name(const std::string_view &name) noexcept
{
return name.find("::") == std::string::npos && !is_reserved_word(name);
}
static void validate_object_name(const std::string_view &name)
template<typename T>
static void validate_object_name(const T &name)
{
if (is_reserved_word(name)) {
throw exception::reserved_word_error(std::string(name));

View File

@ -159,7 +159,7 @@ namespace chaiscript
struct Fold_Right_Binary_Operator_AST_Node : AST_Node_Impl<T> {
Fold_Right_Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children, Boxed_Value t_rhs) :
AST_Node_Impl<T>(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children)),
m_oper(Operators::to_operator(t_oper.c_str())),
m_oper(Operators::to_operator(t_oper)),
m_rhs(std::move(t_rhs))
{ }
@ -204,7 +204,7 @@ namespace chaiscript
struct Binary_Operator_AST_Node : AST_Node_Impl<T> {
Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
AST_Node_Impl<T>(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children)),
m_oper(Operators::to_operator(t_oper.c_str()))
m_oper(Operators::to_operator(t_oper))
{ }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
@ -420,7 +420,7 @@ namespace chaiscript
struct Equation_AST_Node final : AST_Node_Impl<T> {
Equation_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Equation, std::move(t_loc), std::move(t_children)),
m_oper(Operators::to_operator(this->text.c_str()))
m_oper(Operators::to_operator(this->text))
{ assert(this->children.size() == 2); }
@ -1162,7 +1162,7 @@ namespace chaiscript
struct Prefix_AST_Node final : AST_Node_Impl<T> {
Prefix_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Prefix, std::move(t_loc), std::move(t_children)),
m_oper(Operators::to_operator(this->text.c_str(), true))
m_oper(Operators::to_operator(this->text, true))
{ }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override{

View File

@ -241,7 +241,7 @@ namespace chaiscript {
{
try {
const auto &oper = node->text;
const auto parsed = Operators::to_operator(oper.c_str());
const auto parsed = Operators::to_operator(oper);
if (parsed != Operators::Opers::invalid) {
const auto rhs = dynamic_cast<eval::Constant_AST_Node<T> *>(node->children[1].get())->m_value;
if (rhs.get_type_info().is_arithmetic()) {
@ -268,7 +268,7 @@ namespace chaiscript {
{
try {
const auto &oper = node->text;
const auto parsed = Operators::to_operator(oper.c_str(), true);
const auto parsed = Operators::to_operator(oper, true);
const auto lhs = dynamic_cast<const eval::Constant_AST_Node<T> *>(node->children[0].get())->m_value;
const auto match = oper + node->children[0]->text;
@ -308,7 +308,7 @@ namespace chaiscript {
{
try {
const auto &oper = node->text;
const auto parsed = Operators::to_operator(oper.c_str());
const auto parsed = Operators::to_operator(oper);
if (parsed != Operators::Opers::invalid) {
const auto lhs = dynamic_cast<const eval::Constant_AST_Node<T> &>(*node->children[0]).m_value;
const auto rhs = dynamic_cast<const eval::Constant_AST_Node<T> &>(*node->children[1]).m_value;

View File

@ -57,6 +57,10 @@ namespace chaiscript
return fnv1a_32(sv.begin(), sv.end());
}
static std::uint32_t fnv1a_32(const std::string &s) noexcept {
return fnv1a_32(s.begin(), s.end());
}
}
}