From d93e4802c79b5cc1924dae6b974dbb7a6d912215 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 10 Apr 2024 22:45:16 +0800 Subject: [PATCH] fix config string_view --- 3party/{inja => nlohammn}/nlohmann/json.hpp | 0 CMakeLists.txt | 12 +- src/sled/config.cc | 38 +- src/sled/config_test.cc | 2 +- .../inja/inja.hpp => src/sled/nonstd/inja.h | 3535 +++++------------ src/sled/sled.h | 2 +- 6 files changed, 985 insertions(+), 2604 deletions(-) rename 3party/{inja => nlohammn}/nlohmann/json.hpp (100%) rename 3party/inja/inja.hpp => src/sled/nonstd/inja.h (54%) diff --git a/3party/inja/nlohmann/json.hpp b/3party/nlohammn/nlohmann/json.hpp similarity index 100% rename from 3party/inja/nlohmann/json.hpp rename to 3party/nlohammn/nlohmann/json.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 21c1fe1..eaa15ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,11 @@ project( VERSION 0.1.1 LANGUAGES C CXX) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_STANDARD_EXTENSIONS OFF) +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(CMAKE_CXX_STANDARD_EXTENSIONS OFF) +endif() # set(CMAKE_C_FLAGS "-gdwarf-3 -gstrict-dwarf") set(CMAKE_CXX_FLAGS "-gdwarf-3 # -gstrict-dwarf") @@ -48,8 +50,8 @@ if(SLED_LOCATION_PATH) sled PRIVATE __SLED_LOCATION_PATH="${SLED_LOCATION_PATH}") endif() # add_subdirectory(3party/eigen EXCLUDE_FROM_ALL) -target_include_directories(sled PUBLIC src/ 3party/eigen 3party/inja - 3party/rxcpp 3party/toml11) +target_include_directories(sled PUBLIC src/ 3party/eigen 3party/rxcpp + 3party/nlohammn 3party/toml11) target_sources( sled PRIVATE src/sled/async/async.cc diff --git a/src/sled/config.cc b/src/sled/config.cc index 6a191a9..74a98c3 100644 --- a/src/sled/config.cc +++ b/src/sled/config.cc @@ -10,25 +10,25 @@ Config::Config(sled::string_view name) : Config(name, "") {} Config::Config(sled::string_view name, sled::string_view path) : config_name_(name) { - config_paths_.emplace_back(path.to_string()); + config_paths_.emplace_back(sled::to_string(path)); } void Config::AddConfigFullPath(sled::string_view path) { - config_full_paths_.emplace_back(path.to_string()); + config_full_paths_.emplace_back(sled::to_string(path)); } void Config::SetConfigName(sled::string_view name) { - config_name_ = name.to_string(); + config_name_ = sled::to_string(name); } void Config::AddConfigPath(sled::string_view path) { - config_paths_.emplace_back(path.to_string()); + config_paths_.emplace_back(sled::to_string(path)); } bool @@ -36,7 +36,7 @@ Config::ReadInConfig() { const static std::vector extensions = {".toml"}; auto load_config_from = [](sled::string_view full_path, toml::value &value) { - const std::ifstream file(full_path.to_string()); + const std::ifstream file(to_string(full_path)); if (file.good()) { try { std::stringstream ss; @@ -47,7 +47,7 @@ Config::ReadInConfig() return true; // goto config_read_success; } catch (...) { - LOGD("Failed to parse config file: {}", full_path.to_string()); + LOGD("Failed to parse config file: {}", sled::to_string(full_path)); } } return false; @@ -126,13 +126,13 @@ Config::GetStringOr(sled::string_view key, sled::string_view def) const try { if (GetNode(key, value) && value.is_string()) { return value.as_string(); } } catch (...) {} - return def.to_string(); + return sled::to_string(def); } void Config::SetDefault(sled::string_view key, const bool &value) { - default_values_.insert({key.to_string(), value}); + default_values_.insert({sled::to_string(key), value}); } void @@ -144,7 +144,7 @@ Config::SetDefault(sled::string_view key, const char *value) void Config::SetDefault(sled::string_view key, const std::string &value) { - default_values_.insert({key.to_string(), value}); + default_values_.insert({sled::to_string(key), value}); } void @@ -156,19 +156,19 @@ Config::SetDefault(sled::string_view key, sled::string_view value) void Config::SetDefault(sled::string_view key, const int &value) { - default_values_.insert({key.to_string(), value}); + default_values_.insert({sled::to_string(key), value}); } void Config::SetDefault(sled::string_view key, const double &value) { - default_values_.insert({key.to_string(), value}); + default_values_.insert({sled::to_string(key), value}); } void Config::SetValue(sled::string_view key, const bool &value) { - values_.insert({key.to_string(), value}); + values_.insert({sled::to_string(key), value}); } void @@ -181,7 +181,7 @@ void Config::SetValue(sled::string_view key, const std::string &value) { - values_.insert({key.to_string(), value}); + values_.insert({sled::to_string(key), value}); } void @@ -193,13 +193,13 @@ Config::SetValue(sled::string_view key, sled::string_view value) void Config::SetValue(sled::string_view key, const int &value) { - values_.insert({key.to_string(), value}); + values_.insert({sled::to_string(key), value}); } void Config::SetValue(sled::string_view key, const double &value) { - values_.insert({key.to_string(), value}); + values_.insert({sled::to_string(key), value}); } bool @@ -209,7 +209,7 @@ Config::GetNode(sled::string_view key, toml::value &value) const if (GetValueNode(key, value)) { return true; } // 2. 然后从配置文件获取 - auto keys = StrSplit(key.to_string(), "."); + auto keys = StrSplit(sled::to_string(key), "."); auto cur = toml_; for (const auto &k : keys) { try { @@ -228,7 +228,7 @@ Config::GetNode(sled::string_view key, toml::value &value) const bool Config::AddDefaultNode(sled::string_view key, ValueType value) { - auto keys = StrSplit(key.to_string(), "."); + auto keys = StrSplit(sled::to_string(key), "."); if (keys.size() == 1) { auto first_key = keys[0]; switch (value.index()) { @@ -256,7 +256,7 @@ bool Config::GetValueNode(sled::string_view key, toml::value &value) const { - auto iter = values_.find(key.to_string()); + auto iter = values_.find(sled::to_string(key)); if (iter == values_.end()) { return false; } auto &default_value = iter->second; switch (default_value.index()) { @@ -281,7 +281,7 @@ Config::GetValueNode(sled::string_view key, toml::value &value) const bool Config::GetDefaultNode(sled::string_view key, toml::value &value) const { - auto iter = default_values_.find(key.to_string()); + auto iter = default_values_.find(sled::to_string(key)); if (iter == default_values_.end()) { return false; } auto &default_value = iter->second; switch (default_value.index()) { diff --git a/src/sled/config_test.cc b/src/sled/config_test.cc index 8500e6f..ce3dfbe 100644 --- a/src/sled/config_test.cc +++ b/src/sled/config_test.cc @@ -2,7 +2,7 @@ #include static std::string test_config_name = "config_test"; -static std::string test_config_path = sled::StripSuffix(__FILE__, "config_test.cc").to_string(); +static std::string test_config_path = sled::to_string(sled::StripSuffix(__FILE__, "config_test.cc")); TEST_SUITE("Config") { diff --git a/3party/inja/inja.hpp b/src/sled/nonstd/inja.h similarity index 54% rename from 3party/inja/inja.hpp rename to src/sled/nonstd/inja.h index 051a9a1..1b7478e 100644 --- a/3party/inja/inja.hpp +++ b/src/sled/nonstd/inja.h @@ -1,10 +1,10 @@ /* - ___ _ Version 3.3 + ___ _ Version 3.4.0 |_ _|_ __ (_) __ _ https://github.com/pantor/inja | || '_ \ | |/ _` | Licensed under the MIT License . | || | | || | (_| | - |___|_| |_|/ |\__,_| Copyright (c) 2018-2021 Lars Berscheid - |__/ + |___|_| |_|/ |\__,_| Copyright (c) 2018-2023 Lars Berscheid + |__/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights @@ -27,6 +27,14 @@ SOFTWARE. #include +namespace inja { +#ifndef INJA_DATA_TYPE +using json = nlohmann::json; +#else +using json = INJA_DATA_TYPE; +#endif +}// namespace inja + #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION) #ifndef INJA_THROW #define INJA_THROW(exception) throw exception @@ -47,13 +55,13 @@ SOFTWARE. #ifndef INCLUDE_INJA_ENVIRONMENT_HPP_ #define INCLUDE_INJA_ENVIRONMENT_HPP_ +#include "sled/nonstd/string_view.h" #include #include #include #include #include - -#include +// #include // #include "config.hpp" #ifndef INCLUDE_INJA_CONFIG_HPP_ @@ -62,1617 +70,34 @@ SOFTWARE. #include #include -// #include "string_view.hpp" -// Copyright 2017-2019 by Martin Moene -// -// string-view lite, a C++17-like string_view for C++98 and later. -// For more information see https://github.com/martinmoene/string-view-lite -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef NONSTD_SV_LITE_H_INCLUDED -#define NONSTD_SV_LITE_H_INCLUDED - -#define string_view_lite_MAJOR 1 -#define string_view_lite_MINOR 4 -#define string_view_lite_PATCH 0 - -#define string_view_lite_VERSION \ - nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY( \ - string_view_lite_PATCH) - -#define nssv_STRINGIFY(x) nssv_STRINGIFY_(x) -#define nssv_STRINGIFY_(x) #x - -// string-view lite configuration: - -#define nssv_STRING_VIEW_DEFAULT 0 -#define nssv_STRING_VIEW_NONSTD 1 -#define nssv_STRING_VIEW_STD 2 - -#if !defined(nssv_CONFIG_SELECT_STRING_VIEW) -#define nssv_CONFIG_SELECT_STRING_VIEW (nssv_HAVE_STD_STRING_VIEW ? nssv_STRING_VIEW_STD : nssv_STRING_VIEW_NONSTD) -#endif - -#if defined(nssv_CONFIG_SELECT_STD_STRING_VIEW) || defined(nssv_CONFIG_SELECT_NONSTD_STRING_VIEW) -#error nssv_CONFIG_SELECT_STD_STRING_VIEW and nssv_CONFIG_SELECT_NONSTD_STRING_VIEW are deprecated and removed, please use nssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_... -#endif - -#ifndef nssv_CONFIG_STD_SV_OPERATOR -#define nssv_CONFIG_STD_SV_OPERATOR 0 -#endif - -#ifndef nssv_CONFIG_USR_SV_OPERATOR -#define nssv_CONFIG_USR_SV_OPERATOR 1 -#endif - -#ifdef nssv_CONFIG_CONVERSION_STD_STRING -#define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS nssv_CONFIG_CONVERSION_STD_STRING -#define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS nssv_CONFIG_CONVERSION_STD_STRING -#endif - -#ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS -#define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1 -#endif - -#ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS -#define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1 -#endif - -// Control presence of exception handling (try and auto discover): - -#ifndef nssv_CONFIG_NO_EXCEPTIONS -#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) -#define nssv_CONFIG_NO_EXCEPTIONS 0 -#else -#define nssv_CONFIG_NO_EXCEPTIONS 1 -#endif -#endif - -// C++ language version detection (C++20 is speculative): -// Note: VC14.0/1900 (VS2015) lacks too much from C++14. - -#ifndef nssv_CPLUSPLUS -#if defined(_MSVC_LANG) && !defined(__clang__) -#define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG) -#else -#define nssv_CPLUSPLUS __cplusplus -#endif -#endif - -#define nssv_CPP98_OR_GREATER (nssv_CPLUSPLUS >= 199711L) -#define nssv_CPP11_OR_GREATER (nssv_CPLUSPLUS >= 201103L) -#define nssv_CPP11_OR_GREATER_ (nssv_CPLUSPLUS >= 201103L) -#define nssv_CPP14_OR_GREATER (nssv_CPLUSPLUS >= 201402L) -#define nssv_CPP17_OR_GREATER (nssv_CPLUSPLUS >= 201703L) -#define nssv_CPP20_OR_GREATER (nssv_CPLUSPLUS >= 202000L) - -// use C++17 std::string_view if available and requested: - -#if nssv_CPP17_OR_GREATER && defined(__has_include) -#if __has_include( ) -#define nssv_HAVE_STD_STRING_VIEW 1 -#else -#define nssv_HAVE_STD_STRING_VIEW 0 -#endif -#else -#define nssv_HAVE_STD_STRING_VIEW 0 -#endif - -#define nssv_USES_STD_STRING_VIEW \ - ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_STD) \ - || ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_DEFAULT) && nssv_HAVE_STD_STRING_VIEW)) - -#define nssv_HAVE_STARTS_WITH (nssv_CPP20_OR_GREATER || !nssv_USES_STD_STRING_VIEW) -#define nssv_HAVE_ENDS_WITH nssv_HAVE_STARTS_WITH - -// -// Use C++17 std::string_view: -// - -#if nssv_USES_STD_STRING_VIEW - -#include - -// Extensions for std::string: - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { - -template> -std::basic_string -to_string(std::basic_string_view v, Allocator const &a = Allocator()) -{ - return std::basic_string(v.begin(), v.end(), a); -} - -template -std::basic_string_view -to_string_view(std::basic_string const &s) -{ - return std::basic_string_view(s.data(), s.size()); -} - -// Literal operators sv and _sv: - -#if nssv_CONFIG_STD_SV_OPERATOR - -using namespace std::literals::string_view_literals; - -#endif - -#if nssv_CONFIG_USR_SV_OPERATOR - -inline namespace literals { -inline namespace string_view_literals { - -constexpr std::string_view -operator"" _sv(const char *str, size_t len) noexcept// (1) -{ - return std::string_view{str, len}; -} - -constexpr std::u16string_view -operator"" _sv(const char16_t *str, size_t len) noexcept// (2) -{ - return std::u16string_view{str, len}; -} - -constexpr std::u32string_view -operator"" _sv(const char32_t *str, size_t len) noexcept// (3) -{ - return std::u32string_view{str, len}; -} - -constexpr std::wstring_view -operator"" _sv(const wchar_t *str, size_t len) noexcept// (4) -{ - return std::wstring_view{str, len}; -} - -}// namespace string_view_literals -}// namespace literals - -#endif// nssv_CONFIG_USR_SV_OPERATOR - -}// namespace nonstd - -#endif// nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { - -using std::basic_string_view; -using std::string_view; -using std::u16string_view; -using std::u32string_view; -using std::wstring_view; - -// literal "sv" and "_sv", see above - -using std::operator==; -using std::operator!=; -using std::operator<; -using std::operator<=; -using std::operator>; -using std::operator>=; - -using std::operator<<; - -}// namespace nonstd - -#else// nssv_HAVE_STD_STRING_VIEW - -// -// Before C++17: use string_view lite: -// - -// Compiler versions: -// -// MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0) -// MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002) -// MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003) -// MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005) -// MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008) -// MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) -// MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) -// MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) -// MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) -// MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017) - -#if defined(_MSC_VER) && !defined(__clang__) -#define nssv_COMPILER_MSVC_VER (_MSC_VER) -#define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * (5 + (_MSC_VER < 1900))) -#else -#define nssv_COMPILER_MSVC_VER 0 -#define nssv_COMPILER_MSVC_VERSION 0 -#endif - -#define nssv_COMPILER_VERSION(major, minor, patch) (10 * (10 * (major) + (minor)) + (patch)) - -#if defined(__clang__) -#define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) -#else -#define nssv_COMPILER_CLANG_VERSION 0 -#endif - -#if defined(__GNUC__) && !defined(__clang__) -#define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) -#else -#define nssv_COMPILER_GNUC_VERSION 0 -#endif - -// half-open range [lo..hi): -#define nssv_BETWEEN(v, lo, hi) ((lo) <= (v) && (v) < (hi)) - -// Presence of language and library features: - -#ifdef _HAS_CPP0X -#define nssv_HAS_CPP0X _HAS_CPP0X -#else -#define nssv_HAS_CPP0X 0 -#endif - -// Unless defined otherwise below, consider VC14 as C++11 for variant-lite: - -#if nssv_COMPILER_MSVC_VER >= 1900 -#undef nssv_CPP11_OR_GREATER -#define nssv_CPP11_OR_GREATER 1 -#endif - -#define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500) -#define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600) -#define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700) -#define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800) -#define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900) -#define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910) - -#define nssv_CPP14_000 (nssv_CPP14_OR_GREATER) -#define nssv_CPP17_000 (nssv_CPP17_OR_GREATER) - -// Presence of C++11 language features: - -#define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140 -#define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140 -#define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140 -#define nssv_HAVE_NOEXCEPT nssv_CPP11_140 -#define nssv_HAVE_NULLPTR nssv_CPP11_100 -#define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140 -#define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140 -#define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140 -#define nssv_HAVE_WCHAR16_T nssv_CPP11_100 -#define nssv_HAVE_WCHAR32_T nssv_CPP11_100 - -#if !((nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION) || nssv_BETWEEN(nssv_COMPILER_CLANG_VERSION, 300, 400)) -#define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140 -#else -#define nssv_HAVE_STD_DEFINED_LITERALS 0 -#endif - -// Presence of C++14 language features: - -#define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000 - -// Presence of C++17 language features: - -#define nssv_HAVE_NODISCARD nssv_CPP17_000 - -// Presence of C++ library features: - -#define nssv_HAVE_STD_HASH nssv_CPP11_120 - -// C++ feature usage: - -#if nssv_HAVE_CONSTEXPR_11 -#define nssv_constexpr constexpr -#else -#define nssv_constexpr /*constexpr*/ -#endif - -#if nssv_HAVE_CONSTEXPR_14 -#define nssv_constexpr14 constexpr -#else -#define nssv_constexpr14 /*constexpr*/ -#endif - -#if nssv_HAVE_EXPLICIT_CONVERSION -#define nssv_explicit explicit -#else -#define nssv_explicit /*explicit*/ -#endif - -#if nssv_HAVE_INLINE_NAMESPACE -#define nssv_inline_ns inline -#else -#define nssv_inline_ns /*inline*/ -#endif - -#if nssv_HAVE_NOEXCEPT -#define nssv_noexcept noexcept -#else -#define nssv_noexcept /*noexcept*/ -#endif - -//#if nssv_HAVE_REF_QUALIFIER -//# define nssv_ref_qual & -//# define nssv_refref_qual && -//#else -//# define nssv_ref_qual /*&*/ -//# define nssv_refref_qual /*&&*/ -//#endif - -#if nssv_HAVE_NULLPTR -#define nssv_nullptr nullptr -#else -#define nssv_nullptr NULL -#endif - -#if nssv_HAVE_NODISCARD -#define nssv_nodiscard [[nodiscard]] -#else -#define nssv_nodiscard /*[[nodiscard]]*/ -#endif - -// Additional includes: - -#include -#include -#include -#include -#include -#include // std::char_traits<> - -#if !nssv_CONFIG_NO_EXCEPTIONS -#include -#endif - -#if nssv_CPP11_OR_GREATER -#include -#endif - -// Clang, GNUC, MSVC warning suppression macros: - -#if defined(__clang__) -#pragma clang diagnostic ignored "-Wreserved-user-defined-literal" -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wuser-defined-literals" -#elif defined(__GNUC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wliteral-suffix" -#endif// __clang__ - -#if nssv_COMPILER_MSVC_VERSION >= 140 -#define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]] -#define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress : code)) -#define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable : codes)) -#else -#define nssv_SUPPRESS_MSGSL_WARNING(expr) -#define nssv_SUPPRESS_MSVC_WARNING(code, descr) -#define nssv_DISABLE_MSVC_WARNINGS(codes) -#endif - -#if defined(__clang__) -#define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop") -#elif defined(__GNUC__) -#define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop") -#elif nssv_COMPILER_MSVC_VERSION >= 140 -#define nssv_RESTORE_WARNINGS() __pragma(warning(pop)) -#else -#define nssv_RESTORE_WARNINGS() -#endif - -// Suppress the following MSVC (GSL) warnings: -// - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not -// start with an underscore are reserved -// - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions; -// use brace initialization, gsl::narrow_cast or gsl::narow -// - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead - -nssv_DISABLE_MSVC_WARNINGS(4455 26481 26472) - // nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" ) - // nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix ) - - namespace nonstd -{ - namespace sv_lite { - -#if nssv_CPP11_OR_GREATER - - namespace detail { - - // Expect tail call optimization to make length() non-recursive: - - template - inline constexpr std::size_t length(CharT *s, std::size_t result = 0) - { - return *s == '\0' ? result : length(s + 1, result + 1); - } - - }// namespace detail - -#endif// nssv_CPP11_OR_GREATER - - template> - class basic_string_view; - - // - // basic_string_view: - // - - template */ - > - class basic_string_view { - public: - // Member types: - - typedef Traits traits_type; - typedef CharT value_type; - - typedef CharT *pointer; - typedef CharT const *const_pointer; - typedef CharT &reference; - typedef CharT const &const_reference; - - typedef const_pointer iterator; - typedef const_pointer const_iterator; - typedef std::reverse_iterator reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; - - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - - // 24.4.2.1 Construction and assignment: - - nssv_constexpr basic_string_view() nssv_noexcept : data_(nssv_nullptr), size_(0) {} - -#if nssv_CPP11_OR_GREATER - nssv_constexpr basic_string_view(basic_string_view const &other) nssv_noexcept = default; -#else - nssv_constexpr basic_string_view(basic_string_view const &other) nssv_noexcept - : data_(other.data_), - size_(other.size_) - {} -#endif - - nssv_constexpr basic_string_view(CharT const *s, - size_type count) nssv_noexcept// non-standard noexcept - : data_(s), - size_(count) - {} - - nssv_constexpr basic_string_view(CharT const *s) nssv_noexcept// non-standard noexcept - : data_(s) -#if nssv_CPP17_OR_GREATER - , - size_(Traits::length(s)) -#elif nssv_CPP11_OR_GREATER - , - size_(detail::length(s)) -#else - , - size_(Traits::length(s)) -#endif - {} - - // Assignment: - -#if nssv_CPP11_OR_GREATER - nssv_constexpr14 basic_string_view &operator=(basic_string_view const &other) nssv_noexcept = default; -#else - nssv_constexpr14 basic_string_view &operator=(basic_string_view const &other) nssv_noexcept - { - data_ = other.data_; - size_ = other.size_; - return *this; - } -#endif - - // 24.4.2.2 Iterator support: - - nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; } - - nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; } - - nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); } - - nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); } - - nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator(end()); } - - nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator(begin()); } - - nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); } - - nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); } - - // 24.4.2.3 Capacity: - - nssv_constexpr size_type size() const nssv_noexcept { return size_; } - - nssv_constexpr size_type length() const nssv_noexcept { return size_; } - - nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits::max)(); } - - // since C++20 - nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept { return 0 == size_; } - - // 24.4.2.4 Element access: - - nssv_constexpr const_reference operator[](size_type pos) const { return data_at(pos); } - - nssv_constexpr14 const_reference at(size_type pos) const - { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos < size()); -#else - if (pos >= size()) { throw std::out_of_range("nonstd::string_view::at()"); } -#endif - return data_at(pos); - } - - nssv_constexpr const_reference front() const { return data_at(0); } - - nssv_constexpr const_reference back() const { return data_at(size() - 1); } - - nssv_constexpr const_pointer data() const nssv_noexcept { return data_; } - - // 24.4.2.5 Modifiers: - - nssv_constexpr14 void remove_prefix(size_type n) - { - assert(n <= size()); - data_ += n; - size_ -= n; - } - - nssv_constexpr14 void remove_suffix(size_type n) - { - assert(n <= size()); - size_ -= n; - } - - nssv_constexpr14 void swap(basic_string_view &other) nssv_noexcept - { - using std::swap; - swap(data_, other.data_); - swap(size_, other.size_); - } - - // 24.4.2.6 String operations: - - size_type copy(CharT *dest, size_type n, size_type pos = 0) const - { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos <= size()); -#else - if (pos > size()) { throw std::out_of_range("nonstd::string_view::copy()"); } -#endif - const size_type rlen = (std::min)(n, size() - pos); - - (void) Traits::copy(dest, data() + pos, rlen); - - return rlen; - } - - nssv_constexpr14 basic_string_view substr(size_type pos = 0, size_type n = npos) const - { -#if nssv_CONFIG_NO_EXCEPTIONS - assert(pos <= size()); -#else - if (pos > size()) { throw std::out_of_range("nonstd::string_view::substr()"); } -#endif - return basic_string_view(data() + pos, (std::min)(n, size() - pos)); - } - - // compare(), 6x: - - nssv_constexpr14 int compare(basic_string_view other) const nssv_noexcept// (1) - { - if (const int result = Traits::compare(data(), other.data(), (std::min)(size(), other.size()))) { - return result; - } - - return size() == other.size() ? 0 : size() < other.size() ? -1 : 1; - } - - nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other) const// (2) - { - return substr(pos1, n1).compare(other); - } - - nssv_constexpr int compare(size_type pos1, - size_type n1, - basic_string_view other, - size_type pos2, - size_type n2) const// (3) - { - return substr(pos1, n1).compare(other.substr(pos2, n2)); - } - - nssv_constexpr int compare(CharT const *s) const// (4) - { - return compare(basic_string_view(s)); - } - - nssv_constexpr int compare(size_type pos1, size_type n1, CharT const *s) const// (5) - { - return substr(pos1, n1).compare(basic_string_view(s)); - } - - nssv_constexpr int compare(size_type pos1, size_type n1, CharT const *s, size_type n2) const// (6) - { - return substr(pos1, n1).compare(basic_string_view(s, n2)); - } - - // 24.4.2.7 Searching: - - // starts_with(), 3x, since C++20: - - nssv_constexpr bool starts_with(basic_string_view v) const nssv_noexcept// (1) - { - return size() >= v.size() && compare(0, v.size(), v) == 0; - } - - nssv_constexpr bool starts_with(CharT c) const nssv_noexcept// (2) - { - return starts_with(basic_string_view(&c, 1)); - } - - nssv_constexpr bool starts_with(CharT const *s) const// (3) - { - return starts_with(basic_string_view(s)); - } - - // ends_with(), 3x, since C++20: - - nssv_constexpr bool ends_with(basic_string_view v) const nssv_noexcept// (1) - { - return size() >= v.size() && compare(size() - v.size(), npos, v) == 0; - } - - nssv_constexpr bool ends_with(CharT c) const nssv_noexcept// (2) - { - return ends_with(basic_string_view(&c, 1)); - } - - nssv_constexpr bool ends_with(CharT const *s) const// (3) - { - return ends_with(basic_string_view(s)); - } - - // find(), 4x: - - nssv_constexpr14 size_type find(basic_string_view v, - size_type pos = 0) const nssv_noexcept// (1) - { - return assert(v.size() == 0 || v.data() != nssv_nullptr), - pos >= size() ? npos : to_pos(std::search(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); - } - - nssv_constexpr14 size_type find(CharT c, size_type pos = 0) const nssv_noexcept// (2) - { - return find(basic_string_view(&c, 1), pos); - } - - nssv_constexpr14 size_type find(CharT const *s, size_type pos, size_type n) const// (3) - { - return find(basic_string_view(s, n), pos); - } - - nssv_constexpr14 size_type find(CharT const *s, size_type pos = 0) const// (4) - { - return find(basic_string_view(s), pos); - } - - // rfind(), 4x: - - nssv_constexpr14 size_type rfind(basic_string_view v, - size_type pos = npos) const nssv_noexcept// (1) - { - if (size() < v.size()) { return npos; } - - if (v.empty()) { return (std::min)(size(), pos); } - - const_iterator last = cbegin() + (std::min)(size() - v.size(), pos) + v.size(); - const_iterator result = std::find_end(cbegin(), last, v.cbegin(), v.cend(), Traits::eq); - - return result != last ? size_type(result - cbegin()) : npos; - } - - nssv_constexpr14 size_type rfind(CharT c, size_type pos = npos) const nssv_noexcept// (2) - { - return rfind(basic_string_view(&c, 1), pos); - } - - nssv_constexpr14 size_type rfind(CharT const *s, size_type pos, size_type n) const// (3) - { - return rfind(basic_string_view(s, n), pos); - } - - nssv_constexpr14 size_type rfind(CharT const *s, size_type pos = npos) const// (4) - { - return rfind(basic_string_view(s), pos); - } - - // find_first_of(), 4x: - - nssv_constexpr size_type find_first_of(basic_string_view v, - size_type pos = 0) const nssv_noexcept// (1) - { - return pos >= size() ? npos - : to_pos(std::find_first_of(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); - } - - nssv_constexpr size_type find_first_of(CharT c, size_type pos = 0) const nssv_noexcept// (2) - { - return find_first_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_first_of(CharT const *s, size_type pos, - size_type n) const// (3) - { - return find_first_of(basic_string_view(s, n), pos); - } - - nssv_constexpr size_type find_first_of(CharT const *s, size_type pos = 0) const// (4) - { - return find_first_of(basic_string_view(s), pos); - } - - // find_last_of(), 4x: - - nssv_constexpr size_type find_last_of(basic_string_view v, - size_type pos = npos) const nssv_noexcept// (1) - { - return empty() ? npos - : pos >= size() - ? find_last_of(v, size() - 1) - : to_pos(std::find_first_of(const_reverse_iterator(cbegin() + pos + 1), - crend(), - v.cbegin(), - v.cend(), - Traits::eq)); - } - - nssv_constexpr size_type find_last_of(CharT c, - size_type pos = npos) const nssv_noexcept// (2) - { - return find_last_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_last_of(CharT const *s, size_type pos, - size_type count) const// (3) - { - return find_last_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_last_of(CharT const *s, size_type pos = npos) const// (4) - { - return find_last_of(basic_string_view(s), pos); - } - - // find_first_not_of(), 4x: - - nssv_constexpr size_type find_first_not_of(basic_string_view v, - size_type pos = 0) const nssv_noexcept// (1) - { - return pos >= size() ? npos : to_pos(std::find_if(cbegin() + pos, cend(), not_in_view(v))); - } - - nssv_constexpr size_type find_first_not_of(CharT c, - size_type pos = 0) const nssv_noexcept// (2) - { - return find_first_not_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_first_not_of(CharT const *s, size_type pos, - size_type count) const// (3) - { - return find_first_not_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_first_not_of(CharT const *s, size_type pos = 0) const// (4) - { - return find_first_not_of(basic_string_view(s), pos); - } - - // find_last_not_of(), 4x: - - nssv_constexpr size_type find_last_not_of(basic_string_view v, - size_type pos = npos) const nssv_noexcept// (1) - { - return empty() ? npos - : pos >= size() - ? find_last_not_of(v, size() - 1) - : to_pos(std::find_if(const_reverse_iterator(cbegin() + pos + 1), crend(), not_in_view(v))); - } - - nssv_constexpr size_type find_last_not_of(CharT c, - size_type pos = npos) const nssv_noexcept// (2) - { - return find_last_not_of(basic_string_view(&c, 1), pos); - } - - nssv_constexpr size_type find_last_not_of(CharT const *s, size_type pos, - size_type count) const// (3) - { - return find_last_not_of(basic_string_view(s, count), pos); - } - - nssv_constexpr size_type find_last_not_of(CharT const *s, size_type pos = npos) const// (4) - { - return find_last_not_of(basic_string_view(s), pos); - } - - // Constants: - -#if nssv_CPP17_OR_GREATER - static nssv_constexpr size_type npos = size_type(-1); -#elif nssv_CPP11_OR_GREATER - enum : size_type { npos = size_type(-1) }; -#else - enum { npos = size_type(-1) }; -#endif - - private: - struct not_in_view { - const basic_string_view v; - - nssv_constexpr explicit not_in_view(basic_string_view v) : v(v) {} - - nssv_constexpr bool operator()(CharT c) const { return npos == v.find_first_of(c); } - }; - - nssv_constexpr size_type to_pos(const_iterator it) const - { - return it == cend() ? npos : size_type(it - cbegin()); - } - - nssv_constexpr size_type to_pos(const_reverse_iterator it) const - { - return it == crend() ? npos : size_type(crend() - it - 1); - } - - nssv_constexpr const_reference data_at(size_type pos) const - { -#if nssv_BETWEEN(nssv_COMPILER_GNUC_VERSION, 1, 500) - return data_[pos]; -#else - return assert(pos < size()), data_[pos]; -#endif - } - - private: - const_pointer data_; - size_type size_; - - public: -#if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS - - template - basic_string_view(std::basic_string const &s) nssv_noexcept - : data_(s.data()), - size_(s.size()) - {} - -#if nssv_HAVE_EXPLICIT_CONVERSION - - template - explicit operator std::basic_string() const - { - return to_string(Allocator()); - } - -#endif// nssv_HAVE_EXPLICIT_CONVERSION - -#if nssv_CPP11_OR_GREATER - - template> - std::basic_string to_string(Allocator const &a = Allocator()) const - { - return std::basic_string(begin(), end(), a); - } - -#else - - std::basic_string to_string() const { return std::basic_string(begin(), end()); } - - template - std::basic_string to_string(Allocator const &a) const - { - return std::basic_string(begin(), end(), a); - } - -#endif// nssv_CPP11_OR_GREATER - -#endif// nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS - }; - - // - // Non-member functions: - // - - // 24.4.3 Non-member comparison functions: - // lexicographically compare two string views (function template): - - template - nssv_constexpr bool - operator==(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) == 0; - } - - template - nssv_constexpr bool - operator!=(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) != 0; - } - - template - nssv_constexpr bool - operator<(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) < 0; - } - - template - nssv_constexpr bool - operator<=(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) <= 0; - } - - template - nssv_constexpr bool - operator>(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) > 0; - } - - template - nssv_constexpr bool - operator>=(basic_string_view lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) >= 0; - } - - // Let S be basic_string_view, and sv be an instance of S. - // Implementations shall provide sufficient additional overloads marked - // constexpr and noexcept so that an object t with an implicit conversion - // to S can be compared according to Table 67. - -#if !nssv_CPP11_OR_GREATER || nssv_BETWEEN(nssv_COMPILER_MSVC_VERSION, 100, 141) - - // accomodate for older compilers: - - // == - - template - nssv_constexpr bool operator==(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) == 0; - } - - template - nssv_constexpr bool operator==(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) == 0; - } - - template - nssv_constexpr bool - operator==(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; - } - - template - nssv_constexpr bool - operator==(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; - } - - // != - - template - nssv_constexpr bool operator!=(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) != 0; - } - - template - nssv_constexpr bool operator!=(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) != 0; - } - - template - nssv_constexpr bool - operator!=(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.size() != rhs.size() && lhs.compare(rhs) != 0; - } - - template - nssv_constexpr bool - operator!=(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return lhs.size() != rhs.size() || rhs.compare(lhs) != 0; - } - - // < - - template - nssv_constexpr bool operator<(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) < 0; - } - - template - nssv_constexpr bool operator<(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) > 0; - } - - template - nssv_constexpr bool - operator<(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.compare(rhs) < 0; - } - - template - nssv_constexpr bool - operator<(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return rhs.compare(lhs) > 0; - } - - // <= - - template - nssv_constexpr bool operator<=(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) <= 0; - } - - template - nssv_constexpr bool operator<=(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) >= 0; - } - - template - nssv_constexpr bool - operator<=(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.compare(rhs) <= 0; - } - - template - nssv_constexpr bool - operator<=(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return rhs.compare(lhs) >= 0; - } - - // > - - template - nssv_constexpr bool operator>(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) > 0; - } - - template - nssv_constexpr bool operator>(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) < 0; - } - - template - nssv_constexpr bool - operator>(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.compare(rhs) > 0; - } - - template - nssv_constexpr bool - operator>(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return rhs.compare(lhs) < 0; - } - - // >= - - template - nssv_constexpr bool operator>=(basic_string_view lhs, char const *rhs) nssv_noexcept - { - return lhs.compare(rhs) >= 0; - } - - template - nssv_constexpr bool operator>=(char const *lhs, basic_string_view rhs) nssv_noexcept - { - return rhs.compare(lhs) <= 0; - } - - template - nssv_constexpr bool - operator>=(basic_string_view lhs, std::basic_string rhs) nssv_noexcept - { - return lhs.compare(rhs) >= 0; - } - - template - nssv_constexpr bool - operator>=(std::basic_string rhs, basic_string_view lhs) nssv_noexcept - { - return rhs.compare(lhs) <= 0; - } - -#else// newer compilers: - -#define nssv_BASIC_STRING_VIEW_I(T, U) typename std::decay>::type - -#if nssv_BETWEEN(nssv_COMPILER_MSVC_VERSION, 140, 150) -#define nssv_MSVC_ORDER(x) , int = x -#else -#define nssv_MSVC_ORDER(x) /*, int=x*/ -#endif - - // == - - template - nssv_constexpr bool - operator==(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.compare(rhs) == 0; - } - - template - nssv_constexpr bool - operator==(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; - } - - // != - - template - nssv_constexpr bool - operator!=(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.size() != rhs.size() || lhs.compare(rhs) != 0; - } - - template - nssv_constexpr bool - operator!=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) != 0; - } - - // < - - template - nssv_constexpr bool - operator<(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.compare(rhs) < 0; - } - - template - nssv_constexpr bool - operator<(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) < 0; - } - - // <= - - template - nssv_constexpr bool - operator<=(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.compare(rhs) <= 0; - } - - template - nssv_constexpr bool - operator<=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) <= 0; - } - - // > - - template - nssv_constexpr bool - operator>(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.compare(rhs) > 0; - } - - template - nssv_constexpr bool - operator>(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) > 0; - } - - // >= - - template - nssv_constexpr bool - operator>=(basic_string_view lhs, nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept - { - return lhs.compare(rhs) >= 0; - } - - template - nssv_constexpr bool - operator>=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, basic_string_view rhs) nssv_noexcept - { - return lhs.compare(rhs) >= 0; - } - -#undef nssv_MSVC_ORDER -#undef nssv_BASIC_STRING_VIEW_I - -#endif// compiler-dependent approach to comparisons - - // 24.4.4 Inserters and extractors: - - namespace detail { - - template - void write_padding(Stream &os, std::streamsize n) - { - for (std::streamsize i = 0; i < n; ++i) os.rdbuf()->sputc(os.fill()); - } - - template - Stream &write_to_stream(Stream &os, View const &sv) - { - typename Stream::sentry sentry(os); - - if (!os) return os; - - const std::streamsize length = static_cast(sv.length()); - - // Whether, and how, to pad: - const bool pad = (length < os.width()); - const bool left_pad = pad && (os.flags() & std::ios_base::adjustfield) == std::ios_base::right; - - if (left_pad) write_padding(os, os.width() - length); - - // Write span characters: - os.rdbuf()->sputn(sv.begin(), length); - - if (pad && !left_pad) write_padding(os, os.width() - length); - - // Reset output stream width: - os.width(0); - - return os; - } - - }// namespace detail - - template - std::basic_ostream & - operator<<(std::basic_ostream &os, basic_string_view sv) - { - return detail::write_to_stream(os, sv); - } - - // Several typedefs for common character types are provided: - - typedef basic_string_view string_view; - typedef basic_string_view wstring_view; -#if nssv_HAVE_WCHAR16_T - typedef basic_string_view u16string_view; - typedef basic_string_view u32string_view; -#endif - - }// namespace sv_lite -}// namespace nonstd::sv_lite - -// -// 24.4.6 Suffix for basic_string_view literals: -// - -#if nssv_HAVE_USER_DEFINED_LITERALS - -namespace nonstd { -nssv_inline_ns namespace literals -{ - nssv_inline_ns namespace string_view_literals - { - -#if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS - - nssv_constexpr nonstd::sv_lite::string_view operator"" sv(const char *str, size_t len) nssv_noexcept// (1) - { - return nonstd::sv_lite::string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::u16string_view operator"" sv(const char16_t *str, - size_t len) nssv_noexcept// (2) - { - return nonstd::sv_lite::u16string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::u32string_view operator"" sv(const char32_t *str, - size_t len) nssv_noexcept// (3) - { - return nonstd::sv_lite::u32string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::wstring_view operator"" sv(const wchar_t *str, size_t len) nssv_noexcept// (4) - { - return nonstd::sv_lite::wstring_view{str, len}; - } - -#endif// nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS - -#if nssv_CONFIG_USR_SV_OPERATOR - - nssv_constexpr nonstd::sv_lite::string_view operator"" _sv(const char *str, size_t len) nssv_noexcept// (1) - { - return nonstd::sv_lite::string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::u16string_view operator"" _sv(const char16_t *str, - size_t len) nssv_noexcept// (2) - { - return nonstd::sv_lite::u16string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::u32string_view operator"" _sv(const char32_t *str, - size_t len) nssv_noexcept// (3) - { - return nonstd::sv_lite::u32string_view{str, len}; - } - - nssv_constexpr nonstd::sv_lite::wstring_view operator"" _sv(const wchar_t *str, size_t len) nssv_noexcept// (4) - { - return nonstd::sv_lite::wstring_view{str, len}; - } - -#endif// nssv_CONFIG_USR_SV_OPERATOR - } -} -}// namespace nonstd - -#endif - -// -// Extensions for std::string: -// - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -namespace nonstd { -namespace sv_lite { - -// Exclude MSVC 14 (19.00): it yields ambiguous to_string(): - -#if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140 - -template> -std::basic_string -to_string(basic_string_view v, Allocator const &a = Allocator()) -{ - return std::basic_string(v.begin(), v.end(), a); -} - -#else - -template -std::basic_string -to_string(basic_string_view v) -{ - return std::basic_string(v.begin(), v.end()); -} - -template -std::basic_string -to_string(basic_string_view v, Allocator const &a) -{ - return std::basic_string(v.begin(), v.end(), a); -} - -#endif// nssv_CPP11_OR_GREATER - -template -basic_string_view -to_string_view(std::basic_string const &s) -{ - return basic_string_view(s.data(), s.size()); -} - -}// namespace sv_lite -}// namespace nonstd - -#endif// nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS - -// -// make types and algorithms available in namespace nonstd: -// - -namespace nonstd { - -using sv_lite::basic_string_view; -using sv_lite::string_view; -using sv_lite::wstring_view; - -#if nssv_HAVE_WCHAR16_T -using sv_lite::u16string_view; -#endif -#if nssv_HAVE_WCHAR32_T -using sv_lite::u32string_view; -#endif - -// literal "sv" - -using sv_lite::operator==; -using sv_lite::operator!=; -using sv_lite::operator<; -using sv_lite::operator<=; -using sv_lite::operator>; -using sv_lite::operator>=; - -using sv_lite::operator<<; - -#if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS -using sv_lite::to_string; -using sv_lite::to_string_view; -#endif - -}// namespace nonstd - -// 24.4.5 Hash support (C++11): - -// Note: The hash value of a string view object is equal to the hash value of -// the corresponding string object. - -#if nssv_HAVE_STD_HASH - -#include - -namespace std { - -template<> -struct hash { -public: - std::size_t operator()(nonstd::string_view v) const nssv_noexcept - { - return std::hash()(std::string(v.data(), v.size())); - } -}; - -template<> -struct hash { -public: - std::size_t operator()(nonstd::wstring_view v) const nssv_noexcept - { - return std::hash()(std::wstring(v.data(), v.size())); - } -}; - -template<> -struct hash { -public: - std::size_t operator()(nonstd::u16string_view v) const nssv_noexcept - { - return std::hash()(std::u16string(v.data(), v.size())); - } -}; - -template<> -struct hash { -public: - std::size_t operator()(nonstd::u32string_view v) const nssv_noexcept - { - return std::hash()(std::u32string(v.data(), v.size())); - } -}; - -}// namespace std - -#endif// nssv_HAVE_STD_HASH - -nssv_RESTORE_WARNINGS() - -#endif// nssv_HAVE_STD_STRING_VIEW -namespace sled { -using namespace nonstd; -} -#endif// NONSTD_SV_LITE_H_INCLUDED - -namespace inja { - -/*! - * \brief Class for lexer configuration. - */ -struct LexerConfig { - std::string statement_open{"{%"}; - std::string statement_open_no_lstrip{"{%+"}; - std::string statement_open_force_lstrip{"{%-"}; - std::string statement_close{"%}"}; - std::string statement_close_force_rstrip{"-%}"}; - std::string line_statement{"##"}; - std::string expression_open{"{{"}; - std::string expression_open_force_lstrip{"{{-"}; - std::string expression_close{"}}"}; - std::string expression_close_force_rstrip{"-}}"}; - std::string comment_open{"{#"}; - std::string comment_open_force_lstrip{"{#-"}; - std::string comment_close{"#}"}; - std::string comment_close_force_rstrip{"-#}"}; - std::string open_chars{"#{"}; - - bool trim_blocks{false}; - bool lstrip_blocks{false}; - - void update_open_chars() - { - open_chars = ""; - if (open_chars.find(line_statement[0]) == std::string::npos) { open_chars += line_statement[0]; } - if (open_chars.find(statement_open[0]) == std::string::npos) { open_chars += statement_open[0]; } - if (open_chars.find(statement_open_no_lstrip[0]) == std::string::npos) { - open_chars += statement_open_no_lstrip[0]; - } - if (open_chars.find(statement_open_force_lstrip[0]) == std::string::npos) { - open_chars += statement_open_force_lstrip[0]; - } - if (open_chars.find(expression_open[0]) == std::string::npos) { open_chars += expression_open[0]; } - if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) { - open_chars += expression_open_force_lstrip[0]; - } - if (open_chars.find(comment_open[0]) == std::string::npos) { open_chars += comment_open[0]; } - if (open_chars.find(comment_open_force_lstrip[0]) == std::string::npos) { - open_chars += comment_open_force_lstrip[0]; - } - } -}; - -/*! - * \brief Class for parser configuration. - */ -struct ParserConfig { - bool search_included_templates_in_files{true}; -}; - -/*! - * \brief Class for render configuration. - */ -struct RenderConfig { - bool throw_at_missing_includes{true}; -}; - -}// namespace inja - -#endif// INCLUDE_INJA_CONFIG_HPP_ +// #include "template.hpp" +#ifndef INCLUDE_INJA_TEMPLATE_HPP_ +#define INCLUDE_INJA_TEMPLATE_HPP_ + +#include +#include +#include +#include + +// #include "node.hpp" +#ifndef INCLUDE_INJA_NODE_HPP_ +#define INCLUDE_INJA_NODE_HPP_ + +#include "sled/nonstd/string_view.h" +#include +// #include +#include // #include "function_storage.hpp" #ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_ #define INCLUDE_INJA_FUNCTION_STORAGE_HPP_ +// #include +#include "sled/nonstd/string_view.h" #include -// #include "string_view.hpp" - namespace inja { -using json = nlohmann::json; - using Arguments = std::vector; using CallbackFunction = std::function; using VoidCallbackFunction = std::function; @@ -1729,8 +154,6 @@ public: Super, Join, Callback, - ParenLeft, - ParenRight, None, }; @@ -1780,18 +203,18 @@ private: }; public: - void add_builtin(nonstd::string_view name, int num_args, Operation op) + void add_builtin(sled::string_view name, int num_args, Operation op) { function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData{op}); } - void add_callback(nonstd::string_view name, int num_args, const CallbackFunction &callback) + void add_callback(sled::string_view name, int num_args, const CallbackFunction &callback) { function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData{Operation::Callback, callback}); } - FunctionData find_function(nonstd::string_view name, int num_args) const + FunctionData find_function(sled::string_view name, int num_args) const { auto it = function_storage.find(std::make_pair(static_cast(name), num_args)); if (it != function_storage.end()) { @@ -1811,18 +234,15 @@ public: #endif// INCLUDE_INJA_FUNCTION_STORAGE_HPP_ -// #include "parser.hpp" -#ifndef INCLUDE_INJA_PARSER_HPP_ -#define INCLUDE_INJA_PARSER_HPP_ +// #include "utils.hpp" +#ifndef INCLUDE_INJA_UTILS_HPP_ +#define INCLUDE_INJA_UTILS_HPP_ -#include -#include -#include +#include +#include #include +#include #include -#include - -// #include "config.hpp" // #include "exceptions.hpp" #ifndef INCLUDE_INJA_EXCEPTIONS_HPP_ @@ -1879,8 +299,8 @@ struct FileError : public InjaError { {} }; -struct JsonError : public InjaError { - explicit JsonError(const std::string &message, SourceLocation location) : InjaError("json_error", message, location) +struct DataError : public InjaError { + explicit DataError(const std::string &message, SourceLocation location) : InjaError("data_error", message, location) {} }; @@ -1888,157 +308,40 @@ struct JsonError : public InjaError { #endif// INCLUDE_INJA_EXCEPTIONS_HPP_ -// #include "function_storage.hpp" - -// #include "lexer.hpp" -#ifndef INCLUDE_INJA_LEXER_HPP_ -#define INCLUDE_INJA_LEXER_HPP_ - -#include -#include - -// #include "config.hpp" - -// #include "token.hpp" -#ifndef INCLUDE_INJA_TOKEN_HPP_ -#define INCLUDE_INJA_TOKEN_HPP_ - -#include - -// #include "string_view.hpp" - namespace inja { -/*! - * \brief Helper-class for the inja Lexer. - */ -struct Token { - enum class Kind { - Text, - ExpressionOpen, // {{ - ExpressionClose, // }} - LineStatementOpen, // ## - LineStatementClose,// \n - StatementOpen, // {% - StatementClose, // %} - CommentOpen, // {# - CommentClose, // #} - Id, // this, this.foo - Number, // 1, 2, -1, 5.2, -5.3 - String, // "this" - Plus, // + - Minus, // - - Times, // * - Slash, // / - Percent, // % - Power, // ^ - Comma, // , - Dot, // . - Colon, // : - LeftParen, // ( - RightParen, // ) - LeftBracket, // [ - RightBracket, // ] - LeftBrace, // { - RightBrace, // } - Equal, // == - NotEqual, // != - GreaterThan, // > - GreaterEqual, // >= - LessThan, // < - LessEqual, // <= - Unknown, - Eof, - }; - - Kind kind{Kind::Unknown}; - nonstd::string_view text; - - explicit constexpr Token() = default; - - explicit constexpr Token(Kind kind, nonstd::string_view text) : kind(kind), text(text) {} - - std::string describe() const - { - switch (kind) { - case Kind::Text: - return ""; - case Kind::LineStatementClose: - return ""; - case Kind::Eof: - return ""; - default: - return static_cast(text); - } - } -}; - -}// namespace inja - -#endif// INCLUDE_INJA_TOKEN_HPP_ - -// #include "utils.hpp" -#ifndef INCLUDE_INJA_UTILS_HPP_ -#define INCLUDE_INJA_UTILS_HPP_ - -#include -#include -#include -#include - -// #include "exceptions.hpp" - -// #include "string_view.hpp" - -namespace inja { - -inline void -open_file_or_throw(const std::string &path, std::ifstream &file) -{ - file.exceptions(std::ifstream::failbit | std::ifstream::badbit); -#ifndef INJA_NOEXCEPTION - try { - file.open(path); - } catch (const std::ios_base::failure & /*e*/) { - INJA_THROW(FileError("failed accessing file at '" + path + "'")); - } -#else - file.open(path); -#endif -} - namespace string_view { -inline nonstd::string_view -slice(nonstd::string_view view, size_t start, size_t end) +inline sled::string_view +slice(sled::string_view view, size_t start, size_t end) { start = std::min(start, view.size()); end = std::min(std::max(start, end), view.size()); return view.substr(start, end - start); } -inline std::pair -split(nonstd::string_view view, char Separator) +inline std::pair +split(sled::string_view view, char Separator) { size_t idx = view.find(Separator); - if (idx == nonstd::string_view::npos) { return std::make_pair(view, nonstd::string_view()); } - return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, nonstd::string_view::npos)); + if (idx == sled::string_view::npos) { return std::make_pair(view, sled::string_view()); } + return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, sled::string_view::npos)); } inline bool -starts_with(nonstd::string_view view, nonstd::string_view prefix) +starts_with(sled::string_view view, sled::string_view prefix) { return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0); } }// namespace string_view inline SourceLocation -get_source_location(nonstd::string_view content, size_t pos) +get_source_location(sled::string_view content, size_t pos) { // Get line and offset position (starts at 1:1) auto sliced = string_view::slice(content, 0, pos); std::size_t last_newline = sliced.rfind("\n"); - if (last_newline == nonstd::string_view::npos) { return {1, sliced.length() + 1}; } + if (last_newline == sled::string_view::npos) { return {1, sliced.length() + 1}; } // Count newlines size_t count_lines = 0; @@ -2055,7 +358,7 @@ get_source_location(nonstd::string_view content, size_t pos) inline void replace_substring(std::string &s, const std::string &f, const std::string &t) { - if (f.empty()) return; + if (f.empty()) { return; } for (auto pos = s.find(f); // find first occurrence of f pos != std::string::npos; // make sure f was found s.replace(pos, f.size(), t), // replace with t, and @@ -2069,435 +372,12 @@ replace_substring(std::string &s, const std::string &f, const std::string &t) namespace inja { -/*! - * \brief Class for lexing an inja Template. - */ -class Lexer { - enum class State { - Text, - ExpressionStart, - ExpressionStartForceLstrip, - ExpressionBody, - LineStart, - LineBody, - StatementStart, - StatementStartNoLstrip, - StatementStartForceLstrip, - StatementBody, - CommentStart, - CommentStartForceLstrip, - CommentBody, - }; - - enum class MinusState { - Operator, - Number, - }; - - const LexerConfig &config; - - State state; - MinusState minus_state; - nonstd::string_view m_in; - size_t tok_start; - size_t pos; - - Token scan_body(nonstd::string_view close, - Token::Kind closeKind, - nonstd::string_view close_trim = nonstd::string_view(), - bool trim = false) - { - again: - // skip whitespace (except for \n as it might be a close) - if (tok_start >= m_in.size()) { return make_token(Token::Kind::Eof); } - const char ch = m_in[tok_start]; - if (ch == ' ' || ch == '\t' || ch == '\r') { - tok_start += 1; - goto again; - } - - // check for close - if (!close_trim.empty() && inja::string_view::starts_with(m_in.substr(tok_start), close_trim)) { - state = State::Text; - pos = tok_start + close_trim.size(); - const Token tok = make_token(closeKind); - skip_whitespaces_and_newlines(); - return tok; - } - - if (inja::string_view::starts_with(m_in.substr(tok_start), close)) { - state = State::Text; - pos = tok_start + close.size(); - const Token tok = make_token(closeKind); - if (trim) { skip_whitespaces_and_first_newline(); } - return tok; - } - - // skip \n - if (ch == '\n') { - tok_start += 1; - goto again; - } - - pos = tok_start + 1; - if (std::isalpha(ch)) { - minus_state = MinusState::Operator; - return scan_id(); - } - - const MinusState current_minus_state = minus_state; - if (minus_state == MinusState::Operator) { minus_state = MinusState::Number; } - - switch (ch) { - case '+': - return make_token(Token::Kind::Plus); - case '-': - if (current_minus_state == MinusState::Operator) { return make_token(Token::Kind::Minus); } - return scan_number(); - case '*': - return make_token(Token::Kind::Times); - case '/': - return make_token(Token::Kind::Slash); - case '^': - return make_token(Token::Kind::Power); - case '%': - return make_token(Token::Kind::Percent); - case '.': - return make_token(Token::Kind::Dot); - case ',': - return make_token(Token::Kind::Comma); - case ':': - return make_token(Token::Kind::Colon); - case '(': - return make_token(Token::Kind::LeftParen); - case ')': - minus_state = MinusState::Operator; - return make_token(Token::Kind::RightParen); - case '[': - return make_token(Token::Kind::LeftBracket); - case ']': - minus_state = MinusState::Operator; - return make_token(Token::Kind::RightBracket); - case '{': - return make_token(Token::Kind::LeftBrace); - case '}': - minus_state = MinusState::Operator; - return make_token(Token::Kind::RightBrace); - case '>': - if (pos < m_in.size() && m_in[pos] == '=') { - pos += 1; - return make_token(Token::Kind::GreaterEqual); - } - return make_token(Token::Kind::GreaterThan); - case '<': - if (pos < m_in.size() && m_in[pos] == '=') { - pos += 1; - return make_token(Token::Kind::LessEqual); - } - return make_token(Token::Kind::LessThan); - case '=': - if (pos < m_in.size() && m_in[pos] == '=') { - pos += 1; - return make_token(Token::Kind::Equal); - } - return make_token(Token::Kind::Unknown); - case '!': - if (pos < m_in.size() && m_in[pos] == '=') { - pos += 1; - return make_token(Token::Kind::NotEqual); - } - return make_token(Token::Kind::Unknown); - case '\"': - return scan_string(); - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - minus_state = MinusState::Operator; - return scan_number(); - case '_': - case '@': - case '$': - minus_state = MinusState::Operator; - return scan_id(); - default: - return make_token(Token::Kind::Unknown); - } - } - - Token scan_id() - { - for (;;) { - if (pos >= m_in.size()) { break; } - const char ch = m_in[pos]; - if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') { break; } - pos += 1; - } - return make_token(Token::Kind::Id); - } - - Token scan_number() - { - for (;;) { - if (pos >= m_in.size()) { break; } - const char ch = m_in[pos]; - // be very permissive in lexer (we'll catch errors when conversion happens) - if (!std::isdigit(ch) && ch != '.' && ch != 'e' && ch != 'E' && ch != '+' && ch != '-') { break; } - pos += 1; - } - return make_token(Token::Kind::Number); - } - - Token scan_string() - { - bool escape{false}; - for (;;) { - if (pos >= m_in.size()) { break; } - const char ch = m_in[pos++]; - if (ch == '\\') { - escape = true; - } else if (!escape && ch == m_in[tok_start]) { - break; - } else { - escape = false; - } - } - return make_token(Token::Kind::String); - } - - Token make_token(Token::Kind kind) const { return Token(kind, string_view::slice(m_in, tok_start, pos)); } - - void skip_whitespaces_and_newlines() - { - if (pos < m_in.size()) { - while (pos < m_in.size() - && (m_in[pos] == ' ' || m_in[pos] == '\t' || m_in[pos] == '\n' || m_in[pos] == '\r')) { - pos += 1; - } - } - } - - void skip_whitespaces_and_first_newline() - { - if (pos < m_in.size()) { - while (pos < m_in.size() && (m_in[pos] == ' ' || m_in[pos] == '\t')) { pos += 1; } - } - - if (pos < m_in.size()) { - const char ch = m_in[pos]; - if (ch == '\n') { - pos += 1; - } else if (ch == '\r') { - pos += 1; - if (pos < m_in.size() && m_in[pos] == '\n') { pos += 1; } - } - } - } - - static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text) - { - nonstd::string_view result = text; - while (!result.empty()) { - const char ch = result.back(); - if (ch == ' ' || ch == '\t') { - result.remove_suffix(1); - } else if (ch == '\n' || ch == '\r') { - break; - } else { - return text; - } - } - return result; - } - -public: - explicit Lexer(const LexerConfig &config) : config(config), state(State::Text), minus_state(MinusState::Number) {} - - SourceLocation current_position() const { return get_source_location(m_in, tok_start); } - - void start(nonstd::string_view input) - { - m_in = input; - tok_start = 0; - pos = 0; - state = State::Text; - minus_state = MinusState::Number; - - // Consume byte order mark (BOM) for UTF-8 - if (inja::string_view::starts_with(m_in, "\xEF\xBB\xBF")) { m_in = m_in.substr(3); } - } - - Token scan() - { - tok_start = pos; - - again: - if (tok_start >= m_in.size()) { return make_token(Token::Kind::Eof); } - - switch (state) { - default: - case State::Text: { - // fast-scan to first open character - const size_t open_start = m_in.substr(pos).find_first_of(config.open_chars); - if (open_start == nonstd::string_view::npos) { - // didn't find open, return remaining text as text token - pos = m_in.size(); - return make_token(Token::Kind::Text); - } - pos += open_start; - - // try to match one of the opening sequences, and get the close - nonstd::string_view open_str = m_in.substr(pos); - bool must_lstrip = false; - if (inja::string_view::starts_with(open_str, config.expression_open)) { - if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) { - state = State::ExpressionStartForceLstrip; - must_lstrip = true; - } else { - state = State::ExpressionStart; - } - } else if (inja::string_view::starts_with(open_str, config.statement_open)) { - if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) { - state = State::StatementStartNoLstrip; - } else if (inja::string_view::starts_with(open_str, config.statement_open_force_lstrip)) { - state = State::StatementStartForceLstrip; - must_lstrip = true; - } else { - state = State::StatementStart; - must_lstrip = config.lstrip_blocks; - } - } else if (inja::string_view::starts_with(open_str, config.comment_open)) { - if (inja::string_view::starts_with(open_str, config.comment_open_force_lstrip)) { - state = State::CommentStartForceLstrip; - must_lstrip = true; - } else { - state = State::CommentStart; - must_lstrip = config.lstrip_blocks; - } - } else if ((pos == 0 || m_in[pos - 1] == '\n') - && inja::string_view::starts_with(open_str, config.line_statement)) { - state = State::LineStart; - } else { - pos += 1;// wasn't actually an opening sequence - goto again; - } - - nonstd::string_view text = string_view::slice(m_in, tok_start, pos); - if (must_lstrip) { text = clear_final_line_if_whitespace(text); } - - if (text.empty()) { - goto again;// don't generate empty token - } - return Token(Token::Kind::Text, text); - } - case State::ExpressionStart: { - state = State::ExpressionBody; - pos += config.expression_open.size(); - return make_token(Token::Kind::ExpressionOpen); - } - case State::ExpressionStartForceLstrip: { - state = State::ExpressionBody; - pos += config.expression_open_force_lstrip.size(); - return make_token(Token::Kind::ExpressionOpen); - } - case State::LineStart: { - state = State::LineBody; - pos += config.line_statement.size(); - return make_token(Token::Kind::LineStatementOpen); - } - case State::StatementStart: { - state = State::StatementBody; - pos += config.statement_open.size(); - return make_token(Token::Kind::StatementOpen); - } - case State::StatementStartNoLstrip: { - state = State::StatementBody; - pos += config.statement_open_no_lstrip.size(); - return make_token(Token::Kind::StatementOpen); - } - case State::StatementStartForceLstrip: { - state = State::StatementBody; - pos += config.statement_open_force_lstrip.size(); - return make_token(Token::Kind::StatementOpen); - } - case State::CommentStart: { - state = State::CommentBody; - pos += config.comment_open.size(); - return make_token(Token::Kind::CommentOpen); - } - case State::CommentStartForceLstrip: { - state = State::CommentBody; - pos += config.comment_open_force_lstrip.size(); - return make_token(Token::Kind::CommentOpen); - } - case State::ExpressionBody: - return scan_body(config.expression_close, - Token::Kind::ExpressionClose, - config.expression_close_force_rstrip); - case State::LineBody: - return scan_body("\n", Token::Kind::LineStatementClose); - case State::StatementBody: - return scan_body(config.statement_close, - Token::Kind::StatementClose, - config.statement_close_force_rstrip, - config.trim_blocks); - case State::CommentBody: { - // fast-scan to comment close - const size_t end = m_in.substr(pos).find(config.comment_close); - if (end == nonstd::string_view::npos) { - pos = m_in.size(); - return make_token(Token::Kind::Eof); - } - - // Check for trim pattern - const bool must_rstrip - = inja::string_view::starts_with(m_in.substr(pos + end - 1), config.comment_close_force_rstrip); - - // return the entire comment in the close token - state = State::Text; - pos += end + config.comment_close.size(); - Token tok = make_token(Token::Kind::CommentClose); - - if (must_rstrip || config.trim_blocks) { skip_whitespaces_and_first_newline(); } - return tok; - } - } - } - - const LexerConfig &get_config() const { return config; } -}; - -}// namespace inja - -#endif// INCLUDE_INJA_LEXER_HPP_ - -// #include "node.hpp" -#ifndef INCLUDE_INJA_NODE_HPP_ -#define INCLUDE_INJA_NODE_HPP_ - -#include -#include - -#include - -// #include "function_storage.hpp" - -// #include "string_view.hpp" - -namespace inja { - class NodeVisitor; class BlockNode; class TextNode; class ExpressionNode; class LiteralNode; -class JsonNode; +class DataNode; class FunctionNode; class ExpressionListNode; class StatementNode; @@ -2518,7 +398,7 @@ public: virtual void visit(const TextNode &node) = 0; virtual void visit(const ExpressionNode &node) = 0; virtual void visit(const LiteralNode &node) = 0; - virtual void visit(const JsonNode &node) = 0; + virtual void visit(const DataNode &node) = 0; virtual void visit(const FunctionNode &node) = 0; virtual void visit(const ExpressionListNode &node) = 0; virtual void visit(const StatementNode &node) = 0; @@ -2573,23 +453,24 @@ public: class LiteralNode : public ExpressionNode { public: - const nlohmann::json value; + const json value; - explicit LiteralNode(const nlohmann::json &value, size_t pos) : ExpressionNode(pos), value(value) {} + explicit LiteralNode(sled::string_view data_text, size_t pos) : ExpressionNode(pos), value(json::parse(data_text)) + {} void accept(NodeVisitor &v) const { v.visit(*this); } }; -class JsonNode : public ExpressionNode { +class DataNode : public ExpressionNode { public: const std::string name; const json::json_pointer ptr; - static std::string convert_dot_to_json_ptr(nonstd::string_view ptr_name) + static std::string convert_dot_to_ptr(sled::string_view ptr_name) { std::string result; do { - nonstd::string_view part; + sled::string_view part; std::tie(part, ptr_name) = string_view::split(ptr_name, '.'); result.push_back('/'); result.append(part.begin(), part.end()); @@ -2597,10 +478,10 @@ public: return result; } - explicit JsonNode(nonstd::string_view ptr_name, size_t pos) + explicit DataNode(sled::string_view ptr_name, size_t pos) : ExpressionNode(pos), name(ptr_name), - ptr(json::json_pointer(convert_dot_to_json_ptr(ptr_name))) + ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} void accept(NodeVisitor &v) const { v.visit(*this); } @@ -2621,17 +502,17 @@ public: Op operation; std::string name; - int number_args;// Should also be negative -> -1 for unknown number + int number_args;// Can also be negative -> -1 for unknown number std::vector> arguments; CallbackFunction callback; - explicit FunctionNode(nonstd::string_view name, size_t pos) + explicit FunctionNode(sled::string_view name, size_t pos) : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), - number_args(1) + number_args(0) {} explicit FunctionNode(Op operation, size_t pos) : ExpressionNode(pos), operation(operation), number_args(1) @@ -2827,7 +708,7 @@ public: explicit ExtendsStatementNode(const std::string &file, size_t pos) : StatementNode(pos), file(file) {} - void accept(NodeVisitor &v) const { v.visit(*this); }; + void accept(NodeVisitor &v) const { v.visit(*this); } }; class BlockStatementNode : public StatementNode { @@ -2842,7 +723,7 @@ public: parent(parent) {} - void accept(NodeVisitor &v) const { v.visit(*this); }; + void accept(NodeVisitor &v) const { v.visit(*this); } }; class SetStatementNode : public StatementNode { @@ -2859,17 +740,6 @@ public: #endif// INCLUDE_INJA_NODE_HPP_ -// #include "template.hpp" -#ifndef INCLUDE_INJA_TEMPLATE_HPP_ -#define INCLUDE_INJA_TEMPLATE_HPP_ - -#include -#include -#include -#include - -// #include "node.hpp" - // #include "statistics.hpp" #ifndef INCLUDE_INJA_STATISTICS_HPP_ #define INCLUDE_INJA_STATISTICS_HPP_ @@ -2893,7 +763,7 @@ class StatisticsVisitor : public NodeVisitor { void visit(const LiteralNode &) {} - void visit(const JsonNode &) { variable_counter += 1; } + void visit(const DataNode &) { variable_counter += 1; } void visit(const FunctionNode &node) { @@ -2972,11 +842,600 @@ using TemplateStorage = std::map; #endif// INCLUDE_INJA_TEMPLATE_HPP_ +namespace inja { + +/*! + * \brief Class for lexer configuration. + */ +struct LexerConfig { + std::string statement_open{"{%"}; + std::string statement_open_no_lstrip{"{%+"}; + std::string statement_open_force_lstrip{"{%-"}; + std::string statement_close{"%}"}; + std::string statement_close_force_rstrip{"-%}"}; + std::string line_statement{"##"}; + std::string expression_open{"{{"}; + std::string expression_open_force_lstrip{"{{-"}; + std::string expression_close{"}}"}; + std::string expression_close_force_rstrip{"-}}"}; + std::string comment_open{"{#"}; + std::string comment_open_force_lstrip{"{#-"}; + std::string comment_close{"#}"}; + std::string comment_close_force_rstrip{"-#}"}; + std::string open_chars{"#{"}; + + bool trim_blocks{false}; + bool lstrip_blocks{false}; + + void update_open_chars() + { + open_chars = ""; + if (open_chars.find(line_statement[0]) == std::string::npos) { open_chars += line_statement[0]; } + if (open_chars.find(statement_open[0]) == std::string::npos) { open_chars += statement_open[0]; } + if (open_chars.find(statement_open_no_lstrip[0]) == std::string::npos) { + open_chars += statement_open_no_lstrip[0]; + } + if (open_chars.find(statement_open_force_lstrip[0]) == std::string::npos) { + open_chars += statement_open_force_lstrip[0]; + } + if (open_chars.find(expression_open[0]) == std::string::npos) { open_chars += expression_open[0]; } + if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) { + open_chars += expression_open_force_lstrip[0]; + } + if (open_chars.find(comment_open[0]) == std::string::npos) { open_chars += comment_open[0]; } + if (open_chars.find(comment_open_force_lstrip[0]) == std::string::npos) { + open_chars += comment_open_force_lstrip[0]; + } + } +}; + +/*! + * \brief Class for parser configuration. + */ +struct ParserConfig { + bool search_included_templates_in_files{true}; + + std::function include_callback; +}; + +/*! + * \brief Class for render configuration. + */ +struct RenderConfig { + bool throw_at_missing_includes{true}; +}; + +}// namespace inja + +#endif// INCLUDE_INJA_CONFIG_HPP_ + +// #include "function_storage.hpp" + +// #include "parser.hpp" +#ifndef INCLUDE_INJA_PARSER_HPP_ +#define INCLUDE_INJA_PARSER_HPP_ + +#include +#include +#include +#include +#include + +// #include "config.hpp" + +// #include "exceptions.hpp" + +// #include "function_storage.hpp" + +// #include "lexer.hpp" +#ifndef INCLUDE_INJA_LEXER_HPP_ +#define INCLUDE_INJA_LEXER_HPP_ + +#include +#include + +// #include "config.hpp" + // #include "token.hpp" +#ifndef INCLUDE_INJA_TOKEN_HPP_ +#define INCLUDE_INJA_TOKEN_HPP_ + +#include +#include + +namespace inja { + +/*! + * \brief Helper-class for the inja Lexer. + */ +struct Token { + enum class Kind { + Text, + ExpressionOpen, // {{ + ExpressionClose, // }} + LineStatementOpen, // ## + LineStatementClose,// \n + StatementOpen, // {% + StatementClose, // %} + CommentOpen, // {# + CommentClose, // #} + Id, // this, this.foo + Number, // 1, 2, -1, 5.2, -5.3 + String, // "this" + Plus, // + + Minus, // - + Times, // * + Slash, // / + Percent, // % + Power, // ^ + Comma, // , + Dot, // . + Colon, // : + LeftParen, // ( + RightParen, // ) + LeftBracket, // [ + RightBracket, // ] + LeftBrace, // { + RightBrace, // } + Equal, // == + NotEqual, // != + GreaterThan, // > + GreaterEqual, // >= + LessThan, // < + LessEqual, // <= + Unknown, + Eof, + }; + + Kind kind{Kind::Unknown}; + sled::string_view text; + + explicit constexpr Token() = default; + + explicit constexpr Token(Kind kind, sled::string_view text) : kind(kind), text(text) {} + + std::string describe() const + { + switch (kind) { + case Kind::Text: + return ""; + case Kind::LineStatementClose: + return ""; + case Kind::Eof: + return ""; + default: + return static_cast(text); + } + } +}; + +}// namespace inja + +#endif// INCLUDE_INJA_TOKEN_HPP_ // #include "utils.hpp" -#include +namespace inja { + +/*! + * \brief Class for lexing an inja Template. + */ +class Lexer { + enum class State { + Text, + ExpressionStart, + ExpressionStartForceLstrip, + ExpressionBody, + LineStart, + LineBody, + StatementStart, + StatementStartNoLstrip, + StatementStartForceLstrip, + StatementBody, + CommentStart, + CommentStartForceLstrip, + CommentBody, + }; + + enum class MinusState { + Operator, + Number, + }; + + const LexerConfig &config; + + State state; + MinusState minus_state; + sled::string_view m_in; + size_t tok_start; + size_t pos; + + Token scan_body(sled::string_view close, + Token::Kind closeKind, + sled::string_view close_trim = sled::string_view(), + bool trim = false) + { + again: + // skip whitespace (except for \n as it might be a close) + if (tok_start >= m_in.size()) { return make_token(Token::Kind::Eof); } + const char ch = m_in[tok_start]; + if (ch == ' ' || ch == '\t' || ch == '\r') { + tok_start += 1; + goto again; + } + + // check for close + if (!close_trim.empty() && inja::string_view::starts_with(m_in.substr(tok_start), close_trim)) { + state = State::Text; + pos = tok_start + close_trim.size(); + const Token tok = make_token(closeKind); + skip_whitespaces_and_newlines(); + return tok; + } + + if (inja::string_view::starts_with(m_in.substr(tok_start), close)) { + state = State::Text; + pos = tok_start + close.size(); + const Token tok = make_token(closeKind); + if (trim) { skip_whitespaces_and_first_newline(); } + return tok; + } + + // skip \n + if (ch == '\n') { + tok_start += 1; + goto again; + } + + pos = tok_start + 1; + if (std::isalpha(ch)) { + minus_state = MinusState::Operator; + return scan_id(); + } + + const MinusState current_minus_state = minus_state; + if (minus_state == MinusState::Operator) { minus_state = MinusState::Number; } + + switch (ch) { + case '+': + return make_token(Token::Kind::Plus); + case '-': + if (current_minus_state == MinusState::Operator) { return make_token(Token::Kind::Minus); } + return scan_number(); + case '*': + return make_token(Token::Kind::Times); + case '/': + return make_token(Token::Kind::Slash); + case '^': + return make_token(Token::Kind::Power); + case '%': + return make_token(Token::Kind::Percent); + case '.': + return make_token(Token::Kind::Dot); + case ',': + return make_token(Token::Kind::Comma); + case ':': + return make_token(Token::Kind::Colon); + case '(': + return make_token(Token::Kind::LeftParen); + case ')': + minus_state = MinusState::Operator; + return make_token(Token::Kind::RightParen); + case '[': + return make_token(Token::Kind::LeftBracket); + case ']': + minus_state = MinusState::Operator; + return make_token(Token::Kind::RightBracket); + case '{': + return make_token(Token::Kind::LeftBrace); + case '}': + minus_state = MinusState::Operator; + return make_token(Token::Kind::RightBrace); + case '>': + if (pos < m_in.size() && m_in[pos] == '=') { + pos += 1; + return make_token(Token::Kind::GreaterEqual); + } + return make_token(Token::Kind::GreaterThan); + case '<': + if (pos < m_in.size() && m_in[pos] == '=') { + pos += 1; + return make_token(Token::Kind::LessEqual); + } + return make_token(Token::Kind::LessThan); + case '=': + if (pos < m_in.size() && m_in[pos] == '=') { + pos += 1; + return make_token(Token::Kind::Equal); + } + return make_token(Token::Kind::Unknown); + case '!': + if (pos < m_in.size() && m_in[pos] == '=') { + pos += 1; + return make_token(Token::Kind::NotEqual); + } + return make_token(Token::Kind::Unknown); + case '\"': + return scan_string(); + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + minus_state = MinusState::Operator; + return scan_number(); + case '_': + case '@': + case '$': + minus_state = MinusState::Operator; + return scan_id(); + default: + return make_token(Token::Kind::Unknown); + } + } + + Token scan_id() + { + for (;;) { + if (pos >= m_in.size()) { break; } + const char ch = m_in[pos]; + if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') { break; } + pos += 1; + } + return make_token(Token::Kind::Id); + } + + Token scan_number() + { + for (;;) { + if (pos >= m_in.size()) { break; } + const char ch = m_in[pos]; + // be very permissive in lexer (we'll catch errors when conversion happens) + if (!(std::isdigit(ch) || ch == '.' || ch == 'e' || ch == 'E' + || (ch == '+' && (pos == 0 || m_in[pos - 1] == 'e' || m_in[pos - 1] == 'E')) + || (ch == '-' && (pos == 0 || m_in[pos - 1] == 'e' || m_in[pos - 1] == 'E')))) { + break; + } + pos += 1; + } + return make_token(Token::Kind::Number); + } + + Token scan_string() + { + bool escape{false}; + for (;;) { + if (pos >= m_in.size()) { break; } + const char ch = m_in[pos++]; + if (ch == '\\') { + escape = !escape; + } else if (!escape && ch == m_in[tok_start]) { + break; + } else { + escape = false; + } + } + return make_token(Token::Kind::String); + } + + Token make_token(Token::Kind kind) const { return Token(kind, string_view::slice(m_in, tok_start, pos)); } + + void skip_whitespaces_and_newlines() + { + if (pos < m_in.size()) { + while (pos < m_in.size() + && (m_in[pos] == ' ' || m_in[pos] == '\t' || m_in[pos] == '\n' || m_in[pos] == '\r')) { + pos += 1; + } + } + } + + void skip_whitespaces_and_first_newline() + { + if (pos < m_in.size()) { + while (pos < m_in.size() && (m_in[pos] == ' ' || m_in[pos] == '\t')) { pos += 1; } + } + + if (pos < m_in.size()) { + const char ch = m_in[pos]; + if (ch == '\n') { + pos += 1; + } else if (ch == '\r') { + pos += 1; + if (pos < m_in.size() && m_in[pos] == '\n') { pos += 1; } + } + } + } + + static sled::string_view clear_final_line_if_whitespace(sled::string_view text) + { + sled::string_view result = text; + while (!result.empty()) { + const char ch = result.back(); + if (ch == ' ' || ch == '\t') { + result.remove_suffix(1); + } else if (ch == '\n' || ch == '\r') { + break; + } else { + return text; + } + } + return result; + } + +public: + explicit Lexer(const LexerConfig &config) : config(config), state(State::Text), minus_state(MinusState::Number) {} + + SourceLocation current_position() const { return get_source_location(m_in, tok_start); } + + void start(sled::string_view input) + { + m_in = input; + tok_start = 0; + pos = 0; + state = State::Text; + minus_state = MinusState::Number; + + // Consume byte order mark (BOM) for UTF-8 + if (inja::string_view::starts_with(m_in, "\xEF\xBB\xBF")) { m_in = m_in.substr(3); } + } + + Token scan() + { + tok_start = pos; + + again: + if (tok_start >= m_in.size()) { return make_token(Token::Kind::Eof); } + + switch (state) { + default: + case State::Text: { + // fast-scan to first open character + const size_t open_start = m_in.substr(pos).find_first_of(config.open_chars); + if (open_start == sled::string_view::npos) { + // didn't find open, return remaining text as text token + pos = m_in.size(); + return make_token(Token::Kind::Text); + } + pos += open_start; + + // try to match one of the opening sequences, and get the close + sled::string_view open_str = m_in.substr(pos); + bool must_lstrip = false; + if (inja::string_view::starts_with(open_str, config.expression_open)) { + if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) { + state = State::ExpressionStartForceLstrip; + must_lstrip = true; + } else { + state = State::ExpressionStart; + } + } else if (inja::string_view::starts_with(open_str, config.statement_open)) { + if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) { + state = State::StatementStartNoLstrip; + } else if (inja::string_view::starts_with(open_str, config.statement_open_force_lstrip)) { + state = State::StatementStartForceLstrip; + must_lstrip = true; + } else { + state = State::StatementStart; + must_lstrip = config.lstrip_blocks; + } + } else if (inja::string_view::starts_with(open_str, config.comment_open)) { + if (inja::string_view::starts_with(open_str, config.comment_open_force_lstrip)) { + state = State::CommentStartForceLstrip; + must_lstrip = true; + } else { + state = State::CommentStart; + must_lstrip = config.lstrip_blocks; + } + } else if ((pos == 0 || m_in[pos - 1] == '\n') + && inja::string_view::starts_with(open_str, config.line_statement)) { + state = State::LineStart; + } else { + pos += 1;// wasn't actually an opening sequence + goto again; + } + + sled::string_view text = string_view::slice(m_in, tok_start, pos); + if (must_lstrip) { text = clear_final_line_if_whitespace(text); } + + if (text.empty()) { + goto again;// don't generate empty token + } + return Token(Token::Kind::Text, text); + } + case State::ExpressionStart: { + state = State::ExpressionBody; + pos += config.expression_open.size(); + return make_token(Token::Kind::ExpressionOpen); + } + case State::ExpressionStartForceLstrip: { + state = State::ExpressionBody; + pos += config.expression_open_force_lstrip.size(); + return make_token(Token::Kind::ExpressionOpen); + } + case State::LineStart: { + state = State::LineBody; + pos += config.line_statement.size(); + return make_token(Token::Kind::LineStatementOpen); + } + case State::StatementStart: { + state = State::StatementBody; + pos += config.statement_open.size(); + return make_token(Token::Kind::StatementOpen); + } + case State::StatementStartNoLstrip: { + state = State::StatementBody; + pos += config.statement_open_no_lstrip.size(); + return make_token(Token::Kind::StatementOpen); + } + case State::StatementStartForceLstrip: { + state = State::StatementBody; + pos += config.statement_open_force_lstrip.size(); + return make_token(Token::Kind::StatementOpen); + } + case State::CommentStart: { + state = State::CommentBody; + pos += config.comment_open.size(); + return make_token(Token::Kind::CommentOpen); + } + case State::CommentStartForceLstrip: { + state = State::CommentBody; + pos += config.comment_open_force_lstrip.size(); + return make_token(Token::Kind::CommentOpen); + } + case State::ExpressionBody: + return scan_body(config.expression_close, + Token::Kind::ExpressionClose, + config.expression_close_force_rstrip); + case State::LineBody: + return scan_body("\n", Token::Kind::LineStatementClose); + case State::StatementBody: + return scan_body(config.statement_close, + Token::Kind::StatementClose, + config.statement_close_force_rstrip, + config.trim_blocks); + case State::CommentBody: { + // fast-scan to comment close + const size_t end = m_in.substr(pos).find(config.comment_close); + if (end == sled::string_view::npos) { + pos = m_in.size(); + return make_token(Token::Kind::Eof); + } + + // Check for trim pattern + const bool must_rstrip + = inja::string_view::starts_with(m_in.substr(pos + end - 1), config.comment_close_force_rstrip); + + // return the entire comment in the close token + state = State::Text; + pos += end + config.comment_close.size(); + Token tok = make_token(Token::Kind::CommentClose); + + if (must_rstrip || config.trim_blocks) { skip_whitespaces_and_first_newline(); } + return tok; + } + } + } + + const LexerConfig &get_config() const { return config; } +}; + +}// namespace inja + +#endif// INCLUDE_INJA_LEXER_HPP_ + +// #include "node.hpp" + +// #include "template.hpp" + +// #include "token.hpp" + +// #include "utils.hpp" namespace inja { @@ -2984,6 +1443,9 @@ namespace inja { * \brief Class for parsing an inja Template. */ class Parser { + using Arguments = std::vector>; + using OperatorStack = std::stack>; + const ParserConfig &config; Lexer lexer; @@ -2993,23 +1455,16 @@ class Parser { Token tok, peek_tok; bool have_peek_tok{false}; - size_t current_paren_level{0}; - size_t current_bracket_level{0}; - size_t current_brace_level{0}; - - nonstd::string_view json_literal_start; + sled::string_view literal_start; BlockNode *current_block{nullptr}; ExpressionListNode *current_expression_list{nullptr}; - std::stack> function_stack; - std::vector> arguments; - std::stack> operator_stack; std::stack if_statement_stack; std::stack for_statement_stack; std::stack block_statement_stack; - inline void throw_parser_error(const std::string &message) + inline void throw_parser_error(const std::string &message) const { INJA_THROW(ParserError(message, lexer.current_position())); } @@ -3032,18 +1487,19 @@ class Parser { } } - inline void add_json_literal(const char *content_ptr) + inline void add_literal(Arguments &arguments, const char *content_ptr) { - nonstd::string_view json_text(json_literal_start.data(), - tok.text.data() - json_literal_start.data() + tok.text.size()); - arguments.emplace_back(std::make_shared(json::parse(json_text), json_text.data() - content_ptr)); + sled::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size()); + arguments.emplace_back(std::make_shared(data_text, data_text.data() - content_ptr)); } - inline void add_operator() + inline void add_operator(Arguments &arguments, OperatorStack &operator_stack) { auto function = operator_stack.top(); operator_stack.pop(); + if (static_cast(arguments.size()) < function->number_args) { throw_parser_error("too few arguments"); } + for (int i = 0; i < function->number_args; ++i) { function->arguments.insert(function->arguments.begin(), arguments.back()); arguments.pop_back(); @@ -3051,75 +1507,116 @@ class Parser { arguments.emplace_back(function); } - void add_to_template_storage(nonstd::string_view path, std::string &template_name) + void add_to_template_storage(sled::string_view path, std::string &template_name) { - if (config.search_included_templates_in_files - && template_storage.find(template_name) == template_storage.end()) { + if (template_storage.find(template_name) != template_storage.end()) { return; } + + std::string original_path = static_cast(path); + std::string original_name = template_name; + + if (config.search_included_templates_in_files) { // Build the relative path - template_name = static_cast(path) + template_name; + template_name = original_path + original_name; if (template_name.compare(0, 2, "./") == 0) { template_name.erase(0, 2); } if (template_storage.find(template_name) == template_storage.end()) { - auto include_template = Template(load_file(template_name)); - template_storage.emplace(template_name, include_template); - parse_into_template(template_storage[template_name], template_name); + // Load file + std::ifstream file; + file.open(template_name); + if (!file.fail()) { + std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + + auto include_template = Template(text); + template_storage.emplace(template_name, include_template); + parse_into_template(template_storage[template_name], template_name); + return; + } else if (!config.include_callback) { + INJA_THROW(FileError("failed accessing file at '" + template_name + "'")); + } } } + + // Try include callback + if (config.include_callback) { + auto include_template = config.include_callback(original_path, original_name); + template_storage.emplace(template_name, include_template); + } + } + + std::string parse_filename() const + { + if (tok.kind != Token::Kind::String) { throw_parser_error("expected string, got '" + tok.describe() + "'"); } + + if (tok.text.length() < 2) { + throw_parser_error("expected filename, got '" + static_cast(tok.text) + "'"); + } + + // Remove first and last character "" + return std::string{tok.text.substr(1, tok.text.length() - 2)}; } bool parse_expression(Template &tmpl, Token::Kind closing) { - while (tok.kind != closing && tok.kind != Token::Kind::Eof) { + current_expression_list->root = parse_expression(tmpl); + return tok.kind == closing; + } + + std::shared_ptr parse_expression(Template &tmpl) + { + size_t current_bracket_level{0}; + size_t current_brace_level{0}; + Arguments arguments; + OperatorStack operator_stack; + + while (tok.kind != Token::Kind::Eof) { // Literals switch (tok.kind) { case Token::Kind::String: { if (current_brace_level == 0 && current_bracket_level == 0) { - json_literal_start = tok.text; - add_json_literal(tmpl.content.c_str()); + literal_start = tok.text; + add_literal(arguments, tmpl.content.c_str()); } - } break; case Token::Kind::Number: { if (current_brace_level == 0 && current_bracket_level == 0) { - json_literal_start = tok.text; - add_json_literal(tmpl.content.c_str()); + literal_start = tok.text; + add_literal(arguments, tmpl.content.c_str()); } - } break; case Token::Kind::LeftBracket: { - if (current_brace_level == 0 && current_bracket_level == 0) { json_literal_start = tok.text; } + if (current_brace_level == 0 && current_bracket_level == 0) { literal_start = tok.text; } current_bracket_level += 1; - } break; case Token::Kind::LeftBrace: { - if (current_brace_level == 0 && current_bracket_level == 0) { json_literal_start = tok.text; } + if (current_brace_level == 0 && current_bracket_level == 0) { literal_start = tok.text; } current_brace_level += 1; - } break; case Token::Kind::RightBracket: { if (current_bracket_level == 0) { throw_parser_error("unexpected ']'"); } current_bracket_level -= 1; - if (current_brace_level == 0 && current_bracket_level == 0) { add_json_literal(tmpl.content.c_str()); } - + if (current_brace_level == 0 && current_bracket_level == 0) { + add_literal(arguments, tmpl.content.c_str()); + } } break; case Token::Kind::RightBrace: { if (current_brace_level == 0) { throw_parser_error("unexpected '}'"); } current_brace_level -= 1; - if (current_brace_level == 0 && current_bracket_level == 0) { add_json_literal(tmpl.content.c_str()); } - + if (current_brace_level == 0 && current_bracket_level == 0) { + add_literal(arguments, tmpl.content.c_str()); + } } break; case Token::Kind::Id: { get_peek_token(); - // Json Literal + // Data Literal if (tok.text == static_cast("true") || tok.text == static_cast("false") || tok.text == static_cast("null")) { if (current_brace_level == 0 && current_bracket_level == 0) { - json_literal_start = tok.text; - add_json_literal(tmpl.content.c_str()); + literal_start = tok.text; + add_literal(arguments, tmpl.content.c_str()); } // Operator @@ -3128,13 +1625,32 @@ class Parser { // Functions } else if (peek_tok.kind == Token::Kind::LeftParen) { - operator_stack.emplace(std::make_shared(static_cast(tok.text), - tok.text.data() - tmpl.content.c_str())); - function_stack.emplace(operator_stack.top().get(), current_paren_level); + auto func = std::make_shared(tok.text, tok.text.data() - tmpl.content.c_str()); + get_next_token(); + do { + get_next_token(); + auto expr = parse_expression(tmpl); + if (!expr) { break; } + func->number_args += 1; + func->arguments.emplace_back(expr); + } while (tok.kind == Token::Kind::Comma); + if (tok.kind != Token::Kind::RightParen) { + throw_parser_error("expected right parenthesis, got '" + tok.describe() + "'"); + } + + auto function_data = function_storage.find_function(func->name, func->number_args); + if (function_data.operation == FunctionStorage::Operation::None) { + throw_parser_error("unknown function " + func->name); + } + func->operation = function_data.operation; + if (function_data.operation == FunctionStorage::Operation::Callback) { + func->callback = function_data.callback; + } + arguments.emplace_back(func); // Variables } else { - arguments.emplace_back(std::make_shared(static_cast(tok.text), + arguments.emplace_back(std::make_shared(static_cast(tok.text), tok.text.data() - tmpl.content.c_str())); } @@ -3218,89 +1734,48 @@ class Parser { while (!operator_stack.empty() && ((operator_stack.top()->precedence > function_node->precedence) || (operator_stack.top()->precedence == function_node->precedence - && function_node->associativity == FunctionNode::Associativity::Left)) - && (operator_stack.top()->operation != FunctionStorage::Operation::ParenLeft)) { - add_operator(); + && function_node->associativity == FunctionNode::Associativity::Left))) { + add_operator(arguments, operator_stack); } operator_stack.emplace(function_node); - } break; case Token::Kind::Comma: { - if (current_brace_level == 0 && current_bracket_level == 0) { - if (function_stack.empty()) { throw_parser_error("unexpected ','"); } - - function_stack.top().first->number_args += 1; - } - + if (current_brace_level == 0 && current_bracket_level == 0) { goto break_loop; } } break; case Token::Kind::Colon: { if (current_brace_level == 0 && current_bracket_level == 0) { throw_parser_error("unexpected ':'"); } - } break; case Token::Kind::LeftParen: { - current_paren_level += 1; - operator_stack.emplace(std::make_shared(FunctionStorage::Operation::ParenLeft, - tok.text.data() - tmpl.content.c_str())); - - get_peek_token(); - if (peek_tok.kind == Token::Kind::RightParen) { - if (!function_stack.empty() && function_stack.top().second == current_paren_level - 1) { - function_stack.top().first->number_args = 0; - } + get_next_token(); + auto expr = parse_expression(tmpl); + if (tok.kind != Token::Kind::RightParen) { + throw_parser_error("expected right parenthesis, got '" + tok.describe() + "'"); } - + if (!expr) { throw_parser_error("empty expression in parentheses"); } + arguments.emplace_back(expr); } break; - case Token::Kind::RightParen: { - current_paren_level -= 1; - while (!operator_stack.empty() - && operator_stack.top()->operation != FunctionStorage::Operation::ParenLeft) { - add_operator(); - } - - if (!operator_stack.empty() - && operator_stack.top()->operation == FunctionStorage::Operation::ParenLeft) { - operator_stack.pop(); - } - - if (!function_stack.empty() && function_stack.top().second == current_paren_level) { - auto func = function_stack.top().first; - auto function_data = function_storage.find_function(func->name, func->number_args); - if (function_data.operation == FunctionStorage::Operation::None) { - throw_parser_error("unknown function " + func->name); - } - func->operation = function_data.operation; - if (function_data.operation == FunctionStorage::Operation::Callback) { - func->callback = function_data.callback; - } - - if (operator_stack.empty()) { throw_parser_error("internal error at function " + func->name); } - - add_operator(); - function_stack.pop(); - } - } default: - break; + goto break_loop; } get_next_token(); } - while (!operator_stack.empty()) { add_operator(); } + break_loop: + while (!operator_stack.empty()) { add_operator(arguments, operator_stack); } + std::shared_ptr expr; if (arguments.size() == 1) { - current_expression_list->root = arguments[0]; - arguments = {}; - + expr = arguments[0]; + arguments = {}; } else if (arguments.size() > 1) { throw_parser_error("malformed expression"); } - - return true; + return expr; } - bool parse_statement(Template &tmpl, Token::Kind closing, nonstd::string_view path) + bool parse_statement(Template &tmpl, Token::Kind closing, sled::string_view path) { if (tok.kind != Token::Kind::Id) { return false; } @@ -3315,7 +1790,6 @@ class Parser { current_expression_list = &if_statement_node->condition; if (!parse_expression(tmpl, closing)) { return false; } - } else if (tok.text == static_cast("else")) { if (if_statement_stack.empty()) { throw_parser_error("else without matching if"); } auto &if_statement_data = if_statement_stack.top(); @@ -3337,7 +1811,6 @@ class Parser { if (!parse_expression(tmpl, closing)) { return false; } } - } else if (tok.text == static_cast("endif")) { if (if_statement_stack.empty()) { throw_parser_error("endif without matching if"); } @@ -3349,7 +1822,6 @@ class Parser { current_block = if_statement_data->parent; if_statement_stack.pop(); - } else if (tok.text == static_cast("block")) { get_next_token(); @@ -3370,7 +1842,6 @@ class Parser { if (!success.second) { throw_parser_error("block with the name '" + block_name + "' does already exist"); } get_next_token(); - } else if (tok.text == static_cast("endblock")) { if (block_statement_stack.empty()) { throw_parser_error("endblock without matching block"); } @@ -3379,7 +1850,6 @@ class Parser { current_block = block_statement_data->parent; block_statement_stack.pop(); - } else if (tok.text == static_cast("for")) { get_next_token(); @@ -3424,7 +1894,6 @@ class Parser { get_next_token(); if (!parse_expression(tmpl, closing)) { return false; } - } else if (tok.text == static_cast("endfor")) { if (for_statement_stack.empty()) { throw_parser_error("endfor without matching for"); } @@ -3433,37 +1902,26 @@ class Parser { current_block = for_statement_data->parent; for_statement_stack.pop(); - } else if (tok.text == static_cast("include")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(); add_to_template_storage(path, template_name); current_block->nodes.emplace_back( std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); get_next_token(); - } else if (tok.text == static_cast("extends")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(); add_to_template_storage(path, template_name); current_block->nodes.emplace_back( std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); get_next_token(); - } else if (tok.text == static_cast("set")) { get_next_token(); @@ -3484,14 +1942,13 @@ class Parser { get_next_token(); if (!parse_expression(tmpl, closing)) { return false; } - } else { return false; } return true; } - void parse_into(Template &tmpl, nonstd::string_view path) + void parse_into(Template &tmpl, sled::string_view path) { lexer.start(tmpl.content); current_block = &tmpl.root; @@ -3535,10 +1992,6 @@ class Parser { current_expression_list = expression_list_node.get(); if (!parse_expression(tmpl, Token::Kind::ExpressionClose)) { - throw_parser_error("expected expression, got '" + tok.describe() + "'"); - } - - if (tok.kind != Token::Kind::ExpressionClose) { throw_parser_error("expected expression close, got '" + tok.describe() + "'"); } } break; @@ -3566,28 +2019,27 @@ public: function_storage(function_storage) {} - Template parse(nonstd::string_view input, nonstd::string_view path) + Template parse(sled::string_view input, sled::string_view path) { auto result = Template(static_cast(input)); parse_into(result, path); return result; } - Template parse(nonstd::string_view input) { return parse(input, "./"); } - - void parse_into_template(Template &tmpl, nonstd::string_view filename) + void parse_into_template(Template &tmpl, sled::string_view filename) { - nonstd::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1); + sled::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1); // StringRef path = sys::path::parent_path(filename); auto sub_parser = Parser(config, lexer.get_config(), template_storage, function_storage); sub_parser.parse_into(tmpl, path); } - std::string load_file(nonstd::string_view filename) + static std::string load_file(const std::string &filename) { std::ifstream file; - open_file_or_throw(static_cast(filename), file); + file.open(filename); + if (file.fail()) { INJA_THROW(FileError("failed accessing file at '" + filename + "'")); } std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); return text; } @@ -3607,8 +2059,6 @@ public: #include #include -#include - // #include "config.hpp" // #include "exceptions.hpp" @@ -3636,19 +2086,19 @@ class Renderer : public NodeVisitor { std::vector template_stack; std::vector block_statement_stack; - const json *json_input; + const json *data_input; std::ostream *output_stream; - json json_additional_data; - json *current_loop_data = &json_additional_data["loop"]; + json additional_data; + json *current_loop_data = &additional_data["loop"]; - std::vector> json_tmp_stack; - std::stack json_eval_stack; - std::stack not_found_stack; + std::vector> data_tmp_stack; + std::stack data_eval_stack; + std::stack not_found_stack; bool break_rendering{false}; - bool truthy(const json *data) const + static bool truthy(const json *data) { if (data->is_boolean()) { return data->get(); @@ -3660,10 +2110,12 @@ class Renderer : public NodeVisitor { return !data->empty(); } - void print_json(const std::shared_ptr value) + void print_data(const std::shared_ptr value) { if (value->is_string()) { *output_stream << value->get_ref(); + } else if (value->is_number_unsigned()) { + *output_stream << value->get(); } else if (value->is_number_integer()) { *output_stream << value->get(); } else if (value->is_null()) { @@ -3678,14 +2130,14 @@ class Renderer : public NodeVisitor { expression_list.root->accept(*this); - if (json_eval_stack.empty()) { + if (data_eval_stack.empty()) { throw_renderer_error("empty expression", expression_list); - } else if (json_eval_stack.size() != 1) { + } else if (data_eval_stack.size() != 1) { throw_renderer_error("malformed expression", expression_list); } - const auto result = json_eval_stack.top(); - json_eval_stack.pop(); + const auto result = data_eval_stack.top(); + data_eval_stack.pop(); if (!result) { if (not_found_stack.empty()) { throw_renderer_error("expression could not be evaluated", expression_list); } @@ -3704,6 +2156,13 @@ class Renderer : public NodeVisitor { INJA_THROW(RenderError(message, loc)); } + void make_result(const json &&result) + { + auto result_ptr = std::make_shared(result); + data_tmp_stack.push_back(result_ptr); + data_eval_stack.push(result_ptr.get()); + } + template std::array get_arguments(const FunctionNode &node) { @@ -3715,24 +2174,24 @@ class Renderer : public NodeVisitor { for (size_t i = N_start; i < N_start + N; i += 1) { node.arguments[i]->accept(*this); } - if (json_eval_stack.size() < N) { + if (data_eval_stack.size() < N) { throw_renderer_error("function needs " + std::to_string(N) + " variables, but has only found " - + std::to_string(json_eval_stack.size()), + + std::to_string(data_eval_stack.size()), node); } std::array result; for (size_t i = 0; i < N; i += 1) { - result[N - i - 1] = json_eval_stack.top(); - json_eval_stack.pop(); + result[N - i - 1] = data_eval_stack.top(); + data_eval_stack.pop(); if (!result[N - i - 1]) { - const auto json_node = not_found_stack.top(); + const auto data_node = not_found_stack.top(); not_found_stack.pop(); if (throw_not_found) { - throw_renderer_error("variable '" + static_cast(json_node->name) + "' not found", - *json_node); + throw_renderer_error("variable '" + static_cast(data_node->name) + "' not found", + *data_node); } } } @@ -3745,24 +2204,24 @@ class Renderer : public NodeVisitor { const size_t N = node.arguments.size(); for (auto a : node.arguments) { a->accept(*this); } - if (json_eval_stack.size() < N) { + if (data_eval_stack.size() < N) { throw_renderer_error("function needs " + std::to_string(N) + " variables, but has only found " - + std::to_string(json_eval_stack.size()), + + std::to_string(data_eval_stack.size()), node); } Arguments result{N}; for (size_t i = 0; i < N; i += 1) { - result[N - i - 1] = json_eval_stack.top(); - json_eval_stack.pop(); + result[N - i - 1] = data_eval_stack.top(); + data_eval_stack.pop(); if (!result[N - i - 1]) { - const auto json_node = not_found_stack.top(); + const auto data_node = not_found_stack.top(); not_found_stack.pop(); if (throw_not_found) { - throw_renderer_error("variable '" + static_cast(json_node->name) + "' not found", - *json_node); + throw_renderer_error("variable '" + static_cast(data_node->name) + "' not found", + *data_node); } } } @@ -3785,27 +2244,24 @@ class Renderer : public NodeVisitor { void visit(const ExpressionNode &) {} - void visit(const LiteralNode &node) { json_eval_stack.push(&node.value); } + void visit(const LiteralNode &node) { data_eval_stack.push(&node.value); } - void visit(const JsonNode &node) + void visit(const DataNode &node) { - if (json_additional_data.contains(node.ptr)) { - json_eval_stack.push(&(json_additional_data[node.ptr])); - - } else if (json_input->contains(node.ptr)) { - json_eval_stack.push(&(*json_input)[node.ptr]); - + if (additional_data.contains(node.ptr)) { + data_eval_stack.push(&(additional_data[node.ptr])); + } else if (data_input->contains(node.ptr)) { + data_eval_stack.push(&(*data_input)[node.ptr]); } else { // Try to evaluate as a no-argument callback const auto function_data = function_storage.find_function(node.name, 0); if (function_data.operation == FunctionStorage::Operation::Callback) { Arguments empty_args{}; const auto value = std::make_shared(function_data.callback(empty_args)); - json_tmp_stack.push_back(value); - json_eval_stack.push(value.get()); - + data_tmp_stack.push_back(value); + data_eval_stack.push(value.get()); } else { - json_eval_stack.push(nullptr); + data_eval_stack.push(nullptr); not_found_stack.emplace(&node); } } @@ -3813,132 +2269,95 @@ class Renderer : public NodeVisitor { void visit(const FunctionNode &node) { - std::shared_ptr result_ptr; - switch (node.operation) { case Op::Not: { const auto args = get_arguments<1>(node); - result_ptr = std::make_shared(!truthy(args[0])); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(!truthy(args[0])); } break; case Op::And: { - result_ptr - = std::make_shared(truthy(get_arguments<1, 0>(node)[0]) && truthy(get_arguments<1, 1>(node)[0])); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(truthy(get_arguments<1, 0>(node)[0]) && truthy(get_arguments<1, 1>(node)[0])); } break; case Op::Or: { - result_ptr - = std::make_shared(truthy(get_arguments<1, 0>(node)[0]) || truthy(get_arguments<1, 1>(node)[0])); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(truthy(get_arguments<1, 0>(node)[0]) || truthy(get_arguments<1, 1>(node)[0])); } break; case Op::In: { const auto args = get_arguments<2>(node); - result_ptr - = std::make_shared(std::find(args[1]->begin(), args[1]->end(), *args[0]) != args[1]->end()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(std::find(args[1]->begin(), args[1]->end(), *args[0]) != args[1]->end()); } break; case Op::Equal: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] == *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] == *args[1]); } break; case Op::NotEqual: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] != *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] != *args[1]); } break; case Op::Greater: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] > *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] > *args[1]); } break; case Op::GreaterEqual: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] >= *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] >= *args[1]); } break; case Op::Less: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] < *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] < *args[1]); } break; case Op::LessEqual: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(*args[0] <= *args[1]); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(*args[0] <= *args[1]); } break; case Op::Add: { const auto args = get_arguments<2>(node); if (args[0]->is_string() && args[1]->is_string()) { - result_ptr = std::make_shared( - args[0]->get_ref() + args[1]->get_ref()); - json_tmp_stack.push_back(result_ptr); + make_result(args[0]->get_ref() + args[1]->get_ref()); } else if (args[0]->is_number_integer() && args[1]->is_number_integer()) { - result_ptr = std::make_shared(args[0]->get() + args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result( + args[0]->get() + args[1]->get()); } else { - result_ptr = std::make_shared(args[0]->get() + args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result(args[0]->get() + args[1]->get()); } - json_eval_stack.push(result_ptr.get()); } break; case Op::Subtract: { const auto args = get_arguments<2>(node); if (args[0]->is_number_integer() && args[1]->is_number_integer()) { - result_ptr = std::make_shared(args[0]->get() - args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result( + args[0]->get() - args[1]->get()); } else { - result_ptr = std::make_shared(args[0]->get() - args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result(args[0]->get() - args[1]->get()); } - json_eval_stack.push(result_ptr.get()); } break; case Op::Multiplication: { const auto args = get_arguments<2>(node); if (args[0]->is_number_integer() && args[1]->is_number_integer()) { - result_ptr = std::make_shared(args[0]->get() * args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result( + args[0]->get() * args[1]->get()); } else { - result_ptr = std::make_shared(args[0]->get() * args[1]->get()); - json_tmp_stack.push_back(result_ptr); + make_result(args[0]->get() * args[1]->get()); } - json_eval_stack.push(result_ptr.get()); } break; case Op::Division: { const auto args = get_arguments<2>(node); - if (args[1]->get() == 0) { throw_renderer_error("division by zero", node); } - result_ptr = std::make_shared(args[0]->get() / args[1]->get()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + if (args[1]->get() == 0) { throw_renderer_error("division by zero", node); } + make_result(args[0]->get() / args[1]->get()); } break; case Op::Power: { const auto args = get_arguments<2>(node); - if (args[0]->is_number_integer() && args[1]->get() >= 0) { - int result = static_cast(std::pow(args[0]->get(), args[1]->get())); - result_ptr = std::make_shared(std::move(result)); - json_tmp_stack.push_back(result_ptr); + if (args[0]->is_number_integer() && args[1]->get() >= 0) { + const auto result = static_cast( + std::pow(args[0]->get(), + args[1]->get())); + make_result(result); } else { - double result = std::pow(args[0]->get(), args[1]->get()); - result_ptr = std::make_shared(std::move(result)); - json_tmp_stack.push_back(result_ptr); + const auto result = std::pow(args[0]->get(), + args[1]->get()); + make_result(result); } - json_eval_stack.push(result_ptr.get()); } break; case Op::Modulo: { const auto args = get_arguments<2>(node); - result_ptr = std::make_shared(args[0]->get() % args[1]->get()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(args[0]->get() % args[1]->get()); } break; case Op::AtId: { const auto container = get_arguments<1, 0, false>(node)[0]; @@ -3946,170 +2365,133 @@ class Renderer : public NodeVisitor { if (not_found_stack.empty()) { throw_renderer_error("could not find element with given name", node); } const auto id_node = not_found_stack.top(); not_found_stack.pop(); - json_eval_stack.pop(); - json_eval_stack.push(&container->at(id_node->name)); + data_eval_stack.pop(); + data_eval_stack.push(&container->at(id_node->name)); } break; case Op::At: { const auto args = get_arguments<2>(node); if (args[0]->is_object()) { - json_eval_stack.push(&args[0]->at(args[1]->get())); + data_eval_stack.push(&args[0]->at(args[1]->get())); } else { - json_eval_stack.push(&args[0]->at(args[1]->get())); + data_eval_stack.push(&args[0]->at(args[1]->get())); } } break; case Op::Default: { const auto test_arg = get_arguments<1, 0, false>(node)[0]; - json_eval_stack.push(test_arg ? test_arg : get_arguments<1, 1>(node)[0]); + data_eval_stack.push(test_arg ? test_arg : get_arguments<1, 1>(node)[0]); } break; case Op::DivisibleBy: { - const auto args = get_arguments<2>(node); - const int divisor = args[1]->get(); - result_ptr = std::make_shared((divisor != 0) && (args[0]->get() % divisor == 0)); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + const auto args = get_arguments<2>(node); + const auto divisor = args[1]->get(); + make_result((divisor != 0) && (args[0]->get() % divisor == 0)); } break; case Op::Even: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->get() % 2 == 0); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->get() % 2 == 0); } break; case Op::Exists: { - auto &&name = get_arguments<1>(node)[0]->get_ref(); - result_ptr = std::make_shared( - json_input->contains(json::json_pointer(JsonNode::convert_dot_to_json_ptr(name)))); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + auto &&name = get_arguments<1>(node)[0]->get_ref(); + make_result(data_input->contains(json::json_pointer(DataNode::convert_dot_to_ptr(name)))); } break; case Op::ExistsInObject: { const auto args = get_arguments<2>(node); - auto &&name = args[1]->get_ref(); - result_ptr = std::make_shared(args[0]->find(name) != args[0]->end()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + auto &&name = args[1]->get_ref(); + make_result(args[0]->find(name) != args[0]->end()); } break; case Op::First: { const auto result = &get_arguments<1>(node)[0]->front(); - json_eval_stack.push(result); + data_eval_stack.push(result); } break; case Op::Float: { - result_ptr = std::make_shared(std::stod(get_arguments<1>(node)[0]->get_ref())); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(std::stod(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Int: { - result_ptr = std::make_shared(std::stoi(get_arguments<1>(node)[0]->get_ref())); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(std::stoi(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Last: { const auto result = &get_arguments<1>(node)[0]->back(); - json_eval_stack.push(result); + data_eval_stack.push(result); } break; case Op::Length: { const auto val = get_arguments<1>(node)[0]; if (val->is_string()) { - result_ptr = std::make_shared(val->get_ref().length()); + make_result(val->get_ref().length()); } else { - result_ptr = std::make_shared(val->size()); + make_result(val->size()); } - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); } break; case Op::Lower: { - std::string result = get_arguments<1>(node)[0]->get(); - std::transform(result.begin(), result.end(), result.begin(), ::tolower); - result_ptr = std::make_shared(std::move(result)); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + auto result = get_arguments<1>(node)[0]->get(); + std::transform(result.begin(), result.end(), result.begin(), [](char c) { + return static_cast(::tolower(c)); + }); + make_result(std::move(result)); } break; case Op::Max: { const auto args = get_arguments<1>(node); const auto result = std::max_element(args[0]->begin(), args[0]->end()); - json_eval_stack.push(&(*result)); + data_eval_stack.push(&(*result)); } break; case Op::Min: { const auto args = get_arguments<1>(node); const auto result = std::min_element(args[0]->begin(), args[0]->end()); - json_eval_stack.push(&(*result)); + data_eval_stack.push(&(*result)); } break; case Op::Odd: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->get() % 2 != 0); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->get() % 2 != 0); } break; case Op::Range: { - std::vector result(get_arguments<1>(node)[0]->get()); + std::vector result(get_arguments<1>(node)[0]->get()); std::iota(result.begin(), result.end(), 0); - result_ptr = std::make_shared(std::move(result)); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(std::move(result)); } break; case Op::Round: { - const auto args = get_arguments<2>(node); - const int precision = args[1]->get(); - const double result - = std::round(args[0]->get() * std::pow(10.0, precision)) / std::pow(10.0, precision); - if (0 == precision) { - result_ptr = std::make_shared(int(result)); + const auto args = get_arguments<2>(node); + const auto precision = args[1]->get(); + const double result = std::round(args[0]->get() * std::pow(10.0, precision)) + / std::pow(10.0, precision); + if (precision == 0) { + make_result(int(result)); } else { - result_ptr = std::make_shared(std::move(result)); + make_result(result); } - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); } break; case Op::Sort: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->get>()); + auto result_ptr = std::make_shared(get_arguments<1>(node)[0]->get>()); std::sort(result_ptr->begin(), result_ptr->end()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + data_tmp_stack.push_back(result_ptr); + data_eval_stack.push(result_ptr.get()); } break; case Op::Upper: { - std::string result = get_arguments<1>(node)[0]->get(); - std::transform(result.begin(), result.end(), result.begin(), ::toupper); - result_ptr = std::make_shared(std::move(result)); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + auto result = get_arguments<1>(node)[0]->get(); + std::transform(result.begin(), result.end(), result.begin(), [](char c) { + return static_cast(::toupper(c)); + }); + make_result(std::move(result)); } break; case Op::IsBoolean: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_boolean()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_boolean()); } break; case Op::IsNumber: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_number()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_number()); } break; case Op::IsInteger: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_number_integer()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_number_integer()); } break; case Op::IsFloat: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_number_float()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_number_float()); } break; case Op::IsObject: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_object()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_object()); } break; case Op::IsArray: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_array()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_array()); } break; case Op::IsString: { - result_ptr = std::make_shared(get_arguments<1>(node)[0]->is_string()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(get_arguments<1>(node)[0]->is_string()); } break; case Op::Callback: { - auto args = get_argument_vector(node); - result_ptr = std::make_shared(node.callback(args)); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + auto args = get_argument_vector(node); + make_result(node.callback(args)); } break; case Op::Super: { const auto args = get_argument_vector(node); @@ -4138,13 +2520,11 @@ class Renderer : public NodeVisitor { } else { throw_renderer_error("could not find block with name '" + current_block_statement->name + "'", node); } - result_ptr = std::make_shared(nullptr); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(nullptr); } break; case Op::Join: { const auto args = get_arguments<2>(node); - const auto separator = args[1]->get(); + const auto separator = args[1]->get(); std::ostringstream os; std::string sep; for (const auto &value : *args[0]) { @@ -4152,22 +2532,18 @@ class Renderer : public NodeVisitor { if (value.is_string()) { os << value.get();// otherwise the value is surrounded with "" } else { - os << value; + os << value.dump(); } sep = separator; } - result_ptr = std::make_shared(os.str()); - json_tmp_stack.push_back(result_ptr); - json_eval_stack.push(result_ptr.get()); + make_result(os.str()); } break; - case Op::ParenLeft: - case Op::ParenRight: case Op::None: break; } } - void visit(const ExpressionListNode &node) { print_json(eval_expression_list(node)); } + void visit(const ExpressionListNode &node) { print_data(eval_expression_list(node)); } void visit(const StatementNode &) {} @@ -4187,7 +2563,7 @@ class Renderer : public NodeVisitor { (*current_loop_data)["is_first"] = true; (*current_loop_data)["is_last"] = (result->size() <= 1); for (auto it = result->begin(); it != result->end(); ++it) { - json_additional_data[static_cast(node.value)] = *it; + additional_data[static_cast(node.value)] = *it; (*current_loop_data)["index"] = index; (*current_loop_data)["index1"] = index + 1; @@ -4198,12 +2574,12 @@ class Renderer : public NodeVisitor { ++index; } - json_additional_data[static_cast(node.value)].clear(); + additional_data[static_cast(node.value)].clear(); if (!(*current_loop_data)["parent"].empty()) { const auto tmp = (*current_loop_data)["parent"]; *current_loop_data = std::move(tmp); } else { - current_loop_data = &json_additional_data["loop"]; + current_loop_data = &additional_data["loop"]; } } @@ -4218,8 +2594,8 @@ class Renderer : public NodeVisitor { (*current_loop_data)["is_first"] = true; (*current_loop_data)["is_last"] = (result->size() <= 1); for (auto it = result->begin(); it != result->end(); ++it) { - json_additional_data[static_cast(node.key)] = it.key(); - json_additional_data[static_cast(node.value)] = it.value(); + additional_data[static_cast(node.key)] = it.key(); + additional_data[static_cast(node.value)] = it.value(); (*current_loop_data)["index"] = index; (*current_loop_data)["index1"] = index + 1; @@ -4230,12 +2606,12 @@ class Renderer : public NodeVisitor { ++index; } - json_additional_data[static_cast(node.key)].clear(); - json_additional_data[static_cast(node.value)].clear(); + additional_data[static_cast(node.key)].clear(); + additional_data[static_cast(node.value)].clear(); if (!(*current_loop_data)["parent"].empty()) { *current_loop_data = std::move((*current_loop_data)["parent"]); } else { - current_loop_data = &json_additional_data["loop"]; + current_loop_data = &additional_data["loop"]; } } @@ -4254,7 +2630,7 @@ class Renderer : public NodeVisitor { auto sub_renderer = Renderer(config, template_storage, function_storage); const auto included_template_it = template_storage.find(node.file); if (included_template_it != template_storage.end()) { - sub_renderer.render_to(*output_stream, included_template_it->second, *json_input, &json_additional_data); + sub_renderer.render_to(*output_stream, included_template_it->second, *data_input, &additional_data); } else if (config.throw_at_missing_includes) { throw_renderer_error("include '" + node.file + "' not found", node); } @@ -4265,7 +2641,7 @@ class Renderer : public NodeVisitor { const auto included_template_it = template_storage.find(node.file); if (included_template_it != template_storage.end()) { const Template *parent_template = &included_template_it->second; - render_to(*output_stream, *parent_template, *json_input, &json_additional_data); + render_to(*output_stream, *parent_template, *data_input, &additional_data); break_rendering = true; } else if (config.throw_at_missing_includes) { throw_renderer_error("extends '" + node.file + "' not found", node); @@ -4291,8 +2667,8 @@ class Renderer : public NodeVisitor { { std::string ptr = node.key; replace_substring(ptr, ".", "/"); - ptr = "/" + ptr; - json_additional_data[nlohmann::json::json_pointer(ptr)] = *eval_expression_list(node.expression); + ptr = "/" + ptr; + additional_data[json::json_pointer(ptr)] = *eval_expression_list(node.expression); } public: @@ -4308,16 +2684,16 @@ public: { output_stream = &os; current_template = &tmpl; - json_input = &data; + data_input = &data; if (loop_data) { - json_additional_data = *loop_data; - current_loop_data = &json_additional_data["loop"]; + additional_data = *loop_data; + current_loop_data = &additional_data["loop"]; } template_stack.emplace_back(current_template); current_template->root.accept(*this); - json_tmp_stack.clear(); + data_tmp_stack.clear(); } }; @@ -4325,23 +2701,16 @@ public: #endif// INCLUDE_INJA_RENDERER_HPP_ -// #include "string_view.hpp" - // #include "template.hpp" // #include "utils.hpp" namespace inja { -using json = nlohmann::json; - /*! * \brief Class for changing the configuration. */ class Environment { - std::string input_path; - std::string output_path; - LexerConfig lexer_config; ParserConfig parser_config; RenderConfig render_config; @@ -4349,6 +2718,10 @@ class Environment { FunctionStorage function_storage; TemplateStorage template_storage; +protected: + std::string input_path; + std::string output_path; + public: Environment() : Environment("") {} @@ -4412,23 +2785,23 @@ public: /// Sets whether a missing include will throw an error void set_throw_at_missing_includes(bool will_throw) { render_config.throw_at_missing_includes = will_throw; } - Template parse(nonstd::string_view input) + Template parse(sled::string_view input) { Parser parser(parser_config, lexer_config, template_storage, function_storage); - return parser.parse(input); + return parser.parse(input, input_path); } Template parse_template(const std::string &filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); - auto result = Template(parser.load_file(input_path + static_cast(filename))); + auto result = Template(Parser::load_file(input_path + static_cast(filename))); parser.parse_into_template(result, input_path + static_cast(filename)); return result; } Template parse_file(const std::string &filename) { return parse_template(filename); } - std::string render(nonstd::string_view input, const json &data) { return render(parse(input), data); } + std::string render(sled::string_view input, const json &data) { return render(parse(input), data); } std::string render(const Template &tmpl, const json &data) { @@ -4484,16 +2857,16 @@ public: std::string load_file(const std::string &filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); - return parser.load_file(input_path + filename); + return Parser::load_file(input_path + filename); } json load_json(const std::string &filename) { std::ifstream file; - open_file_or_throw(input_path + filename, file); - json j; - file >> j; - return j; + file.open(input_path + filename); + if (file.fail()) { INJA_THROW(FileError("failed accessing file at '" + input_path + filename + "'")); } + + return json::parse(std::istreambuf_iterator(file), std::istreambuf_iterator()); } /*! @@ -4533,13 +2906,21 @@ public: * include "" syntax. */ void include_template(const std::string &name, const Template &tmpl) { template_storage[name] = tmpl; } + + /*! + @brief Sets a function that is called when an included file is not found + */ + void set_include_callback(const std::function &callback) + { + parser_config.include_callback = callback; + } }; /*! @brief render with default settings to a string */ inline std::string -render(nonstd::string_view input, const json &data) +render(sled::string_view input, const json &data) { return Environment().render(input, data); } @@ -4548,7 +2929,7 @@ render(nonstd::string_view input, const json &data) @brief render with default settings to the given output stream */ inline void -render_to(std::ostream &os, nonstd::string_view input, const json &data) +render_to(std::ostream &os, sled::string_view input, const json &data) { Environment env; env.render_to(os, env.parse(input), data); @@ -4564,8 +2945,6 @@ render_to(std::ostream &os, nonstd::string_view input, const json &data) // #include "renderer.hpp" -// #include "string_view.hpp" - // #include "template.hpp" #endif// INCLUDE_INJA_INJA_HPP_ diff --git a/src/sled/sled.h b/src/sled/sled.h index 3ee3c5d..2d2d4cb 100644 --- a/src/sled/sled.h +++ b/src/sled/sled.h @@ -3,11 +3,11 @@ #define SLED_SLED_H // thrid_party -#include "inja.hpp" #include "rx.h" #include "sled/async/async.h" #include "sled/nonstd/cxxopts.h" #include "sled/nonstd/expected.h" +#include "sled/nonstd/inja.h" #include "sled/nonstd/string_view.h" #include "toml.hpp"