Changed to show AST results.

pull/13/head
Yuji Hirose 9 years ago
parent 98c6f7d6d6
commit adcd259268
  1. 41
      lint/online/main.cc
  2. 1
      lint/online/playground.vcxproj
  3. 45
      lint/online/www/index.html

@ -33,15 +33,39 @@ bool parse_grammar(const string& text, peg::parser& peg, string& json)
return ret;
}
bool parse_code(const string& text, peg::parser& peg, string& json)
bool parse_code(const string& text, peg::parser& peg, string& json, shared_ptr<peg::Ast>& ast)
{
peg.enable_ast();
peg.log = makeJSONFormatter(json);
json += "[";
auto ret = peg.parse_n(text.data(), text.size());
auto ret = peg.parse_n(text.data(), text.size(), ast);
json += "]";
return ret;
}
template <typename T>
void dump_ast(const shared_ptr<T>& ptr, string& json, int level = 0)
{
const auto& ast = *ptr;
for (auto i = 0; i < level; i++) {
json += " ";
}
string name;
if (ast.name == ast.original_name) {
name = ast.name;
} else {
name = ast.original_name + " (" + ast.name + ")";
}
if (ast.is_token) {
json += "- " + name + "(" + ast.token + ")\\n";
} else {
json += "+ " + name +"\\n";
}
for (auto node : ast.nodes) {
dump_ast(node, json, level + 1);
}
}
int main(void)
{
Server svr;
@ -56,21 +80,28 @@ int main(void)
string grammarResult;
string codeResult;
string astResult;
string astResultOptimized;
peg::parser peg;
auto ret = parse_grammar(grammarText, peg, grammarResult);
if (ret && peg) {
const auto& codeText = req.params.at("code");
parse_code(codeText, peg, codeResult);
shared_ptr<peg::Ast> ast;
if (parse_code(codeText, peg, codeResult, ast)) {
dump_ast(ast, astResult);
dump_ast(peg::AstOptimizer(true).optimize(ast), astResultOptimized);
}
}
string json;
json += "{";
json += "\"grammar\":" + grammarResult;
if (!codeResult.empty()) {
json += +",";
json += "\"code\":" + codeResult;
json += ",\"code\":" + codeResult;
json += ",\"ast\":\"" + astResult + "\"";
json += ",\"astOptimized\":\"" + astResultOptimized + "\"";
}
json += "}";

@ -14,6 +14,7 @@
<ClCompile Include="main.cc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\peglib.h" />
<ClInclude Include="httplib.h" />
</ItemGroup>
<ItemGroup>

@ -89,13 +89,13 @@ body {
<li><span id="grammar-validation" class="editor-validation">Valid</span></li>
</ul>
<pre id="grammar-editor" class="editor-area">
EXPRESSION <- _ TERM (TERM_OPERATOR TERM)*
EXPRESSION <- TERM (TERM_OPERATOR TERM)*
TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
FACTOR <- NUMBER / '(' _ EXPRESSION ')' _
TERM_OPERATOR <- < [-+] > _
FACTOR_OPERATOR <- < [/*] > _
NUMBER <- < [0-9]+ > _
~_ <- [ \t\r\n]*</pre>
FACTOR <- NUMBER / '(' EXPRESSION ')'
TERM_OPERATOR <- [-+]
FACTOR_OPERATOR <- [/*]
NUMBER <- [0-9]+
%whitespace <- [ \t\r\n]*</pre>
<div id="grammar-info" class="editor-info"></div>
</div>
<div class="editor-container">
@ -103,7 +103,9 @@ NUMBER <- < [0-9]+ > _
<li><span>Code:</span></li>
<li><span id="code-validation" class="editor-validation">Valid</span></li>
</ul>
<pre id="code-editor" class="editor-area">2 * (3 + 4)</pre>
<pre id="code-editor" class="editor-area">2 * (3 + 4)</pre>
<pre id="code-ast" class="editor-area"></pre>
<pre id="code-ast-optimized" class="editor-area"></pre>
<div id="code-info" class="editor-info"></div>
</div>
</div>
@ -118,6 +120,24 @@ NUMBER <- < [0-9]+ > _
var code = ace.edit("code-editor");
code.setShowPrintMargin(false);
var codeAst = ace.edit("code-ast");
codeAst.setShowPrintMargin(false);
codeAst.setOptions({
readOnly: true,
highlightActiveLine: false,
highlightGutterLine: false
})
codeAst.renderer.$cursorLayer.element.style.opacity=0;
var codeAstOptimized = ace.edit("code-ast-optimized");
codeAstOptimized.setShowPrintMargin(false);
codeAstOptimized.setOptions({
readOnly: true,
highlightActiveLine: false,
highlightGutterLine: false
})
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
var generateErrorListHTML = function (errors) {
var html = '<ul>';
@ -148,15 +168,20 @@ NUMBER <- < [0-9]+ > _
$grammarInfo.html('');
$grammarValidation.removeClass('editor-validation-invalid').text('Valid');
var isValid = data.code.length === 0;
codeAst.setValue('');
codeAstOptimized.setValue('');
var isValid = data.code.length === 0;
if (isValid) {
$codeInfo.html('');
$codeValidation.removeClass('editor-validation-invalid').text('Valid');
} else {
codeAst.insert(data.ast);
codeAstOptimized.insert(data.astOptimized);
} else {
var html = generateErrorListHTML(data.code);
$codeInfo.html(html);
$codeValidation.addClass('editor-validation-invalid').text('Invalid');
}
}
$codeValidation.show();
} else {
var html = generateErrorListHTML(data.grammar);

Loading…
Cancel
Save