[dingus] Render the output

This commit is contained in:
John Doty 2024-10-01 18:47:37 -07:00
parent 0620ae0be5
commit 5a84086469
4 changed files with 62 additions and 10 deletions

View file

@ -27,7 +27,7 @@
</div> </div>
<div class="results-container"> <div class="results-container">
<textarea readonly id="output" name="output" class="main-textarea"></textarea> <div id="output-root" />
</div> </div>
</div> </div>

View file

@ -12,7 +12,8 @@ function chain_document_submit(kind, editor, on_success) {
let pending = null; let pending = null;
let next = null; let next = null;
function do_submit(document) { function do_submit() {
const document = editor.doc.getValue();
if (window.localStorage) { if (window.localStorage) {
window.localStorage.setItem(kind, document); window.localStorage.setItem(kind, document);
} }
@ -36,7 +37,8 @@ function chain_document_submit(kind, editor, on_success) {
worker.postMessage({kind, data: document}); worker.postMessage({kind, data: document});
console.log("Posted another document"); console.log("Posted another document");
} }
if (message.kind === "ok" && on_success) {
if (message.status === "ok" && on_success) {
on_success(message); on_success(message);
} }
} }
@ -47,7 +49,7 @@ function chain_document_submit(kind, editor, on_success) {
clearTimeout(change_timer_id); clearTimeout(change_timer_id);
change_timer_id = setTimeout(() => { change_timer_id = setTimeout(() => {
change_timer_id = null; change_timer_id = null;
do_submit(editor.doc.getValue()); do_submit();
}, 100); }, 100);
}); });
@ -57,6 +59,8 @@ function chain_document_submit(kind, editor, on_success) {
editor.doc.setValue(value); editor.doc.setValue(value);
} }
} }
return do_submit;
} }
const worker = new Worker('worker.js'); const worker = new Worker('worker.js');
@ -71,28 +75,66 @@ worker.onmessage = (e) => {
STATUS.innerText = message.message; STATUS.innerText = message.message;
}; };
let grammar_editor = null;
let input_editor = null;
function render_parse_results(message) { function render_parse_results(message) {
// What console.log("WHAT?");
function render_tree_node(parent, node) {
const tree_div = document.createElement("div");
tree_div.classList.add("parsed-node");
const node_label = document.createElement("a");
node_label.innerText = node.name;
node_label.setAttribute("href", "#");
node_label.onclick = () => {
const doc = input_editor.doc;
doc.setSelection(
doc.posFromIndex(node.start),
doc.posFromIndex(node.end),
{scroll: true},
);
};
tree_div.appendChild(node_label);
if (node.kind === "tree") {
tree_div.classList.add("parsed-tree");
for (const child of node.children) {
render_tree_node(tree_div, child);
}
} else {
tree_div.classList.add("parsed-token");
}
parent.appendChild(tree_div);
}
const root = document.getElementById("output-root");
root.innerHTML = "";
render_tree_node(root, message.tree);
} }
function setup_editors() { function setup_editors() {
const grammar_editor = CodeMirror.fromTextArea( grammar_editor = CodeMirror.fromTextArea(
document.getElementById("grammar"), document.getElementById("grammar"),
{ {
lineNumbers: true, lineNumbers: true,
mode: "python", mode: "python",
value: "from parser import Grammar\n\nclass MyGrammar(Grammar):\n pass\n",
}, },
); );
chain_document_submit("grammar", grammar_editor);
const input_editor = CodeMirror.fromTextArea( input_editor = CodeMirror.fromTextArea(
document.getElementById("input"), document.getElementById("input"),
{ {
lineNumbers: true, lineNumbers: true,
}, },
); );
chain_document_submit("input", input_editor, render_parse_results);
const submit_input = chain_document_submit(
"input", input_editor, render_parse_results);
chain_document_submit(
"grammar", grammar_editor, submit_input);
} }
setup_editors(); setup_editors();

View file

@ -49,6 +49,12 @@ body {
grid-column: 3; grid-column: 3;
grid-row: 2; grid-row: 2;
padding: 0.25rem; padding: 0.25rem;
overflow-y: scroll;
}
.parsed-node {
margin-left: 1rem;
} }
.main-textarea { .main-textarea {

View file

@ -118,6 +118,7 @@ def eval_grammar(code):
LEXER = grammar.compile_lexer() LEXER = grammar.compile_lexer()
dingus.post_grammar_loaded(grammar.name) dingus.post_grammar_loaded(grammar.name)
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()
dingus.post_grammar_error(f"{e}") dingus.post_grammar_error(f"{e}")
@ -125,6 +126,7 @@ def eval_grammar(code):
def tree_to_js(tree): def tree_to_js(tree):
if tree is None: if tree is None:
return None return None
elif isinstance(tree, runtime.Tree): elif isinstance(tree, runtime.Tree):
return { return {
"kind": "tree", "kind": "tree",
@ -133,9 +135,11 @@ def tree_to_js(tree):
"end": tree.end, "end": tree.end,
"children": [tree_to_js(child) for child in tree.children], "children": [tree_to_js(child) for child in tree.children],
} }
else: else:
return { return {
"kind": "token", "kind": "token",
"name": tree.kind,
"start": tree.start, "start": tree.start,
"end": tree.end, "end": tree.end,
} }