From 1a8c768772762ed8f0edb90a78630ee97c02dc0e Mon Sep 17 00:00:00 2001 From: John Doty Date: Sun, 6 Oct 2024 07:44:11 -0700 Subject: [PATCH 1/3] [dingus] Reset the grammar globals every time Now I can rewrite the grammar --- dingus/worker.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dingus/worker.js b/dingus/worker.js index d9dc326..4d508f6 100644 --- a/dingus/worker.js +++ b/dingus/worker.js @@ -80,23 +80,22 @@ import parser.runtime as runtime import pyodide.code import pyodide.ffi as ffi -GRAMMAR_GLOBALS = {} GRAMMAR = None PARSE_TABLE = None LEXER = None def eval_grammar(code): - global GRAMMAR_GLOBALS global GRAMMAR global PARSE_TABLE global LEXER try: dingus.post_grammar_status("Evaluating grammar...") - pyodide.code.eval_code(code, globals=GRAMMAR_GLOBALS) + grammar_globals={} + pyodide.code.eval_code(code, globals=grammar_globals) grammar = None - for key, value in GRAMMAR_GLOBALS.items(): + for key, value in grammar_globals.items(): if isinstance(value, type) and issubclass(value, parser.Grammar) and value is not parser.Grammar: value = value() From 15a7feac078d56dc54ac643fa178884324d5c002 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sun, 6 Oct 2024 08:06:54 -0700 Subject: [PATCH 2/3] [dingus] Color syntax nodes red when they are errors --- dingus/dingus.js | 6 ++++-- dingus/style.css | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dingus/dingus.js b/dingus/dingus.js index ca70cc3..d43f1bd 100644 --- a/dingus/dingus.js +++ b/dingus/dingus.js @@ -72,8 +72,6 @@ let grammar_editor = null; let input_editor = null; function render_parse_results(message) { - console.log("WHAT?"); - function render_tree_node(parent, node) { const tree_div = document.createElement("div"); tree_div.classList.add("parsed-node"); @@ -89,8 +87,12 @@ function render_parse_results(message) { {scroll: true}, ); }; + if (node.start == node.end) { + node_label.classList.add("parsed-error-node"); + } tree_div.appendChild(node_label); + if (node.kind === "tree") { tree_div.classList.add("parsed-tree"); for (const child of node.children) { diff --git a/dingus/style.css b/dingus/style.css index 0b9426f..4ae038f 100644 --- a/dingus/style.css +++ b/dingus/style.css @@ -57,6 +57,10 @@ body { margin-left: 1rem; } +.parsed-error-node { + color: red; +} + .main-textarea { height: 100%; width: 100%; From ba65c1929c2621ea81ffbeb2f478e7a351daa96f Mon Sep 17 00:00:00 2001 From: John Doty Date: Tue, 8 Oct 2024 06:42:54 -0700 Subject: [PATCH 3/3] [dingus] Start to render errors --- dingus/dingus.html | 28 +++++++++++++++---- dingus/dingus.js | 23 +++++++++++++++- dingus/style.css | 68 ++++++++++++++++++++++++++++++++-------------- dingus/worker.js | 35 +++++++++++++++++------- 4 files changed, 118 insertions(+), 36 deletions(-) diff --git a/dingus/dingus.html b/dingus/dingus.html index e15ccca..b5ae407 100644 --- a/dingus/dingus.html +++ b/dingus/dingus.html @@ -15,19 +15,37 @@
-
+
+ Write Your Grammar Here +
+ +
-
+ +
Loading python...
-
+
+ Write In Your Language Here +
+ +
-
-
+
+ Results + + + + +
+ +
+

+        
diff --git a/dingus/dingus.js b/dingus/dingus.js index d43f1bd..9a4c6a9 100644 --- a/dingus/dingus.js +++ b/dingus/dingus.js @@ -1,4 +1,16 @@ const STATUS = document.getElementById("status-line"); +const ERRORS = document.getElementById("error-root"); +const TREE = document.getElementById("tree-root"); + +function show_tree() { + ERRORS.classList.add("hidden"); + TREE.classList.remove("hidden"); +} + +function show_errors() { + ERRORS.classList.remove("hidden"); + TREE.classList.add("hidden"); +} const DOC_CHAINS = {}; function chain_document_submit(kind, editor, on_success) { @@ -34,6 +46,10 @@ function chain_document_submit(kind, editor, on_success) { if (message.status === "ok" && on_success) { on_success(message); } + + if (message.status === "error") { + render_errors(message.errors); + } } DOC_CHAINS[kind] = on_result; @@ -71,6 +87,10 @@ worker.onmessage = (e) => { let grammar_editor = null; let input_editor = null; +function render_errors(errors) { + ERRORS.innerText = errors.join("\n") +} + function render_parse_results(message) { function render_tree_node(parent, node) { const tree_div = document.createElement("div"); @@ -104,9 +124,10 @@ function render_parse_results(message) { parent.appendChild(tree_div); } - const root = document.getElementById("output-root"); + const root = document.getElementById("tree-root"); root.innerHTML = ""; render_tree_node(root, message.tree); + render_errors(message.errors); } function setup_editors() { diff --git a/dingus/style.css b/dingus/style.css index 4ae038f..eb95b4a 100644 --- a/dingus/style.css +++ b/dingus/style.css @@ -8,49 +8,73 @@ body { .page-container { display: grid; grid-template-columns: 1fr 1fr 1fr; - grid-template-rows: 4rem 1fr 2em; + grid-template-rows: 4rem 2rem 1fr 2rem; width: 100%; height: 100%; } -.grammar-container { - grid-column: 1; - grid-row: 2; +.panel { padding: 0.25rem; /* Establishing minimum sizes here causes the codemirror CSS to work */ min-height: 0; min-width: 0; + overflow-y: scroll; + border-left: 1px solid; + border-right: 1px solid; + border-bottom: 1px solid; } -.status { - grid-column: 1 / 3; +.panel-top { + padding: 0.5rem; + border-bottom: 1px dashed; + border-right: 1px solid; + border-left: 1px solid; + border-top: 1px solid; +} + +.grammar-title { + grid-column: 1; + grid-row: 2; +} + +.grammar-container { + grid-column: 1; grid-row: 3; - padding: 0.25rem; +} + +.input-title { + grid-column: 2; + grid-row: 2; } .input-container { grid-column: 2; - grid-row: 2; - padding: 0.25rem; - - /* Establishing minimum sizes here causes the codemirror CSS to work */ - min-height: 0; - min-width: 0; + grid-row: 3; } -.input-status { - grid-column: 2; - grid-row: 3; - padding: 0.25rem; +.result-selector { + position: relative; + grid-column: 3; + grid-row: 2; +} + +.result-selector-buttons { + position: absolute; + top: 0.25rem; + right: 1rem; } .results-container { grid-column: 3; - grid-row: 2; - padding: 0.25rem; + grid-row: 3; +} - overflow-y: scroll; +.status { + grid-column: 1 / 4; + grid-row: 4; + padding: 0.25rem; + border: 1px solid; } .parsed-node { @@ -66,6 +90,10 @@ body { width: 100%; } +.hidden { + display: none; +} + .CodeMirror { height: 100%; } diff --git a/dingus/worker.js b/dingus/worker.js index 4d508f6..adeb615 100644 --- a/dingus/worker.js +++ b/dingus/worker.js @@ -31,9 +31,15 @@ const dingus_module = { postMessage({kind: "grammar", status: "ok", message: msg}); }, - post_grammar_error: function(error) { - console.log("Grammar Error:", error); - postMessage({kind:"grammar", status: "error", message: error}); + post_grammar_error: function(errors) { + errors = data_to_js(errors); + console.log("Grammar Error:", errors); + postMessage({ + kind:"grammar", + status: "error", + message: "An error occurred loading the grammar", + errors: errors, + }); }, post_doc_parse: function(tree, errors) { @@ -50,9 +56,15 @@ const dingus_module = { }); }, - post_doc_error: function(error) { - console.log("Doc Error:", error); - postMessage({kind:"input", status: "error", message: error}); + post_doc_error: function(errors) { + errors = data_to_js(errors); + console.log("Doc Error:", errors); + postMessage({ + kind:"input", + status: "error", + message: "An error occurred parsing the document", + errors: errors, + }); }, }; @@ -91,6 +103,7 @@ def eval_grammar(code): try: dingus.post_grammar_status("Evaluating grammar...") + print("Hey?") grammar_globals={} pyodide.code.eval_code(code, globals=grammar_globals) @@ -119,8 +132,9 @@ def eval_grammar(code): dingus.post_grammar_loaded(grammar.name) except Exception as e: - traceback.print_exc() - dingus.post_grammar_error(f"{e}") + ohno = traceback.format_exc() + print(f"grammar: {ohno}") + dingus.post_grammar_error(ohno.splitlines()) def tree_to_js(tree): if tree is None: @@ -151,8 +165,9 @@ def eval_document(code): tree, errors = runtime.parse(PARSE_TABLE, LEXER, code) dingus.post_doc_parse(tree_to_js(tree), errors) except Exception as e: - traceback.print_exc() - dingus.post_doc_error(f"{e}") + ohno = traceback.format_exc() + print(f"doc: {ohno}") + dingus.post_doc_error(ohno.splitlines()) `); dingus_module.post_grammar_status("Ready.");