mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-11-14 15:05:29 +00:00
clangformat
This commit is contained in:
parent
fdfd1f4267
commit
95f9140464
@ -5,18 +5,15 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <peglib.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <peglib.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
inline bool read_file(const char* path, vector<char>& buff)
|
inline bool read_file(const char *path, vector<char> &buff) {
|
||||||
{
|
|
||||||
ifstream ifs(path, ios::in | ios::binary);
|
ifstream ifs(path, ios::in | ios::binary);
|
||||||
if (ifs.fail()) {
|
if (ifs.fail()) { return false; }
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
|
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
|
||||||
if (!buff.empty()) {
|
if (!buff.empty()) {
|
||||||
@ -25,8 +22,7 @@ inline bool read_file(const char* path, vector<char>& buff)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char** argv)
|
int main(int argc, const char **argv) {
|
||||||
{
|
|
||||||
auto opt_ast = false;
|
auto opt_ast = false;
|
||||||
auto opt_optimize_ast_nodes = false;
|
auto opt_optimize_ast_nodes = false;
|
||||||
auto opt_help = false;
|
auto opt_help = false;
|
||||||
@ -42,7 +38,8 @@ int main(int argc, const char** argv)
|
|||||||
opt_help = true;
|
opt_help = true;
|
||||||
} else if (string("--ast") == arg) {
|
} else if (string("--ast") == arg) {
|
||||||
opt_ast = true;
|
opt_ast = true;
|
||||||
} else if (string("--optimize_ast_nodes") == arg || string("--opt") == arg) {
|
} else if (string("--optimize_ast_nodes") == arg ||
|
||||||
|
string("--opt") == arg) {
|
||||||
opt_optimize_ast_nodes = true;
|
opt_optimize_ast_nodes = true;
|
||||||
} else if (string("--source") == arg) {
|
} else if (string("--source") == arg) {
|
||||||
opt_source = true;
|
opt_source = true;
|
||||||
@ -58,7 +55,9 @@ int main(int argc, const char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path_list.empty() || opt_help) {
|
if (path_list.empty() || opt_help) {
|
||||||
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source text] [--trace] [grammar file path] [source file path]" << endl;
|
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source "
|
||||||
|
"text] [--trace] [grammar file path] [source file path]"
|
||||||
|
<< endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,13 +76,9 @@ int main(int argc, const char** argv)
|
|||||||
cerr << syntax_path << ":" << ln << ":" << col << ": " << msg << endl;
|
cerr << syntax_path << ":" << ln << ":" << col << ": " << msg << endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!parser.load_grammar(syntax.data(), syntax.size())) {
|
if (!parser.load_grammar(syntax.data(), syntax.size())) { return -1; }
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path_list.size() < 2 && !opt_source) {
|
if (path_list.size() < 2 && !opt_source) { return 0; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check source
|
// Check source
|
||||||
std::string source_path = "[commandline]";
|
std::string source_path = "[commandline]";
|
||||||
@ -102,38 +97,30 @@ int main(int argc, const char** argv)
|
|||||||
if (opt_trace) {
|
if (opt_trace) {
|
||||||
size_t prev_pos = 0;
|
size_t prev_pos = 0;
|
||||||
parser.enable_trace(
|
parser.enable_trace(
|
||||||
[&](const char* name,
|
[&](const char *name, const char *s, size_t /*n*/,
|
||||||
const char* s,
|
const peg::SemanticValues & /*sv*/, const peg::Context &c,
|
||||||
size_t /*n*/,
|
|
||||||
const peg::SemanticValues& /*sv*/,
|
|
||||||
const peg::Context& c,
|
|
||||||
const peg::any & /*dt*/) {
|
const peg::any & /*dt*/) {
|
||||||
auto pos = static_cast<size_t>(s - c.s);
|
auto pos = static_cast<size_t>(s - c.s);
|
||||||
auto backtrack = (pos < prev_pos ? "*" : "");
|
auto backtrack = (pos < prev_pos ? "*" : "");
|
||||||
string indent;
|
string indent;
|
||||||
auto level = c.trace_ids.size() - 1;
|
auto level = c.trace_ids.size() - 1;
|
||||||
while (level--) { indent += "│"; }
|
while (level--) {
|
||||||
std::cout
|
indent += "│";
|
||||||
<< "E " << pos << backtrack << "\t"
|
}
|
||||||
<< indent << "┌" << name
|
std::cout << "E " << pos << backtrack << "\t" << indent << "┌" << name
|
||||||
<< " #" << c.trace_ids.back()
|
<< " #" << c.trace_ids.back() << std::endl;
|
||||||
<< std::endl;
|
|
||||||
prev_pos = static_cast<size_t>(pos);
|
prev_pos = static_cast<size_t>(pos);
|
||||||
},
|
},
|
||||||
[&](const char* name,
|
[&](const char *name, const char *s, size_t /*n*/,
|
||||||
const char* s,
|
const peg::SemanticValues &sv, const peg::Context &c,
|
||||||
size_t /*n*/,
|
const peg::any & /*dt*/, size_t len) {
|
||||||
const peg::SemanticValues& sv,
|
|
||||||
const peg::Context& c,
|
|
||||||
const peg::any& /*dt*/,
|
|
||||||
size_t len) {
|
|
||||||
auto pos = static_cast<size_t>(s - c.s);
|
auto pos = static_cast<size_t>(s - c.s);
|
||||||
if (len != static_cast<size_t>(-1)) {
|
if (len != static_cast<size_t>(-1)) { pos += len; }
|
||||||
pos += len;
|
|
||||||
}
|
|
||||||
string indent;
|
string indent;
|
||||||
auto level = c.trace_ids.size() - 1;
|
auto level = c.trace_ids.size() - 1;
|
||||||
while (level--) { indent += "│"; }
|
while (level--) {
|
||||||
|
indent += "│";
|
||||||
|
}
|
||||||
auto ret = len != static_cast<size_t>(-1) ? "└o " : "└x ";
|
auto ret = len != static_cast<size_t>(-1) ? "└o " : "└x ";
|
||||||
std::stringstream choice;
|
std::stringstream choice;
|
||||||
if (sv.choice_count() > 0) {
|
if (sv.choice_count() > 0) {
|
||||||
@ -144,32 +131,22 @@ int main(int argc, const char** argv)
|
|||||||
const auto &tok = sv.tokens[0];
|
const auto &tok = sv.tokens[0];
|
||||||
token += " '" + std::string(tok.first, tok.second) + "'";
|
token += " '" + std::string(tok.first, tok.second) + "'";
|
||||||
}
|
}
|
||||||
std::cout
|
std::cout << "L " << pos << "\t" << indent << ret << name << " #"
|
||||||
<< "L " << pos << "\t"
|
<< c.trace_ids.back() << choice.str() << token << std::endl;
|
||||||
<< indent << ret << name
|
});
|
||||||
<< " #" << c.trace_ids.back()
|
|
||||||
<< choice.str()
|
|
||||||
<< token
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opt_ast) {
|
if (opt_ast) {
|
||||||
parser.enable_ast();
|
parser.enable_ast();
|
||||||
|
|
||||||
std::shared_ptr<peg::Ast> ast;
|
std::shared_ptr<peg::Ast> ast;
|
||||||
if (!parser.parse_n(source.data(), source.size(), ast)) {
|
if (!parser.parse_n(source.data(), source.size(), ast)) { return -1; }
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
|
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
|
||||||
std::cout << peg::ast_to_s(ast);
|
std::cout << peg::ast_to_s(ast);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!parser.parse_n(source.data(), source.size())) {
|
if (!parser.parse_n(source.data(), source.size())) { return -1; }
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user