mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2025-01-22 21:35:29 +00:00
Better error report.
This commit is contained in:
parent
a7c2de1c32
commit
f3f73e5c35
@ -1,5 +1,6 @@
|
|||||||
#include "interpreter.hpp"
|
#include "interpreter.hpp"
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using namespace peglib;
|
using namespace peglib;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -187,19 +188,28 @@ std::ostream& operator<<(std::ostream& os, const Value& val)
|
|||||||
return val.out(os);
|
return val.out(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool run(Env& env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast)
|
bool run(const string& path, Env& env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
shared_ptr<Ast> ast;
|
shared_ptr<Ast> ast;
|
||||||
if (get_parser().parse_n(expr, len, ast)) {
|
|
||||||
|
auto& parser = get_parser();
|
||||||
|
|
||||||
|
parser.log = [&](size_t ln, size_t col, const string& err_msg) {
|
||||||
|
stringstream ss;
|
||||||
|
ss << path << ":" << ln << ":" << col << ": " << err_msg << endl;
|
||||||
|
msg = ss.str();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (parser.parse_n(expr, len, ast)) {
|
||||||
if (print_ast) {
|
if (print_ast) {
|
||||||
ast->print();
|
ast->print();
|
||||||
}
|
}
|
||||||
|
|
||||||
val = Eval::eval(*ast, env);
|
val = Eval::eval(*ast, env);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (exception& e) {
|
} catch (runtime_error& e) {
|
||||||
msg = e.what();
|
msg = e.what();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,4 +193,4 @@ private:
|
|||||||
std::map<std::string, Value> dic_;
|
std::map<std::string, Value> dic_;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool run(Env& env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast);
|
bool run(const std::string& path, Env& env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast);
|
||||||
|
@ -44,27 +44,31 @@ int main(int argc, const char** argv)
|
|||||||
shell = path_list.empty();
|
shell = path_list.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
Env env;
|
try {
|
||||||
env.setup_built_in_functions();
|
Env env;
|
||||||
|
env.setup_built_in_functions();
|
||||||
|
|
||||||
for (auto path: path_list) {
|
for (auto path: path_list) {
|
||||||
vector<char> buff;
|
vector<char> buff;
|
||||||
if (!read_file(path, buff)) {
|
if (!read_file(path, buff)) {
|
||||||
cerr << "can't open '" << path << "'." << endl;
|
cerr << "can't open '" << path << "'." << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value val;
|
||||||
|
string msg;
|
||||||
|
if (!run(path, env, buff.data(), buff.size(), val, msg, print_ast)) {
|
||||||
|
cerr << msg;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value val;
|
if (shell) {
|
||||||
string msg;
|
repl(env, print_ast);
|
||||||
if (!run(env, buff.data(), buff.size(), val, msg, print_ast)) {
|
|
||||||
cerr << "error in '" << path << "'." << endl;
|
|
||||||
cerr << msg.c_str() << endl;
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
} catch (exception& e) {
|
||||||
|
cerr << e.what() << endl;
|
||||||
if (shell) {
|
return -1;
|
||||||
repl(env, print_ast);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -40,33 +40,46 @@ static auto g_grammar = R"(
|
|||||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
||||||
)";
|
)";
|
||||||
|
|
||||||
static auto g_parser = peg(g_grammar)
|
peglib::peg& get_parser()
|
||||||
.ast_node_optimizable("STATEMENTS", Statements)
|
|
||||||
.ast_node("WHILE", While)
|
|
||||||
.ast_node("ASSIGNMENT", Assignment)
|
|
||||||
.ast_node("IF", If)
|
|
||||||
.ast_node("FUNCTION", Function)
|
|
||||||
.ast_node("PARAMETERS")
|
|
||||||
.ast_node("FUNCTION_CALL", FunctionCall)
|
|
||||||
.ast_node("ARGUMENTS")
|
|
||||||
.ast_node_optimizable("PRIMARY", Condition)
|
|
||||||
.ast_node_optimizable("CONDITION", BinExpresion)
|
|
||||||
.ast_node_optimizable("TERM", BinExpresion)
|
|
||||||
.ast_token("CONDITION_OPERATOR")
|
|
||||||
.ast_token("TERM_OPERATOR")
|
|
||||||
.ast_token("FACTOR_OPERATOR")
|
|
||||||
.ast_token("NUMBER", Number)
|
|
||||||
.ast_token("BOOLEAN", Boolean)
|
|
||||||
.ast_token("STRING")
|
|
||||||
.ast_token("IDENTIFIER", Identifier)
|
|
||||||
.ast_end()
|
|
||||||
.set_logger([&](size_t ln, size_t col, const string& msg) {
|
|
||||||
cerr << ln << ":" << col << ": " << msg << endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
const peglib::peg& get_parser()
|
|
||||||
{
|
{
|
||||||
return g_parser;
|
static peg parser;
|
||||||
|
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
initialized = true;
|
||||||
|
|
||||||
|
parser.log = [&](size_t ln, size_t col, const string& msg) {
|
||||||
|
cerr << ln << ":" << col << ": " << msg << endl;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!parser.load_grammar(g_grammar)) {
|
||||||
|
throw logic_error("invalid peg grammar");
|
||||||
|
}
|
||||||
|
|
||||||
|
parser
|
||||||
|
.ast_node_optimizable("STATEMENTS", Statements)
|
||||||
|
.ast_node("WHILE", While)
|
||||||
|
.ast_node("ASSIGNMENT", Assignment)
|
||||||
|
.ast_node("IF", If)
|
||||||
|
.ast_node("FUNCTION", Function)
|
||||||
|
.ast_node("PARAMETERS")
|
||||||
|
.ast_node("FUNCTION_CALL", FunctionCall)
|
||||||
|
.ast_node("ARGUMENTS")
|
||||||
|
.ast_node_optimizable("PRIMARY", Condition)
|
||||||
|
.ast_node_optimizable("CONDITION", BinExpresion)
|
||||||
|
.ast_node_optimizable("TERM", BinExpresion)
|
||||||
|
.ast_token("CONDITION_OPERATOR")
|
||||||
|
.ast_token("TERM_OPERATOR")
|
||||||
|
.ast_token("FACTOR_OPERATOR")
|
||||||
|
.ast_token("NUMBER", Number)
|
||||||
|
.ast_token("BOOLEAN", Boolean)
|
||||||
|
.ast_token("STRING")
|
||||||
|
.ast_token("IDENTIFIER", Identifier)
|
||||||
|
.ast_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
return parser;
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||||
|
@ -6,6 +6,6 @@ enum AstType
|
|||||||
Identifier, Number, Boolean, Function
|
Identifier, Number, Boolean, Function
|
||||||
};
|
};
|
||||||
|
|
||||||
const peglib::peg& get_parser();
|
peglib::peg& get_parser();
|
||||||
|
|
||||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||||
|
@ -16,11 +16,11 @@ int repl(Env& env, bool print_ast)
|
|||||||
if (!line.empty()) {
|
if (!line.empty()) {
|
||||||
Value val;
|
Value val;
|
||||||
string msg;
|
string msg;
|
||||||
if (run(env, line.c_str(), line.size(), val, msg, print_ast)) {
|
if (run("(repl)", env, line.c_str(), line.size(), val, msg, print_ast)) {
|
||||||
cout << val << endl;
|
cout << val << endl;
|
||||||
linenoise::AddHistory(line.c_str());
|
linenoise::AddHistory(line.c_str());
|
||||||
} else if (!msg.empty()) {
|
} else if (!msg.empty()) {
|
||||||
cout << msg << endl;
|
cout << msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user