Fixed PEG payground problem when parsed size is less than the actual text size

This commit is contained in:
yhirose 2021-04-24 17:21:07 -04:00
parent 02d009777c
commit 4109480a0c
4 changed files with 15 additions and 8 deletions

View File

@ -84,12 +84,16 @@ function parse() {
codeAst.insert(data.ast); codeAst.insert(data.ast);
codeAstOptimized.insert(data.astOptimized); codeAstOptimized.insert(data.astOptimized);
$codeValidation.removeClass('editor-validation-invalid').text('Valid').show();
if (data.source_valid) {
$codeValidation.removeClass('editor-validation-invalid').text('Valid').show();
} else {
$codeValidation.addClass('editor-validation-invalid').text('Invalid').show();
}
if (data.code.length > 0) { if (data.code.length > 0) {
const html = generateErrorListHTML(data.code); const html = generateErrorListHTML(data.code);
$codeInfo.html(html); $codeInfo.html(html);
$codeValidation.addClass('editor-validation-invalid').text('Invalid').show();
} }
} else { } else {
$grammarValidation.addClass('editor-validation-invalid').text('Invalid').show(); $grammarValidation.addClass('editor-validation-invalid').text('Invalid').show();

View File

@ -43,7 +43,7 @@ bool parse_grammar(const std::string &text, peg::parser &peg,
return ret; return ret;
} }
void parse_code(const std::string &text, peg::parser &peg, std::string &json, bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
std::shared_ptr<peg::Ast> &ast) { std::shared_ptr<peg::Ast> &ast) {
peg.enable_ast(); peg.enable_ast();
bool init; bool init;
@ -51,6 +51,7 @@ void parse_code(const std::string &text, peg::parser &peg, std::string &json,
json += "["; json += "[";
auto ret = peg.parse_n(text.data(), text.size(), ast); auto ret = peg.parse_n(text.data(), text.size(), ast);
json += "]"; json += "]";
return ret;
} }
std::string lint(const std::string &grammarText, const std::string &codeText, bool opt_mode) { std::string lint(const std::string &grammarText, const std::string &codeText, bool opt_mode) {
@ -60,11 +61,12 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
std::string astResultOptimized; std::string astResultOptimized;
peg::parser peg; peg::parser peg;
auto ret = parse_grammar(grammarText, peg, grammarResult); auto is_grammar_valid = parse_grammar(grammarText, peg, grammarResult);
auto is_source_valid = false;
if (ret && peg) { if (is_grammar_valid && peg) {
std::shared_ptr<peg::Ast> ast; std::shared_ptr<peg::Ast> ast;
parse_code(codeText, peg, codeResult, ast); is_source_valid = parse_code(codeText, peg, codeResult, ast);
if (ast) { if (ast) {
astResult = escape_json(peg::ast_to_s(ast)); astResult = escape_json(peg::ast_to_s(ast));
astResultOptimized = escape_json( astResultOptimized = escape_json(
@ -74,8 +76,9 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
std::string json; std::string json;
json += "{"; json += "{";
json += std::string("\"grammar_valid\":") + (ret ? "true" : "false"); json += std::string("\"grammar_valid\":") + (is_grammar_valid ? "true" : "false");
json += ",\"grammar\":" + grammarResult; json += ",\"grammar\":" + grammarResult;
json += std::string(",\"source_valid\":") + (is_source_valid ? "true" : "false");
if (!codeResult.empty()) { if (!codeResult.empty()) {
json += ",\"code\":" + codeResult; json += ",\"code\":" + codeResult;
json += ",\"ast\":\"" + astResult + "\""; json += ",\"ast\":\"" + astResult + "\"";

File diff suppressed because one or more lines are too long

Binary file not shown.