Skip to content

Commit

Permalink
add INJA_NOEXCEPTION
Browse files Browse the repository at this point in the history
  • Loading branch information
pantor committed Jan 16, 2021
1 parent a3b0b41 commit 9d59943
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 22 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ Comments can be written with the `{# ... #}` syntax.
render("Hello{# Todo #}!", data); // "Hello!"
```
### Exceptions
Inja uses exceptions to handle ill-formed template input. However, exceptions can be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol `INJA_NOEXCEPTION`. In this case, exceptions are replaced by `abort()` calls.
## Supported compilers
Inja uses `string_view` from C++17, but includes the [polyfill](https://github.com/martinmoene/string-view-lite) from martinmoene. This way, the minimum version is C++11. Currently, the following compilers are tested:
Expand Down
7 changes: 7 additions & 0 deletions include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

#include <nlohmann/json.hpp>

#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
#define INJA_THROW(exception) throw exception
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#endif

#include "environment.hpp"
#include "exceptions.hpp"
#include "parser.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Parser {
std::stack<ForStatementNode*> for_statement_stack;

void throw_parser_error(const std::string &message) {
throw ParserError(message, lexer.current_position());
INJA_THROW(ParserError(message, lexer.current_position()));
}

void get_next_token() {
Expand Down
2 changes: 1 addition & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Renderer : public NodeVisitor {

void throw_renderer_error(const std::string &message, const AstNode& node) {
SourceLocation loc = get_source_location(current_template->content, node.pos);
throw RenderError(message, loc);
INJA_THROW(RenderError(message, loc));
}

template<size_t N, bool throw_not_found=true>
Expand Down
2 changes: 1 addition & 1 deletion include/inja/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
try {
file.open(path);
} catch (const std::ios_base::failure & /*e*/) {
throw FileError("failed accessing file at '" + path + "'");
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
}
}

Expand Down
13 changes: 10 additions & 3 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

#include <nlohmann/json.hpp>

#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
#define INJA_THROW(exception) throw exception
#else
#include <cstdlib>
#define INJA_THROW(exception) std::abort()
#endif

// #include "environment.hpp"
// Copyright (c) 2019 Pantor. All rights reserved.

Expand Down Expand Up @@ -1838,7 +1845,7 @@ inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
try {
file.open(path);
} catch (const std::ios_base::failure & /*e*/) {
throw FileError("failed accessing file at '" + path + "'");
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
}
}

Expand Down Expand Up @@ -2780,7 +2787,7 @@ class Parser {
std::stack<ForStatementNode*> for_statement_stack;

void throw_parser_error(const std::string &message) {
throw ParserError(message, lexer.current_position());
INJA_THROW(ParserError(message, lexer.current_position()));
}

void get_next_token() {
Expand Down Expand Up @@ -3409,7 +3416,7 @@ class Renderer : public NodeVisitor {

void throw_renderer_error(const std::string &message, const AstNode& node) {
SourceLocation loc = get_source_location(current_template->content, node.pos);
throw RenderError(message, loc);
INJA_THROW(RenderError(message, loc));
}

template<size_t N, bool throw_not_found=true>
Expand Down
4 changes: 0 additions & 4 deletions test/test-files.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright (c) 2020 Pantor. All rights reserved.

#include "doctest/doctest.h"
#include "inja/inja.hpp"


TEST_CASE("loading") {
inja::Environment env;
json data;
Expand Down
4 changes: 0 additions & 4 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright (c) 2020 Pantor. All rights reserved.

#include "doctest/doctest.h"
#include "inja/inja.hpp"


TEST_CASE("functions") {
inja::Environment env;

Expand Down
4 changes: 0 additions & 4 deletions test/test-renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright (c) 2020 Pantor. All rights reserved.

#include "doctest/doctest.h"
#include "inja/inja.hpp"


TEST_CASE("types") {
inja::Environment env;
json data;
Expand Down
4 changes: 0 additions & 4 deletions test/test-units.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright (c) 2020 Pantor. All rights reserved.

#include "doctest/doctest.h"
#include "inja/inja.hpp"


TEST_CASE("source location") {
std::string content = R""""(Lorem Ipsum
Dolor
Expand Down

0 comments on commit 9d59943

Please sign in to comment.