Skip to content

Commit 9d59943

Browse files
committed
add INJA_NOEXCEPTION
1 parent a3b0b41 commit 9d59943

File tree

10 files changed

+25
-22
lines changed

10 files changed

+25
-22
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ Comments can be written with the `{# ... #}` syntax.
321321
render("Hello{# Todo #}!", data); // "Hello!"
322322
```
323323
324+
### Exceptions
325+
326+
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.
327+
328+
324329
## Supported compilers
325330
326331
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:

include/inja/inja.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
#include <nlohmann/json.hpp>
77

8+
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
9+
#define INJA_THROW(exception) throw exception
10+
#else
11+
#include <cstdlib>
12+
#define INJA_THROW(exception) std::abort()
13+
#endif
14+
815
#include "environment.hpp"
916
#include "exceptions.hpp"
1017
#include "parser.hpp"

include/inja/parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Parser {
5151
std::stack<ForStatementNode*> for_statement_stack;
5252

5353
void throw_parser_error(const std::string &message) {
54-
throw ParserError(message, lexer.current_position());
54+
INJA_THROW(ParserError(message, lexer.current_position()));
5555
}
5656

5757
void get_next_token() {

include/inja/renderer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Renderer : public NodeVisitor {
9191

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

9797
template<size_t N, bool throw_not_found=true>

include/inja/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
1818
try {
1919
file.open(path);
2020
} catch (const std::ios_base::failure & /*e*/) {
21-
throw FileError("failed accessing file at '" + path + "'");
21+
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
2222
}
2323
}
2424

single_include/inja/inja.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
#include <nlohmann/json.hpp>
77

8+
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
9+
#define INJA_THROW(exception) throw exception
10+
#else
11+
#include <cstdlib>
12+
#define INJA_THROW(exception) std::abort()
13+
#endif
14+
815
// #include "environment.hpp"
916
// Copyright (c) 2019 Pantor. All rights reserved.
1017

@@ -1838,7 +1845,7 @@ inline void open_file_or_throw(const std::string &path, std::ifstream &file) {
18381845
try {
18391846
file.open(path);
18401847
} catch (const std::ios_base::failure & /*e*/) {
1841-
throw FileError("failed accessing file at '" + path + "'");
1848+
INJA_THROW(FileError("failed accessing file at '" + path + "'"));
18421849
}
18431850
}
18441851

@@ -2780,7 +2787,7 @@ class Parser {
27802787
std::stack<ForStatementNode*> for_statement_stack;
27812788

27822789
void throw_parser_error(const std::string &message) {
2783-
throw ParserError(message, lexer.current_position());
2790+
INJA_THROW(ParserError(message, lexer.current_position()));
27842791
}
27852792

27862793
void get_next_token() {
@@ -3409,7 +3416,7 @@ class Renderer : public NodeVisitor {
34093416

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

34153422
template<size_t N, bool throw_not_found=true>

test/test-files.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include "doctest/doctest.h"
4-
#include "inja/inja.hpp"
5-
6-
73
TEST_CASE("loading") {
84
inja::Environment env;
95
json data;

test/test-functions.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include "doctest/doctest.h"
4-
#include "inja/inja.hpp"
5-
6-
73
TEST_CASE("functions") {
84
inja::Environment env;
95

test/test-renderer.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include "doctest/doctest.h"
4-
#include "inja/inja.hpp"
5-
6-
73
TEST_CASE("types") {
84
inja::Environment env;
95
json data;

test/test-units.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include "doctest/doctest.h"
4-
#include "inja/inja.hpp"
5-
6-
73
TEST_CASE("source location") {
84
std::string content = R""""(Lorem Ipsum
95
Dolor

0 commit comments

Comments
 (0)