Skip to content

Commit

Permalink
improve syntax error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vmatare committed Jan 30, 2021
1 parent 7d946a5 commit 837b997
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/model/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class NameNotMapped : public std::runtime_error {

class SyntaxError : public UserError {
public:
using UserError::UserError;
SyntaxError(const string &code, string::size_type offset, const string &msg);
};

Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct BatParser : grammar<void(Scope &)> {
initialize_cyclic_expressions();
initialize_cyclic_values();

on_error<rethrow>(program, phoenix::bind(&handle_error, _1, _3, _2, _4));
on_error<rethrow>(program, phoenix::bind(&throw_syntax_error, _1, _3, _2, _4));

GOLOGPP_DEBUG_NODE(program)
}
Expand Down
14 changes: 8 additions & 6 deletions src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace gologpp {
namespace parser {



void parse_string(const std::string &code)
{
BatParser parser;
Expand All @@ -50,15 +49,15 @@ void parse_string(const std::string &code)
}


void parse_recursive(const std::string &filename)
void parse_recursive(const path &filename)
{
std::ifstream file(filename);
if (!file.is_open())
throw std::runtime_error(filename + ": " + ::strerror(errno));
throw std::runtime_error(filename.string() + ": " + ::strerror(errno));
std::stringstream buffer;
buffer << file.rdbuf();
if (!file.good())
throw std::runtime_error(filename + ": " + ::strerror(errno));
throw std::runtime_error(filename.string() + ": " + ::strerror(errno));

std::string content { buffer.str() };
std::string::size_type offs = 0;
Expand All @@ -69,9 +68,12 @@ void parse_recursive(const std::string &filename)
if (content[offs] != '"')
throw SyntaxError(content, offs, "Expected: \"");
++offs;
string cur_path = filename.substr(0, filename.find_last_of("/"));
string inc_filename = content.substr(offs, content.find_first_of("\"", offs) - offs);
parse_recursive(cur_path + '/' + inc_filename);

path incpath = filename.parent_path();
incpath /= path(inc_filename);

parse_recursive(incpath);
}
std::cout << "Parsing " << filename << "..." << std::endl;

Expand Down
2 changes: 2 additions & 0 deletions src/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#include "utilities.h"

#include <tuple>
#include <filesystem>

namespace gologpp {
namespace parser {

using path = std::filesystem::path;

void parse_string(const std::string &code);

Expand Down
8 changes: 5 additions & 3 deletions src/parser/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <model/platform/clock_formula.h>
#include <parser/platform/constraint.h>

#include <iostream>
#include <sstream>

namespace gologpp {
namespace parser {
Expand Down Expand Up @@ -128,15 +128,17 @@ string get_error_context(



void handle_error(
void throw_syntax_error(
const iterator &begin,
const iterator &errpos,
const iterator &end,
const boost::spirit::info &expected
) {
std::cout << "Syntax error at line " << get_line(errpos) << ":" << std::endl
std::stringstream ss;
ss << "Syntax error at line " << get_line(errpos) << ":" << std::endl
<< get_error_context(begin, errpos, end)
<< "Expected: " << expected << std::endl;
throw SyntaxError(ss.str());
}


Expand Down
4 changes: 2 additions & 2 deletions src/parser/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define GOLOGPP_PARSER_UTILITIES_H_

#include <model/expressions.h>
#include <model/error.h>

#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <boost/spirit/include/qi_nonterminal.hpp>
Expand All @@ -31,7 +32,6 @@
namespace gologpp {
namespace parser {


namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
Expand Down Expand Up @@ -92,7 +92,7 @@ string get_error_context(
const iterator &end
);

void handle_error(
void throw_syntax_error(
const iterator &begin,
const iterator &errpos,
const iterator &end,
Expand Down
2 changes: 1 addition & 1 deletion src/semantics/platform/taptenc/transformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ unique_ptr<Plan> TaptencTransformation::transform(Plan &&p)
return unique_ptr<Plan>(new Plan(std::move(p)));
else if (tt_automata_.size() == tt_constraints_.size()) {
auto tt_plan = plan_gpp_to_taptenc(std::move(p));
{ auto with_debug_logging { Logger::Guard(LogLevel::DBG) };
{ Logger::Guard with_debug_logging { LogLevel::DBG };
return plan_taptenc_to_gpp(
taptenc::transformation::transform_plan(
tt_plan,
Expand Down
34 changes: 20 additions & 14 deletions src/tests/gologpp-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/spirit/home/qi/detail/expectation_failure.hpp>

#include <model/platform/semantics.h>

Expand Down Expand Up @@ -102,26 +103,31 @@ int main(int argc, char **argv) {

Logger::instance().log_lvl() = static_cast<LogLevel>(loglevel);

parser::parse_file(filename);
try {
parser::parse_file(filename);
shared_ptr<Procedure> mainproc = global_scope().lookup_global<Procedure>("main");
if (!mainproc) {
log(LogLevel::ERR) << "No procedure main() in " << filename << flush;
return -2;
}

shared_ptr<Procedure> mainproc = global_scope().lookup_global<Procedure>("main");
if (!mainproc) {
log(LogLevel::ERR) << "No procedure main() in " << filename << flush;
return -2;
}
eclipse_opts options;
options.trace = !vm["trace"].empty() || !vm["guitrace"].empty();
options.toplevel = false;
options.guitrace = !vm["guitrace"].empty();

eclipse_opts options;
options.trace = !vm["trace"].empty() || !vm["guitrace"].empty();
options.toplevel = false;
options.guitrace = !vm["guitrace"].empty();
ReadylogContext::init(options);

ReadylogContext::init(options);
int rv = test_file(unique_ptr<Instruction>(mainproc->ref({})));

int rv = test_file(unique_ptr<Instruction>(mainproc->ref({})));
ReadylogContext::shutdown();

ReadylogContext::shutdown();
return rv;
} catch (SyntaxError &e) {
log(LogLevel::ERR) << e.what() << flush;
return -2;
}

return rv;
} catch (boost::bad_any_cast &e) {
desc.print(std::cerr);
return -1;
Expand Down

0 comments on commit 837b997

Please sign in to comment.