| 1 | /* |
| 2 | ___ _ Version 3.3 |
| 3 | |_ _|_ __ (_) __ _ https://github.com/pantor/inja |
| 4 | | || '_ \ | |/ _` | Licensed under the MIT License <http://opensource.org/licenses/MIT>. |
| 5 | | || | | || | (_| | |
| 6 | |___|_| |_|/ |\__,_| Copyright (c) 2018-2021 Lars Berscheid |
| 7 | |__/ |
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | of this software and associated documentation files (the "Software"), to deal |
| 10 | in the Software without restriction, including without limitation the rights |
| 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the Software is |
| 13 | furnished to do so, subject to the following conditions: |
| 14 | The above copyright notice and this permission notice shall be included in all |
| 15 | copies or substantial portions of the Software. |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #ifndef INCLUDE_INJA_INJA_HPP_ |
| 26 | #define INCLUDE_INJA_INJA_HPP_ |
| 27 | |
| 28 | #include <nlohmann/json.hpp> |
| 29 | |
| 30 | #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION) |
| 31 | #ifndef INJA_THROW |
| 32 | #define INJA_THROW(exception) throw exception |
| 33 | #endif |
| 34 | #else |
| 35 | #include <cstdlib> |
| 36 | #ifndef INJA_THROW |
| 37 | #define INJA_THROW(exception) std::abort(); std::ignore = exception |
| 38 | #endif |
| 39 | #ifndef INJA_NOEXCEPTION |
| 40 | #define INJA_NOEXCEPTION |
| 41 | #endif |
| 42 | #endif |
| 43 | |
| 44 | // #include "environment.hpp" |
| 45 | #ifndef INCLUDE_INJA_ENVIRONMENT_HPP_ |
| 46 | #define INCLUDE_INJA_ENVIRONMENT_HPP_ |
| 47 | |
| 48 | #include <fstream> |
| 49 | #include <iostream> |
| 50 | #include <memory> |
| 51 | #include <sstream> |
| 52 | #include <string> |
| 53 | |
| 54 | #include <nlohmann/json.hpp> |
| 55 | |
| 56 | // #include "config.hpp" |
| 57 | #ifndef INCLUDE_INJA_CONFIG_HPP_ |
| 58 | #define INCLUDE_INJA_CONFIG_HPP_ |
| 59 | |
| 60 | #include <functional> |
| 61 | #include <string> |
| 62 | |
| 63 | // #include "string_view.hpp" |
| 64 | // Copyright 2017-2019 by Martin Moene |
| 65 | // |
| 66 | // string-view lite, a C++17-like string_view for C++98 and later. |
| 67 | // For more information see https://github.com/martinmoene/string-view-lite |
| 68 | // |
| 69 | // Distributed under the Boost Software License, Version 1.0. |
| 70 | // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 71 | |
| 72 | |
| 73 | |
| 74 | #ifndef NONSTD_SV_LITE_H_INCLUDED |
| 75 | #define NONSTD_SV_LITE_H_INCLUDED |
| 76 | |
| 77 | #define string_view_lite_MAJOR 1 |
| 78 | #define string_view_lite_MINOR 4 |
| 79 | #define string_view_lite_PATCH 0 |
| 80 | |
| 81 | #define string_view_lite_VERSION \ |
| 82 | nssv_STRINGIFY(string_view_lite_MAJOR) "." nssv_STRINGIFY(string_view_lite_MINOR) "." nssv_STRINGIFY( \ |
| 83 | string_view_lite_PATCH) |
| 84 | |
| 85 | #define nssv_STRINGIFY(x) nssv_STRINGIFY_(x) |
| 86 | #define nssv_STRINGIFY_(x) #x |
| 87 | |
| 88 | // string-view lite configuration: |
| 89 | |
| 90 | #define nssv_STRING_VIEW_DEFAULT 0 |
| 91 | #define nssv_STRING_VIEW_NONSTD 1 |
| 92 | #define nssv_STRING_VIEW_STD 2 |
| 93 | |
| 94 | #if !defined(nssv_CONFIG_SELECT_STRING_VIEW) |
| 95 | #define nssv_CONFIG_SELECT_STRING_VIEW (nssv_HAVE_STD_STRING_VIEW ? nssv_STRING_VIEW_STD : nssv_STRING_VIEW_NONSTD) |
| 96 | #endif |
| 97 | |
| 98 | #if defined(nssv_CONFIG_SELECT_STD_STRING_VIEW) || defined(nssv_CONFIG_SELECT_NONSTD_STRING_VIEW) |
| 99 | #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_... |
| 100 | #endif |
| 101 | |
| 102 | #ifndef nssv_CONFIG_STD_SV_OPERATOR |
| 103 | #define nssv_CONFIG_STD_SV_OPERATOR 0 |
| 104 | #endif |
| 105 | |
| 106 | #ifndef nssv_CONFIG_USR_SV_OPERATOR |
| 107 | #define nssv_CONFIG_USR_SV_OPERATOR 1 |
| 108 | #endif |
| 109 | |
| 110 | #ifdef nssv_CONFIG_CONVERSION_STD_STRING |
| 111 | #define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS nssv_CONFIG_CONVERSION_STD_STRING |
| 112 | #define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS nssv_CONFIG_CONVERSION_STD_STRING |
| 113 | #endif |
| 114 | |
| 115 | #ifndef nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS |
| 116 | #define nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS 1 |
| 117 | #endif |
| 118 | |
| 119 | #ifndef nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 120 | #define nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS 1 |
| 121 | #endif |
| 122 | |
| 123 | // Control presence of exception handling (try and auto discover): |
| 124 | |
| 125 | #ifndef nssv_CONFIG_NO_EXCEPTIONS |
| 126 | #if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) |
| 127 | #define nssv_CONFIG_NO_EXCEPTIONS 0 |
| 128 | #else |
| 129 | #define nssv_CONFIG_NO_EXCEPTIONS 1 |
| 130 | #endif |
| 131 | #endif |
| 132 | |
| 133 | // C++ language version detection (C++20 is speculative): |
| 134 | // Note: VC14.0/1900 (VS2015) lacks too much from C++14. |
| 135 | |
| 136 | #ifndef nssv_CPLUSPLUS |
| 137 | #if defined(_MSVC_LANG) && !defined(__clang__) |
| 138 | #define nssv_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG) |
| 139 | #else |
| 140 | #define nssv_CPLUSPLUS __cplusplus |
| 141 | #endif |
| 142 | #endif |
| 143 | |
| 144 | #define nssv_CPP98_OR_GREATER (nssv_CPLUSPLUS >= 199711L) |
| 145 | #define nssv_CPP11_OR_GREATER (nssv_CPLUSPLUS >= 201103L) |
| 146 | #define nssv_CPP11_OR_GREATER_ (nssv_CPLUSPLUS >= 201103L) |
| 147 | #define nssv_CPP14_OR_GREATER (nssv_CPLUSPLUS >= 201402L) |
| 148 | #define nssv_CPP17_OR_GREATER (nssv_CPLUSPLUS >= 201703L) |
| 149 | #define nssv_CPP20_OR_GREATER (nssv_CPLUSPLUS >= 202000L) |
| 150 | |
| 151 | // use C++17 std::string_view if available and requested: |
| 152 | |
| 153 | #if nssv_CPP17_OR_GREATER && defined(__has_include) |
| 154 | #if __has_include(<string_view> ) |
| 155 | #define nssv_HAVE_STD_STRING_VIEW 1 |
| 156 | #else |
| 157 | #define nssv_HAVE_STD_STRING_VIEW 0 |
| 158 | #endif |
| 159 | #else |
| 160 | #define nssv_HAVE_STD_STRING_VIEW 0 |
| 161 | #endif |
| 162 | |
| 163 | #define nssv_USES_STD_STRING_VIEW \ |
| 164 | ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_STD) || \ |
| 165 | ((nssv_CONFIG_SELECT_STRING_VIEW == nssv_STRING_VIEW_DEFAULT) && nssv_HAVE_STD_STRING_VIEW)) |
| 166 | |
| 167 | #define nssv_HAVE_STARTS_WITH (nssv_CPP20_OR_GREATER || !nssv_USES_STD_STRING_VIEW) |
| 168 | #define nssv_HAVE_ENDS_WITH nssv_HAVE_STARTS_WITH |
| 169 | |
| 170 | // |
| 171 | // Use C++17 std::string_view: |
| 172 | // |
| 173 | |
| 174 | #if nssv_USES_STD_STRING_VIEW |
| 175 | |
| 176 | #include <string_view> |
| 177 | |
| 178 | // Extensions for std::string: |
| 179 | |
| 180 | #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 181 | |
| 182 | namespace nonstd { |
| 183 | |
| 184 | template <class CharT, class Traits, class Allocator = std::allocator<CharT>> |
| 185 | std::basic_string<CharT, Traits, Allocator> to_string(std::basic_string_view<CharT, Traits> v, |
| 186 | Allocator const &a = Allocator()) { |
| 187 | return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a); |
| 188 | } |
| 189 | |
| 190 | template <class CharT, class Traits, class Allocator> |
| 191 | std::basic_string_view<CharT, Traits> to_string_view(std::basic_string<CharT, Traits, Allocator> const &s) { |
| 192 | return std::basic_string_view<CharT, Traits>(s.data(), s.size()); |
| 193 | } |
| 194 | |
| 195 | // Literal operators sv and _sv: |
| 196 | |
| 197 | #if nssv_CONFIG_STD_SV_OPERATOR |
| 198 | |
| 199 | using namespace std::literals::string_view_literals; |
| 200 | |
| 201 | #endif |
| 202 | |
| 203 | #if nssv_CONFIG_USR_SV_OPERATOR |
| 204 | |
| 205 | inline namespace literals { |
| 206 | inline namespace string_view_literals { |
| 207 | |
| 208 | constexpr std::string_view operator""_sv(const char *str, size_t len) noexcept // (1) |
| 209 | { |
| 210 | return std::string_view {str, len}; |
| 211 | } |
| 212 | |
| 213 | constexpr std::u16string_view operator""_sv(const char16_t *str, size_t len) noexcept // (2) |
| 214 | { |
| 215 | return std::u16string_view {str, len}; |
| 216 | } |
| 217 | |
| 218 | constexpr std::u32string_view operator""_sv(const char32_t *str, size_t len) noexcept // (3) |
| 219 | { |
| 220 | return std::u32string_view {str, len}; |
| 221 | } |
| 222 | |
| 223 | constexpr std::wstring_view operator""_sv(const wchar_t *str, size_t len) noexcept // (4) |
| 224 | { |
| 225 | return std::wstring_view {str, len}; |
| 226 | } |
| 227 | |
| 228 | } // namespace string_view_literals |
| 229 | } // namespace literals |
| 230 | |
| 231 | #endif // nssv_CONFIG_USR_SV_OPERATOR |
| 232 | |
| 233 | } // namespace nonstd |
| 234 | |
| 235 | #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 236 | |
| 237 | namespace nonstd { |
| 238 | |
| 239 | using std::basic_string_view; |
| 240 | using std::string_view; |
| 241 | using std::u16string_view; |
| 242 | using std::u32string_view; |
| 243 | using std::wstring_view; |
| 244 | |
| 245 | // literal "sv" and "_sv", see above |
| 246 | |
| 247 | using std::operator==; |
| 248 | using std::operator!=; |
| 249 | using std::operator<; |
| 250 | using std::operator<=; |
| 251 | using std::operator>; |
| 252 | using std::operator>=; |
| 253 | |
| 254 | using std::operator<<; |
| 255 | |
| 256 | } // namespace nonstd |
| 257 | |
| 258 | #else // nssv_HAVE_STD_STRING_VIEW |
| 259 | |
| 260 | // |
| 261 | // Before C++17: use string_view lite: |
| 262 | // |
| 263 | |
| 264 | // Compiler versions: |
| 265 | // |
| 266 | // MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0) |
| 267 | // MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002) |
| 268 | // MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003) |
| 269 | // MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005) |
| 270 | // MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008) |
| 271 | // MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) |
| 272 | // MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) |
| 273 | // MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) |
| 274 | // MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) |
| 275 | // MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017) |
| 276 | |
| 277 | #if defined(_MSC_VER) && !defined(__clang__) |
| 278 | #define nssv_COMPILER_MSVC_VER (_MSC_VER) |
| 279 | #define nssv_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * (5 + (_MSC_VER < 1900))) |
| 280 | #else |
| 281 | #define nssv_COMPILER_MSVC_VER 0 |
| 282 | #define nssv_COMPILER_MSVC_VERSION 0 |
| 283 | #endif |
| 284 | |
| 285 | #define nssv_COMPILER_VERSION(major, minor, patch) (10 * (10 * (major) + (minor)) + (patch)) |
| 286 | |
| 287 | #if defined(__clang__) |
| 288 | #define nssv_COMPILER_CLANG_VERSION nssv_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) |
| 289 | #else |
| 290 | #define nssv_COMPILER_CLANG_VERSION 0 |
| 291 | #endif |
| 292 | |
| 293 | #if defined(__GNUC__) && !defined(__clang__) |
| 294 | #define nssv_COMPILER_GNUC_VERSION nssv_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) |
| 295 | #else |
| 296 | #define nssv_COMPILER_GNUC_VERSION 0 |
| 297 | #endif |
| 298 | |
| 299 | // half-open range [lo..hi): |
| 300 | #define nssv_BETWEEN(v, lo, hi) ((lo) <= (v) && (v) < (hi)) |
| 301 | |
| 302 | // Presence of language and library features: |
| 303 | |
| 304 | #ifdef _HAS_CPP0X |
| 305 | #define nssv_HAS_CPP0X _HAS_CPP0X |
| 306 | #else |
| 307 | #define nssv_HAS_CPP0X 0 |
| 308 | #endif |
| 309 | |
| 310 | // Unless defined otherwise below, consider VC14 as C++11 for variant-lite: |
| 311 | |
| 312 | #if nssv_COMPILER_MSVC_VER >= 1900 |
| 313 | #undef nssv_CPP11_OR_GREATER |
| 314 | #define nssv_CPP11_OR_GREATER 1 |
| 315 | #endif |
| 316 | |
| 317 | #define nssv_CPP11_90 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1500) |
| 318 | #define nssv_CPP11_100 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1600) |
| 319 | #define nssv_CPP11_110 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1700) |
| 320 | #define nssv_CPP11_120 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1800) |
| 321 | #define nssv_CPP11_140 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1900) |
| 322 | #define nssv_CPP11_141 (nssv_CPP11_OR_GREATER_ || nssv_COMPILER_MSVC_VER >= 1910) |
| 323 | |
| 324 | #define nssv_CPP14_000 (nssv_CPP14_OR_GREATER) |
| 325 | #define nssv_CPP17_000 (nssv_CPP17_OR_GREATER) |
| 326 | |
| 327 | // Presence of C++11 language features: |
| 328 | |
| 329 | #define nssv_HAVE_CONSTEXPR_11 nssv_CPP11_140 |
| 330 | #define nssv_HAVE_EXPLICIT_CONVERSION nssv_CPP11_140 |
| 331 | #define nssv_HAVE_INLINE_NAMESPACE nssv_CPP11_140 |
| 332 | #define nssv_HAVE_NOEXCEPT nssv_CPP11_140 |
| 333 | #define nssv_HAVE_NULLPTR nssv_CPP11_100 |
| 334 | #define nssv_HAVE_REF_QUALIFIER nssv_CPP11_140 |
| 335 | #define nssv_HAVE_UNICODE_LITERALS nssv_CPP11_140 |
| 336 | #define nssv_HAVE_USER_DEFINED_LITERALS nssv_CPP11_140 |
| 337 | #define nssv_HAVE_WCHAR16_T nssv_CPP11_100 |
| 338 | #define nssv_HAVE_WCHAR32_T nssv_CPP11_100 |
| 339 | |
| 340 | #if !((nssv_CPP11_OR_GREATER && nssv_COMPILER_CLANG_VERSION) || nssv_BETWEEN(nssv_COMPILER_CLANG_VERSION, 300, 400)) |
| 341 | #define nssv_HAVE_STD_DEFINED_LITERALS nssv_CPP11_140 |
| 342 | #else |
| 343 | #define nssv_HAVE_STD_DEFINED_LITERALS 0 |
| 344 | #endif |
| 345 | |
| 346 | // Presence of C++14 language features: |
| 347 | |
| 348 | #define nssv_HAVE_CONSTEXPR_14 nssv_CPP14_000 |
| 349 | |
| 350 | // Presence of C++17 language features: |
| 351 | |
| 352 | #define nssv_HAVE_NODISCARD nssv_CPP17_000 |
| 353 | |
| 354 | // Presence of C++ library features: |
| 355 | |
| 356 | #define nssv_HAVE_STD_HASH nssv_CPP11_120 |
| 357 | |
| 358 | // C++ feature usage: |
| 359 | |
| 360 | #if nssv_HAVE_CONSTEXPR_11 |
| 361 | #define nssv_constexpr constexpr |
| 362 | #else |
| 363 | #define nssv_constexpr /*constexpr*/ |
| 364 | #endif |
| 365 | |
| 366 | #if nssv_HAVE_CONSTEXPR_14 |
| 367 | #define nssv_constexpr14 constexpr |
| 368 | #else |
| 369 | #define nssv_constexpr14 /*constexpr*/ |
| 370 | #endif |
| 371 | |
| 372 | #if nssv_HAVE_EXPLICIT_CONVERSION |
| 373 | #define nssv_explicit explicit |
| 374 | #else |
| 375 | #define nssv_explicit /*explicit*/ |
| 376 | #endif |
| 377 | |
| 378 | #if nssv_HAVE_INLINE_NAMESPACE |
| 379 | #define nssv_inline_ns inline |
| 380 | #else |
| 381 | #define nssv_inline_ns /*inline*/ |
| 382 | #endif |
| 383 | |
| 384 | #if nssv_HAVE_NOEXCEPT |
| 385 | #define nssv_noexcept noexcept |
| 386 | #else |
| 387 | #define nssv_noexcept /*noexcept*/ |
| 388 | #endif |
| 389 | |
| 390 | //#if nssv_HAVE_REF_QUALIFIER |
| 391 | //# define nssv_ref_qual & |
| 392 | //# define nssv_refref_qual && |
| 393 | //#else |
| 394 | //# define nssv_ref_qual /*&*/ |
| 395 | //# define nssv_refref_qual /*&&*/ |
| 396 | //#endif |
| 397 | |
| 398 | #if nssv_HAVE_NULLPTR |
| 399 | #define nssv_nullptr nullptr |
| 400 | #else |
| 401 | #define nssv_nullptr NULL |
| 402 | #endif |
| 403 | |
| 404 | #if nssv_HAVE_NODISCARD |
| 405 | #define nssv_nodiscard [[nodiscard]] |
| 406 | #else |
| 407 | #define nssv_nodiscard /*[[nodiscard]]*/ |
| 408 | #endif |
| 409 | |
| 410 | // Additional includes: |
| 411 | |
| 412 | #include <algorithm> |
| 413 | #include <cassert> |
| 414 | #include <iterator> |
| 415 | #include <limits> |
| 416 | #include <ostream> |
| 417 | #include <string> // std::char_traits<> |
| 418 | |
| 419 | #if !nssv_CONFIG_NO_EXCEPTIONS |
| 420 | #include <stdexcept> |
| 421 | #endif |
| 422 | |
| 423 | #if nssv_CPP11_OR_GREATER |
| 424 | #include <type_traits> |
| 425 | #endif |
| 426 | |
| 427 | // Clang, GNUC, MSVC warning suppression macros: |
| 428 | |
| 429 | #if nssv_COMPILER_MSVC_VERSION >= 140 |
| 430 | #define nssv_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]] |
| 431 | #define nssv_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress : code)) |
| 432 | #define nssv_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable : codes)) |
| 433 | #else |
| 434 | #define nssv_SUPPRESS_MSGSL_WARNING(expr) |
| 435 | #define nssv_SUPPRESS_MSVC_WARNING(code, descr) |
| 436 | #define nssv_DISABLE_MSVC_WARNINGS(codes) |
| 437 | #endif |
| 438 | |
| 439 | #if defined(__clang__) |
| 440 | #define nssv_RESTORE_WARNINGS() _Pragma("clang diagnostic pop") |
| 441 | #elif defined(__GNUC__) |
| 442 | #define nssv_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop") |
| 443 | #elif nssv_COMPILER_MSVC_VERSION >= 140 |
| 444 | #define nssv_RESTORE_WARNINGS() __pragma(warning(pop)) |
| 445 | #else |
| 446 | #define nssv_RESTORE_WARNINGS() |
| 447 | #endif |
| 448 | |
| 449 | // Suppress the following MSVC (GSL) warnings: |
| 450 | // - C4455, non-gsl : 'operator ""sv': literal suffix identifiers that do not |
| 451 | // start with an underscore are reserved |
| 452 | // - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions; |
| 453 | // use brace initialization, gsl::narrow_cast or gsl::narow |
| 454 | // - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead |
| 455 | |
| 456 | nssv_DISABLE_MSVC_WARNINGS(4455 26481 26472) |
| 457 | // nssv_DISABLE_CLANG_WARNINGS( "-Wuser-defined-literals" ) |
| 458 | // nssv_DISABLE_GNUC_WARNINGS( -Wliteral-suffix ) |
| 459 | |
| 460 | namespace nonstd { |
| 461 | namespace sv_lite { |
| 462 | |
| 463 | #if nssv_CPP11_OR_GREATER |
| 464 | |
| 465 | namespace detail { |
| 466 | |
| 467 | // Expect tail call optimization to make length() non-recursive: |
| 468 | |
| 469 | template <typename CharT> inline constexpr std::size_t length(CharT *s, std::size_t result = 0) { |
| 470 | return *s == '\0' ? result : length(s + 1, result + 1); |
| 471 | } |
| 472 | |
| 473 | } // namespace detail |
| 474 | |
| 475 | #endif // nssv_CPP11_OR_GREATER |
| 476 | |
| 477 | template <class CharT, class Traits = std::char_traits<CharT>> class basic_string_view; |
| 478 | |
| 479 | // |
| 480 | // basic_string_view: |
| 481 | // |
| 482 | |
| 483 | template <class CharT, class Traits /* = std::char_traits<CharT> */ |
| 484 | > |
| 485 | class basic_string_view { |
| 486 | public: |
| 487 | // Member types: |
| 488 | |
| 489 | typedef Traits traits_type; |
| 490 | typedef CharT value_type; |
| 491 | |
| 492 | typedef CharT *pointer; |
| 493 | typedef CharT const *const_pointer; |
| 494 | typedef CharT &reference; |
| 495 | typedef CharT const &const_reference; |
| 496 | |
| 497 | typedef const_pointer iterator; |
| 498 | typedef const_pointer const_iterator; |
| 499 | typedef std::reverse_iterator<const_iterator> reverse_iterator; |
| 500 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 501 | |
| 502 | typedef std::size_t size_type; |
| 503 | typedef std::ptrdiff_t difference_type; |
| 504 | |
| 505 | // 24.4.2.1 Construction and assignment: |
| 506 | |
| 507 | nssv_constexpr basic_string_view() nssv_noexcept : data_(nssv_nullptr), size_(0) {} |
| 508 | |
| 509 | #if nssv_CPP11_OR_GREATER |
| 510 | nssv_constexpr basic_string_view(basic_string_view const &other) nssv_noexcept = default; |
| 511 | #else |
| 512 | nssv_constexpr basic_string_view(basic_string_view const &other) nssv_noexcept : data_(other.data_), |
| 513 | size_(other.size_) {} |
| 514 | #endif |
| 515 | |
| 516 | nssv_constexpr basic_string_view(CharT const *s, size_type count) nssv_noexcept // non-standard noexcept |
| 517 | : data_(s), |
| 518 | size_(count) {} |
| 519 | |
| 520 | nssv_constexpr basic_string_view(CharT const *s) nssv_noexcept // non-standard noexcept |
| 521 | : data_(s) |
| 522 | #if nssv_CPP17_OR_GREATER |
| 523 | , |
| 524 | size_(Traits::length(s)) |
| 525 | #elif nssv_CPP11_OR_GREATER |
| 526 | , |
| 527 | size_(detail::length(s)) |
| 528 | #else |
| 529 | , |
| 530 | size_(Traits::length(s)) |
| 531 | #endif |
| 532 | { |
| 533 | } |
| 534 | |
| 535 | // Assignment: |
| 536 | |
| 537 | #if nssv_CPP11_OR_GREATER |
| 538 | nssv_constexpr14 basic_string_view &operator=(basic_string_view const &other) nssv_noexcept = default; |
| 539 | #else |
| 540 | nssv_constexpr14 basic_string_view &operator=(basic_string_view const &other) nssv_noexcept { |
| 541 | data_ = other.data_; |
| 542 | size_ = other.size_; |
| 543 | return *this; |
| 544 | } |
| 545 | #endif |
| 546 | |
| 547 | // 24.4.2.2 Iterator support: |
| 548 | |
| 549 | nssv_constexpr const_iterator begin() const nssv_noexcept { return data_; } |
| 550 | nssv_constexpr const_iterator end() const nssv_noexcept { return data_ + size_; } |
| 551 | |
| 552 | nssv_constexpr const_iterator cbegin() const nssv_noexcept { return begin(); } |
| 553 | nssv_constexpr const_iterator cend() const nssv_noexcept { return end(); } |
| 554 | |
| 555 | nssv_constexpr const_reverse_iterator rbegin() const nssv_noexcept { return const_reverse_iterator(end()); } |
| 556 | nssv_constexpr const_reverse_iterator rend() const nssv_noexcept { return const_reverse_iterator(begin()); } |
| 557 | |
| 558 | nssv_constexpr const_reverse_iterator crbegin() const nssv_noexcept { return rbegin(); } |
| 559 | nssv_constexpr const_reverse_iterator crend() const nssv_noexcept { return rend(); } |
| 560 | |
| 561 | // 24.4.2.3 Capacity: |
| 562 | |
| 563 | nssv_constexpr size_type size() const nssv_noexcept { return size_; } |
| 564 | nssv_constexpr size_type length() const nssv_noexcept { return size_; } |
| 565 | nssv_constexpr size_type max_size() const nssv_noexcept { return (std::numeric_limits<size_type>::max)(); } |
| 566 | |
| 567 | // since C++20 |
| 568 | nssv_nodiscard nssv_constexpr bool empty() const nssv_noexcept { return 0 == size_; } |
| 569 | |
| 570 | // 24.4.2.4 Element access: |
| 571 | |
| 572 | nssv_constexpr const_reference operator[](size_type pos) const { return data_at(pos); } |
| 573 | |
| 574 | nssv_constexpr14 const_reference at(size_type pos) const { |
| 575 | #if nssv_CONFIG_NO_EXCEPTIONS |
| 576 | assert(pos < size()); |
| 577 | #else |
| 578 | if (pos >= size()) { |
| 579 | throw std::out_of_range("nonstd::string_view::at()"); |
| 580 | } |
| 581 | #endif |
| 582 | return data_at(pos); |
| 583 | } |
| 584 | |
| 585 | nssv_constexpr const_reference front() const { return data_at(0); } |
| 586 | nssv_constexpr const_reference back() const { return data_at(size() - 1); } |
| 587 | |
| 588 | nssv_constexpr const_pointer data() const nssv_noexcept { return data_; } |
| 589 | |
| 590 | // 24.4.2.5 Modifiers: |
| 591 | |
| 592 | nssv_constexpr14 void remove_prefix(size_type n) { |
| 593 | assert(n <= size()); |
| 594 | data_ += n; |
| 595 | size_ -= n; |
| 596 | } |
| 597 | |
| 598 | nssv_constexpr14 void remove_suffix(size_type n) { |
| 599 | assert(n <= size()); |
| 600 | size_ -= n; |
| 601 | } |
| 602 | |
| 603 | nssv_constexpr14 void swap(basic_string_view &other) nssv_noexcept { |
| 604 | using std::swap; |
| 605 | swap(data_, other.data_); |
| 606 | swap(size_, other.size_); |
| 607 | } |
| 608 | |
| 609 | // 24.4.2.6 String operations: |
| 610 | |
| 611 | size_type copy(CharT *dest, size_type n, size_type pos = 0) const { |
| 612 | #if nssv_CONFIG_NO_EXCEPTIONS |
| 613 | assert(pos <= size()); |
| 614 | #else |
| 615 | if (pos > size()) { |
| 616 | throw std::out_of_range("nonstd::string_view::copy()"); |
| 617 | } |
| 618 | #endif |
| 619 | const size_type rlen = (std::min)(n, size() - pos); |
| 620 | |
| 621 | (void)Traits::copy(dest, data() + pos, rlen); |
| 622 | |
| 623 | return rlen; |
| 624 | } |
| 625 | |
| 626 | nssv_constexpr14 basic_string_view substr(size_type pos = 0, size_type n = npos) const { |
| 627 | #if nssv_CONFIG_NO_EXCEPTIONS |
| 628 | assert(pos <= size()); |
| 629 | #else |
| 630 | if (pos > size()) { |
| 631 | throw std::out_of_range("nonstd::string_view::substr()"); |
| 632 | } |
| 633 | #endif |
| 634 | return basic_string_view(data() + pos, (std::min)(n, size() - pos)); |
| 635 | } |
| 636 | |
| 637 | // compare(), 6x: |
| 638 | |
| 639 | nssv_constexpr14 int compare(basic_string_view other) const nssv_noexcept // (1) |
| 640 | { |
| 641 | if (const int result = Traits::compare(data(), other.data(), (std::min)(size(), other.size()))) { |
| 642 | return result; |
| 643 | } |
| 644 | |
| 645 | return size() == other.size() ? 0 : size() < other.size() ? -1 : 1; |
| 646 | } |
| 647 | |
| 648 | nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other) const // (2) |
| 649 | { |
| 650 | return substr(pos1, n1).compare(other); |
| 651 | } |
| 652 | |
| 653 | nssv_constexpr int compare(size_type pos1, size_type n1, basic_string_view other, size_type pos2, |
| 654 | size_type n2) const // (3) |
| 655 | { |
| 656 | return substr(pos1, n1).compare(other.substr(pos2, n2)); |
| 657 | } |
| 658 | |
| 659 | nssv_constexpr int compare(CharT const *s) const // (4) |
| 660 | { |
| 661 | return compare(basic_string_view(s)); |
| 662 | } |
| 663 | |
| 664 | nssv_constexpr int compare(size_type pos1, size_type n1, CharT const *s) const // (5) |
| 665 | { |
| 666 | return substr(pos1, n1).compare(basic_string_view(s)); |
| 667 | } |
| 668 | |
| 669 | nssv_constexpr int compare(size_type pos1, size_type n1, CharT const *s, size_type n2) const // (6) |
| 670 | { |
| 671 | return substr(pos1, n1).compare(basic_string_view(s, n2)); |
| 672 | } |
| 673 | |
| 674 | // 24.4.2.7 Searching: |
| 675 | |
| 676 | // starts_with(), 3x, since C++20: |
| 677 | |
| 678 | nssv_constexpr bool starts_with(basic_string_view v) const nssv_noexcept // (1) |
| 679 | { |
| 680 | return size() >= v.size() && compare(0, v.size(), v) == 0; |
| 681 | } |
| 682 | |
| 683 | nssv_constexpr bool starts_with(CharT c) const nssv_noexcept // (2) |
| 684 | { |
| 685 | return starts_with(basic_string_view(&c, 1)); |
| 686 | } |
| 687 | |
| 688 | nssv_constexpr bool starts_with(CharT const *s) const // (3) |
| 689 | { |
| 690 | return starts_with(basic_string_view(s)); |
| 691 | } |
| 692 | |
| 693 | // ends_with(), 3x, since C++20: |
| 694 | |
| 695 | nssv_constexpr bool ends_with(basic_string_view v) const nssv_noexcept // (1) |
| 696 | { |
| 697 | return size() >= v.size() && compare(size() - v.size(), npos, v) == 0; |
| 698 | } |
| 699 | |
| 700 | nssv_constexpr bool ends_with(CharT c) const nssv_noexcept // (2) |
| 701 | { |
| 702 | return ends_with(basic_string_view(&c, 1)); |
| 703 | } |
| 704 | |
| 705 | nssv_constexpr bool ends_with(CharT const *s) const // (3) |
| 706 | { |
| 707 | return ends_with(basic_string_view(s)); |
| 708 | } |
| 709 | |
| 710 | // find(), 4x: |
| 711 | |
| 712 | nssv_constexpr14 size_type find(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) |
| 713 | { |
| 714 | return assert(v.size() == 0 || v.data() != nssv_nullptr), |
| 715 | pos >= size() ? npos : to_pos(std::search(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); |
| 716 | } |
| 717 | |
| 718 | nssv_constexpr14 size_type find(CharT c, size_type pos = 0) const nssv_noexcept // (2) |
| 719 | { |
| 720 | return find(basic_string_view(&c, 1), pos); |
| 721 | } |
| 722 | |
| 723 | nssv_constexpr14 size_type find(CharT const *s, size_type pos, size_type n) const // (3) |
| 724 | { |
| 725 | return find(basic_string_view(s, n), pos); |
| 726 | } |
| 727 | |
| 728 | nssv_constexpr14 size_type find(CharT const *s, size_type pos = 0) const // (4) |
| 729 | { |
| 730 | return find(basic_string_view(s), pos); |
| 731 | } |
| 732 | |
| 733 | // rfind(), 4x: |
| 734 | |
| 735 | nssv_constexpr14 size_type rfind(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) |
| 736 | { |
| 737 | if (size() < v.size()) { |
| 738 | return npos; |
| 739 | } |
| 740 | |
| 741 | if (v.empty()) { |
| 742 | return (std::min)(size(), pos); |
| 743 | } |
| 744 | |
| 745 | const_iterator last = cbegin() + (std::min)(size() - v.size(), pos) + v.size(); |
| 746 | const_iterator result = std::find_end(cbegin(), last, v.cbegin(), v.cend(), Traits::eq); |
| 747 | |
| 748 | return result != last ? size_type(result - cbegin()) : npos; |
| 749 | } |
| 750 | |
| 751 | nssv_constexpr14 size_type rfind(CharT c, size_type pos = npos) const nssv_noexcept // (2) |
| 752 | { |
| 753 | return rfind(basic_string_view(&c, 1), pos); |
| 754 | } |
| 755 | |
| 756 | nssv_constexpr14 size_type rfind(CharT const *s, size_type pos, size_type n) const // (3) |
| 757 | { |
| 758 | return rfind(basic_string_view(s, n), pos); |
| 759 | } |
| 760 | |
| 761 | nssv_constexpr14 size_type rfind(CharT const *s, size_type pos = npos) const // (4) |
| 762 | { |
| 763 | return rfind(basic_string_view(s), pos); |
| 764 | } |
| 765 | |
| 766 | // find_first_of(), 4x: |
| 767 | |
| 768 | nssv_constexpr size_type find_first_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) |
| 769 | { |
| 770 | return pos >= size() ? npos |
| 771 | : to_pos(std::find_first_of(cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq)); |
| 772 | } |
| 773 | |
| 774 | nssv_constexpr size_type find_first_of(CharT c, size_type pos = 0) const nssv_noexcept // (2) |
| 775 | { |
| 776 | return find_first_of(basic_string_view(&c, 1), pos); |
| 777 | } |
| 778 | |
| 779 | nssv_constexpr size_type find_first_of(CharT const *s, size_type pos, size_type n) const // (3) |
| 780 | { |
| 781 | return find_first_of(basic_string_view(s, n), pos); |
| 782 | } |
| 783 | |
| 784 | nssv_constexpr size_type find_first_of(CharT const *s, size_type pos = 0) const // (4) |
| 785 | { |
| 786 | return find_first_of(basic_string_view(s), pos); |
| 787 | } |
| 788 | |
| 789 | // find_last_of(), 4x: |
| 790 | |
| 791 | nssv_constexpr size_type find_last_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) |
| 792 | { |
| 793 | return empty() ? npos |
| 794 | : pos >= size() ? find_last_of(v, size() - 1) |
| 795 | : to_pos(std::find_first_of(const_reverse_iterator(cbegin() + pos + 1), crend(), |
| 796 | v.cbegin(), v.cend(), Traits::eq)); |
| 797 | } |
| 798 | |
| 799 | nssv_constexpr size_type find_last_of(CharT c, size_type pos = npos) const nssv_noexcept // (2) |
| 800 | { |
| 801 | return find_last_of(basic_string_view(&c, 1), pos); |
| 802 | } |
| 803 | |
| 804 | nssv_constexpr size_type find_last_of(CharT const *s, size_type pos, size_type count) const // (3) |
| 805 | { |
| 806 | return find_last_of(basic_string_view(s, count), pos); |
| 807 | } |
| 808 | |
| 809 | nssv_constexpr size_type find_last_of(CharT const *s, size_type pos = npos) const // (4) |
| 810 | { |
| 811 | return find_last_of(basic_string_view(s), pos); |
| 812 | } |
| 813 | |
| 814 | // find_first_not_of(), 4x: |
| 815 | |
| 816 | nssv_constexpr size_type find_first_not_of(basic_string_view v, size_type pos = 0) const nssv_noexcept // (1) |
| 817 | { |
| 818 | return pos >= size() ? npos : to_pos(std::find_if(cbegin() + pos, cend(), not_in_view(v))); |
| 819 | } |
| 820 | |
| 821 | nssv_constexpr size_type find_first_not_of(CharT c, size_type pos = 0) const nssv_noexcept // (2) |
| 822 | { |
| 823 | return find_first_not_of(basic_string_view(&c, 1), pos); |
| 824 | } |
| 825 | |
| 826 | nssv_constexpr size_type find_first_not_of(CharT const *s, size_type pos, size_type count) const // (3) |
| 827 | { |
| 828 | return find_first_not_of(basic_string_view(s, count), pos); |
| 829 | } |
| 830 | |
| 831 | nssv_constexpr size_type find_first_not_of(CharT const *s, size_type pos = 0) const // (4) |
| 832 | { |
| 833 | return find_first_not_of(basic_string_view(s), pos); |
| 834 | } |
| 835 | |
| 836 | // find_last_not_of(), 4x: |
| 837 | |
| 838 | nssv_constexpr size_type find_last_not_of(basic_string_view v, size_type pos = npos) const nssv_noexcept // (1) |
| 839 | { |
| 840 | return empty() ? npos |
| 841 | : pos >= size() |
| 842 | ? find_last_not_of(v, size() - 1) |
| 843 | : to_pos(std::find_if(const_reverse_iterator(cbegin() + pos + 1), crend(), not_in_view(v))); |
| 844 | } |
| 845 | |
| 846 | nssv_constexpr size_type find_last_not_of(CharT c, size_type pos = npos) const nssv_noexcept // (2) |
| 847 | { |
| 848 | return find_last_not_of(basic_string_view(&c, 1), pos); |
| 849 | } |
| 850 | |
| 851 | nssv_constexpr size_type find_last_not_of(CharT const *s, size_type pos, size_type count) const // (3) |
| 852 | { |
| 853 | return find_last_not_of(basic_string_view(s, count), pos); |
| 854 | } |
| 855 | |
| 856 | nssv_constexpr size_type find_last_not_of(CharT const *s, size_type pos = npos) const // (4) |
| 857 | { |
| 858 | return find_last_not_of(basic_string_view(s), pos); |
| 859 | } |
| 860 | |
| 861 | // Constants: |
| 862 | |
| 863 | #if nssv_CPP17_OR_GREATER |
| 864 | static nssv_constexpr size_type npos = size_type(-1); |
| 865 | #elif nssv_CPP11_OR_GREATER |
| 866 | enum : size_type { npos = size_type(-1) }; |
| 867 | #else |
| 868 | enum { npos = size_type(-1) }; |
| 869 | #endif |
| 870 | |
| 871 | private: |
| 872 | struct not_in_view { |
| 873 | const basic_string_view v; |
| 874 | |
| 875 | nssv_constexpr explicit not_in_view(basic_string_view v) : v(v) {} |
| 876 | |
| 877 | nssv_constexpr bool operator()(CharT c) const { return npos == v.find_first_of(c); } |
| 878 | }; |
| 879 | |
| 880 | nssv_constexpr size_type to_pos(const_iterator it) const { return it == cend() ? npos : size_type(it - cbegin()); } |
| 881 | |
| 882 | nssv_constexpr size_type to_pos(const_reverse_iterator it) const { |
| 883 | return it == crend() ? npos : size_type(crend() - it - 1); |
| 884 | } |
| 885 | |
| 886 | nssv_constexpr const_reference data_at(size_type pos) const { |
| 887 | #if nssv_BETWEEN(nssv_COMPILER_GNUC_VERSION, 1, 500) |
| 888 | return data_[pos]; |
| 889 | #else |
| 890 | return assert(pos < size()), data_[pos]; |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | private: |
| 895 | const_pointer data_; |
| 896 | size_type size_; |
| 897 | |
| 898 | public: |
| 899 | #if nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS |
| 900 | |
| 901 | template <class Allocator> |
| 902 | basic_string_view(std::basic_string<CharT, Traits, Allocator> const &s) nssv_noexcept : data_(s.data()), |
| 903 | size_(s.size()) {} |
| 904 | |
| 905 | #if nssv_HAVE_EXPLICIT_CONVERSION |
| 906 | |
| 907 | template <class Allocator> explicit operator std::basic_string<CharT, Traits, Allocator>() const { |
| 908 | return to_string(Allocator()); |
| 909 | } |
| 910 | |
| 911 | #endif // nssv_HAVE_EXPLICIT_CONVERSION |
| 912 | |
| 913 | #if nssv_CPP11_OR_GREATER |
| 914 | |
| 915 | template <class Allocator = std::allocator<CharT>> |
| 916 | std::basic_string<CharT, Traits, Allocator> to_string(Allocator const &a = Allocator()) const { |
| 917 | return std::basic_string<CharT, Traits, Allocator>(begin(), end(), a); |
| 918 | } |
| 919 | |
| 920 | #else |
| 921 | |
| 922 | std::basic_string<CharT, Traits> to_string() const { return std::basic_string<CharT, Traits>(begin(), end()); } |
| 923 | |
| 924 | template <class Allocator> std::basic_string<CharT, Traits, Allocator> to_string(Allocator const &a) const { |
| 925 | return std::basic_string<CharT, Traits, Allocator>(begin(), end(), a); |
| 926 | } |
| 927 | |
| 928 | #endif // nssv_CPP11_OR_GREATER |
| 929 | |
| 930 | #endif // nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS |
| 931 | }; |
| 932 | |
| 933 | // |
| 934 | // Non-member functions: |
| 935 | // |
| 936 | |
| 937 | // 24.4.3 Non-member comparison functions: |
| 938 | // lexicographically compare two string views (function template): |
| 939 | |
| 940 | template <class CharT, class Traits> |
| 941 | nssv_constexpr bool operator==(basic_string_view<CharT, Traits> lhs, |
| 942 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 943 | return lhs.compare(rhs) == 0; |
| 944 | } |
| 945 | |
| 946 | template <class CharT, class Traits> |
| 947 | nssv_constexpr bool operator!=(basic_string_view<CharT, Traits> lhs, |
| 948 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 949 | return lhs.compare(rhs) != 0; |
| 950 | } |
| 951 | |
| 952 | template <class CharT, class Traits> |
| 953 | nssv_constexpr bool operator<(basic_string_view<CharT, Traits> lhs, |
| 954 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 955 | return lhs.compare(rhs) < 0; |
| 956 | } |
| 957 | |
| 958 | template <class CharT, class Traits> |
| 959 | nssv_constexpr bool operator<=(basic_string_view<CharT, Traits> lhs, |
| 960 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 961 | return lhs.compare(rhs) <= 0; |
| 962 | } |
| 963 | |
| 964 | template <class CharT, class Traits> |
| 965 | nssv_constexpr bool operator>(basic_string_view<CharT, Traits> lhs, |
| 966 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 967 | return lhs.compare(rhs) > 0; |
| 968 | } |
| 969 | |
| 970 | template <class CharT, class Traits> |
| 971 | nssv_constexpr bool operator>=(basic_string_view<CharT, Traits> lhs, |
| 972 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 973 | return lhs.compare(rhs) >= 0; |
| 974 | } |
| 975 | |
| 976 | // Let S be basic_string_view<CharT, Traits>, and sv be an instance of S. |
| 977 | // Implementations shall provide sufficient additional overloads marked |
| 978 | // constexpr and noexcept so that an object t with an implicit conversion |
| 979 | // to S can be compared according to Table 67. |
| 980 | |
| 981 | #if !nssv_CPP11_OR_GREATER || nssv_BETWEEN(nssv_COMPILER_MSVC_VERSION, 100, 141) |
| 982 | |
| 983 | // accomodate for older compilers: |
| 984 | |
| 985 | // == |
| 986 | |
| 987 | template <class CharT, class Traits> |
| 988 | nssv_constexpr bool operator==(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 989 | return lhs.compare(rhs) == 0; |
| 990 | } |
| 991 | |
| 992 | template <class CharT, class Traits> |
| 993 | nssv_constexpr bool operator==(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 994 | return rhs.compare(lhs) == 0; |
| 995 | } |
| 996 | |
| 997 | template <class CharT, class Traits> |
| 998 | nssv_constexpr bool operator==(basic_string_view<CharT, Traits> lhs, |
| 999 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1000 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 1001 | } |
| 1002 | |
| 1003 | template <class CharT, class Traits> |
| 1004 | nssv_constexpr bool operator==(std::basic_string<CharT, Traits> rhs, |
| 1005 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1006 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 1007 | } |
| 1008 | |
| 1009 | // != |
| 1010 | |
| 1011 | template <class CharT, class Traits> |
| 1012 | nssv_constexpr bool operator!=(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 1013 | return lhs.compare(rhs) != 0; |
| 1014 | } |
| 1015 | |
| 1016 | template <class CharT, class Traits> |
| 1017 | nssv_constexpr bool operator!=(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1018 | return rhs.compare(lhs) != 0; |
| 1019 | } |
| 1020 | |
| 1021 | template <class CharT, class Traits> |
| 1022 | nssv_constexpr bool operator!=(basic_string_view<CharT, Traits> lhs, |
| 1023 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1024 | return lhs.size() != rhs.size() && lhs.compare(rhs) != 0; |
| 1025 | } |
| 1026 | |
| 1027 | template <class CharT, class Traits> |
| 1028 | nssv_constexpr bool operator!=(std::basic_string<CharT, Traits> rhs, |
| 1029 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1030 | return lhs.size() != rhs.size() || rhs.compare(lhs) != 0; |
| 1031 | } |
| 1032 | |
| 1033 | // < |
| 1034 | |
| 1035 | template <class CharT, class Traits> |
| 1036 | nssv_constexpr bool operator<(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 1037 | return lhs.compare(rhs) < 0; |
| 1038 | } |
| 1039 | |
| 1040 | template <class CharT, class Traits> |
| 1041 | nssv_constexpr bool operator<(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1042 | return rhs.compare(lhs) > 0; |
| 1043 | } |
| 1044 | |
| 1045 | template <class CharT, class Traits> |
| 1046 | nssv_constexpr bool operator<(basic_string_view<CharT, Traits> lhs, |
| 1047 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1048 | return lhs.compare(rhs) < 0; |
| 1049 | } |
| 1050 | |
| 1051 | template <class CharT, class Traits> |
| 1052 | nssv_constexpr bool operator<(std::basic_string<CharT, Traits> rhs, |
| 1053 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1054 | return rhs.compare(lhs) > 0; |
| 1055 | } |
| 1056 | |
| 1057 | // <= |
| 1058 | |
| 1059 | template <class CharT, class Traits> |
| 1060 | nssv_constexpr bool operator<=(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 1061 | return lhs.compare(rhs) <= 0; |
| 1062 | } |
| 1063 | |
| 1064 | template <class CharT, class Traits> |
| 1065 | nssv_constexpr bool operator<=(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1066 | return rhs.compare(lhs) >= 0; |
| 1067 | } |
| 1068 | |
| 1069 | template <class CharT, class Traits> |
| 1070 | nssv_constexpr bool operator<=(basic_string_view<CharT, Traits> lhs, |
| 1071 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1072 | return lhs.compare(rhs) <= 0; |
| 1073 | } |
| 1074 | |
| 1075 | template <class CharT, class Traits> |
| 1076 | nssv_constexpr bool operator<=(std::basic_string<CharT, Traits> rhs, |
| 1077 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1078 | return rhs.compare(lhs) >= 0; |
| 1079 | } |
| 1080 | |
| 1081 | // > |
| 1082 | |
| 1083 | template <class CharT, class Traits> |
| 1084 | nssv_constexpr bool operator>(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 1085 | return lhs.compare(rhs) > 0; |
| 1086 | } |
| 1087 | |
| 1088 | template <class CharT, class Traits> |
| 1089 | nssv_constexpr bool operator>(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1090 | return rhs.compare(lhs) < 0; |
| 1091 | } |
| 1092 | |
| 1093 | template <class CharT, class Traits> |
| 1094 | nssv_constexpr bool operator>(basic_string_view<CharT, Traits> lhs, |
| 1095 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1096 | return lhs.compare(rhs) > 0; |
| 1097 | } |
| 1098 | |
| 1099 | template <class CharT, class Traits> |
| 1100 | nssv_constexpr bool operator>(std::basic_string<CharT, Traits> rhs, |
| 1101 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1102 | return rhs.compare(lhs) < 0; |
| 1103 | } |
| 1104 | |
| 1105 | // >= |
| 1106 | |
| 1107 | template <class CharT, class Traits> |
| 1108 | nssv_constexpr bool operator>=(basic_string_view<CharT, Traits> lhs, char const *rhs) nssv_noexcept { |
| 1109 | return lhs.compare(rhs) >= 0; |
| 1110 | } |
| 1111 | |
| 1112 | template <class CharT, class Traits> |
| 1113 | nssv_constexpr bool operator>=(char const *lhs, basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1114 | return rhs.compare(lhs) <= 0; |
| 1115 | } |
| 1116 | |
| 1117 | template <class CharT, class Traits> |
| 1118 | nssv_constexpr bool operator>=(basic_string_view<CharT, Traits> lhs, |
| 1119 | std::basic_string<CharT, Traits> rhs) nssv_noexcept { |
| 1120 | return lhs.compare(rhs) >= 0; |
| 1121 | } |
| 1122 | |
| 1123 | template <class CharT, class Traits> |
| 1124 | nssv_constexpr bool operator>=(std::basic_string<CharT, Traits> rhs, |
| 1125 | basic_string_view<CharT, Traits> lhs) nssv_noexcept { |
| 1126 | return rhs.compare(lhs) <= 0; |
| 1127 | } |
| 1128 | |
| 1129 | #else // newer compilers: |
| 1130 | |
| 1131 | #define nssv_BASIC_STRING_VIEW_I(T, U) typename std::decay<basic_string_view<T, U>>::type |
| 1132 | |
| 1133 | #if nssv_BETWEEN(nssv_COMPILER_MSVC_VERSION, 140, 150) |
| 1134 | #define nssv_MSVC_ORDER(x) , int = x |
| 1135 | #else |
| 1136 | #define nssv_MSVC_ORDER(x) /*, int=x*/ |
| 1137 | #endif |
| 1138 | |
| 1139 | // == |
| 1140 | |
| 1141 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1142 | nssv_constexpr bool operator==(basic_string_view<CharT, Traits> lhs, |
| 1143 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1144 | return lhs.compare(rhs) == 0; |
| 1145 | } |
| 1146 | |
| 1147 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1148 | nssv_constexpr bool operator==(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1149 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1150 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 1151 | } |
| 1152 | |
| 1153 | // != |
| 1154 | |
| 1155 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1156 | nssv_constexpr bool operator!=(basic_string_view<CharT, Traits> lhs, |
| 1157 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1158 | return lhs.size() != rhs.size() || lhs.compare(rhs) != 0; |
| 1159 | } |
| 1160 | |
| 1161 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1162 | nssv_constexpr bool operator!=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1163 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1164 | return lhs.compare(rhs) != 0; |
| 1165 | } |
| 1166 | |
| 1167 | // < |
| 1168 | |
| 1169 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1170 | nssv_constexpr bool operator<(basic_string_view<CharT, Traits> lhs, |
| 1171 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1172 | return lhs.compare(rhs) < 0; |
| 1173 | } |
| 1174 | |
| 1175 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1176 | nssv_constexpr bool operator<(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1177 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1178 | return lhs.compare(rhs) < 0; |
| 1179 | } |
| 1180 | |
| 1181 | // <= |
| 1182 | |
| 1183 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1184 | nssv_constexpr bool operator<=(basic_string_view<CharT, Traits> lhs, |
| 1185 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1186 | return lhs.compare(rhs) <= 0; |
| 1187 | } |
| 1188 | |
| 1189 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1190 | nssv_constexpr bool operator<=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1191 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1192 | return lhs.compare(rhs) <= 0; |
| 1193 | } |
| 1194 | |
| 1195 | // > |
| 1196 | |
| 1197 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1198 | nssv_constexpr bool operator>(basic_string_view<CharT, Traits> lhs, |
| 1199 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1200 | return lhs.compare(rhs) > 0; |
| 1201 | } |
| 1202 | |
| 1203 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1204 | nssv_constexpr bool operator>(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1205 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1206 | return lhs.compare(rhs) > 0; |
| 1207 | } |
| 1208 | |
| 1209 | // >= |
| 1210 | |
| 1211 | template <class CharT, class Traits nssv_MSVC_ORDER(1)> |
| 1212 | nssv_constexpr bool operator>=(basic_string_view<CharT, Traits> lhs, |
| 1213 | nssv_BASIC_STRING_VIEW_I(CharT, Traits) rhs) nssv_noexcept { |
| 1214 | return lhs.compare(rhs) >= 0; |
| 1215 | } |
| 1216 | |
| 1217 | template <class CharT, class Traits nssv_MSVC_ORDER(2)> |
| 1218 | nssv_constexpr bool operator>=(nssv_BASIC_STRING_VIEW_I(CharT, Traits) lhs, |
| 1219 | basic_string_view<CharT, Traits> rhs) nssv_noexcept { |
| 1220 | return lhs.compare(rhs) >= 0; |
| 1221 | } |
| 1222 | |
| 1223 | #undef nssv_MSVC_ORDER |
| 1224 | #undef nssv_BASIC_STRING_VIEW_I |
| 1225 | |
| 1226 | #endif // compiler-dependent approach to comparisons |
| 1227 | |
| 1228 | // 24.4.4 Inserters and extractors: |
| 1229 | |
| 1230 | namespace detail { |
| 1231 | |
| 1232 | template <class Stream> void write_padding(Stream &os, std::streamsize n) { |
| 1233 | for (std::streamsize i = 0; i < n; ++i) |
| 1234 | os.rdbuf()->sputc(os.fill()); |
| 1235 | } |
| 1236 | |
| 1237 | template <class Stream, class View> Stream &write_to_stream(Stream &os, View const &sv) { |
| 1238 | typename Stream::sentry sentry(os); |
| 1239 | |
| 1240 | if (!os) |
| 1241 | return os; |
| 1242 | |
| 1243 | const std::streamsize length = static_cast<std::streamsize>(sv.length()); |
| 1244 | |
| 1245 | // Whether, and how, to pad: |
| 1246 | const bool pad = (length < os.width()); |
| 1247 | const bool left_pad = pad && (os.flags() & std::ios_base::adjustfield) == std::ios_base::right; |
| 1248 | |
| 1249 | if (left_pad) |
| 1250 | write_padding(os, os.width() - length); |
| 1251 | |
| 1252 | // Write span characters: |
| 1253 | os.rdbuf()->sputn(sv.begin(), length); |
| 1254 | |
| 1255 | if (pad && !left_pad) |
| 1256 | write_padding(os, os.width() - length); |
| 1257 | |
| 1258 | // Reset output stream width: |
| 1259 | os.width(0); |
| 1260 | |
| 1261 | return os; |
| 1262 | } |
| 1263 | |
| 1264 | } // namespace detail |
| 1265 | |
| 1266 | template <class CharT, class Traits> |
| 1267 | std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, |
| 1268 | basic_string_view<CharT, Traits> sv) { |
| 1269 | return detail::write_to_stream(os, sv); |
| 1270 | } |
| 1271 | |
| 1272 | // Several typedefs for common character types are provided: |
| 1273 | |
| 1274 | typedef basic_string_view<char> string_view; |
| 1275 | typedef basic_string_view<wchar_t> wstring_view; |
| 1276 | #if nssv_HAVE_WCHAR16_T |
| 1277 | typedef basic_string_view<char16_t> u16string_view; |
| 1278 | typedef basic_string_view<char32_t> u32string_view; |
| 1279 | #endif |
| 1280 | |
| 1281 | } // namespace sv_lite |
| 1282 | } // namespace nonstd::sv_lite |
| 1283 | |
| 1284 | // |
| 1285 | // 24.4.6 Suffix for basic_string_view literals: |
| 1286 | // |
| 1287 | |
| 1288 | #if nssv_HAVE_USER_DEFINED_LITERALS |
| 1289 | |
| 1290 | namespace nonstd { |
| 1291 | nssv_inline_ns namespace literals { |
| 1292 | nssv_inline_ns namespace string_view_literals { |
| 1293 | |
| 1294 | #if nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS |
| 1295 | |
| 1296 | nssv_constexpr nonstd::sv_lite::string_view operator"" sv(const char *str, size_t len) nssv_noexcept // (1) |
| 1297 | { |
| 1298 | return nonstd::sv_lite::string_view {str, len}; |
| 1299 | } |
| 1300 | |
| 1301 | nssv_constexpr nonstd::sv_lite::u16string_view operator"" sv(const char16_t *str, size_t len) nssv_noexcept // (2) |
| 1302 | { |
| 1303 | return nonstd::sv_lite::u16string_view {str, len}; |
| 1304 | } |
| 1305 | |
| 1306 | nssv_constexpr nonstd::sv_lite::u32string_view operator"" sv(const char32_t *str, size_t len) nssv_noexcept // (3) |
| 1307 | { |
| 1308 | return nonstd::sv_lite::u32string_view {str, len}; |
| 1309 | } |
| 1310 | |
| 1311 | nssv_constexpr nonstd::sv_lite::wstring_view operator"" sv(const wchar_t *str, size_t len) nssv_noexcept // (4) |
| 1312 | { |
| 1313 | return nonstd::sv_lite::wstring_view {str, len}; |
| 1314 | } |
| 1315 | |
| 1316 | #endif // nssv_CONFIG_STD_SV_OPERATOR && nssv_HAVE_STD_DEFINED_LITERALS |
| 1317 | |
| 1318 | #if nssv_CONFIG_USR_SV_OPERATOR |
| 1319 | |
| 1320 | nssv_constexpr nonstd::sv_lite::string_view operator""_sv(const char *str, size_t len) nssv_noexcept // (1) |
| 1321 | { |
| 1322 | return nonstd::sv_lite::string_view {str, len}; |
| 1323 | } |
| 1324 | |
| 1325 | nssv_constexpr nonstd::sv_lite::u16string_view operator""_sv(const char16_t *str, size_t len) nssv_noexcept // (2) |
| 1326 | { |
| 1327 | return nonstd::sv_lite::u16string_view {str, len}; |
| 1328 | } |
| 1329 | |
| 1330 | nssv_constexpr nonstd::sv_lite::u32string_view operator""_sv(const char32_t *str, size_t len) nssv_noexcept // (3) |
| 1331 | { |
| 1332 | return nonstd::sv_lite::u32string_view {str, len}; |
| 1333 | } |
| 1334 | |
| 1335 | nssv_constexpr nonstd::sv_lite::wstring_view operator""_sv(const wchar_t *str, size_t len) nssv_noexcept // (4) |
| 1336 | { |
| 1337 | return nonstd::sv_lite::wstring_view {str, len}; |
| 1338 | } |
| 1339 | |
| 1340 | #endif // nssv_CONFIG_USR_SV_OPERATOR |
| 1341 | } |
| 1342 | } |
| 1343 | } // namespace nonstd |
| 1344 | |
| 1345 | #endif |
| 1346 | |
| 1347 | // |
| 1348 | // Extensions for std::string: |
| 1349 | // |
| 1350 | |
| 1351 | #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 1352 | |
| 1353 | namespace nonstd { |
| 1354 | namespace sv_lite { |
| 1355 | |
| 1356 | // Exclude MSVC 14 (19.00): it yields ambiguous to_string(): |
| 1357 | |
| 1358 | #if nssv_CPP11_OR_GREATER && nssv_COMPILER_MSVC_VERSION != 140 |
| 1359 | |
| 1360 | template <class CharT, class Traits, class Allocator = std::allocator<CharT>> |
| 1361 | std::basic_string<CharT, Traits, Allocator> to_string(basic_string_view<CharT, Traits> v, |
| 1362 | Allocator const &a = Allocator()) { |
| 1363 | return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a); |
| 1364 | } |
| 1365 | |
| 1366 | #else |
| 1367 | |
| 1368 | template <class CharT, class Traits> std::basic_string<CharT, Traits> to_string(basic_string_view<CharT, Traits> v) { |
| 1369 | return std::basic_string<CharT, Traits>(v.begin(), v.end()); |
| 1370 | } |
| 1371 | |
| 1372 | template <class CharT, class Traits, class Allocator> |
| 1373 | std::basic_string<CharT, Traits, Allocator> to_string(basic_string_view<CharT, Traits> v, Allocator const &a) { |
| 1374 | return std::basic_string<CharT, Traits, Allocator>(v.begin(), v.end(), a); |
| 1375 | } |
| 1376 | |
| 1377 | #endif // nssv_CPP11_OR_GREATER |
| 1378 | |
| 1379 | template <class CharT, class Traits, class Allocator> |
| 1380 | basic_string_view<CharT, Traits> to_string_view(std::basic_string<CharT, Traits, Allocator> const &s) { |
| 1381 | return basic_string_view<CharT, Traits>(s.data(), s.size()); |
| 1382 | } |
| 1383 | |
| 1384 | } // namespace sv_lite |
| 1385 | } // namespace nonstd |
| 1386 | |
| 1387 | #endif // nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 1388 | |
| 1389 | // |
| 1390 | // make types and algorithms available in namespace nonstd: |
| 1391 | // |
| 1392 | |
| 1393 | namespace nonstd { |
| 1394 | |
| 1395 | using sv_lite::basic_string_view; |
| 1396 | using sv_lite::string_view; |
| 1397 | using sv_lite::wstring_view; |
| 1398 | |
| 1399 | #if nssv_HAVE_WCHAR16_T |
| 1400 | using sv_lite::u16string_view; |
| 1401 | #endif |
| 1402 | #if nssv_HAVE_WCHAR32_T |
| 1403 | using sv_lite::u32string_view; |
| 1404 | #endif |
| 1405 | |
| 1406 | // literal "sv" |
| 1407 | |
| 1408 | using sv_lite::operator==; |
| 1409 | using sv_lite::operator!=; |
| 1410 | using sv_lite::operator<; |
| 1411 | using sv_lite::operator<=; |
| 1412 | using sv_lite::operator>; |
| 1413 | using sv_lite::operator>=; |
| 1414 | |
| 1415 | using sv_lite::operator<<; |
| 1416 | |
| 1417 | #if nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS |
| 1418 | using sv_lite::to_string; |
| 1419 | using sv_lite::to_string_view; |
| 1420 | #endif |
| 1421 | |
| 1422 | } // namespace nonstd |
| 1423 | |
| 1424 | // 24.4.5 Hash support (C++11): |
| 1425 | |
| 1426 | // Note: The hash value of a string view object is equal to the hash value of |
| 1427 | // the corresponding string object. |
| 1428 | |
| 1429 | #if nssv_HAVE_STD_HASH |
| 1430 | |
| 1431 | #include <functional> |
| 1432 | |
| 1433 | namespace std { |
| 1434 | |
| 1435 | template <> struct hash<nonstd::string_view> { |
| 1436 | public: |
| 1437 | std::size_t operator()(nonstd::string_view v) const nssv_noexcept { |
| 1438 | return std::hash<std::string>()(std::string(v.data(), v.size())); |
| 1439 | } |
| 1440 | }; |
| 1441 | |
| 1442 | template <> struct hash<nonstd::wstring_view> { |
| 1443 | public: |
| 1444 | std::size_t operator()(nonstd::wstring_view v) const nssv_noexcept { |
| 1445 | return std::hash<std::wstring>()(std::wstring(v.data(), v.size())); |
| 1446 | } |
| 1447 | }; |
| 1448 | |
| 1449 | template <> struct hash<nonstd::u16string_view> { |
| 1450 | public: |
| 1451 | std::size_t operator()(nonstd::u16string_view v) const nssv_noexcept { |
| 1452 | return std::hash<std::u16string>()(std::u16string(v.data(), v.size())); |
| 1453 | } |
| 1454 | }; |
| 1455 | |
| 1456 | template <> struct hash<nonstd::u32string_view> { |
| 1457 | public: |
| 1458 | std::size_t operator()(nonstd::u32string_view v) const nssv_noexcept { |
| 1459 | return std::hash<std::u32string>()(std::u32string(v.data(), v.size())); |
| 1460 | } |
| 1461 | }; |
| 1462 | |
| 1463 | } // namespace std |
| 1464 | |
| 1465 | #endif // nssv_HAVE_STD_HASH |
| 1466 | |
| 1467 | nssv_RESTORE_WARNINGS() |
| 1468 | |
| 1469 | #endif // nssv_HAVE_STD_STRING_VIEW |
| 1470 | #endif // NONSTD_SV_LITE_H_INCLUDED |
| 1471 | |
| 1472 | |
| 1473 | namespace inja { |
| 1474 | |
| 1475 | /*! |
| 1476 | * \brief Class for lexer configuration. |
| 1477 | */ |
| 1478 | struct LexerConfig { |
| 1479 | std::string statement_open {"{%"}; |
| 1480 | std::string statement_open_no_lstrip {"{%+"}; |
| 1481 | std::string statement_open_force_lstrip {"{%-"}; |
| 1482 | std::string statement_close {"%}"}; |
| 1483 | std::string statement_close_force_rstrip {"-%}"}; |
| 1484 | std::string line_statement {"##"}; |
| 1485 | std::string expression_open {"{{"}; |
| 1486 | std::string expression_open_force_lstrip {"{{-"}; |
| 1487 | std::string expression_close {"}}"}; |
| 1488 | std::string expression_close_force_rstrip {"-}}"}; |
| 1489 | std::string comment_open {"{#"}; |
| 1490 | std::string comment_open_force_lstrip {"{#-"}; |
| 1491 | std::string comment_close {"#}"}; |
| 1492 | std::string comment_close_force_rstrip {"-#}"}; |
| 1493 | std::string open_chars {"#{"}; |
| 1494 | |
| 1495 | bool trim_blocks {false}; |
| 1496 | bool lstrip_blocks {false}; |
| 1497 | |
| 1498 | void update_open_chars() { |
| 1499 | open_chars = ""; |
| 1500 | if (open_chars.find(line_statement[0]) == std::string::npos) { |
| 1501 | open_chars += line_statement[0]; |
| 1502 | } |
| 1503 | if (open_chars.find(statement_open[0]) == std::string::npos) { |
| 1504 | open_chars += statement_open[0]; |
| 1505 | } |
| 1506 | if (open_chars.find(statement_open_no_lstrip[0]) == std::string::npos) { |
| 1507 | open_chars += statement_open_no_lstrip[0]; |
| 1508 | } |
| 1509 | if (open_chars.find(statement_open_force_lstrip[0]) == std::string::npos) { |
| 1510 | open_chars += statement_open_force_lstrip[0]; |
| 1511 | } |
| 1512 | if (open_chars.find(expression_open[0]) == std::string::npos) { |
| 1513 | open_chars += expression_open[0]; |
| 1514 | } |
| 1515 | if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) { |
| 1516 | open_chars += expression_open_force_lstrip[0]; |
| 1517 | } |
| 1518 | if (open_chars.find(comment_open[0]) == std::string::npos) { |
| 1519 | open_chars += comment_open[0]; |
| 1520 | } |
| 1521 | if (open_chars.find(comment_open_force_lstrip[0]) == std::string::npos) { |
| 1522 | open_chars += comment_open_force_lstrip[0]; |
| 1523 | } |
| 1524 | } |
| 1525 | }; |
| 1526 | |
| 1527 | /*! |
| 1528 | * \brief Class for parser configuration. |
| 1529 | */ |
| 1530 | struct ParserConfig { |
| 1531 | bool search_included_templates_in_files {true}; |
| 1532 | }; |
| 1533 | |
| 1534 | /*! |
| 1535 | * \brief Class for render configuration. |
| 1536 | */ |
| 1537 | struct RenderConfig { |
| 1538 | bool throw_at_missing_includes {true}; |
| 1539 | }; |
| 1540 | |
| 1541 | } // namespace inja |
| 1542 | |
| 1543 | #endif // INCLUDE_INJA_CONFIG_HPP_ |
| 1544 | |
| 1545 | // #include "function_storage.hpp" |
| 1546 | #ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_ |
| 1547 | #define INCLUDE_INJA_FUNCTION_STORAGE_HPP_ |
| 1548 | |
| 1549 | #include <vector> |
| 1550 | |
| 1551 | // #include "string_view.hpp" |
| 1552 | |
| 1553 | |
| 1554 | namespace inja { |
| 1555 | |
| 1556 | using json = nlohmann::json; |
| 1557 | |
| 1558 | using Arguments = std::vector<const json *>; |
| 1559 | using CallbackFunction = std::function<json(Arguments &args)>; |
| 1560 | using VoidCallbackFunction = std::function<void(Arguments &args)>; |
| 1561 | |
| 1562 | /*! |
| 1563 | * \brief Class for builtin functions and user-defined callbacks. |
| 1564 | */ |
| 1565 | class FunctionStorage { |
| 1566 | public: |
| 1567 | enum class Operation { |
| 1568 | Not, |
| 1569 | And, |
| 1570 | Or, |
| 1571 | In, |
| 1572 | Equal, |
| 1573 | NotEqual, |
| 1574 | Greater, |
| 1575 | GreaterEqual, |
| 1576 | Less, |
| 1577 | LessEqual, |
| 1578 | Add, |
| 1579 | Subtract, |
| 1580 | Multiplication, |
| 1581 | Division, |
| 1582 | Power, |
| 1583 | Modulo, |
| 1584 | AtId, |
| 1585 | At, |
| 1586 | Default, |
| 1587 | DivisibleBy, |
| 1588 | Even, |
| 1589 | Exists, |
| 1590 | ExistsInObject, |
| 1591 | First, |
| 1592 | Float, |
| 1593 | Int, |
| 1594 | IsArray, |
| 1595 | IsBoolean, |
| 1596 | IsFloat, |
| 1597 | IsInteger, |
| 1598 | IsNumber, |
| 1599 | IsObject, |
| 1600 | IsString, |
| 1601 | Last, |
| 1602 | Length, |
| 1603 | Lower, |
| 1604 | Max, |
| 1605 | Min, |
| 1606 | Odd, |
| 1607 | Range, |
| 1608 | Round, |
| 1609 | Sort, |
| 1610 | Upper, |
| 1611 | Super, |
| 1612 | Join, |
| 1613 | Callback, |
| 1614 | ParenLeft, |
| 1615 | ParenRight, |
| 1616 | None, |
| 1617 | }; |
| 1618 | |
| 1619 | struct FunctionData { |
| 1620 | explicit FunctionData(const Operation &op, const CallbackFunction &cb = CallbackFunction{}) : operation(op), callback(cb) {} |
| 1621 | const Operation operation; |
| 1622 | const CallbackFunction callback; |
| 1623 | }; |
| 1624 | |
| 1625 | private: |
| 1626 | const int VARIADIC {-1}; |
| 1627 | |
| 1628 | std::map<std::pair<std::string, int>, FunctionData> function_storage = { |
| 1629 | {std::make_pair("at", 2), FunctionData { Operation::At }}, |
| 1630 | {std::make_pair("default", 2), FunctionData { Operation::Default }}, |
| 1631 | {std::make_pair("divisibleBy", 2), FunctionData { Operation::DivisibleBy }}, |
| 1632 | {std::make_pair("even", 1), FunctionData { Operation::Even }}, |
| 1633 | {std::make_pair("exists", 1), FunctionData { Operation::Exists }}, |
| 1634 | {std::make_pair("existsIn", 2), FunctionData { Operation::ExistsInObject }}, |
| 1635 | {std::make_pair("first", 1), FunctionData { Operation::First }}, |
| 1636 | {std::make_pair("float", 1), FunctionData { Operation::Float }}, |
| 1637 | {std::make_pair("int", 1), FunctionData { Operation::Int }}, |
| 1638 | {std::make_pair("isArray", 1), FunctionData { Operation::IsArray }}, |
| 1639 | {std::make_pair("isBoolean", 1), FunctionData { Operation::IsBoolean }}, |
| 1640 | {std::make_pair("isFloat", 1), FunctionData { Operation::IsFloat }}, |
| 1641 | {std::make_pair("isInteger", 1), FunctionData { Operation::IsInteger }}, |
| 1642 | {std::make_pair("isNumber", 1), FunctionData { Operation::IsNumber }}, |
| 1643 | {std::make_pair("isObject", 1), FunctionData { Operation::IsObject }}, |
| 1644 | {std::make_pair("isString", 1), FunctionData { Operation::IsString }}, |
| 1645 | {std::make_pair("last", 1), FunctionData { Operation::Last }}, |
| 1646 | {std::make_pair("length", 1), FunctionData { Operation::Length }}, |
| 1647 | {std::make_pair("lower", 1), FunctionData { Operation::Lower }}, |
| 1648 | {std::make_pair("max", 1), FunctionData { Operation::Max }}, |
| 1649 | {std::make_pair("min", 1), FunctionData { Operation::Min }}, |
| 1650 | {std::make_pair("odd", 1), FunctionData { Operation::Odd }}, |
| 1651 | {std::make_pair("range", 1), FunctionData { Operation::Range }}, |
| 1652 | {std::make_pair("round", 2), FunctionData { Operation::Round }}, |
| 1653 | {std::make_pair("sort", 1), FunctionData { Operation::Sort }}, |
| 1654 | {std::make_pair("upper", 1), FunctionData { Operation::Upper }}, |
| 1655 | {std::make_pair("super", 0), FunctionData { Operation::Super }}, |
| 1656 | {std::make_pair("super", 1), FunctionData { Operation::Super }}, |
| 1657 | {std::make_pair("join", 2), FunctionData { Operation::Join }}, |
| 1658 | }; |
| 1659 | |
| 1660 | public: |
| 1661 | void add_builtin(nonstd::string_view name, int num_args, Operation op) { |
| 1662 | function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { op }); |
| 1663 | } |
| 1664 | |
| 1665 | void add_callback(nonstd::string_view name, int num_args, const CallbackFunction &callback) { |
| 1666 | function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { Operation::Callback, callback }); |
| 1667 | } |
| 1668 | |
| 1669 | FunctionData find_function(nonstd::string_view name, int num_args) const { |
| 1670 | auto it = function_storage.find(std::make_pair(static_cast<std::string>(name), num_args)); |
| 1671 | if (it != function_storage.end()) { |
| 1672 | return it->second; |
| 1673 | |
| 1674 | // Find variadic function |
| 1675 | } else if (num_args > 0) { |
| 1676 | it = function_storage.find(std::make_pair(static_cast<std::string>(name), VARIADIC)); |
| 1677 | if (it != function_storage.end()) { |
| 1678 | return it->second; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | return FunctionData { Operation::None }; |
| 1683 | } |
| 1684 | }; |
| 1685 | |
| 1686 | } // namespace inja |
| 1687 | |
| 1688 | #endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_ |
| 1689 | |
| 1690 | // #include "parser.hpp" |
| 1691 | #ifndef INCLUDE_INJA_PARSER_HPP_ |
| 1692 | #define INCLUDE_INJA_PARSER_HPP_ |
| 1693 | |
| 1694 | #include <limits> |
| 1695 | #include <stack> |
| 1696 | #include <string> |
| 1697 | #include <utility> |
| 1698 | #include <queue> |
| 1699 | #include <vector> |
| 1700 | |
| 1701 | // #include "config.hpp" |
| 1702 | |
| 1703 | // #include "exceptions.hpp" |
| 1704 | #ifndef INCLUDE_INJA_EXCEPTIONS_HPP_ |
| 1705 | #define INCLUDE_INJA_EXCEPTIONS_HPP_ |
| 1706 | |
| 1707 | #include <stdexcept> |
| 1708 | #include <string> |
| 1709 | |
| 1710 | namespace inja { |
| 1711 | |
| 1712 | struct SourceLocation { |
| 1713 | size_t line; |
| 1714 | size_t column; |
| 1715 | }; |
| 1716 | |
| 1717 | struct InjaError : public std::runtime_error { |
| 1718 | const std::string type; |
| 1719 | const std::string message; |
| 1720 | |
| 1721 | const SourceLocation location; |
| 1722 | |
| 1723 | explicit InjaError(const std::string &type, const std::string &message) |
| 1724 | : std::runtime_error("[inja.exception." + type + "] " + message), type(type), message(message), location({0, 0}) {} |
| 1725 | |
| 1726 | explicit InjaError(const std::string &type, const std::string &message, SourceLocation location) |
| 1727 | : std::runtime_error("[inja.exception." + type + "] (at " + std::to_string(location.line) + ":" + |
| 1728 | std::to_string(location.column) + ") " + message), |
| 1729 | type(type), message(message), location(location) {} |
| 1730 | }; |
| 1731 | |
| 1732 | struct ParserError : public InjaError { |
| 1733 | explicit ParserError(const std::string &message, SourceLocation location) : InjaError("parser_error", message, location) {} |
| 1734 | }; |
| 1735 | |
| 1736 | struct RenderError : public InjaError { |
| 1737 | explicit RenderError(const std::string &message, SourceLocation location) : InjaError("render_error", message, location) {} |
| 1738 | }; |
| 1739 | |
| 1740 | struct FileError : public InjaError { |
| 1741 | explicit FileError(const std::string &message) : InjaError("file_error", message) {} |
| 1742 | explicit FileError(const std::string &message, SourceLocation location) : InjaError("file_error", message, location) {} |
| 1743 | }; |
| 1744 | |
| 1745 | struct JsonError : public InjaError { |
| 1746 | explicit JsonError(const std::string &message, SourceLocation location) : InjaError("json_error", message, location) {} |
| 1747 | }; |
| 1748 | |
| 1749 | } // namespace inja |
| 1750 | |
| 1751 | #endif // INCLUDE_INJA_EXCEPTIONS_HPP_ |
| 1752 | |
| 1753 | // #include "function_storage.hpp" |
| 1754 | |
| 1755 | // #include "lexer.hpp" |
| 1756 | #ifndef INCLUDE_INJA_LEXER_HPP_ |
| 1757 | #define INCLUDE_INJA_LEXER_HPP_ |
| 1758 | |
| 1759 | #include <cctype> |
| 1760 | #include <locale> |
| 1761 | |
| 1762 | // #include "config.hpp" |
| 1763 | |
| 1764 | // #include "token.hpp" |
| 1765 | #ifndef INCLUDE_INJA_TOKEN_HPP_ |
| 1766 | #define INCLUDE_INJA_TOKEN_HPP_ |
| 1767 | |
| 1768 | #include <string> |
| 1769 | |
| 1770 | // #include "string_view.hpp" |
| 1771 | |
| 1772 | |
| 1773 | namespace inja { |
| 1774 | |
| 1775 | /*! |
| 1776 | * \brief Helper-class for the inja Lexer. |
| 1777 | */ |
| 1778 | struct Token { |
| 1779 | enum class Kind { |
| 1780 | Text, |
| 1781 | ExpressionOpen, // {{ |
| 1782 | ExpressionClose, // }} |
| 1783 | LineStatementOpen, // ## |
| 1784 | LineStatementClose, // \n |
| 1785 | StatementOpen, // {% |
| 1786 | StatementClose, // %} |
| 1787 | CommentOpen, // {# |
| 1788 | CommentClose, // #} |
| 1789 | Id, // this, this.foo |
| 1790 | Number, // 1, 2, -1, 5.2, -5.3 |
| 1791 | String, // "this" |
| 1792 | Plus, // + |
| 1793 | Minus, // - |
| 1794 | Times, // * |
| 1795 | Slash, // / |
| 1796 | Percent, // % |
| 1797 | Power, // ^ |
| 1798 | Comma, // , |
| 1799 | Dot, // . |
| 1800 | Colon, // : |
| 1801 | LeftParen, // ( |
| 1802 | RightParen, // ) |
| 1803 | LeftBracket, // [ |
| 1804 | RightBracket, // ] |
| 1805 | LeftBrace, // { |
| 1806 | RightBrace, // } |
| 1807 | Equal, // == |
| 1808 | NotEqual, // != |
| 1809 | GreaterThan, // > |
| 1810 | GreaterEqual, // >= |
| 1811 | LessThan, // < |
| 1812 | LessEqual, // <= |
| 1813 | Unknown, |
| 1814 | Eof, |
| 1815 | }; |
| 1816 | |
| 1817 | Kind kind {Kind::Unknown}; |
| 1818 | nonstd::string_view text; |
| 1819 | |
| 1820 | explicit constexpr Token() = default; |
| 1821 | explicit constexpr Token(Kind kind, nonstd::string_view text) : kind(kind), text(text) {} |
| 1822 | |
| 1823 | std::string describe() const { |
| 1824 | switch (kind) { |
| 1825 | case Kind::Text: |
| 1826 | return "<text>"; |
| 1827 | case Kind::LineStatementClose: |
| 1828 | return "<eol>"; |
| 1829 | case Kind::Eof: |
| 1830 | return "<eof>"; |
| 1831 | default: |
| 1832 | return static_cast<std::string>(text); |
| 1833 | } |
| 1834 | } |
| 1835 | }; |
| 1836 | |
| 1837 | } // namespace inja |
| 1838 | |
| 1839 | #endif // INCLUDE_INJA_TOKEN_HPP_ |
| 1840 | |
| 1841 | // #include "utils.hpp" |
| 1842 | #ifndef INCLUDE_INJA_UTILS_HPP_ |
| 1843 | #define INCLUDE_INJA_UTILS_HPP_ |
| 1844 | |
| 1845 | #include <algorithm> |
| 1846 | #include <fstream> |
| 1847 | #include <string> |
| 1848 | #include <utility> |
| 1849 | |
| 1850 | // #include "exceptions.hpp" |
| 1851 | |
| 1852 | // #include "string_view.hpp" |
| 1853 | |
| 1854 | |
| 1855 | namespace inja { |
| 1856 | |
| 1857 | inline void open_file_or_throw(const std::string &path, std::ifstream &file) { |
| 1858 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 1859 | #ifndef INJA_NOEXCEPTION |
| 1860 | try { |
| 1861 | file.open(path); |
| 1862 | } catch (const std::ios_base::failure & /*e*/) { |
| 1863 | INJA_THROW(FileError("failed accessing file at '" + path + "'")); |
| 1864 | } |
| 1865 | #else |
| 1866 | file.open(path); |
| 1867 | #endif |
| 1868 | } |
| 1869 | |
| 1870 | namespace string_view { |
| 1871 | inline nonstd::string_view slice(nonstd::string_view view, size_t start, size_t end) { |
| 1872 | start = std::min(start, view.size()); |
| 1873 | end = std::min(std::max(start, end), view.size()); |
| 1874 | return view.substr(start, end - start); |
| 1875 | } |
| 1876 | |
| 1877 | inline std::pair<nonstd::string_view, nonstd::string_view> split(nonstd::string_view view, char Separator) { |
| 1878 | size_t idx = view.find(Separator); |
| 1879 | if (idx == nonstd::string_view::npos) { |
| 1880 | return std::make_pair(view, nonstd::string_view()); |
| 1881 | } |
| 1882 | return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, nonstd::string_view::npos)); |
| 1883 | } |
| 1884 | |
| 1885 | inline bool starts_with(nonstd::string_view view, nonstd::string_view prefix) { |
| 1886 | return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0); |
| 1887 | } |
| 1888 | } // namespace string_view |
| 1889 | |
| 1890 | inline SourceLocation get_source_location(nonstd::string_view content, size_t pos) { |
| 1891 | // Get line and offset position (starts at 1:1) |
| 1892 | auto sliced = string_view::slice(content, 0, pos); |
| 1893 | std::size_t last_newline = sliced.rfind("\n"); |
| 1894 | |
| 1895 | if (last_newline == nonstd::string_view::npos) { |
| 1896 | return {1, sliced.length() + 1}; |
| 1897 | } |
| 1898 | |
| 1899 | // Count newlines |
| 1900 | size_t count_lines = 0; |
| 1901 | size_t search_start = 0; |
| 1902 | while (search_start <= sliced.size()) { |
| 1903 | search_start = sliced.find("\n", search_start) + 1; |
| 1904 | if (search_start == 0) { |
| 1905 | break; |
| 1906 | } |
| 1907 | count_lines += 1; |
| 1908 | } |
| 1909 | |
| 1910 | return {count_lines + 1, sliced.length() - last_newline}; |
| 1911 | } |
| 1912 | |
| 1913 | inline void replace_substring(std::string& s, const std::string& f, |
| 1914 | const std::string& t) |
| 1915 | { |
| 1916 | if (f.empty()) return; |
| 1917 | for (auto pos = s.find(f); // find first occurrence of f |
| 1918 | pos != std::string::npos; // make sure f was found |
| 1919 | s.replace(pos, f.size(), t), // replace with t, and |
| 1920 | pos = s.find(f, pos + t.size())) // find next occurrence of f |
| 1921 | {} |
| 1922 | } |
| 1923 | |
| 1924 | } // namespace inja |
| 1925 | |
| 1926 | #endif // INCLUDE_INJA_UTILS_HPP_ |
| 1927 | |
| 1928 | |
| 1929 | namespace inja { |
| 1930 | |
| 1931 | /*! |
| 1932 | * \brief Class for lexing an inja Template. |
| 1933 | */ |
| 1934 | class Lexer { |
| 1935 | enum class State { |
| 1936 | Text, |
| 1937 | ExpressionStart, |
| 1938 | ExpressionStartForceLstrip, |
| 1939 | ExpressionBody, |
| 1940 | LineStart, |
| 1941 | LineBody, |
| 1942 | StatementStart, |
| 1943 | StatementStartNoLstrip, |
| 1944 | StatementStartForceLstrip, |
| 1945 | StatementBody, |
| 1946 | CommentStart, |
| 1947 | CommentStartForceLstrip, |
| 1948 | CommentBody, |
| 1949 | }; |
| 1950 | |
| 1951 | enum class MinusState { |
| 1952 | Operator, |
| 1953 | Number, |
| 1954 | }; |
| 1955 | |
| 1956 | const LexerConfig &config; |
| 1957 | |
| 1958 | State state; |
| 1959 | MinusState minus_state; |
| 1960 | nonstd::string_view m_in; |
| 1961 | size_t tok_start; |
| 1962 | size_t pos; |
| 1963 | |
| 1964 | |
| 1965 | Token scan_body(nonstd::string_view close, Token::Kind closeKind, nonstd::string_view close_trim = nonstd::string_view(), bool trim = false) { |
| 1966 | again: |
| 1967 | // skip whitespace (except for \n as it might be a close) |
| 1968 | if (tok_start >= m_in.size()) { |
| 1969 | return make_token(Token::Kind::Eof); |
| 1970 | } |
| 1971 | const char ch = m_in[tok_start]; |
| 1972 | if (ch == ' ' || ch == '\t' || ch == '\r') { |
| 1973 | tok_start += 1; |
| 1974 | goto again; |
| 1975 | } |
| 1976 | |
| 1977 | // check for close |
| 1978 | if (!close_trim.empty() && inja::string_view::starts_with(m_in.substr(tok_start), close_trim)) { |
| 1979 | state = State::Text; |
| 1980 | pos = tok_start + close_trim.size(); |
| 1981 | const Token tok = make_token(closeKind); |
| 1982 | skip_whitespaces_and_newlines(); |
| 1983 | return tok; |
| 1984 | } |
| 1985 | |
| 1986 | if (inja::string_view::starts_with(m_in.substr(tok_start), close)) { |
| 1987 | state = State::Text; |
| 1988 | pos = tok_start + close.size(); |
| 1989 | const Token tok = make_token(closeKind); |
| 1990 | if (trim) { |
| 1991 | skip_whitespaces_and_first_newline(); |
| 1992 | } |
| 1993 | return tok; |
| 1994 | } |
| 1995 | |
| 1996 | // skip \n |
| 1997 | if (ch == '\n') { |
| 1998 | tok_start += 1; |
| 1999 | goto again; |
| 2000 | } |
| 2001 | |
| 2002 | pos = tok_start + 1; |
| 2003 | if (std::isalpha(ch)) { |
| 2004 | minus_state = MinusState::Operator; |
| 2005 | return scan_id(); |
| 2006 | } |
| 2007 | |
| 2008 | const MinusState current_minus_state = minus_state; |
| 2009 | if (minus_state == MinusState::Operator) { |
| 2010 | minus_state = MinusState::Number; |
| 2011 | } |
| 2012 | |
| 2013 | switch (ch) { |
| 2014 | case '+': |
| 2015 | return make_token(Token::Kind::Plus); |
| 2016 | case '-': |
| 2017 | if (current_minus_state == MinusState::Operator) { |
| 2018 | return make_token(Token::Kind::Minus); |
| 2019 | } |
| 2020 | return scan_number(); |
| 2021 | case '*': |
| 2022 | return make_token(Token::Kind::Times); |
| 2023 | case '/': |
| 2024 | return make_token(Token::Kind::Slash); |
| 2025 | case '^': |
| 2026 | return make_token(Token::Kind::Power); |
| 2027 | case '%': |
| 2028 | return make_token(Token::Kind::Percent); |
| 2029 | case '.': |
| 2030 | return make_token(Token::Kind::Dot); |
| 2031 | case ',': |
| 2032 | return make_token(Token::Kind::Comma); |
| 2033 | case ':': |
| 2034 | return make_token(Token::Kind::Colon); |
| 2035 | case '(': |
| 2036 | return make_token(Token::Kind::LeftParen); |
| 2037 | case ')': |
| 2038 | minus_state = MinusState::Operator; |
| 2039 | return make_token(Token::Kind::RightParen); |
| 2040 | case '[': |
| 2041 | return make_token(Token::Kind::LeftBracket); |
| 2042 | case ']': |
| 2043 | minus_state = MinusState::Operator; |
| 2044 | return make_token(Token::Kind::RightBracket); |
| 2045 | case '{': |
| 2046 | return make_token(Token::Kind::LeftBrace); |
| 2047 | case '}': |
| 2048 | minus_state = MinusState::Operator; |
| 2049 | return make_token(Token::Kind::RightBrace); |
| 2050 | case '>': |
| 2051 | if (pos < m_in.size() && m_in[pos] == '=') { |
| 2052 | pos += 1; |
| 2053 | return make_token(Token::Kind::GreaterEqual); |
| 2054 | } |
| 2055 | return make_token(Token::Kind::GreaterThan); |
| 2056 | case '<': |
| 2057 | if (pos < m_in.size() && m_in[pos] == '=') { |
| 2058 | pos += 1; |
| 2059 | return make_token(Token::Kind::LessEqual); |
| 2060 | } |
| 2061 | return make_token(Token::Kind::LessThan); |
| 2062 | case '=': |
| 2063 | if (pos < m_in.size() && m_in[pos] == '=') { |
| 2064 | pos += 1; |
| 2065 | return make_token(Token::Kind::Equal); |
| 2066 | } |
| 2067 | return make_token(Token::Kind::Unknown); |
| 2068 | case '!': |
| 2069 | if (pos < m_in.size() && m_in[pos] == '=') { |
| 2070 | pos += 1; |
| 2071 | return make_token(Token::Kind::NotEqual); |
| 2072 | } |
| 2073 | return make_token(Token::Kind::Unknown); |
| 2074 | case '\"': |
| 2075 | return scan_string(); |
| 2076 | case '0': |
| 2077 | case '1': |
| 2078 | case '2': |
| 2079 | case '3': |
| 2080 | case '4': |
| 2081 | case '5': |
| 2082 | case '6': |
| 2083 | case '7': |
| 2084 | case '8': |
| 2085 | case '9': |
| 2086 | minus_state = MinusState::Operator; |
| 2087 | return scan_number(); |
| 2088 | case '_': |
| 2089 | case '@': |
| 2090 | case '$': |
| 2091 | minus_state = MinusState::Operator; |
| 2092 | return scan_id(); |
| 2093 | default: |
| 2094 | return make_token(Token::Kind::Unknown); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | Token scan_id() { |
| 2099 | for (;;) { |
| 2100 | if (pos >= m_in.size()) { |
| 2101 | break; |
| 2102 | } |
| 2103 | const char ch = m_in[pos]; |
| 2104 | if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') { |
| 2105 | break; |
| 2106 | } |
| 2107 | pos += 1; |
| 2108 | } |
| 2109 | return make_token(Token::Kind::Id); |
| 2110 | } |
| 2111 | |
| 2112 | Token scan_number() { |
| 2113 | for (;;) { |
| 2114 | if (pos >= m_in.size()) { |
| 2115 | break; |
| 2116 | } |
| 2117 | const char ch = m_in[pos]; |
| 2118 | // be very permissive in lexer (we'll catch errors when conversion happens) |
| 2119 | if (!std::isdigit(ch) && ch != '.' && ch != 'e' && ch != 'E' && ch != '+' && ch != '-') { |
| 2120 | break; |
| 2121 | } |
| 2122 | pos += 1; |
| 2123 | } |
| 2124 | return make_token(Token::Kind::Number); |
| 2125 | } |
| 2126 | |
| 2127 | Token scan_string() { |
| 2128 | bool escape {false}; |
| 2129 | for (;;) { |
| 2130 | if (pos >= m_in.size()) { |
| 2131 | break; |
| 2132 | } |
| 2133 | const char ch = m_in[pos++]; |
| 2134 | if (ch == '\\') { |
| 2135 | escape = true; |
| 2136 | } else if (!escape && ch == m_in[tok_start]) { |
| 2137 | break; |
| 2138 | } else { |
| 2139 | escape = false; |
| 2140 | } |
| 2141 | } |
| 2142 | return make_token(Token::Kind::String); |
| 2143 | } |
| 2144 | |
| 2145 | Token make_token(Token::Kind kind) const { return Token(kind, string_view::slice(m_in, tok_start, pos)); } |
| 2146 | |
| 2147 | void skip_whitespaces_and_newlines() { |
| 2148 | if (pos < m_in.size()) { |
| 2149 | while (pos < m_in.size() && (m_in[pos] == ' ' || m_in[pos] == '\t' || m_in[pos] == '\n' || m_in[pos] == '\r')) { |
| 2150 | pos += 1; |
| 2151 | } |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | void skip_whitespaces_and_first_newline() { |
| 2156 | if (pos < m_in.size()) { |
| 2157 | while (pos < m_in.size() && (m_in[pos] == ' ' || m_in[pos] == '\t')) { |
| 2158 | pos += 1; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | if (pos < m_in.size()) { |
| 2163 | const char ch = m_in[pos]; |
| 2164 | if (ch == '\n') { |
| 2165 | pos += 1; |
| 2166 | } else if (ch == '\r') { |
| 2167 | pos += 1; |
| 2168 | if (pos < m_in.size() && m_in[pos] == '\n') { |
| 2169 | pos += 1; |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text) { |
| 2176 | nonstd::string_view result = text; |
| 2177 | while (!result.empty()) { |
| 2178 | const char ch = result.back(); |
| 2179 | if (ch == ' ' || ch == '\t') { |
| 2180 | result.remove_suffix(1); |
| 2181 | } else if (ch == '\n' || ch == '\r') { |
| 2182 | break; |
| 2183 | } else { |
| 2184 | return text; |
| 2185 | } |
| 2186 | } |
| 2187 | return result; |
| 2188 | } |
| 2189 | |
| 2190 | public: |
| 2191 | explicit Lexer(const LexerConfig &config) : config(config), state(State::Text), minus_state(MinusState::Number) {} |
| 2192 | |
| 2193 | SourceLocation current_position() const { |
| 2194 | return get_source_location(m_in, tok_start); |
| 2195 | } |
| 2196 | |
| 2197 | void start(nonstd::string_view input) { |
| 2198 | m_in = input; |
| 2199 | tok_start = 0; |
| 2200 | pos = 0; |
| 2201 | state = State::Text; |
| 2202 | minus_state = MinusState::Number; |
| 2203 | |
| 2204 | // Consume byte order mark (BOM) for UTF-8 |
| 2205 | if (inja::string_view::starts_with(m_in, "\xEF\xBB\xBF")) { |
| 2206 | m_in = m_in.substr(3); |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | Token scan() { |
| 2211 | tok_start = pos; |
| 2212 | |
| 2213 | again: |
| 2214 | if (tok_start >= m_in.size()) { |
| 2215 | return make_token(Token::Kind::Eof); |
| 2216 | } |
| 2217 | |
| 2218 | switch (state) { |
| 2219 | default: |
| 2220 | case State::Text: { |
| 2221 | // fast-scan to first open character |
| 2222 | const size_t open_start = m_in.substr(pos).find_first_of(config.open_chars); |
| 2223 | if (open_start == nonstd::string_view::npos) { |
| 2224 | // didn't find open, return remaining text as text token |
| 2225 | pos = m_in.size(); |
| 2226 | return make_token(Token::Kind::Text); |
| 2227 | } |
| 2228 | pos += open_start; |
| 2229 | |
| 2230 | // try to match one of the opening sequences, and get the close |
| 2231 | nonstd::string_view open_str = m_in.substr(pos); |
| 2232 | bool must_lstrip = false; |
| 2233 | if (inja::string_view::starts_with(open_str, config.expression_open)) { |
| 2234 | if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) { |
| 2235 | state = State::ExpressionStartForceLstrip; |
| 2236 | must_lstrip = true; |
| 2237 | } else { |
| 2238 | state = State::ExpressionStart; |
| 2239 | } |
| 2240 | } else if (inja::string_view::starts_with(open_str, config.statement_open)) { |
| 2241 | if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) { |
| 2242 | state = State::StatementStartNoLstrip; |
| 2243 | } else if (inja::string_view::starts_with(open_str, config.statement_open_force_lstrip )) { |
| 2244 | state = State::StatementStartForceLstrip; |
| 2245 | must_lstrip = true; |
| 2246 | } else { |
| 2247 | state = State::StatementStart; |
| 2248 | must_lstrip = config.lstrip_blocks; |
| 2249 | } |
| 2250 | } else if (inja::string_view::starts_with(open_str, config.comment_open)) { |
| 2251 | if (inja::string_view::starts_with(open_str, config.comment_open_force_lstrip)) { |
| 2252 | state = State::CommentStartForceLstrip; |
| 2253 | must_lstrip = true; |
| 2254 | } else { |
| 2255 | state = State::CommentStart; |
| 2256 | must_lstrip = config.lstrip_blocks; |
| 2257 | } |
| 2258 | } else if ((pos == 0 || m_in[pos - 1] == '\n') && config.line_statement.length() > 0 && inja::string_view::starts_with(open_str, config.line_statement)) { |
| 2259 | state = State::LineStart; |
| 2260 | } else { |
| 2261 | pos += 1; // wasn't actually an opening sequence |
| 2262 | goto again; |
| 2263 | } |
| 2264 | |
| 2265 | nonstd::string_view text = string_view::slice(m_in, tok_start, pos); |
| 2266 | if (must_lstrip) { |
| 2267 | text = clear_final_line_if_whitespace(text); |
| 2268 | } |
| 2269 | |
| 2270 | if (text.empty()) { |
| 2271 | goto again; // don't generate empty token |
| 2272 | } |
| 2273 | return Token(Token::Kind::Text, text); |
| 2274 | } |
| 2275 | case State::ExpressionStart: { |
| 2276 | state = State::ExpressionBody; |
| 2277 | pos += config.expression_open.size(); |
| 2278 | return make_token(Token::Kind::ExpressionOpen); |
| 2279 | } |
| 2280 | case State::ExpressionStartForceLstrip: { |
| 2281 | state = State::ExpressionBody; |
| 2282 | pos += config.expression_open_force_lstrip.size(); |
| 2283 | return make_token(Token::Kind::ExpressionOpen); |
| 2284 | } |
| 2285 | case State::LineStart: { |
| 2286 | state = State::LineBody; |
| 2287 | pos += config.line_statement.size(); |
| 2288 | return make_token(Token::Kind::LineStatementOpen); |
| 2289 | } |
| 2290 | case State::StatementStart: { |
| 2291 | state = State::StatementBody; |
| 2292 | pos += config.statement_open.size(); |
| 2293 | return make_token(Token::Kind::StatementOpen); |
| 2294 | } |
| 2295 | case State::StatementStartNoLstrip: { |
| 2296 | state = State::StatementBody; |
| 2297 | pos += config.statement_open_no_lstrip.size(); |
| 2298 | return make_token(Token::Kind::StatementOpen); |
| 2299 | } |
| 2300 | case State::StatementStartForceLstrip: { |
| 2301 | state = State::StatementBody; |
| 2302 | pos += config.statement_open_force_lstrip.size(); |
| 2303 | return make_token(Token::Kind::StatementOpen); |
| 2304 | } |
| 2305 | case State::CommentStart: { |
| 2306 | state = State::CommentBody; |
| 2307 | pos += config.comment_open.size(); |
| 2308 | return make_token(Token::Kind::CommentOpen); |
| 2309 | } |
| 2310 | case State::CommentStartForceLstrip: { |
| 2311 | state = State::CommentBody; |
| 2312 | pos += config.comment_open_force_lstrip.size(); |
| 2313 | return make_token(Token::Kind::CommentOpen); |
| 2314 | } |
| 2315 | case State::ExpressionBody: |
| 2316 | return scan_body(config.expression_close, Token::Kind::ExpressionClose, config.expression_close_force_rstrip); |
| 2317 | case State::LineBody: |
| 2318 | return scan_body("\n", Token::Kind::LineStatementClose); |
| 2319 | case State::StatementBody: |
| 2320 | return scan_body(config.statement_close, Token::Kind::StatementClose, config.statement_close_force_rstrip, config.trim_blocks); |
| 2321 | case State::CommentBody: { |
| 2322 | // fast-scan to comment close |
| 2323 | const size_t end = m_in.substr(pos).find(config.comment_close); |
| 2324 | if (end == nonstd::string_view::npos) { |
| 2325 | pos = m_in.size(); |
| 2326 | return make_token(Token::Kind::Eof); |
| 2327 | } |
| 2328 | |
| 2329 | // Check for trim pattern |
| 2330 | const bool must_rstrip = inja::string_view::starts_with(m_in.substr(pos + end - 1), config.comment_close_force_rstrip); |
| 2331 | |
| 2332 | // return the entire comment in the close token |
| 2333 | state = State::Text; |
| 2334 | pos += end + config.comment_close.size(); |
| 2335 | Token tok = make_token(Token::Kind::CommentClose); |
| 2336 | |
| 2337 | if (must_rstrip || config.trim_blocks) { |
| 2338 | skip_whitespaces_and_first_newline(); |
| 2339 | } |
| 2340 | return tok; |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | const LexerConfig &get_config() const { |
| 2346 | return config; |
| 2347 | } |
| 2348 | }; |
| 2349 | |
| 2350 | } // namespace inja |
| 2351 | |
| 2352 | #endif // INCLUDE_INJA_LEXER_HPP_ |
| 2353 | |
| 2354 | // #include "node.hpp" |
| 2355 | #ifndef INCLUDE_INJA_NODE_HPP_ |
| 2356 | #define INCLUDE_INJA_NODE_HPP_ |
| 2357 | |
| 2358 | #include <string> |
| 2359 | #include <utility> |
| 2360 | |
| 2361 | #include <nlohmann/json.hpp> |
| 2362 | |
| 2363 | // #include "function_storage.hpp" |
| 2364 | |
| 2365 | // #include "string_view.hpp" |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | namespace inja { |
| 2370 | |
| 2371 | class NodeVisitor; |
| 2372 | class BlockNode; |
| 2373 | class TextNode; |
| 2374 | class ExpressionNode; |
| 2375 | class LiteralNode; |
| 2376 | class JsonNode; |
| 2377 | class FunctionNode; |
| 2378 | class ExpressionListNode; |
| 2379 | class StatementNode; |
| 2380 | class ForStatementNode; |
| 2381 | class ForArrayStatementNode; |
| 2382 | class ForObjectStatementNode; |
| 2383 | class IfStatementNode; |
| 2384 | class IncludeStatementNode; |
| 2385 | class ExtendsStatementNode; |
| 2386 | class BlockStatementNode; |
| 2387 | class SetStatementNode; |
| 2388 | |
| 2389 | |
| 2390 | class NodeVisitor { |
| 2391 | public: |
| 2392 | virtual ~NodeVisitor() = default; |
| 2393 | |
| 2394 | virtual void visit(const BlockNode& node) = 0; |
| 2395 | virtual void visit(const TextNode& node) = 0; |
| 2396 | virtual void visit(const ExpressionNode& node) = 0; |
| 2397 | virtual void visit(const LiteralNode& node) = 0; |
| 2398 | virtual void visit(const JsonNode& node) = 0; |
| 2399 | virtual void visit(const FunctionNode& node) = 0; |
| 2400 | virtual void visit(const ExpressionListNode& node) = 0; |
| 2401 | virtual void visit(const StatementNode& node) = 0; |
| 2402 | virtual void visit(const ForStatementNode& node) = 0; |
| 2403 | virtual void visit(const ForArrayStatementNode& node) = 0; |
| 2404 | virtual void visit(const ForObjectStatementNode& node) = 0; |
| 2405 | virtual void visit(const IfStatementNode& node) = 0; |
| 2406 | virtual void visit(const IncludeStatementNode& node) = 0; |
| 2407 | virtual void visit(const ExtendsStatementNode& node) = 0; |
| 2408 | virtual void visit(const BlockStatementNode& node) = 0; |
| 2409 | virtual void visit(const SetStatementNode& node) = 0; |
| 2410 | }; |
| 2411 | |
| 2412 | /*! |
| 2413 | * \brief Base node class for the abstract syntax tree (AST). |
| 2414 | */ |
| 2415 | class AstNode { |
| 2416 | public: |
| 2417 | virtual void accept(NodeVisitor& v) const = 0; |
| 2418 | |
| 2419 | size_t pos; |
| 2420 | |
| 2421 | AstNode(size_t pos) : pos(pos) { } |
| 2422 | virtual ~AstNode() { } |
| 2423 | }; |
| 2424 | |
| 2425 | |
| 2426 | class BlockNode : public AstNode { |
| 2427 | public: |
| 2428 | std::vector<std::shared_ptr<AstNode>> nodes; |
| 2429 | |
| 2430 | explicit BlockNode() : AstNode(0) {} |
| 2431 | |
| 2432 | void accept(NodeVisitor& v) const { |
| 2433 | v.visit(*this); |
| 2434 | } |
| 2435 | }; |
| 2436 | |
| 2437 | class TextNode : public AstNode { |
| 2438 | public: |
| 2439 | const size_t length; |
| 2440 | |
| 2441 | explicit TextNode(size_t pos, size_t length): AstNode(pos), length(length) { } |
| 2442 | |
| 2443 | void accept(NodeVisitor& v) const { |
| 2444 | v.visit(*this); |
| 2445 | } |
| 2446 | }; |
| 2447 | |
| 2448 | class ExpressionNode : public AstNode { |
| 2449 | public: |
| 2450 | explicit ExpressionNode(size_t pos) : AstNode(pos) {} |
| 2451 | |
| 2452 | void accept(NodeVisitor& v) const { |
| 2453 | v.visit(*this); |
| 2454 | } |
| 2455 | }; |
| 2456 | |
| 2457 | class LiteralNode : public ExpressionNode { |
| 2458 | public: |
| 2459 | const nlohmann::json value; |
| 2460 | |
| 2461 | explicit LiteralNode(const nlohmann::json& value, size_t pos) : ExpressionNode(pos), value(value) { } |
| 2462 | |
| 2463 | void accept(NodeVisitor& v) const { |
| 2464 | v.visit(*this); |
| 2465 | } |
| 2466 | }; |
| 2467 | |
| 2468 | class JsonNode : public ExpressionNode { |
| 2469 | public: |
| 2470 | const std::string name; |
| 2471 | const json::json_pointer ptr; |
| 2472 | |
| 2473 | static std::string convert_dot_to_json_ptr(nonstd::string_view ptr_name) { |
| 2474 | std::string result; |
| 2475 | do { |
| 2476 | nonstd::string_view part; |
| 2477 | std::tie(part, ptr_name) = string_view::split(ptr_name, '.'); |
| 2478 | result.push_back('/'); |
| 2479 | result.append(part.begin(), part.end()); |
| 2480 | } while (!ptr_name.empty()); |
| 2481 | return result; |
| 2482 | } |
| 2483 | |
| 2484 | explicit JsonNode(nonstd::string_view ptr_name, size_t pos) : ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_json_ptr(ptr_name))) { } |
| 2485 | |
| 2486 | void accept(NodeVisitor& v) const { |
| 2487 | v.visit(*this); |
| 2488 | } |
| 2489 | }; |
| 2490 | |
| 2491 | class FunctionNode : public ExpressionNode { |
| 2492 | using Op = FunctionStorage::Operation; |
| 2493 | |
| 2494 | public: |
| 2495 | enum class Associativity { |
| 2496 | Left, |
| 2497 | Right, |
| 2498 | }; |
| 2499 | |
| 2500 | unsigned int precedence; |
| 2501 | Associativity associativity; |
| 2502 | |
| 2503 | Op operation; |
| 2504 | |
| 2505 | std::string name; |
| 2506 | int number_args; // Should also be negative -> -1 for unknown number |
| 2507 | std::vector<std::shared_ptr<ExpressionNode>> arguments; |
| 2508 | CallbackFunction callback; |
| 2509 | |
| 2510 | explicit FunctionNode(nonstd::string_view name, size_t pos) : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(1) { } |
| 2511 | explicit FunctionNode(Op operation, size_t pos) : ExpressionNode(pos), operation(operation), number_args(1) { |
| 2512 | switch (operation) { |
| 2513 | case Op::Not: { |
| 2514 | number_args = 1; |
| 2515 | precedence = 4; |
| 2516 | associativity = Associativity::Left; |
| 2517 | } break; |
| 2518 | case Op::And: { |
| 2519 | number_args = 2; |
| 2520 | precedence = 1; |
| 2521 | associativity = Associativity::Left; |
| 2522 | } break; |
| 2523 | case Op::Or: { |
| 2524 | number_args = 2; |
| 2525 | precedence = 1; |
| 2526 | associativity = Associativity::Left; |
| 2527 | } break; |
| 2528 | case Op::In: { |
| 2529 | number_args = 2; |
| 2530 | precedence = 2; |
| 2531 | associativity = Associativity::Left; |
| 2532 | } break; |
| 2533 | case Op::Equal: { |
| 2534 | number_args = 2; |
| 2535 | precedence = 2; |
| 2536 | associativity = Associativity::Left; |
| 2537 | } break; |
| 2538 | case Op::NotEqual: { |
| 2539 | number_args = 2; |
| 2540 | precedence = 2; |
| 2541 | associativity = Associativity::Left; |
| 2542 | } break; |
| 2543 | case Op::Greater: { |
| 2544 | number_args = 2; |
| 2545 | precedence = 2; |
| 2546 | associativity = Associativity::Left; |
| 2547 | } break; |
| 2548 | case Op::GreaterEqual: { |
| 2549 | number_args = 2; |
| 2550 | precedence = 2; |
| 2551 | associativity = Associativity::Left; |
| 2552 | } break; |
| 2553 | case Op::Less: { |
| 2554 | number_args = 2; |
| 2555 | precedence = 2; |
| 2556 | associativity = Associativity::Left; |
| 2557 | } break; |
| 2558 | case Op::LessEqual: { |
| 2559 | number_args = 2; |
| 2560 | precedence = 2; |
| 2561 | associativity = Associativity::Left; |
| 2562 | } break; |
| 2563 | case Op::Add: { |
| 2564 | number_args = 2; |
| 2565 | precedence = 3; |
| 2566 | associativity = Associativity::Left; |
| 2567 | } break; |
| 2568 | case Op::Subtract: { |
| 2569 | number_args = 2; |
| 2570 | precedence = 3; |
| 2571 | associativity = Associativity::Left; |
| 2572 | } break; |
| 2573 | case Op::Multiplication: { |
| 2574 | number_args = 2; |
| 2575 | precedence = 4; |
| 2576 | associativity = Associativity::Left; |
| 2577 | } break; |
| 2578 | case Op::Division: { |
| 2579 | number_args = 2; |
| 2580 | precedence = 4; |
| 2581 | associativity = Associativity::Left; |
| 2582 | } break; |
| 2583 | case Op::Power: { |
| 2584 | number_args = 2; |
| 2585 | precedence = 5; |
| 2586 | associativity = Associativity::Right; |
| 2587 | } break; |
| 2588 | case Op::Modulo: { |
| 2589 | number_args = 2; |
| 2590 | precedence = 4; |
| 2591 | associativity = Associativity::Left; |
| 2592 | } break; |
| 2593 | case Op::AtId: { |
| 2594 | number_args = 2; |
| 2595 | precedence = 8; |
| 2596 | associativity = Associativity::Left; |
| 2597 | } break; |
| 2598 | default: { |
| 2599 | precedence = 1; |
| 2600 | associativity = Associativity::Left; |
| 2601 | } |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | void accept(NodeVisitor& v) const { |
| 2606 | v.visit(*this); |
| 2607 | } |
| 2608 | }; |
| 2609 | |
| 2610 | class ExpressionListNode : public AstNode { |
| 2611 | public: |
| 2612 | std::shared_ptr<ExpressionNode> root; |
| 2613 | |
| 2614 | explicit ExpressionListNode() : AstNode(0) { } |
| 2615 | explicit ExpressionListNode(size_t pos) : AstNode(pos) { } |
| 2616 | |
| 2617 | void accept(NodeVisitor& v) const { |
| 2618 | v.visit(*this); |
| 2619 | } |
| 2620 | }; |
| 2621 | |
| 2622 | class StatementNode : public AstNode { |
| 2623 | public: |
| 2624 | StatementNode(size_t pos) : AstNode(pos) { } |
| 2625 | |
| 2626 | virtual void accept(NodeVisitor& v) const = 0; |
| 2627 | }; |
| 2628 | |
| 2629 | class ForStatementNode : public StatementNode { |
| 2630 | public: |
| 2631 | ExpressionListNode condition; |
| 2632 | BlockNode body; |
| 2633 | BlockNode *const parent; |
| 2634 | |
| 2635 | ForStatementNode(BlockNode *const parent, size_t pos) : StatementNode(pos), parent(parent) { } |
| 2636 | |
| 2637 | virtual void accept(NodeVisitor& v) const = 0; |
| 2638 | }; |
| 2639 | |
| 2640 | class ForArrayStatementNode : public ForStatementNode { |
| 2641 | public: |
| 2642 | const std::string value; |
| 2643 | |
| 2644 | explicit ForArrayStatementNode(const std::string& value, BlockNode *const parent, size_t pos) : ForStatementNode(parent, pos), value(value) { } |
| 2645 | |
| 2646 | void accept(NodeVisitor& v) const { |
| 2647 | v.visit(*this); |
| 2648 | } |
| 2649 | }; |
| 2650 | |
| 2651 | class ForObjectStatementNode : public ForStatementNode { |
| 2652 | public: |
| 2653 | const std::string key; |
| 2654 | const std::string value; |
| 2655 | |
| 2656 | explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode *const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) { } |
| 2657 | |
| 2658 | void accept(NodeVisitor& v) const { |
| 2659 | v.visit(*this); |
| 2660 | } |
| 2661 | }; |
| 2662 | |
| 2663 | class IfStatementNode : public StatementNode { |
| 2664 | public: |
| 2665 | ExpressionListNode condition; |
| 2666 | BlockNode true_statement; |
| 2667 | BlockNode false_statement; |
| 2668 | BlockNode *const parent; |
| 2669 | |
| 2670 | const bool is_nested; |
| 2671 | bool has_false_statement {false}; |
| 2672 | |
| 2673 | explicit IfStatementNode(BlockNode *const parent, size_t pos) : StatementNode(pos), parent(parent), is_nested(false) { } |
| 2674 | explicit IfStatementNode(bool is_nested, BlockNode *const parent, size_t pos) : StatementNode(pos), parent(parent), is_nested(is_nested) { } |
| 2675 | |
| 2676 | void accept(NodeVisitor& v) const { |
| 2677 | v.visit(*this); |
| 2678 | } |
| 2679 | }; |
| 2680 | |
| 2681 | class IncludeStatementNode : public StatementNode { |
| 2682 | public: |
| 2683 | const std::string file; |
| 2684 | |
| 2685 | explicit IncludeStatementNode(const std::string& file, size_t pos) : StatementNode(pos), file(file) { } |
| 2686 | |
| 2687 | void accept(NodeVisitor& v) const { |
| 2688 | v.visit(*this); |
| 2689 | } |
| 2690 | }; |
| 2691 | |
| 2692 | class ExtendsStatementNode : public StatementNode { |
| 2693 | public: |
| 2694 | const std::string file; |
| 2695 | |
| 2696 | explicit ExtendsStatementNode(const std::string& file, size_t pos) : StatementNode(pos), file(file) { } |
| 2697 | |
| 2698 | void accept(NodeVisitor& v) const { |
| 2699 | v.visit(*this); |
| 2700 | }; |
| 2701 | }; |
| 2702 | |
| 2703 | class BlockStatementNode : public StatementNode { |
| 2704 | public: |
| 2705 | const std::string name; |
| 2706 | BlockNode block; |
| 2707 | BlockNode *const parent; |
| 2708 | |
| 2709 | explicit BlockStatementNode(BlockNode *const parent, const std::string& name, size_t pos) : StatementNode(pos), name(name), parent(parent) { } |
| 2710 | |
| 2711 | void accept(NodeVisitor& v) const { |
| 2712 | v.visit(*this); |
| 2713 | }; |
| 2714 | }; |
| 2715 | |
| 2716 | class SetStatementNode : public StatementNode { |
| 2717 | public: |
| 2718 | const std::string key; |
| 2719 | ExpressionListNode expression; |
| 2720 | |
| 2721 | explicit SetStatementNode(const std::string& key, size_t pos) : StatementNode(pos), key(key) { } |
| 2722 | |
| 2723 | void accept(NodeVisitor& v) const { |
| 2724 | v.visit(*this); |
| 2725 | } |
| 2726 | }; |
| 2727 | |
| 2728 | } // namespace inja |
| 2729 | |
| 2730 | #endif // INCLUDE_INJA_NODE_HPP_ |
| 2731 | |
| 2732 | // #include "template.hpp" |
| 2733 | #ifndef INCLUDE_INJA_TEMPLATE_HPP_ |
| 2734 | #define INCLUDE_INJA_TEMPLATE_HPP_ |
| 2735 | |
| 2736 | #include <map> |
| 2737 | #include <memory> |
| 2738 | #include <string> |
| 2739 | #include <vector> |
| 2740 | |
| 2741 | // #include "node.hpp" |
| 2742 | |
| 2743 | // #include "statistics.hpp" |
| 2744 | #ifndef INCLUDE_INJA_STATISTICS_HPP_ |
| 2745 | #define INCLUDE_INJA_STATISTICS_HPP_ |
| 2746 | |
| 2747 | // #include "node.hpp" |
| 2748 | |
| 2749 | |
| 2750 | |
| 2751 | namespace inja { |
| 2752 | |
| 2753 | /*! |
| 2754 | * \brief A class for counting statistics on a Template. |
| 2755 | */ |
| 2756 | class StatisticsVisitor : public NodeVisitor { |
| 2757 | void visit(const BlockNode& node) { |
| 2758 | for (auto& n : node.nodes) { |
| 2759 | n->accept(*this); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | void visit(const TextNode&) { } |
| 2764 | void visit(const ExpressionNode&) { } |
| 2765 | void visit(const LiteralNode&) { } |
| 2766 | |
| 2767 | void visit(const JsonNode&) { |
| 2768 | variable_counter += 1; |
| 2769 | } |
| 2770 | |
| 2771 | void visit(const FunctionNode& node) { |
| 2772 | for (auto& n : node.arguments) { |
| 2773 | n->accept(*this); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | void visit(const ExpressionListNode& node) { |
| 2778 | node.root->accept(*this); |
| 2779 | } |
| 2780 | |
| 2781 | void visit(const StatementNode&) { } |
| 2782 | void visit(const ForStatementNode&) { } |
| 2783 | |
| 2784 | void visit(const ForArrayStatementNode& node) { |
| 2785 | node.condition.accept(*this); |
| 2786 | node.body.accept(*this); |
| 2787 | } |
| 2788 | |
| 2789 | void visit(const ForObjectStatementNode& node) { |
| 2790 | node.condition.accept(*this); |
| 2791 | node.body.accept(*this); |
| 2792 | } |
| 2793 | |
| 2794 | void visit(const IfStatementNode& node) { |
| 2795 | node.condition.accept(*this); |
| 2796 | node.true_statement.accept(*this); |
| 2797 | node.false_statement.accept(*this); |
| 2798 | } |
| 2799 | |
| 2800 | void visit(const IncludeStatementNode&) { } |
| 2801 | |
| 2802 | void visit(const ExtendsStatementNode&) { } |
| 2803 | |
| 2804 | void visit(const BlockStatementNode& node) { |
| 2805 | node.block.accept(*this); |
| 2806 | } |
| 2807 | |
| 2808 | void visit(const SetStatementNode&) { } |
| 2809 | |
| 2810 | public: |
| 2811 | unsigned int variable_counter; |
| 2812 | |
| 2813 | explicit StatisticsVisitor() : variable_counter(0) { } |
| 2814 | }; |
| 2815 | |
| 2816 | } // namespace inja |
| 2817 | |
| 2818 | #endif // INCLUDE_INJA_STATISTICS_HPP_ |
| 2819 | |
| 2820 | |
| 2821 | |
| 2822 | namespace inja { |
| 2823 | |
| 2824 | /*! |
| 2825 | * \brief The main inja Template. |
| 2826 | */ |
| 2827 | struct Template { |
| 2828 | BlockNode root; |
| 2829 | std::string content; |
| 2830 | std::map<std::string, std::shared_ptr<BlockStatementNode>> block_storage; |
| 2831 | |
| 2832 | explicit Template() { } |
| 2833 | explicit Template(const std::string& content): content(content) { } |
| 2834 | |
| 2835 | /// Return number of variables (total number, not distinct ones) in the template |
| 2836 | int count_variables() { |
| 2837 | auto statistic_visitor = StatisticsVisitor(); |
| 2838 | root.accept(statistic_visitor); |
| 2839 | return statistic_visitor.variable_counter; |
| 2840 | } |
| 2841 | }; |
| 2842 | |
| 2843 | using TemplateStorage = std::map<std::string, Template>; |
| 2844 | |
| 2845 | } // namespace inja |
| 2846 | |
| 2847 | #endif // INCLUDE_INJA_TEMPLATE_HPP_ |
| 2848 | |
| 2849 | // #include "token.hpp" |
| 2850 | |
| 2851 | // #include "utils.hpp" |
| 2852 | |
| 2853 | |
| 2854 | #include <nlohmann/json.hpp> |
| 2855 | |
| 2856 | namespace inja { |
| 2857 | |
| 2858 | /*! |
| 2859 | * \brief Class for parsing an inja Template. |
| 2860 | */ |
| 2861 | class Parser { |
| 2862 | const ParserConfig &config; |
| 2863 | |
| 2864 | Lexer lexer; |
| 2865 | TemplateStorage &template_storage; |
| 2866 | const FunctionStorage &function_storage; |
| 2867 | |
| 2868 | Token tok, peek_tok; |
| 2869 | bool have_peek_tok {false}; |
| 2870 | |
| 2871 | size_t current_paren_level {0}; |
| 2872 | size_t current_bracket_level {0}; |
| 2873 | size_t current_brace_level {0}; |
| 2874 | |
| 2875 | nonstd::string_view json_literal_start; |
| 2876 | |
| 2877 | BlockNode *current_block {nullptr}; |
| 2878 | ExpressionListNode *current_expression_list {nullptr}; |
| 2879 | std::stack<std::pair<FunctionNode*, size_t>> function_stack; |
| 2880 | std::vector<std::shared_ptr<ExpressionNode>> arguments; |
| 2881 | |
| 2882 | std::stack<std::shared_ptr<FunctionNode>> operator_stack; |
| 2883 | std::stack<IfStatementNode*> if_statement_stack; |
| 2884 | std::stack<ForStatementNode*> for_statement_stack; |
| 2885 | std::stack<BlockStatementNode*> block_statement_stack; |
| 2886 | |
| 2887 | inline void throw_parser_error(const std::string &message) { |
| 2888 | INJA_THROW(ParserError(message, lexer.current_position())); |
| 2889 | } |
| 2890 | |
| 2891 | inline void get_next_token() { |
| 2892 | if (have_peek_tok) { |
| 2893 | tok = peek_tok; |
| 2894 | have_peek_tok = false; |
| 2895 | } else { |
| 2896 | tok = lexer.scan(); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | inline void get_peek_token() { |
| 2901 | if (!have_peek_tok) { |
| 2902 | peek_tok = lexer.scan(); |
| 2903 | have_peek_tok = true; |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | inline void add_json_literal(const char* content_ptr) { |
| 2908 | nonstd::string_view json_text(json_literal_start.data(), tok.text.data() - json_literal_start.data() + tok.text.size()); |
| 2909 | arguments.emplace_back(std::make_shared<LiteralNode>(json::parse(json_text), json_text.data() - content_ptr)); |
| 2910 | } |
| 2911 | |
| 2912 | inline void add_operator() { |
| 2913 | auto function = operator_stack.top(); |
| 2914 | operator_stack.pop(); |
| 2915 | |
| 2916 | for (int i = 0; i < function->number_args; ++i) { |
| 2917 | function->arguments.insert(function->arguments.begin(), arguments.back()); |
| 2918 | arguments.pop_back(); |
| 2919 | } |
| 2920 | arguments.emplace_back(function); |
| 2921 | } |
| 2922 | |
| 2923 | void add_to_template_storage(nonstd::string_view path, std::string& template_name) { |
| 2924 | if (config.search_included_templates_in_files && template_storage.find(template_name) == template_storage.end()) { |
| 2925 | // Build the relative path |
| 2926 | template_name = static_cast<std::string>(path) + template_name; |
| 2927 | if (template_name.compare(0, 2, "./") == 0) { |
| 2928 | template_name.erase(0, 2); |
| 2929 | } |
| 2930 | |
| 2931 | if (template_storage.find(template_name) == template_storage.end()) { |
| 2932 | auto include_template = Template(load_file(template_name)); |
| 2933 | template_storage.emplace(template_name, include_template); |
| 2934 | parse_into_template(template_storage[template_name], template_name); |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | bool parse_expression(Template &tmpl, Token::Kind closing) { |
| 2940 | while (tok.kind != closing && tok.kind != Token::Kind::Eof) { |
| 2941 | // Literals |
| 2942 | switch (tok.kind) { |
| 2943 | case Token::Kind::String: { |
| 2944 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2945 | json_literal_start = tok.text; |
| 2946 | add_json_literal(tmpl.content.c_str()); |
| 2947 | } |
| 2948 | |
| 2949 | } break; |
| 2950 | case Token::Kind::Number: { |
| 2951 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2952 | json_literal_start = tok.text; |
| 2953 | add_json_literal(tmpl.content.c_str()); |
| 2954 | } |
| 2955 | |
| 2956 | } break; |
| 2957 | case Token::Kind::LeftBracket: { |
| 2958 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2959 | json_literal_start = tok.text; |
| 2960 | } |
| 2961 | current_bracket_level += 1; |
| 2962 | |
| 2963 | } break; |
| 2964 | case Token::Kind::LeftBrace: { |
| 2965 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2966 | json_literal_start = tok.text; |
| 2967 | } |
| 2968 | current_brace_level += 1; |
| 2969 | |
| 2970 | } break; |
| 2971 | case Token::Kind::RightBracket: { |
| 2972 | if (current_bracket_level == 0) { |
| 2973 | throw_parser_error("unexpected ']'"); |
| 2974 | } |
| 2975 | |
| 2976 | current_bracket_level -= 1; |
| 2977 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2978 | add_json_literal(tmpl.content.c_str()); |
| 2979 | } |
| 2980 | |
| 2981 | } break; |
| 2982 | case Token::Kind::RightBrace: { |
| 2983 | if (current_brace_level == 0) { |
| 2984 | throw_parser_error("unexpected '}'"); |
| 2985 | } |
| 2986 | |
| 2987 | current_brace_level -= 1; |
| 2988 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2989 | add_json_literal(tmpl.content.c_str()); |
| 2990 | } |
| 2991 | |
| 2992 | } break; |
| 2993 | case Token::Kind::Id: { |
| 2994 | get_peek_token(); |
| 2995 | |
| 2996 | // Json Literal |
| 2997 | if (tok.text == static_cast<decltype(tok.text)>("true") || tok.text == static_cast<decltype(tok.text)>("false") || tok.text == static_cast<decltype(tok.text)>("null")) { |
| 2998 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 2999 | json_literal_start = tok.text; |
| 3000 | add_json_literal(tmpl.content.c_str()); |
| 3001 | } |
| 3002 | |
| 3003 | // Operator |
| 3004 | } else if (tok.text == "and" || tok.text == "or" || tok.text == "in" || tok.text == "not") { |
| 3005 | goto parse_operator; |
| 3006 | |
| 3007 | // Functions |
| 3008 | } else if (peek_tok.kind == Token::Kind::LeftParen) { |
| 3009 | operator_stack.emplace(std::make_shared<FunctionNode>(static_cast<std::string>(tok.text), tok.text.data() - tmpl.content.c_str())); |
| 3010 | function_stack.emplace(operator_stack.top().get(), current_paren_level); |
| 3011 | |
| 3012 | // Variables |
| 3013 | } else { |
| 3014 | arguments.emplace_back(std::make_shared<JsonNode>(static_cast<std::string>(tok.text), tok.text.data() - tmpl.content.c_str())); |
| 3015 | } |
| 3016 | |
| 3017 | // Operators |
| 3018 | } break; |
| 3019 | case Token::Kind::Equal: |
| 3020 | case Token::Kind::NotEqual: |
| 3021 | case Token::Kind::GreaterThan: |
| 3022 | case Token::Kind::GreaterEqual: |
| 3023 | case Token::Kind::LessThan: |
| 3024 | case Token::Kind::LessEqual: |
| 3025 | case Token::Kind::Plus: |
| 3026 | case Token::Kind::Minus: |
| 3027 | case Token::Kind::Times: |
| 3028 | case Token::Kind::Slash: |
| 3029 | case Token::Kind::Power: |
| 3030 | case Token::Kind::Percent: |
| 3031 | case Token::Kind::Dot: { |
| 3032 | |
| 3033 | parse_operator: |
| 3034 | FunctionStorage::Operation operation; |
| 3035 | switch (tok.kind) { |
| 3036 | case Token::Kind::Id: { |
| 3037 | if (tok.text == "and") { |
| 3038 | operation = FunctionStorage::Operation::And; |
| 3039 | } else if (tok.text == "or") { |
| 3040 | operation = FunctionStorage::Operation::Or; |
| 3041 | } else if (tok.text == "in") { |
| 3042 | operation = FunctionStorage::Operation::In; |
| 3043 | } else if (tok.text == "not") { |
| 3044 | operation = FunctionStorage::Operation::Not; |
| 3045 | } else { |
| 3046 | throw_parser_error("unknown operator in parser."); |
| 3047 | } |
| 3048 | } break; |
| 3049 | case Token::Kind::Equal: { |
| 3050 | operation = FunctionStorage::Operation::Equal; |
| 3051 | } break; |
| 3052 | case Token::Kind::NotEqual: { |
| 3053 | operation = FunctionStorage::Operation::NotEqual; |
| 3054 | } break; |
| 3055 | case Token::Kind::GreaterThan: { |
| 3056 | operation = FunctionStorage::Operation::Greater; |
| 3057 | } break; |
| 3058 | case Token::Kind::GreaterEqual: { |
| 3059 | operation = FunctionStorage::Operation::GreaterEqual; |
| 3060 | } break; |
| 3061 | case Token::Kind::LessThan: { |
| 3062 | operation = FunctionStorage::Operation::Less; |
| 3063 | } break; |
| 3064 | case Token::Kind::LessEqual: { |
| 3065 | operation = FunctionStorage::Operation::LessEqual; |
| 3066 | } break; |
| 3067 | case Token::Kind::Plus: { |
| 3068 | operation = FunctionStorage::Operation::Add; |
| 3069 | } break; |
| 3070 | case Token::Kind::Minus: { |
| 3071 | operation = FunctionStorage::Operation::Subtract; |
| 3072 | } break; |
| 3073 | case Token::Kind::Times: { |
| 3074 | operation = FunctionStorage::Operation::Multiplication; |
| 3075 | } break; |
| 3076 | case Token::Kind::Slash: { |
| 3077 | operation = FunctionStorage::Operation::Division; |
| 3078 | } break; |
| 3079 | case Token::Kind::Power: { |
| 3080 | operation = FunctionStorage::Operation::Power; |
| 3081 | } break; |
| 3082 | case Token::Kind::Percent: { |
| 3083 | operation = FunctionStorage::Operation::Modulo; |
| 3084 | } break; |
| 3085 | case Token::Kind::Dot: { |
| 3086 | operation = FunctionStorage::Operation::AtId; |
| 3087 | } break; |
| 3088 | default: { |
| 3089 | throw_parser_error("unknown operator in parser."); |
| 3090 | } |
| 3091 | } |
| 3092 | auto function_node = std::make_shared<FunctionNode>(operation, tok.text.data() - tmpl.content.c_str()); |
| 3093 | |
| 3094 | 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)) { |
| 3095 | add_operator(); |
| 3096 | } |
| 3097 | |
| 3098 | operator_stack.emplace(function_node); |
| 3099 | |
| 3100 | } break; |
| 3101 | case Token::Kind::Comma: { |
| 3102 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 3103 | if (function_stack.empty()) { |
| 3104 | throw_parser_error("unexpected ','"); |
| 3105 | } |
| 3106 | |
| 3107 | function_stack.top().first->number_args += 1; |
| 3108 | } |
| 3109 | |
| 3110 | } break; |
| 3111 | case Token::Kind::Colon: { |
| 3112 | if (current_brace_level == 0 && current_bracket_level == 0) { |
| 3113 | throw_parser_error("unexpected ':'"); |
| 3114 | } |
| 3115 | |
| 3116 | } break; |
| 3117 | case Token::Kind::LeftParen: { |
| 3118 | current_paren_level += 1; |
| 3119 | operator_stack.emplace(std::make_shared<FunctionNode>(FunctionStorage::Operation::ParenLeft, tok.text.data() - tmpl.content.c_str())); |
| 3120 | |
| 3121 | get_peek_token(); |
| 3122 | if (peek_tok.kind == Token::Kind::RightParen) { |
| 3123 | if (!function_stack.empty() && function_stack.top().second == current_paren_level - 1) { |
| 3124 | function_stack.top().first->number_args = 0; |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | } break; |
| 3129 | case Token::Kind::RightParen: { |
| 3130 | current_paren_level -= 1; |
| 3131 | while (!operator_stack.empty() && operator_stack.top()->operation != FunctionStorage::Operation::ParenLeft) { |
| 3132 | add_operator(); |
| 3133 | } |
| 3134 | |
| 3135 | if (!operator_stack.empty() && operator_stack.top()->operation == FunctionStorage::Operation::ParenLeft) { |
| 3136 | operator_stack.pop(); |
| 3137 | } |
| 3138 | |
| 3139 | if (!function_stack.empty() && function_stack.top().second == current_paren_level) { |
| 3140 | auto func = function_stack.top().first; |
| 3141 | auto function_data = function_storage.find_function(func->name, func->number_args); |
| 3142 | if (function_data.operation == FunctionStorage::Operation::None) { |
| 3143 | throw_parser_error("unknown function " + func->name); |
| 3144 | } |
| 3145 | func->operation = function_data.operation; |
| 3146 | if (function_data.operation == FunctionStorage::Operation::Callback) { |
| 3147 | func->callback = function_data.callback; |
| 3148 | } |
| 3149 | |
| 3150 | if (operator_stack.empty()) { |
| 3151 | throw_parser_error("internal error at function " + func->name); |
| 3152 | } |
| 3153 | |
| 3154 | add_operator(); |
| 3155 | function_stack.pop(); |
| 3156 | } |
| 3157 | } |
| 3158 | default: |
| 3159 | break; |
| 3160 | } |
| 3161 | |
| 3162 | get_next_token(); |
| 3163 | } |
| 3164 | |
| 3165 | while (!operator_stack.empty()) { |
| 3166 | add_operator(); |
| 3167 | } |
| 3168 | |
| 3169 | if (arguments.size() == 1) { |
| 3170 | current_expression_list->root = arguments[0]; |
| 3171 | arguments = {}; |
| 3172 | |
| 3173 | } else if (arguments.size() > 1) { |
| 3174 | throw_parser_error("malformed expression"); |
| 3175 | } |
| 3176 | |
| 3177 | return true; |
| 3178 | } |
| 3179 | |
| 3180 | bool parse_statement(Template &tmpl, Token::Kind closing, nonstd::string_view path) { |
| 3181 | if (tok.kind != Token::Kind::Id) { |
| 3182 | return false; |
| 3183 | } |
| 3184 | |
| 3185 | if (tok.text == static_cast<decltype(tok.text)>("if")) { |
| 3186 | get_next_token(); |
| 3187 | |
| 3188 | auto if_statement_node = std::make_shared<IfStatementNode>(current_block, tok.text.data() - tmpl.content.c_str()); |
| 3189 | current_block->nodes.emplace_back(if_statement_node); |
| 3190 | if_statement_stack.emplace(if_statement_node.get()); |
| 3191 | current_block = &if_statement_node->true_statement; |
| 3192 | current_expression_list = &if_statement_node->condition; |
| 3193 | |
| 3194 | if (!parse_expression(tmpl, closing)) { |
| 3195 | return false; |
| 3196 | } |
| 3197 | |
| 3198 | } else if (tok.text == static_cast<decltype(tok.text)>("else")) { |
| 3199 | if (if_statement_stack.empty()) { |
| 3200 | throw_parser_error("else without matching if"); |
| 3201 | } |
| 3202 | auto &if_statement_data = if_statement_stack.top(); |
| 3203 | get_next_token(); |
| 3204 | |
| 3205 | if_statement_data->has_false_statement = true; |
| 3206 | current_block = &if_statement_data->false_statement; |
| 3207 | |
| 3208 | // Chained else if |
| 3209 | if (tok.kind == Token::Kind::Id && tok.text == static_cast<decltype(tok.text)>("if")) { |
| 3210 | get_next_token(); |
| 3211 | |
| 3212 | auto if_statement_node = std::make_shared<IfStatementNode>(true, current_block, tok.text.data() - tmpl.content.c_str()); |
| 3213 | current_block->nodes.emplace_back(if_statement_node); |
| 3214 | if_statement_stack.emplace(if_statement_node.get()); |
| 3215 | current_block = &if_statement_node->true_statement; |
| 3216 | current_expression_list = &if_statement_node->condition; |
| 3217 | |
| 3218 | if (!parse_expression(tmpl, closing)) { |
| 3219 | return false; |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | } else if (tok.text == static_cast<decltype(tok.text)>("endif")) { |
| 3224 | if (if_statement_stack.empty()) { |
| 3225 | throw_parser_error("endif without matching if"); |
| 3226 | } |
| 3227 | |
| 3228 | // Nested if statements |
| 3229 | while (if_statement_stack.top()->is_nested) { |
| 3230 | if_statement_stack.pop(); |
| 3231 | } |
| 3232 | |
| 3233 | auto &if_statement_data = if_statement_stack.top(); |
| 3234 | get_next_token(); |
| 3235 | |
| 3236 | current_block = if_statement_data->parent; |
| 3237 | if_statement_stack.pop(); |
| 3238 | |
| 3239 | } else if (tok.text == static_cast<decltype(tok.text)>("block")) { |
| 3240 | get_next_token(); |
| 3241 | |
| 3242 | if (tok.kind != Token::Kind::Id) { |
| 3243 | throw_parser_error("expected block name, got '" + tok.describe() + "'"); |
| 3244 | } |
| 3245 | |
| 3246 | const std::string block_name = static_cast<std::string>(tok.text); |
| 3247 | |
| 3248 | auto block_statement_node = std::make_shared<BlockStatementNode>(current_block, block_name, tok.text.data() - tmpl.content.c_str()); |
| 3249 | current_block->nodes.emplace_back(block_statement_node); |
| 3250 | block_statement_stack.emplace(block_statement_node.get()); |
| 3251 | current_block = &block_statement_node->block; |
| 3252 | auto success = tmpl.block_storage.emplace(block_name, block_statement_node); |
| 3253 | if (!success.second) { |
| 3254 | throw_parser_error("block with the name '" + block_name + "' does already exist"); |
| 3255 | } |
| 3256 | |
| 3257 | get_next_token(); |
| 3258 | |
| 3259 | } else if (tok.text == static_cast<decltype(tok.text)>("endblock")) { |
| 3260 | if (block_statement_stack.empty()) { |
| 3261 | throw_parser_error("endblock without matching block"); |
| 3262 | } |
| 3263 | |
| 3264 | auto &block_statement_data = block_statement_stack.top(); |
| 3265 | get_next_token(); |
| 3266 | |
| 3267 | current_block = block_statement_data->parent; |
| 3268 | block_statement_stack.pop(); |
| 3269 | |
| 3270 | } else if (tok.text == static_cast<decltype(tok.text)>("for")) { |
| 3271 | get_next_token(); |
| 3272 | |
| 3273 | // options: for a in arr; for a, b in obj |
| 3274 | if (tok.kind != Token::Kind::Id) { |
| 3275 | throw_parser_error("expected id, got '" + tok.describe() + "'"); |
| 3276 | } |
| 3277 | |
| 3278 | Token value_token = tok; |
| 3279 | get_next_token(); |
| 3280 | |
| 3281 | // Object type |
| 3282 | std::shared_ptr<ForStatementNode> for_statement_node; |
| 3283 | if (tok.kind == Token::Kind::Comma) { |
| 3284 | get_next_token(); |
| 3285 | if (tok.kind != Token::Kind::Id) { |
| 3286 | throw_parser_error("expected id, got '" + tok.describe() + "'"); |
| 3287 | } |
| 3288 | |
| 3289 | Token key_token = std::move(value_token); |
| 3290 | value_token = tok; |
| 3291 | get_next_token(); |
| 3292 | |
| 3293 | for_statement_node = std::make_shared<ForObjectStatementNode>(static_cast<std::string>(key_token.text), static_cast<std::string>(value_token.text), current_block, tok.text.data() - tmpl.content.c_str()); |
| 3294 | |
| 3295 | // Array type |
| 3296 | } else { |
| 3297 | for_statement_node = std::make_shared<ForArrayStatementNode>(static_cast<std::string>(value_token.text), current_block, tok.text.data() - tmpl.content.c_str()); |
| 3298 | } |
| 3299 | |
| 3300 | current_block->nodes.emplace_back(for_statement_node); |
| 3301 | for_statement_stack.emplace(for_statement_node.get()); |
| 3302 | current_block = &for_statement_node->body; |
| 3303 | current_expression_list = &for_statement_node->condition; |
| 3304 | |
| 3305 | if (tok.kind != Token::Kind::Id || tok.text != static_cast<decltype(tok.text)>("in")) { |
| 3306 | throw_parser_error("expected 'in', got '" + tok.describe() + "'"); |
| 3307 | } |
| 3308 | get_next_token(); |
| 3309 | |
| 3310 | if (!parse_expression(tmpl, closing)) { |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
| 3314 | } else if (tok.text == static_cast<decltype(tok.text)>("endfor")) { |
| 3315 | if (for_statement_stack.empty()) { |
| 3316 | throw_parser_error("endfor without matching for"); |
| 3317 | } |
| 3318 | |
| 3319 | auto &for_statement_data = for_statement_stack.top(); |
| 3320 | get_next_token(); |
| 3321 | |
| 3322 | current_block = for_statement_data->parent; |
| 3323 | for_statement_stack.pop(); |
| 3324 | |
| 3325 | } else if (tok.text == static_cast<decltype(tok.text)>("include")) { |
| 3326 | get_next_token(); |
| 3327 | |
| 3328 | if (tok.kind != Token::Kind::String) { |
| 3329 | throw_parser_error("expected string, got '" + tok.describe() + "'"); |
| 3330 | } |
| 3331 | |
| 3332 | std::string template_name = json::parse(tok.text).get_ref<const std::string &>(); |
| 3333 | add_to_template_storage(path, template_name); |
| 3334 | |
| 3335 | current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str())); |
| 3336 | |
| 3337 | get_next_token(); |
| 3338 | |
| 3339 | } else if (tok.text == static_cast<decltype(tok.text)>("extends")) { |
| 3340 | get_next_token(); |
| 3341 | |
| 3342 | if (tok.kind != Token::Kind::String) { |
| 3343 | throw_parser_error("expected string, got '" + tok.describe() + "'"); |
| 3344 | } |
| 3345 | |
| 3346 | std::string template_name = json::parse(tok.text).get_ref<const std::string &>(); |
| 3347 | add_to_template_storage(path, template_name); |
| 3348 | |
| 3349 | current_block->nodes.emplace_back(std::make_shared<ExtendsStatementNode>(template_name, tok.text.data() - tmpl.content.c_str())); |
| 3350 | |
| 3351 | get_next_token(); |
| 3352 | |
| 3353 | } else if (tok.text == static_cast<decltype(tok.text)>("set")) { |
| 3354 | get_next_token(); |
| 3355 | |
| 3356 | if (tok.kind != Token::Kind::Id) { |
| 3357 | throw_parser_error("expected variable name, got '" + tok.describe() + "'"); |
| 3358 | } |
| 3359 | |
| 3360 | std::string key = static_cast<std::string>(tok.text); |
| 3361 | get_next_token(); |
| 3362 | |
| 3363 | auto set_statement_node = std::make_shared<SetStatementNode>(key, tok.text.data() - tmpl.content.c_str()); |
| 3364 | current_block->nodes.emplace_back(set_statement_node); |
| 3365 | current_expression_list = &set_statement_node->expression; |
| 3366 | |
| 3367 | if (tok.text != static_cast<decltype(tok.text)>("=")) { |
| 3368 | throw_parser_error("expected '=', got '" + tok.describe() + "'"); |
| 3369 | } |
| 3370 | get_next_token(); |
| 3371 | |
| 3372 | if (!parse_expression(tmpl, closing)) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | |
| 3376 | } else { |
| 3377 | return false; |
| 3378 | } |
| 3379 | return true; |
| 3380 | } |
| 3381 | |
| 3382 | void parse_into(Template &tmpl, nonstd::string_view path) { |
| 3383 | lexer.start(tmpl.content); |
| 3384 | current_block = &tmpl.root; |
| 3385 | |
| 3386 | for (;;) { |
| 3387 | get_next_token(); |
| 3388 | switch (tok.kind) { |
| 3389 | case Token::Kind::Eof: { |
| 3390 | if (!if_statement_stack.empty()) { |
| 3391 | throw_parser_error("unmatched if"); |
| 3392 | } |
| 3393 | if (!for_statement_stack.empty()) { |
| 3394 | throw_parser_error("unmatched for"); |
| 3395 | } |
| 3396 | } return; |
| 3397 | case Token::Kind::Text: { |
| 3398 | current_block->nodes.emplace_back(std::make_shared<TextNode>(tok.text.data() - tmpl.content.c_str(), tok.text.size())); |
| 3399 | } break; |
| 3400 | case Token::Kind::StatementOpen: { |
| 3401 | get_next_token(); |
| 3402 | if (!parse_statement(tmpl, Token::Kind::StatementClose, path)) { |
| 3403 | throw_parser_error("expected statement, got '" + tok.describe() + "'"); |
| 3404 | } |
| 3405 | if (tok.kind != Token::Kind::StatementClose) { |
| 3406 | throw_parser_error("expected statement close, got '" + tok.describe() + "'"); |
| 3407 | } |
| 3408 | } break; |
| 3409 | case Token::Kind::LineStatementOpen: { |
| 3410 | get_next_token(); |
| 3411 | if (!parse_statement(tmpl, Token::Kind::LineStatementClose, path)) { |
| 3412 | throw_parser_error("expected statement, got '" + tok.describe() + "'"); |
| 3413 | } |
| 3414 | if (tok.kind != Token::Kind::LineStatementClose && tok.kind != Token::Kind::Eof) { |
| 3415 | throw_parser_error("expected line statement close, got '" + tok.describe() + "'"); |
| 3416 | } |
| 3417 | } break; |
| 3418 | case Token::Kind::ExpressionOpen: { |
| 3419 | get_next_token(); |
| 3420 | |
| 3421 | auto expression_list_node = std::make_shared<ExpressionListNode>(tok.text.data() - tmpl.content.c_str()); |
| 3422 | current_block->nodes.emplace_back(expression_list_node); |
| 3423 | current_expression_list = expression_list_node.get(); |
| 3424 | |
| 3425 | if (!parse_expression(tmpl, Token::Kind::ExpressionClose)) { |
| 3426 | throw_parser_error("expected expression, got '" + tok.describe() + "'"); |
| 3427 | } |
| 3428 | |
| 3429 | if (tok.kind != Token::Kind::ExpressionClose) { |
| 3430 | throw_parser_error("expected expression close, got '" + tok.describe() + "'"); |
| 3431 | } |
| 3432 | } break; |
| 3433 | case Token::Kind::CommentOpen: { |
| 3434 | get_next_token(); |
| 3435 | if (tok.kind != Token::Kind::CommentClose) { |
| 3436 | throw_parser_error("expected comment close, got '" + tok.describe() + "'"); |
| 3437 | } |
| 3438 | } break; |
| 3439 | default: { |
| 3440 | throw_parser_error("unexpected token '" + tok.describe() + "'"); |
| 3441 | } break; |
| 3442 | } |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | |
| 3447 | public: |
| 3448 | explicit Parser(const ParserConfig &parser_config, const LexerConfig &lexer_config, |
| 3449 | TemplateStorage &template_storage, const FunctionStorage &function_storage) |
| 3450 | : config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) { } |
| 3451 | |
| 3452 | Template parse(nonstd::string_view input, nonstd::string_view path) { |
| 3453 | auto result = Template(static_cast<std::string>(input)); |
| 3454 | parse_into(result, path); |
| 3455 | return result; |
| 3456 | } |
| 3457 | |
| 3458 | Template parse(nonstd::string_view input) { |
| 3459 | return parse(input, "./"); |
| 3460 | } |
| 3461 | |
| 3462 | void parse_into_template(Template& tmpl, nonstd::string_view filename) { |
| 3463 | nonstd::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1); |
| 3464 | |
| 3465 | // StringRef path = sys::path::parent_path(filename); |
| 3466 | auto sub_parser = Parser(config, lexer.get_config(), template_storage, function_storage); |
| 3467 | sub_parser.parse_into(tmpl, path); |
| 3468 | } |
| 3469 | |
| 3470 | std::string load_file(nonstd::string_view filename) { |
| 3471 | std::ifstream file; |
| 3472 | open_file_or_throw(static_cast<std::string>(filename), file); |
| 3473 | std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
| 3474 | return text; |
| 3475 | } |
| 3476 | }; |
| 3477 | |
| 3478 | } // namespace inja |
| 3479 | |
| 3480 | #endif // INCLUDE_INJA_PARSER_HPP_ |
| 3481 | |
| 3482 | // #include "renderer.hpp" |
| 3483 | #ifndef INCLUDE_INJA_RENDERER_HPP_ |
| 3484 | #define INCLUDE_INJA_RENDERER_HPP_ |
| 3485 | |
| 3486 | #include <algorithm> |
| 3487 | #include <numeric> |
| 3488 | #include <string> |
| 3489 | #include <utility> |
| 3490 | #include <vector> |
| 3491 | |
| 3492 | #include <nlohmann/json.hpp> |
| 3493 | |
| 3494 | // #include "config.hpp" |
| 3495 | |
| 3496 | // #include "exceptions.hpp" |
| 3497 | |
| 3498 | // #include "node.hpp" |
| 3499 | |
| 3500 | // #include "template.hpp" |
| 3501 | |
| 3502 | // #include "utils.hpp" |
| 3503 | |
| 3504 | |
| 3505 | namespace inja { |
| 3506 | |
| 3507 | /*! |
| 3508 | * \brief Class for rendering a Template with data. |
| 3509 | */ |
| 3510 | class Renderer : public NodeVisitor { |
| 3511 | using Op = FunctionStorage::Operation; |
| 3512 | |
| 3513 | const RenderConfig config; |
| 3514 | const TemplateStorage &template_storage; |
| 3515 | const FunctionStorage &function_storage; |
| 3516 | |
| 3517 | const Template *current_template; |
| 3518 | size_t current_level {0}; |
| 3519 | std::vector<const Template*> template_stack; |
| 3520 | std::vector<const BlockStatementNode*> block_statement_stack; |
| 3521 | |
| 3522 | const json *json_input; |
| 3523 | std::ostream *output_stream; |
| 3524 | |
| 3525 | json json_additional_data; |
| 3526 | json* current_loop_data = &json_additional_data["loop"]; |
| 3527 | |
| 3528 | std::vector<std::shared_ptr<json>> json_tmp_stack; |
| 3529 | std::stack<const json*> json_eval_stack; |
| 3530 | std::stack<const JsonNode*> not_found_stack; |
| 3531 | |
| 3532 | bool break_rendering {false}; |
| 3533 | |
| 3534 | bool truthy(const json* data) const { |
| 3535 | if (data->is_boolean()) { |
| 3536 | return data->get<bool>(); |
| 3537 | } else if (data->is_number()) { |
| 3538 | return (*data != 0); |
| 3539 | } else if (data->is_null()) { |
| 3540 | return false; |
| 3541 | } |
| 3542 | return !data->empty(); |
| 3543 | } |
| 3544 | |
| 3545 | void print_json(const std::shared_ptr<json> value) { |
| 3546 | if (value->is_string()) { |
| 3547 | *output_stream << value->get_ref<const json::string_t&>(); |
| 3548 | } else if (value->is_number_integer()) { |
| 3549 | *output_stream << value->get<const json::number_integer_t>(); |
| 3550 | } else if (value->is_null()) { |
| 3551 | } else { |
| 3552 | *output_stream << value->dump(); |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | const std::shared_ptr<json> eval_expression_list(const ExpressionListNode& expression_list) { |
| 3557 | if (!expression_list.root) { |
| 3558 | throw_renderer_error("empty expression", expression_list); |
| 3559 | } |
| 3560 | |
| 3561 | expression_list.root->accept(*this); |
| 3562 | |
| 3563 | if (json_eval_stack.empty()) { |
| 3564 | throw_renderer_error("empty expression", expression_list); |
| 3565 | } else if (json_eval_stack.size() != 1) { |
| 3566 | throw_renderer_error("malformed expression", expression_list); |
| 3567 | } |
| 3568 | |
| 3569 | const auto result = json_eval_stack.top(); |
| 3570 | json_eval_stack.pop(); |
| 3571 | |
| 3572 | if (!result) { |
| 3573 | if (not_found_stack.empty()) { |
| 3574 | throw_renderer_error("expression could not be evaluated", expression_list); |
| 3575 | } |
| 3576 | |
| 3577 | auto node = not_found_stack.top(); |
| 3578 | not_found_stack.pop(); |
| 3579 | |
| 3580 | throw_renderer_error("variable '" + static_cast<std::string>(node->name) + "' not found", *node); |
| 3581 | } |
| 3582 | return std::make_shared<json>(*result); |
| 3583 | } |
| 3584 | |
| 3585 | void throw_renderer_error(const std::string &message, const AstNode& node) { |
| 3586 | SourceLocation loc = get_source_location(current_template->content, node.pos); |
| 3587 | INJA_THROW(RenderError(message, loc)); |
| 3588 | } |
| 3589 | |
| 3590 | template<size_t N, size_t N_start = 0, bool throw_not_found=true> |
| 3591 | std::array<const json*, N> get_arguments(const FunctionNode& node) { |
| 3592 | if (node.arguments.size() < N_start + N) { |
| 3593 | throw_renderer_error("function needs " + std::to_string(N_start + N) + " variables, but has only found " + std::to_string(node.arguments.size()), node); |
| 3594 | } |
| 3595 | |
| 3596 | for (size_t i = N_start; i < N_start + N; i += 1) { |
| 3597 | node.arguments[i]->accept(*this); |
| 3598 | } |
| 3599 | |
| 3600 | if (json_eval_stack.size() < N) { |
| 3601 | throw_renderer_error("function needs " + std::to_string(N) + " variables, but has only found " + std::to_string(json_eval_stack.size()), node); |
| 3602 | } |
| 3603 | |
| 3604 | std::array<const json*, N> result; |
| 3605 | for (size_t i = 0; i < N; i += 1) { |
| 3606 | result[N - i - 1] = json_eval_stack.top(); |
| 3607 | json_eval_stack.pop(); |
| 3608 | |
| 3609 | if (!result[N - i - 1]) { |
| 3610 | const auto json_node = not_found_stack.top(); |
| 3611 | not_found_stack.pop(); |
| 3612 | |
| 3613 | if (throw_not_found) { |
| 3614 | throw_renderer_error("variable '" + static_cast<std::string>(json_node->name) + "' not found", *json_node); |
| 3615 | } |
| 3616 | } |
| 3617 | } |
| 3618 | return result; |
| 3619 | } |
| 3620 | |
| 3621 | template<bool throw_not_found=true> |
| 3622 | Arguments get_argument_vector(const FunctionNode& node) { |
| 3623 | const size_t N = node.arguments.size(); |
| 3624 | for (auto a: node.arguments) { |
| 3625 | a->accept(*this); |
| 3626 | } |
| 3627 | |
| 3628 | if (json_eval_stack.size() < N) { |
| 3629 | throw_renderer_error("function needs " + std::to_string(N) + " variables, but has only found " + std::to_string(json_eval_stack.size()), node); |
| 3630 | } |
| 3631 | |
| 3632 | Arguments result {N}; |
| 3633 | for (size_t i = 0; i < N; i += 1) { |
| 3634 | result[N - i - 1] = json_eval_stack.top(); |
| 3635 | json_eval_stack.pop(); |
| 3636 | |
| 3637 | if (!result[N - i - 1]) { |
| 3638 | const auto json_node = not_found_stack.top(); |
| 3639 | not_found_stack.pop(); |
| 3640 | |
| 3641 | if (throw_not_found) { |
| 3642 | throw_renderer_error("variable '" + static_cast<std::string>(json_node->name) + "' not found", *json_node); |
| 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | return result; |
| 3647 | } |
| 3648 | |
| 3649 | void visit(const BlockNode& node) { |
| 3650 | for (auto& n : node.nodes) { |
| 3651 | n->accept(*this); |
| 3652 | |
| 3653 | if (break_rendering) { |
| 3654 | break; |
| 3655 | } |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | void visit(const TextNode& node) { |
| 3660 | output_stream->write(current_template->content.c_str() + node.pos, node.length); |
| 3661 | } |
| 3662 | |
| 3663 | void visit(const ExpressionNode&) { } |
| 3664 | |
| 3665 | void visit(const LiteralNode& node) { |
| 3666 | json_eval_stack.push(&node.value); |
| 3667 | } |
| 3668 | |
| 3669 | void visit(const JsonNode& node) { |
| 3670 | if (json_additional_data.contains(node.ptr)) { |
| 3671 | json_eval_stack.push(&(json_additional_data[node.ptr])); |
| 3672 | |
| 3673 | } else if (json_input->contains(node.ptr)) { |
| 3674 | json_eval_stack.push(&(*json_input)[node.ptr]); |
| 3675 | |
| 3676 | } else { |
| 3677 | // Try to evaluate as a no-argument callback |
| 3678 | const auto function_data = function_storage.find_function(node.name, 0); |
| 3679 | if (function_data.operation == FunctionStorage::Operation::Callback) { |
| 3680 | Arguments empty_args {}; |
| 3681 | const auto value = std::make_shared<json>(function_data.callback(empty_args)); |
| 3682 | json_tmp_stack.push_back(value); |
| 3683 | json_eval_stack.push(value.get()); |
| 3684 | |
| 3685 | } else { |
| 3686 | json_eval_stack.push(nullptr); |
| 3687 | not_found_stack.emplace(&node); |
| 3688 | } |
| 3689 | } |
| 3690 | } |
| 3691 | |
| 3692 | void visit(const FunctionNode& node) { |
| 3693 | std::shared_ptr<json> result_ptr; |
| 3694 | |
| 3695 | switch (node.operation) { |
| 3696 | case Op::Not: { |
| 3697 | const auto args = get_arguments<1>(node); |
| 3698 | result_ptr = std::make_shared<json>(!truthy(args[0])); |
| 3699 | json_tmp_stack.push_back(result_ptr); |
| 3700 | json_eval_stack.push(result_ptr.get()); |
| 3701 | } break; |
| 3702 | case Op::And: { |
| 3703 | result_ptr = std::make_shared<json>(truthy(get_arguments<1, 0>(node)[0]) && truthy(get_arguments<1, 1>(node)[0])); |
| 3704 | json_tmp_stack.push_back(result_ptr); |
| 3705 | json_eval_stack.push(result_ptr.get()); |
| 3706 | } break; |
| 3707 | case Op::Or: { |
| 3708 | result_ptr = std::make_shared<json>(truthy(get_arguments<1, 0>(node)[0]) || truthy(get_arguments<1, 1>(node)[0])); |
| 3709 | json_tmp_stack.push_back(result_ptr); |
| 3710 | json_eval_stack.push(result_ptr.get()); |
| 3711 | } break; |
| 3712 | case Op::In: { |
| 3713 | const auto args = get_arguments<2>(node); |
| 3714 | result_ptr = std::make_shared<json>(std::find(args[1]->begin(), args[1]->end(), *args[0]) != args[1]->end()); |
| 3715 | json_tmp_stack.push_back(result_ptr); |
| 3716 | json_eval_stack.push(result_ptr.get()); |
| 3717 | } break; |
| 3718 | case Op::Equal: { |
| 3719 | const auto args = get_arguments<2>(node); |
| 3720 | result_ptr = std::make_shared<json>(*args[0] == *args[1]); |
| 3721 | json_tmp_stack.push_back(result_ptr); |
| 3722 | json_eval_stack.push(result_ptr.get()); |
| 3723 | } break; |
| 3724 | case Op::NotEqual: { |
| 3725 | const auto args = get_arguments<2>(node); |
| 3726 | result_ptr = std::make_shared<json>(*args[0] != *args[1]); |
| 3727 | json_tmp_stack.push_back(result_ptr); |
| 3728 | json_eval_stack.push(result_ptr.get()); |
| 3729 | } break; |
| 3730 | case Op::Greater: { |
| 3731 | const auto args = get_arguments<2>(node); |
| 3732 | result_ptr = std::make_shared<json>(*args[0] > *args[1]); |
| 3733 | json_tmp_stack.push_back(result_ptr); |
| 3734 | json_eval_stack.push(result_ptr.get()); |
| 3735 | } break; |
| 3736 | case Op::GreaterEqual: { |
| 3737 | const auto args = get_arguments<2>(node); |
| 3738 | result_ptr = std::make_shared<json>(*args[0] >= *args[1]); |
| 3739 | json_tmp_stack.push_back(result_ptr); |
| 3740 | json_eval_stack.push(result_ptr.get()); |
| 3741 | } break; |
| 3742 | case Op::Less: { |
| 3743 | const auto args = get_arguments<2>(node); |
| 3744 | result_ptr = std::make_shared<json>(*args[0] < *args[1]); |
| 3745 | json_tmp_stack.push_back(result_ptr); |
| 3746 | json_eval_stack.push(result_ptr.get()); |
| 3747 | } break; |
| 3748 | case Op::LessEqual: { |
| 3749 | const auto args = get_arguments<2>(node); |
| 3750 | result_ptr = std::make_shared<json>(*args[0] <= *args[1]); |
| 3751 | json_tmp_stack.push_back(result_ptr); |
| 3752 | json_eval_stack.push(result_ptr.get()); |
| 3753 | } break; |
| 3754 | case Op::Add: { |
| 3755 | const auto args = get_arguments<2>(node); |
| 3756 | if (args[0]->is_string() && args[1]->is_string()) { |
| 3757 | result_ptr = std::make_shared<json>(args[0]->get_ref<const std::string&>() + args[1]->get_ref<const std::string&>()); |
| 3758 | json_tmp_stack.push_back(result_ptr); |
| 3759 | } else if (args[0]->is_number_integer() && args[1]->is_number_integer()) { |
| 3760 | result_ptr = std::make_shared<json>(args[0]->get<int>() + args[1]->get<int>()); |
| 3761 | json_tmp_stack.push_back(result_ptr); |
| 3762 | } else { |
| 3763 | result_ptr = std::make_shared<json>(args[0]->get<double>() + args[1]->get<double>()); |
| 3764 | json_tmp_stack.push_back(result_ptr); |
| 3765 | } |
| 3766 | json_eval_stack.push(result_ptr.get()); |
| 3767 | } break; |
| 3768 | case Op::Subtract: { |
| 3769 | const auto args = get_arguments<2>(node); |
| 3770 | if (args[0]->is_number_integer() && args[1]->is_number_integer()) { |
| 3771 | result_ptr = std::make_shared<json>(args[0]->get<int>() - args[1]->get<int>()); |
| 3772 | json_tmp_stack.push_back(result_ptr); |
| 3773 | } else { |
| 3774 | result_ptr = std::make_shared<json>(args[0]->get<double>() - args[1]->get<double>()); |
| 3775 | json_tmp_stack.push_back(result_ptr); |
| 3776 | } |
| 3777 | json_eval_stack.push(result_ptr.get()); |
| 3778 | } break; |
| 3779 | case Op::Multiplication: { |
| 3780 | const auto args = get_arguments<2>(node); |
| 3781 | if (args[0]->is_number_integer() && args[1]->is_number_integer()) { |
| 3782 | result_ptr = std::make_shared<json>(args[0]->get<int>() * args[1]->get<int>()); |
| 3783 | json_tmp_stack.push_back(result_ptr); |
| 3784 | } else { |
| 3785 | result_ptr = std::make_shared<json>(args[0]->get<double>() * args[1]->get<double>()); |
| 3786 | json_tmp_stack.push_back(result_ptr); |
| 3787 | } |
| 3788 | json_eval_stack.push(result_ptr.get()); |
| 3789 | } break; |
| 3790 | case Op::Division: { |
| 3791 | const auto args = get_arguments<2>(node); |
| 3792 | if (args[1]->get<double>() == 0) { |
| 3793 | throw_renderer_error("division by zero", node); |
| 3794 | } |
| 3795 | result_ptr = std::make_shared<json>(args[0]->get<double>() / args[1]->get<double>()); |
| 3796 | json_tmp_stack.push_back(result_ptr); |
| 3797 | json_eval_stack.push(result_ptr.get()); |
| 3798 | } break; |
| 3799 | case Op::Power: { |
| 3800 | const auto args = get_arguments<2>(node); |
| 3801 | if (args[0]->is_number_integer() && args[1]->get<int>() >= 0) { |
| 3802 | int result = static_cast<int>(std::pow(args[0]->get<int>(), args[1]->get<int>())); |
| 3803 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3804 | json_tmp_stack.push_back(result_ptr); |
| 3805 | } else { |
| 3806 | double result = std::pow(args[0]->get<double>(), args[1]->get<int>()); |
| 3807 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3808 | json_tmp_stack.push_back(result_ptr); |
| 3809 | } |
| 3810 | json_eval_stack.push(result_ptr.get()); |
| 3811 | } break; |
| 3812 | case Op::Modulo: { |
| 3813 | const auto args = get_arguments<2>(node); |
| 3814 | result_ptr = std::make_shared<json>(args[0]->get<int>() % args[1]->get<int>()); |
| 3815 | json_tmp_stack.push_back(result_ptr); |
| 3816 | json_eval_stack.push(result_ptr.get()); |
| 3817 | } break; |
| 3818 | case Op::AtId: { |
| 3819 | const auto container = get_arguments<1, 0, false>(node)[0]; |
| 3820 | node.arguments[1]->accept(*this); |
| 3821 | if (not_found_stack.empty()) { |
| 3822 | throw_renderer_error("could not find element with given name", node); |
| 3823 | } |
| 3824 | const auto id_node = not_found_stack.top(); |
| 3825 | not_found_stack.pop(); |
| 3826 | json_eval_stack.pop(); |
| 3827 | json_eval_stack.push(&container->at(id_node->name)); |
| 3828 | } break; |
| 3829 | case Op::At: { |
| 3830 | const auto args = get_arguments<2>(node); |
| 3831 | if (args[0]->is_object()) { |
| 3832 | json_eval_stack.push(&args[0]->at(args[1]->get<std::string>())); |
| 3833 | } else { |
| 3834 | json_eval_stack.push(&args[0]->at(args[1]->get<int>())); |
| 3835 | } |
| 3836 | } break; |
| 3837 | case Op::Default: { |
| 3838 | const auto test_arg = get_arguments<1, 0, false>(node)[0]; |
| 3839 | json_eval_stack.push(test_arg ? test_arg : get_arguments<1, 1>(node)[0]); |
| 3840 | } break; |
| 3841 | case Op::DivisibleBy: { |
| 3842 | const auto args = get_arguments<2>(node); |
| 3843 | const int divisor = args[1]->get<int>(); |
| 3844 | result_ptr = std::make_shared<json>((divisor != 0) && (args[0]->get<int>() % divisor == 0)); |
| 3845 | json_tmp_stack.push_back(result_ptr); |
| 3846 | json_eval_stack.push(result_ptr.get()); |
| 3847 | } break; |
| 3848 | case Op::Even: { |
| 3849 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->get<int>() % 2 == 0); |
| 3850 | json_tmp_stack.push_back(result_ptr); |
| 3851 | json_eval_stack.push(result_ptr.get()); |
| 3852 | } break; |
| 3853 | case Op::Exists: { |
| 3854 | auto &&name = get_arguments<1>(node)[0]->get_ref<const std::string &>(); |
| 3855 | result_ptr = std::make_shared<json>(json_input->contains(json::json_pointer(JsonNode::convert_dot_to_json_ptr(name)))); |
| 3856 | json_tmp_stack.push_back(result_ptr); |
| 3857 | json_eval_stack.push(result_ptr.get()); |
| 3858 | } break; |
| 3859 | case Op::ExistsInObject: { |
| 3860 | const auto args = get_arguments<2>(node); |
| 3861 | auto &&name = args[1]->get_ref<const std::string &>(); |
| 3862 | result_ptr = std::make_shared<json>(args[0]->find(name) != args[0]->end()); |
| 3863 | json_tmp_stack.push_back(result_ptr); |
| 3864 | json_eval_stack.push(result_ptr.get()); |
| 3865 | } break; |
| 3866 | case Op::First: { |
| 3867 | const auto result = &get_arguments<1>(node)[0]->front(); |
| 3868 | json_eval_stack.push(result); |
| 3869 | } break; |
| 3870 | case Op::Float: { |
| 3871 | result_ptr = std::make_shared<json>(std::stod(get_arguments<1>(node)[0]->get_ref<const std::string &>())); |
| 3872 | json_tmp_stack.push_back(result_ptr); |
| 3873 | json_eval_stack.push(result_ptr.get()); |
| 3874 | } break; |
| 3875 | case Op::Int: { |
| 3876 | result_ptr = std::make_shared<json>(std::stoi(get_arguments<1>(node)[0]->get_ref<const std::string &>())); |
| 3877 | json_tmp_stack.push_back(result_ptr); |
| 3878 | json_eval_stack.push(result_ptr.get()); |
| 3879 | } break; |
| 3880 | case Op::Last: { |
| 3881 | const auto result = &get_arguments<1>(node)[0]->back(); |
| 3882 | json_eval_stack.push(result); |
| 3883 | } break; |
| 3884 | case Op::Length: { |
| 3885 | const auto val = get_arguments<1>(node)[0]; |
| 3886 | if (val->is_string()) { |
| 3887 | result_ptr = std::make_shared<json>(val->get_ref<const std::string &>().length()); |
| 3888 | } else { |
| 3889 | result_ptr = std::make_shared<json>(val->size()); |
| 3890 | } |
| 3891 | json_tmp_stack.push_back(result_ptr); |
| 3892 | json_eval_stack.push(result_ptr.get()); |
| 3893 | } break; |
| 3894 | case Op::Lower: { |
| 3895 | std::string result = get_arguments<1>(node)[0]->get<std::string>(); |
| 3896 | std::transform(result.begin(), result.end(), result.begin(), ::tolower); |
| 3897 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3898 | json_tmp_stack.push_back(result_ptr); |
| 3899 | json_eval_stack.push(result_ptr.get()); |
| 3900 | } break; |
| 3901 | case Op::Max: { |
| 3902 | const auto args = get_arguments<1>(node); |
| 3903 | const auto result = std::max_element(args[0]->begin(), args[0]->end()); |
| 3904 | json_eval_stack.push(&(*result)); |
| 3905 | } break; |
| 3906 | case Op::Min: { |
| 3907 | const auto args = get_arguments<1>(node); |
| 3908 | const auto result = std::min_element(args[0]->begin(), args[0]->end()); |
| 3909 | json_eval_stack.push(&(*result)); |
| 3910 | } break; |
| 3911 | case Op::Odd: { |
| 3912 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->get<int>() % 2 != 0); |
| 3913 | json_tmp_stack.push_back(result_ptr); |
| 3914 | json_eval_stack.push(result_ptr.get()); |
| 3915 | } break; |
| 3916 | case Op::Range: { |
| 3917 | std::vector<int> result(get_arguments<1>(node)[0]->get<int>()); |
| 3918 | std::iota(result.begin(), result.end(), 0); |
| 3919 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3920 | json_tmp_stack.push_back(result_ptr); |
| 3921 | json_eval_stack.push(result_ptr.get()); |
| 3922 | } break; |
| 3923 | case Op::Round: { |
| 3924 | const auto args = get_arguments<2>(node); |
| 3925 | const int precision = args[1]->get<int>(); |
| 3926 | const double result = std::round(args[0]->get<double>() * std::pow(10.0, precision)) / std::pow(10.0, precision); |
| 3927 | if(0==precision){ |
| 3928 | result_ptr = std::make_shared<json>(int(result)); |
| 3929 | }else{ |
| 3930 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3931 | } |
| 3932 | json_tmp_stack.push_back(result_ptr); |
| 3933 | json_eval_stack.push(result_ptr.get()); |
| 3934 | } break; |
| 3935 | case Op::Sort: { |
| 3936 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->get<std::vector<json>>()); |
| 3937 | std::sort(result_ptr->begin(), result_ptr->end()); |
| 3938 | json_tmp_stack.push_back(result_ptr); |
| 3939 | json_eval_stack.push(result_ptr.get()); |
| 3940 | } break; |
| 3941 | case Op::Upper: { |
| 3942 | std::string result = get_arguments<1>(node)[0]->get<std::string>(); |
| 3943 | std::transform(result.begin(), result.end(), result.begin(), ::toupper); |
| 3944 | result_ptr = std::make_shared<json>(std::move(result)); |
| 3945 | json_tmp_stack.push_back(result_ptr); |
| 3946 | json_eval_stack.push(result_ptr.get()); |
| 3947 | } break; |
| 3948 | case Op::IsBoolean: { |
| 3949 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_boolean()); |
| 3950 | json_tmp_stack.push_back(result_ptr); |
| 3951 | json_eval_stack.push(result_ptr.get()); |
| 3952 | } break; |
| 3953 | case Op::IsNumber: { |
| 3954 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_number()); |
| 3955 | json_tmp_stack.push_back(result_ptr); |
| 3956 | json_eval_stack.push(result_ptr.get()); |
| 3957 | } break; |
| 3958 | case Op::IsInteger: { |
| 3959 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_number_integer()); |
| 3960 | json_tmp_stack.push_back(result_ptr); |
| 3961 | json_eval_stack.push(result_ptr.get()); |
| 3962 | } break; |
| 3963 | case Op::IsFloat: { |
| 3964 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_number_float()); |
| 3965 | json_tmp_stack.push_back(result_ptr); |
| 3966 | json_eval_stack.push(result_ptr.get()); |
| 3967 | } break; |
| 3968 | case Op::IsObject: { |
| 3969 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_object()); |
| 3970 | json_tmp_stack.push_back(result_ptr); |
| 3971 | json_eval_stack.push(result_ptr.get()); |
| 3972 | } break; |
| 3973 | case Op::IsArray: { |
| 3974 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_array()); |
| 3975 | json_tmp_stack.push_back(result_ptr); |
| 3976 | json_eval_stack.push(result_ptr.get()); |
| 3977 | } break; |
| 3978 | case Op::IsString: { |
| 3979 | result_ptr = std::make_shared<json>(get_arguments<1>(node)[0]->is_string()); |
| 3980 | json_tmp_stack.push_back(result_ptr); |
| 3981 | json_eval_stack.push(result_ptr.get()); |
| 3982 | } break; |
| 3983 | case Op::Callback: { |
| 3984 | auto args = get_argument_vector(node); |
| 3985 | result_ptr = std::make_shared<json>(node.callback(args)); |
| 3986 | json_tmp_stack.push_back(result_ptr); |
| 3987 | json_eval_stack.push(result_ptr.get()); |
| 3988 | } break; |
| 3989 | case Op::Super: { |
| 3990 | const auto args = get_argument_vector(node); |
| 3991 | const size_t old_level = current_level; |
| 3992 | const size_t level_diff = (args.size() == 1) ? args[0]->get<int>() : 1; |
| 3993 | const size_t level = current_level + level_diff; |
| 3994 | |
| 3995 | if (block_statement_stack.empty()) { |
| 3996 | throw_renderer_error("super() call is not within a block", node); |
| 3997 | } |
| 3998 | |
| 3999 | if (level < 1 || level > template_stack.size() - 1) { |
| 4000 | throw_renderer_error("level of super() call does not match parent templates (between 1 and " + std::to_string(template_stack.size() - 1) + ")", node); |
| 4001 | } |
| 4002 | |
| 4003 | const auto current_block_statement = block_statement_stack.back(); |
| 4004 | const Template *new_template = template_stack.at(level); |
| 4005 | const Template *old_template = current_template; |
| 4006 | const auto block_it = new_template->block_storage.find(current_block_statement->name); |
| 4007 | if (block_it != new_template->block_storage.end()) { |
| 4008 | current_template = new_template; |
| 4009 | current_level = level; |
| 4010 | block_it->second->block.accept(*this); |
| 4011 | current_level = old_level; |
| 4012 | current_template = old_template; |
| 4013 | } else { |
| 4014 | throw_renderer_error("could not find block with name '" + current_block_statement->name + "'", node); |
| 4015 | } |
| 4016 | result_ptr = std::make_shared<json>(nullptr); |
| 4017 | json_tmp_stack.push_back(result_ptr); |
| 4018 | json_eval_stack.push(result_ptr.get()); |
| 4019 | } break; |
| 4020 | case Op::Join: { |
| 4021 | const auto args = get_arguments<2>(node); |
| 4022 | const auto separator = args[1]->get<std::string>(); |
| 4023 | std::ostringstream os; |
| 4024 | std::string sep; |
| 4025 | for (const auto& value : *args[0]) { |
| 4026 | os << sep; |
| 4027 | if (value.is_string()) { |
| 4028 | os << value.get<std::string>(); // otherwise the value is surrounded with "" |
| 4029 | } else { |
| 4030 | os << value; |
| 4031 | } |
| 4032 | sep = separator; |
| 4033 | } |
| 4034 | result_ptr = std::make_shared<json>(os.str()); |
| 4035 | json_tmp_stack.push_back(result_ptr); |
| 4036 | json_eval_stack.push(result_ptr.get()); |
| 4037 | } break; |
| 4038 | case Op::ParenLeft: |
| 4039 | case Op::ParenRight: |
| 4040 | case Op::None: |
| 4041 | break; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | void visit(const ExpressionListNode& node) { |
| 4046 | print_json(eval_expression_list(node)); |
| 4047 | } |
| 4048 | |
| 4049 | void visit(const StatementNode&) { } |
| 4050 | |
| 4051 | void visit(const ForStatementNode&) { } |
| 4052 | |
| 4053 | void visit(const ForArrayStatementNode& node) { |
| 4054 | const auto result = eval_expression_list(node.condition); |
| 4055 | if (!result->is_array()) { |
| 4056 | throw_renderer_error("object must be an array", node); |
| 4057 | } |
| 4058 | |
| 4059 | if (!current_loop_data->empty()) { |
| 4060 | auto tmp = *current_loop_data; // Because of clang-3 |
| 4061 | (*current_loop_data)["parent"] = std::move(tmp); |
| 4062 | } |
| 4063 | |
| 4064 | size_t index = 0; |
| 4065 | (*current_loop_data)["is_first"] = true; |
| 4066 | (*current_loop_data)["is_last"] = (result->size() <= 1); |
| 4067 | for (auto it = result->begin(); it != result->end(); ++it) { |
| 4068 | json_additional_data[static_cast<std::string>(node.value)] = *it; |
| 4069 | |
| 4070 | (*current_loop_data)["index"] = index; |
| 4071 | (*current_loop_data)["index1"] = index + 1; |
| 4072 | if (index == 1) { |
| 4073 | (*current_loop_data)["is_first"] = false; |
| 4074 | } |
| 4075 | if (index == result->size() - 1) { |
| 4076 | (*current_loop_data)["is_last"] = true; |
| 4077 | } |
| 4078 | |
| 4079 | node.body.accept(*this); |
| 4080 | ++index; |
| 4081 | } |
| 4082 | |
| 4083 | json_additional_data[static_cast<std::string>(node.value)].clear(); |
| 4084 | if (!(*current_loop_data)["parent"].empty()) { |
| 4085 | const auto tmp = (*current_loop_data)["parent"]; |
| 4086 | *current_loop_data = std::move(tmp); |
| 4087 | } else { |
| 4088 | current_loop_data = &json_additional_data["loop"]; |
| 4089 | } |
| 4090 | } |
| 4091 | |
| 4092 | void visit(const ForObjectStatementNode& node) { |
| 4093 | const auto result = eval_expression_list(node.condition); |
| 4094 | if (!result->is_object()) { |
| 4095 | throw_renderer_error("object must be an object", node); |
| 4096 | } |
| 4097 | |
| 4098 | if (!current_loop_data->empty()) { |
| 4099 | (*current_loop_data)["parent"] = std::move(*current_loop_data); |
| 4100 | } |
| 4101 | |
| 4102 | size_t index = 0; |
| 4103 | (*current_loop_data)["is_first"] = true; |
| 4104 | (*current_loop_data)["is_last"] = (result->size() <= 1); |
| 4105 | for (auto it = result->begin(); it != result->end(); ++it) { |
| 4106 | json_additional_data[static_cast<std::string>(node.key)] = it.key(); |
| 4107 | json_additional_data[static_cast<std::string>(node.value)] = it.value(); |
| 4108 | |
| 4109 | (*current_loop_data)["index"] = index; |
| 4110 | (*current_loop_data)["index1"] = index + 1; |
| 4111 | if (index == 1) { |
| 4112 | (*current_loop_data)["is_first"] = false; |
| 4113 | } |
| 4114 | if (index == result->size() - 1) { |
| 4115 | (*current_loop_data)["is_last"] = true; |
| 4116 | } |
| 4117 | |
| 4118 | node.body.accept(*this); |
| 4119 | ++index; |
| 4120 | } |
| 4121 | |
| 4122 | json_additional_data[static_cast<std::string>(node.key)].clear(); |
| 4123 | json_additional_data[static_cast<std::string>(node.value)].clear(); |
| 4124 | if (!(*current_loop_data)["parent"].empty()) { |
| 4125 | *current_loop_data = std::move((*current_loop_data)["parent"]); |
| 4126 | } else { |
| 4127 | current_loop_data = &json_additional_data["loop"]; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | void visit(const IfStatementNode& node) { |
| 4132 | const auto result = eval_expression_list(node.condition); |
| 4133 | if (truthy(result.get())) { |
| 4134 | node.true_statement.accept(*this); |
| 4135 | } else if (node.has_false_statement) { |
| 4136 | node.false_statement.accept(*this); |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | void visit(const IncludeStatementNode& node) { |
| 4141 | auto sub_renderer = Renderer(config, template_storage, function_storage); |
| 4142 | const auto included_template_it = template_storage.find(node.file); |
| 4143 | if (included_template_it != template_storage.end()) { |
| 4144 | sub_renderer.render_to(*output_stream, included_template_it->second, *json_input, &json_additional_data); |
| 4145 | } else if (config.throw_at_missing_includes) { |
| 4146 | throw_renderer_error("include '" + node.file + "' not found", node); |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | void visit(const ExtendsStatementNode& node) { |
| 4151 | const auto included_template_it = template_storage.find(node.file); |
| 4152 | if (included_template_it != template_storage.end()) { |
| 4153 | const Template *parent_template = &included_template_it->second; |
| 4154 | render_to(*output_stream, *parent_template, *json_input, &json_additional_data); |
| 4155 | break_rendering = true; |
| 4156 | } else if (config.throw_at_missing_includes) { |
| 4157 | throw_renderer_error("extends '" + node.file + "' not found", node); |
| 4158 | } |
| 4159 | } |
| 4160 | |
| 4161 | void visit(const BlockStatementNode& node) { |
| 4162 | const size_t old_level = current_level; |
| 4163 | current_level = 0; |
| 4164 | current_template = template_stack.front(); |
| 4165 | const auto block_it = current_template->block_storage.find(node.name); |
| 4166 | if (block_it != current_template->block_storage.end()) { |
| 4167 | block_statement_stack.emplace_back(&node); |
| 4168 | block_it->second->block.accept(*this); |
| 4169 | block_statement_stack.pop_back(); |
| 4170 | } |
| 4171 | current_level = old_level; |
| 4172 | current_template = template_stack.back(); |
| 4173 | } |
| 4174 | |
| 4175 | void visit(const SetStatementNode& node) { |
| 4176 | std::string ptr = node.key; |
| 4177 | replace_substring(ptr, ".", "/"); |
| 4178 | ptr = "/" + ptr; |
| 4179 | json_additional_data[nlohmann::json::json_pointer(ptr)] = *eval_expression_list(node.expression); |
| 4180 | } |
| 4181 | |
| 4182 | public: |
| 4183 | Renderer(const RenderConfig& config, const TemplateStorage &template_storage, const FunctionStorage &function_storage) |
| 4184 | : config(config), template_storage(template_storage), function_storage(function_storage) { } |
| 4185 | |
| 4186 | void render_to(std::ostream &os, const Template &tmpl, const json &data, json *loop_data = nullptr) { |
| 4187 | output_stream = &os; |
| 4188 | current_template = &tmpl; |
| 4189 | json_input = &data; |
| 4190 | if (loop_data) { |
| 4191 | json_additional_data = *loop_data; |
| 4192 | current_loop_data = &json_additional_data["loop"]; |
| 4193 | } |
| 4194 | |
| 4195 | template_stack.emplace_back(current_template); |
| 4196 | current_template->root.accept(*this); |
| 4197 | |
| 4198 | json_tmp_stack.clear(); |
| 4199 | } |
| 4200 | }; |
| 4201 | |
| 4202 | } // namespace inja |
| 4203 | |
| 4204 | #endif // INCLUDE_INJA_RENDERER_HPP_ |
| 4205 | |
| 4206 | // #include "string_view.hpp" |
| 4207 | |
| 4208 | // #include "template.hpp" |
| 4209 | |
| 4210 | // #include "utils.hpp" |
| 4211 | |
| 4212 | |
| 4213 | namespace inja { |
| 4214 | |
| 4215 | using json = nlohmann::json; |
| 4216 | |
| 4217 | /*! |
| 4218 | * \brief Class for changing the configuration. |
| 4219 | */ |
| 4220 | class Environment { |
| 4221 | std::string input_path; |
| 4222 | std::string output_path; |
| 4223 | |
| 4224 | LexerConfig lexer_config; |
| 4225 | ParserConfig parser_config; |
| 4226 | RenderConfig render_config; |
| 4227 | |
| 4228 | FunctionStorage function_storage; |
| 4229 | TemplateStorage template_storage; |
| 4230 | |
| 4231 | public: |
| 4232 | Environment() : Environment("./") {} |
| 4233 | |
| 4234 | explicit Environment(const std::string &global_path) : input_path(global_path), output_path(global_path) {} |
| 4235 | |
| 4236 | Environment(const std::string &input_path, const std::string &output_path) |
| 4237 | : input_path(input_path), output_path(output_path) {} |
| 4238 | |
| 4239 | /// Sets the opener and closer for template statements |
| 4240 | void set_statement(const std::string &open, const std::string &close) { |
| 4241 | lexer_config.statement_open = open; |
| 4242 | lexer_config.statement_open_no_lstrip = open + "+"; |
| 4243 | lexer_config.statement_open_force_lstrip = open + "-"; |
| 4244 | lexer_config.statement_close = close; |
| 4245 | lexer_config.statement_close_force_rstrip = "-" + close; |
| 4246 | lexer_config.update_open_chars(); |
| 4247 | } |
| 4248 | |
| 4249 | /// Sets the opener for template line statements |
| 4250 | void set_line_statement(const std::string &open) { |
| 4251 | lexer_config.line_statement = open; |
| 4252 | lexer_config.update_open_chars(); |
| 4253 | } |
| 4254 | |
| 4255 | /// Sets the opener and closer for template expressions |
| 4256 | void set_expression(const std::string &open, const std::string &close) { |
| 4257 | lexer_config.expression_open = open; |
| 4258 | lexer_config.expression_open_force_lstrip = open + "-"; |
| 4259 | lexer_config.expression_close = close; |
| 4260 | lexer_config.expression_close_force_rstrip = "-" + close; |
| 4261 | lexer_config.update_open_chars(); |
| 4262 | } |
| 4263 | |
| 4264 | /// Sets the opener and closer for template comments |
| 4265 | void set_comment(const std::string &open, const std::string &close) { |
| 4266 | lexer_config.comment_open = open; |
| 4267 | lexer_config.comment_open_force_lstrip = open + "-"; |
| 4268 | lexer_config.comment_close = close; |
| 4269 | lexer_config.comment_close_force_rstrip = "-" + close; |
| 4270 | lexer_config.update_open_chars(); |
| 4271 | } |
| 4272 | |
| 4273 | /// Sets whether to remove the first newline after a block |
| 4274 | void set_trim_blocks(bool trim_blocks) { |
| 4275 | lexer_config.trim_blocks = trim_blocks; |
| 4276 | } |
| 4277 | |
| 4278 | /// Sets whether to strip the spaces and tabs from the start of a line to a block |
| 4279 | void set_lstrip_blocks(bool lstrip_blocks) { |
| 4280 | lexer_config.lstrip_blocks = lstrip_blocks; |
| 4281 | } |
| 4282 | |
| 4283 | /// Sets the element notation syntax |
| 4284 | void set_search_included_templates_in_files(bool search_in_files) { |
| 4285 | parser_config.search_included_templates_in_files = search_in_files; |
| 4286 | } |
| 4287 | |
| 4288 | /// Sets whether a missing include will throw an error |
| 4289 | void set_throw_at_missing_includes(bool will_throw) { |
| 4290 | render_config.throw_at_missing_includes = will_throw; |
| 4291 | } |
| 4292 | |
| 4293 | Template parse(nonstd::string_view input) { |
| 4294 | Parser parser(parser_config, lexer_config, template_storage, function_storage); |
| 4295 | return parser.parse(input, input_path); |
| 4296 | } |
| 4297 | |
| 4298 | Template parse_template(const std::string &filename) { |
| 4299 | Parser parser(parser_config, lexer_config, template_storage, function_storage); |
| 4300 | auto result = Template(parser.load_file(input_path + static_cast<std::string>(filename))); |
| 4301 | parser.parse_into_template(result, input_path + static_cast<std::string>(filename)); |
| 4302 | return result; |
| 4303 | } |
| 4304 | |
| 4305 | Template parse_file(const std::string &filename) { |
| 4306 | return parse_template(filename); |
| 4307 | } |
| 4308 | |
| 4309 | std::string render(nonstd::string_view input, const json &data) { return render(parse(input), data); } |
| 4310 | |
| 4311 | std::string render(const Template &tmpl, const json &data) { |
| 4312 | std::stringstream os; |
| 4313 | render_to(os, tmpl, data); |
| 4314 | return os.str(); |
| 4315 | } |
| 4316 | |
| 4317 | std::string render_file(const std::string &filename, const json &data) { |
| 4318 | return render(parse_template(filename), data); |
| 4319 | } |
| 4320 | |
| 4321 | std::string render_file_with_json_file(const std::string &filename, const std::string &filename_data) { |
| 4322 | const json data = load_json(filename_data); |
| 4323 | return render_file(filename, data); |
| 4324 | } |
| 4325 | |
| 4326 | void write(const std::string &filename, const json &data, const std::string &filename_out) { |
| 4327 | std::ofstream file(output_path + filename_out); |
| 4328 | file << render_file(filename, data); |
| 4329 | file.close(); |
| 4330 | } |
| 4331 | |
| 4332 | void write(const Template &temp, const json &data, const std::string &filename_out) { |
| 4333 | std::ofstream file(output_path + filename_out); |
| 4334 | file << render(temp, data); |
| 4335 | file.close(); |
| 4336 | } |
| 4337 | |
| 4338 | void write_with_json_file(const std::string &filename, const std::string &filename_data, |
| 4339 | const std::string &filename_out) { |
| 4340 | const json data = load_json(filename_data); |
| 4341 | write(filename, data, filename_out); |
| 4342 | } |
| 4343 | |
| 4344 | void write_with_json_file(const Template &temp, const std::string &filename_data, const std::string &filename_out) { |
| 4345 | const json data = load_json(filename_data); |
| 4346 | write(temp, data, filename_out); |
| 4347 | } |
| 4348 | |
| 4349 | std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) { |
| 4350 | Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data); |
| 4351 | return os; |
| 4352 | } |
| 4353 | |
| 4354 | std::string load_file(const std::string &filename) { |
| 4355 | Parser parser(parser_config, lexer_config, template_storage, function_storage); |
| 4356 | return parser.load_file(input_path + filename); |
| 4357 | } |
| 4358 | |
| 4359 | json load_json(const std::string &filename) { |
| 4360 | std::ifstream file; |
| 4361 | open_file_or_throw(input_path + filename, file); |
| 4362 | json j; |
| 4363 | file >> j; |
| 4364 | return j; |
| 4365 | } |
| 4366 | |
| 4367 | /*! |
| 4368 | @brief Adds a variadic callback |
| 4369 | */ |
| 4370 | void add_callback(const std::string &name, const CallbackFunction &callback) { |
| 4371 | add_callback(name, -1, callback); |
| 4372 | } |
| 4373 | |
| 4374 | /*! |
| 4375 | @brief Adds a variadic void callback |
| 4376 | */ |
| 4377 | void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) { |
| 4378 | add_void_callback(name, -1, callback); |
| 4379 | } |
| 4380 | |
| 4381 | /*! |
| 4382 | @brief Adds a callback with given number or arguments |
| 4383 | */ |
| 4384 | void add_callback(const std::string &name, int num_args, const CallbackFunction &callback) { |
| 4385 | function_storage.add_callback(name, num_args, callback); |
| 4386 | } |
| 4387 | |
| 4388 | /*! |
| 4389 | @brief Adds a void callback with given number or arguments |
| 4390 | */ |
| 4391 | void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) { |
| 4392 | function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); }); |
| 4393 | } |
| 4394 | |
| 4395 | /** Includes a template with a given name into the environment. |
| 4396 | * Then, a template can be rendered in another template using the |
| 4397 | * include "<name>" syntax. |
| 4398 | */ |
| 4399 | void include_template(const std::string &name, const Template &tmpl) { |
| 4400 | template_storage[name] = tmpl; |
| 4401 | } |
| 4402 | }; |
| 4403 | |
| 4404 | /*! |
| 4405 | @brief render with default settings to a string |
| 4406 | */ |
| 4407 | inline std::string render(nonstd::string_view input, const json &data) { |
| 4408 | return Environment().render(input, data); |
| 4409 | } |
| 4410 | |
| 4411 | /*! |
| 4412 | @brief render with default settings to the given output stream |
| 4413 | */ |
| 4414 | inline void render_to(std::ostream &os, nonstd::string_view input, const json &data) { |
| 4415 | Environment env; |
| 4416 | env.render_to(os, env.parse(input), data); |
| 4417 | } |
| 4418 | |
| 4419 | } // namespace inja |
| 4420 | |
| 4421 | #endif // INCLUDE_INJA_ENVIRONMENT_HPP_ |
| 4422 | |
| 4423 | // #include "exceptions.hpp" |
| 4424 | |
| 4425 | // #include "parser.hpp" |
| 4426 | |
| 4427 | // #include "renderer.hpp" |
| 4428 | |
| 4429 | // #include "string_view.hpp" |
| 4430 | |
| 4431 | // #include "template.hpp" |
| 4432 | |
| 4433 | |
| 4434 | #endif // INCLUDE_INJA_INJA_HPP_ |