Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser error position #134

Merged
merged 34 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b570d9f
add function to get position of current token
sabelka Feb 2, 2020
291320a
new exception parser_error with error location
sabelka Feb 2, 2020
889d430
parser throws parser_error with error location
sabelka Feb 2, 2020
dd52a90
update single include
sabelka Feb 2, 2020
0aec22a
get error position in text
pantor Mar 17, 2020
29a9ab8
bump to 2.3
pantor Mar 17, 2020
4ac18b1
subclass exceptions
pantor Mar 18, 2020
f41feb6
update single include from every directory
pantor Mar 18, 2020
225a468
center badges in readme
pantor Mar 18, 2020
23b5edd
github ci windows, mac
pantor Mar 18, 2020
46b1e81
use default compiler
pantor Mar 18, 2020
54f9ccb
check ci
pantor Mar 18, 2020
b89cbd9
fix ci
pantor Mar 18, 2020
0386008
test ci
pantor Mar 18, 2020
9f6a574
fix ci
pantor Mar 18, 2020
18b2e53
fix ci
pantor Mar 18, 2020
92afd50
test ci
pantor Mar 18, 2020
3f624db
badges to github action
pantor Mar 18, 2020
773005b
add check for single include
pantor Mar 18, 2020
87de628
fix check
pantor Mar 18, 2020
b9f5588
check (should fail)
pantor Mar 18, 2020
7f0f99d
fix all ci
pantor Mar 18, 2020
f69654f
add artifact in single include ci
pantor Mar 18, 2020
10ac6d8
update to newest nlohmann/json
pantor Mar 18, 2020
62d8593
move unit tests to doctest
pantor Mar 18, 2020
e88b2e3
move to json v3.6.1
pantor Mar 21, 2020
e688fd5
add function to get position of current token
sabelka Feb 2, 2020
dba0cc9
new exception parser_error with error location
sabelka Feb 2, 2020
630d1fb
parser throws parser_error with error location
sabelka Feb 2, 2020
af3a6b8
update single include
sabelka Feb 2, 2020
011542f
get error position in text
pantor Mar 17, 2020
7b0f710
bump to 2.3
pantor Mar 17, 2020
67916f3
subclass exceptions
pantor Mar 18, 2020
2903d1a
Merge branch 'parser-error-position' of https://github.com/sabelka/in…
pantor Mar 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@ name: C++ CI

on: push

env:
BUILD_TYPE: Release

jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [
ubuntu-latest,
ubuntu-16.04,
windows-latest,
windows-2016,
macos-latest
]

steps:
- uses: actions/checkout@v1

- name: Install Dependencies
env:
dependency_packages: build-essential g++-5
run: sudo apt-get update && sudo apt-get -y install ${dependency_packages}
#- name: Install Dependencies
# env:
# dependency_packages: build-essential g++-5
# run: sudo apt-get update && sudo apt-get -y install ${dependency_packages}

- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/inja/build

- name: Build
working-directory: ${{runner.workspace}}/inja/build
shell: bash
run: |
mkdir -p build && cd build
cmake ..
cmake --build . --config Release -- -j4
cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
cmake --build . --config $BUILD_TYPE -j4

- name: Test
run: ctest -C Release -V
working-directory: ${{runner.workspace}}/inja/build
shell: bash
run: |
ctest -C $BUILD_TYPE -V
24 changes: 24 additions & 0 deletions .github/workflows/single-include.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Single Include

on: push

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master

- name: update single include
run: |
mv single_include/inja/inja.hpp single_include/inja/inja_old.hpp
bash scripts/update_single_include.sh

- name: check if equal
working-directory: ${{runner.workspace}}/inja/single_include/
shell: bash
run: diff inja/inja.hpp inja/inja_old.hpp >/dev/null

- uses: actions/upload-artifact@v1
with:
name: single_include_inja
path: single_include/inja/inja.hpp
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5)


project(inja LANGUAGES CXX VERSION 2.2.0)
project(inja LANGUAGES CXX VERSION 2.3.0)


option(INJA_USE_EMBEDDED_JSON "Use the shipped json header if not available on the system" ON)
Expand Down
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
[<div align="center"><img width="500" src="https://raw.githubusercontent.com/pantor/inja/master/doc/logo.jpg"></div>](https://github.com/pantor/inja/releases)


[![Build Status](https://travis-ci.org/pantor/inja.svg?branch=master)](https://travis-ci.org/pantor/inja)
[![Build status](https://ci.appveyor.com/api/projects/status/qtgniyyg6fn8ich8/branch/master?svg=true)](https://ci.appveyor.com/project/pantor/inja)
[![Codacy Status](https://api.codacy.com/project/badge/Grade/aa2041f1e6e648ae83945d29cfa0da17)](https://www.codacy.com/app/pantor/inja?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=pantor/inja&amp;utm_campaign=Badge_Grade)
[![Github Releases](https://img.shields.io/github/release/pantor/inja.svg)](https://github.com/pantor/inja/releases)
[![Github Issues](https://img.shields.io/github/issues/pantor/inja.svg)](http://github.com/pantor/inja/issues)
[![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/pantor/inja/master/LICENSE)
<p align="center">
<a href="https://github.com/pantor/inja/actions">
<img src="https://github.com/pantor/inja/workflows/C++%20CI/badge.svg" alt="CI Status">
</a>

<a href="https://github.com/pantor/inja/actions">
<img src="https://github.com/pantor/inja/workflows/Documentation/badge.svg" alt="Documentation Status">
</a>

<a href="https://www.codacy.com/app/pantor/inja?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=pantor/inja&amp;utm_campaign=Badge_Grade">
<img src="https://api.codacy.com/project/badge/Grade/aa2041f1e6e648ae83945d29cfa0da17" alt="Codacy Status">
</a>

<a href="https://github.com/pantor/inja/releases">
<img src="https://img.shields.io/github/release/pantor/inja.svg" alt="Github Releases">
</a>

<a href="http://github.com/pantor/inja/issues">
<img src="https://img.shields.io/github/issues/pantor/inja.svg" alt="Github Issues">
</a>

<a href="https://raw.githubusercontent.com/pantor/inja/master/LICENSE">
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="GitHub License">
</a>
</p>


Inja is a template engine for modern C++, loosely inspired by [jinja](http://jinja.pocoo.org) for python. It has an easy and yet powerful template syntax with all variables, loops, conditions, includes, callbacks, comments you need, nested and combined as you like. Inja uses the wonderful [json](https://github.com/nlohmann/json) library by nlohmann for data input and handling. Most importantly, *inja* needs only two header files, which is (nearly) as trivial as integration in C++ can get. Of course, everything is tested on all relevant compilers. Here is what it looks like:
Expand Down
59 changes: 59 additions & 0 deletions include/inja/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2020 Pantor. All rights reserved.

#ifndef INCLUDE_INJA_EXCEPTIONS_HPP_
#define INCLUDE_INJA_EXCEPTIONS_HPP_

#include <stdexcept>
#include <string>


namespace inja {

struct SourceLocation {
size_t line;
size_t column;
};

struct InjaError : public std::runtime_error {
std::string type;
std::string message;

bool has_location {false};
SourceLocation location;

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

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), has_location(true), location(location) { }
};

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

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

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

struct JsonError : public InjaError {
JsonError(const std::string& message) : InjaError("json_error", message) { }
JsonError(const std::string& message, SourceLocation location)
: InjaError("json_error", message, location) { }
};

} // namespace inja

#endif // INCLUDE_INJA_EXCEPTIONS_HPP_
1 change: 1 addition & 0 deletions include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <nlohmann/json.hpp>

#include "exceptions.hpp"
#include "environment.hpp"
#include "string_view.hpp"
#include "template.hpp"
Expand Down
23 changes: 21 additions & 2 deletions include/inja/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ class Lexer {
public:
explicit Lexer(const LexerConfig& config) : m_config(config) {}

SourceLocation current_position() const {
// Get line and offset position (starts at 1:1)
auto sliced = string_view::slice(m_in, 0, m_tok_start);
std::size_t last_newline = sliced.rfind("\n");

if (last_newline == nonstd::string_view::npos) {
return {1, sliced.length() + 1};
}

// Count newlines
size_t count_lines = 0;
size_t search_start = 0;
while (search_start < sliced.size()) {
search_start = sliced.find("\n", search_start + 1);
count_lines += 1;
}

return {count_lines + 1, sliced.length() - last_newline + 1};
}

void start(nonstd::string_view in) {
m_in = in;
m_tok_start = 0;
Expand Down Expand Up @@ -287,8 +307,7 @@ class Lexer {
}
}

static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text)
{
static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text) {
nonstd::string_view result = text;
while (!result.empty()) {
char ch = result.back();
Expand Down
Loading