Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
pantor committed Nov 12, 2021
1 parent 81cfa14 commit eb04bfc
Show file tree
Hide file tree
Showing 19 changed files with 556 additions and 610 deletions.
11 changes: 9 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
---
BasedOnStyle: LLVM
IndentWidth: 2
ColumnLimit: 160

---
Language: Cpp

BasedOnStyle: LLVM
ColumnLimit: 120
SpaceBeforeCpp11BracedList: true
PointerAlignment: Left
AllowShortFunctionsOnASingleLine: Empty
AllowShortBlocksOnASingleLine: Empty
SpaceBeforeCtorInitializerColon: false
...
19 changes: 11 additions & 8 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ class Environment {
TemplateStorage template_storage;

public:
Environment() : Environment("") {}
Environment(): Environment("") {}

explicit Environment(const std::string& global_path) : input_path(global_path), output_path(global_path) {}
explicit Environment(const std::string& global_path): input_path(global_path), output_path(global_path) {}

Environment(const std::string& input_path, const std::string& output_path)
: input_path(input_path), output_path(output_path) {}
Environment(const std::string& input_path, const std::string& output_path): input_path(input_path), output_path(output_path) {}

/// Sets the opener and closer for template statements
void set_statement(const std::string& open, const std::string& close) {
Expand Down Expand Up @@ -109,7 +108,9 @@ class Environment {
return parse_template(filename);
}

std::string render(std::string_view input, const json& data) { return render(parse(input), data); }
std::string render(std::string_view input, const json& data) {
return render(parse(input), data);
}

std::string render(const Template& tmpl, const json& data) {
std::stringstream os;
Expand Down Expand Up @@ -138,8 +139,7 @@ class Environment {
file.close();
}

void write_with_json_file(const std::string& filename, const std::string& filename_data,
const std::string& filename_out) {
void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) {
const json data = load_json(filename_data);
write(filename, data, filename_out);
}
Expand Down Expand Up @@ -195,7 +195,10 @@ class Environment {
@brief Adds a void callback with given number or arguments
*/
void add_void_callback(const std::string& name, int num_args, const VoidCallbackFunction& callback) {
function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); });
function_storage.add_callback(name, num_args, [callback](Arguments& args) {
callback(args);
return json();
});
}

/** Includes a template with a given name into the environment.
Expand Down
17 changes: 8 additions & 9 deletions include/inja/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,29 @@ struct InjaError : public std::runtime_error {

const SourceLocation location;

explicit InjaError(const std::string &type, const std::string &message)
explicit InjaError(const std::string& type, const std::string& message)
: std::runtime_error("[inja.exception." + type + "] " + message), type(type), message(message), location({0, 0}) {}

explicit InjaError(const std::string &type, const std::string &message, SourceLocation location)
: std::runtime_error("[inja.exception." + type + "] (at " + std::to_string(location.line) + ":" +
std::to_string(location.column) + ") " + message),
explicit InjaError(const std::string& type, const std::string& message, SourceLocation location)
: std::runtime_error("[inja.exception." + type + "] (at " + std::to_string(location.line) + ":" + std::to_string(location.column) + ") " + message),
type(type), message(message), location(location) {}
};

struct ParserError : public InjaError {
explicit ParserError(const std::string &message, SourceLocation location) : InjaError("parser_error", message, location) {}
explicit ParserError(const std::string& message, SourceLocation location): InjaError("parser_error", message, location) {}
};

struct RenderError : public InjaError {
explicit RenderError(const std::string &message, SourceLocation location) : InjaError("render_error", message, location) {}
explicit RenderError(const std::string& message, SourceLocation location): InjaError("render_error", message, location) {}
};

struct FileError : public InjaError {
explicit FileError(const std::string &message) : InjaError("file_error", message) {}
explicit FileError(const std::string &message, SourceLocation location) : InjaError("file_error", message, location) {}
explicit FileError(const std::string& message): InjaError("file_error", message) {}
explicit FileError(const std::string& message, SourceLocation location): InjaError("file_error", message, location) {}
};

struct DataError : public InjaError {
explicit DataError(const std::string &message, SourceLocation location) : InjaError("data_error", message, location) {}
explicit DataError(const std::string& message, SourceLocation location): InjaError("data_error", message, location) {}
};

} // namespace inja
Expand Down
69 changes: 34 additions & 35 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string_view>
#include <vector>


namespace inja {

using Arguments = std::vector<const json*>;
Expand Down Expand Up @@ -69,7 +68,7 @@ class FunctionStorage {
};

struct FunctionData {
explicit FunctionData(const Operation& op, const CallbackFunction& cb = CallbackFunction{}) : operation(op), callback(cb) {}
explicit FunctionData(const Operation& op, const CallbackFunction& cb = CallbackFunction {}): operation(op), callback(cb) {}
const Operation operation;
const CallbackFunction callback;
};
Expand All @@ -78,60 +77,60 @@ class FunctionStorage {
const int VARIADIC {-1};

std::map<std::pair<std::string, int>, FunctionData> function_storage = {
{std::make_pair("at", 2), FunctionData { Operation::At }},
{std::make_pair("default", 2), FunctionData { Operation::Default }},
{std::make_pair("divisibleBy", 2), FunctionData { Operation::DivisibleBy }},
{std::make_pair("even", 1), FunctionData { Operation::Even }},
{std::make_pair("exists", 1), FunctionData { Operation::Exists }},
{std::make_pair("existsIn", 2), FunctionData { Operation::ExistsInObject }},
{std::make_pair("first", 1), FunctionData { Operation::First }},
{std::make_pair("float", 1), FunctionData { Operation::Float }},
{std::make_pair("int", 1), FunctionData { Operation::Int }},
{std::make_pair("isArray", 1), FunctionData { Operation::IsArray }},
{std::make_pair("isBoolean", 1), FunctionData { Operation::IsBoolean }},
{std::make_pair("isFloat", 1), FunctionData { Operation::IsFloat }},
{std::make_pair("isInteger", 1), FunctionData { Operation::IsInteger }},
{std::make_pair("isNumber", 1), FunctionData { Operation::IsNumber }},
{std::make_pair("isObject", 1), FunctionData { Operation::IsObject }},
{std::make_pair("isString", 1), FunctionData { Operation::IsString }},
{std::make_pair("last", 1), FunctionData { Operation::Last }},
{std::make_pair("length", 1), FunctionData { Operation::Length }},
{std::make_pair("lower", 1), FunctionData { Operation::Lower }},
{std::make_pair("max", 1), FunctionData { Operation::Max }},
{std::make_pair("min", 1), FunctionData { Operation::Min }},
{std::make_pair("odd", 1), FunctionData { Operation::Odd }},
{std::make_pair("range", 1), FunctionData { Operation::Range }},
{std::make_pair("round", 2), FunctionData { Operation::Round }},
{std::make_pair("sort", 1), FunctionData { Operation::Sort }},
{std::make_pair("upper", 1), FunctionData { Operation::Upper }},
{std::make_pair("super", 0), FunctionData { Operation::Super }},
{std::make_pair("super", 1), FunctionData { Operation::Super }},
{std::make_pair("join", 2), FunctionData { Operation::Join }},
{std::make_pair("at", 2), FunctionData {Operation::At}},
{std::make_pair("default", 2), FunctionData {Operation::Default}},
{std::make_pair("divisibleBy", 2), FunctionData {Operation::DivisibleBy}},
{std::make_pair("even", 1), FunctionData {Operation::Even}},
{std::make_pair("exists", 1), FunctionData {Operation::Exists}},
{std::make_pair("existsIn", 2), FunctionData {Operation::ExistsInObject}},
{std::make_pair("first", 1), FunctionData {Operation::First}},
{std::make_pair("float", 1), FunctionData {Operation::Float}},
{std::make_pair("int", 1), FunctionData {Operation::Int}},
{std::make_pair("isArray", 1), FunctionData {Operation::IsArray}},
{std::make_pair("isBoolean", 1), FunctionData {Operation::IsBoolean}},
{std::make_pair("isFloat", 1), FunctionData {Operation::IsFloat}},
{std::make_pair("isInteger", 1), FunctionData {Operation::IsInteger}},
{std::make_pair("isNumber", 1), FunctionData {Operation::IsNumber}},
{std::make_pair("isObject", 1), FunctionData {Operation::IsObject}},
{std::make_pair("isString", 1), FunctionData {Operation::IsString}},
{std::make_pair("last", 1), FunctionData {Operation::Last}},
{std::make_pair("length", 1), FunctionData {Operation::Length}},
{std::make_pair("lower", 1), FunctionData {Operation::Lower}},
{std::make_pair("max", 1), FunctionData {Operation::Max}},
{std::make_pair("min", 1), FunctionData {Operation::Min}},
{std::make_pair("odd", 1), FunctionData {Operation::Odd}},
{std::make_pair("range", 1), FunctionData {Operation::Range}},
{std::make_pair("round", 2), FunctionData {Operation::Round}},
{std::make_pair("sort", 1), FunctionData {Operation::Sort}},
{std::make_pair("upper", 1), FunctionData {Operation::Upper}},
{std::make_pair("super", 0), FunctionData {Operation::Super}},
{std::make_pair("super", 1), FunctionData {Operation::Super}},
{std::make_pair("join", 2), FunctionData {Operation::Join}},
};

public:
void add_builtin(std::string_view name, int num_args, Operation op) {
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { op });
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData {op});
}

void add_callback(std::string_view name, int num_args, const CallbackFunction& callback) {
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { Operation::Callback, callback });
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData {Operation::Callback, callback});
}

FunctionData find_function(std::string_view name, int num_args) const {
auto it = function_storage.find(std::make_pair(static_cast<std::string>(name), num_args));
if (it != function_storage.end()) {
return it->second;

// Find variadic function
// Find variadic function
} else if (num_args > 0) {
it = function_storage.find(std::make_pair(static_cast<std::string>(name), VARIADIC));
if (it != function_storage.end()) {
return it->second;
}
}

return FunctionData { Operation::None };
return FunctionData {Operation::None};
}
};

Expand Down
30 changes: 16 additions & 14 deletions include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
| || '_ \ | |/ _` | Licensed under the MIT License <http://opensource.org/licenses/MIT>.
| || | | || | (_| |
|___|_| |_|/ |\__,_| Copyright (c) 2018-2021 Lars Berscheid
|__/
|__/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -29,24 +29,26 @@ SOFTWARE.

namespace inja {
#ifndef INJA_DATA_TYPE
using json = nlohmann::json;
using json = nlohmann::json;
#else
using json = INJA_DATA_TYPE;
using json = INJA_DATA_TYPE;
#endif
}
} // namespace inja

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

#include "environment.hpp"
Expand Down
9 changes: 5 additions & 4 deletions include/inja/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Lexer {
size_t tok_start;
size_t pos;


Token scan_body(std::string_view close, Token::Kind closeKind, std::string_view close_trim = std::string_view(), bool trim = false) {
again:
// skip whitespace (except for \n as it might be a close)
Expand Down Expand Up @@ -224,7 +223,9 @@ class Lexer {
return make_token(Token::Kind::String);
}

Token make_token(Token::Kind kind) const { return Token(kind, string_view::slice(m_in, tok_start, pos)); }
Token make_token(Token::Kind kind) const {
return Token(kind, string_view::slice(m_in, tok_start, pos));
}

void skip_whitespaces_and_newlines() {
if (pos < m_in.size()) {
Expand Down Expand Up @@ -270,7 +271,7 @@ class Lexer {
}

public:
explicit Lexer(const LexerConfig& config) : config(config), state(State::Text), minus_state(MinusState::Number) {}
explicit Lexer(const LexerConfig& config): config(config), state(State::Text), minus_state(MinusState::Number) {}

SourceLocation current_position() const {
return get_source_location(m_in, tok_start);
Expand Down Expand Up @@ -322,7 +323,7 @@ class Lexer {
} else if (inja::string_view::starts_with(open_str, config.statement_open)) {
if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) {
state = State::StatementStartNoLstrip;
} else if (inja::string_view::starts_with(open_str, config.statement_open_force_lstrip )) {
} else if (inja::string_view::starts_with(open_str, config.statement_open_force_lstrip)) {
state = State::StatementStartForceLstrip;
must_lstrip = true;
} else {
Expand Down
Loading

0 comments on commit eb04bfc

Please sign in to comment.