[dingus] Parse and report status
This commit is contained in:
parent
efc6eac842
commit
bcf1bbb948
2 changed files with 126 additions and 13 deletions
|
|
@ -1,6 +1,10 @@
|
||||||
|
// TODO: Abstract/Factor
|
||||||
let pending_grammar = null;
|
let pending_grammar = null;
|
||||||
let next_grammar = null;
|
let next_grammar = null;
|
||||||
|
|
||||||
|
let pending_document = null;
|
||||||
|
let next_document = null;
|
||||||
|
|
||||||
const STATUS = document.getElementById("status-line");
|
const STATUS = document.getElementById("status-line");
|
||||||
|
|
||||||
function submit_grammar(code) {
|
function submit_grammar(code) {
|
||||||
|
|
@ -14,6 +18,17 @@ function submit_grammar(code) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function submit_document(code) {
|
||||||
|
if (pending_document) {
|
||||||
|
console.log("Document still pending, parking it");
|
||||||
|
next_document = code;
|
||||||
|
} else {
|
||||||
|
pending_document = code;
|
||||||
|
worker.postMessage({kind: "document", data: code});
|
||||||
|
console.log("Document posted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const worker = new Worker('worker.js');
|
const worker = new Worker('worker.js');
|
||||||
worker.onmessage = (e) => {
|
worker.onmessage = (e) => {
|
||||||
const message = e.data;
|
const message = e.data;
|
||||||
|
|
@ -31,6 +46,21 @@ worker.onmessage = (e) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.kind === "doc_status") {
|
||||||
|
STATUS.innerText = message.message;
|
||||||
|
|
||||||
|
if ((message.status === "ok") || (message.status === "error")) {
|
||||||
|
pending_document = null;
|
||||||
|
if (next_document) {
|
||||||
|
pending_document = next_document;
|
||||||
|
next_document = null;
|
||||||
|
|
||||||
|
worker.postMessage({kind: "document", data: pending_document});
|
||||||
|
console.log("Posted another document");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -44,11 +74,11 @@ function setup_editors() {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let change_timer_id = null;
|
let grammar_change_timer_id = null;
|
||||||
grammar_editor.doc.on("change", () => {
|
grammar_editor.doc.on("change", () => {
|
||||||
clearTimeout(change_timer_id);
|
clearTimeout(grammar_change_timer_id);
|
||||||
change_timer_id = setTimeout(() => {
|
grammar_change_timer_id = setTimeout(() => {
|
||||||
change_timer_id = null;
|
grammar_change_timer_id = null;
|
||||||
submit_grammar(grammar_editor.doc.getValue());
|
submit_grammar(grammar_editor.doc.getValue());
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
|
|
@ -59,6 +89,15 @@ function setup_editors() {
|
||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let input_change_timer_id = null;
|
||||||
|
input_editor.doc.on("change", () => {
|
||||||
|
clearTimeout(input_change_timer_id);
|
||||||
|
input_change_timer_id = setTimeout(() => {
|
||||||
|
input_change_timer_id = null;
|
||||||
|
submit_document(input_editor.doc.getValue());
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_editors();
|
setup_editors();
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,21 @@ const PARSER_PACKAGE = "./wheel/lrparsers-0.7.9-py3-none-any.whl"
|
||||||
// Load the whole pyodide thingy.
|
// Load the whole pyodide thingy.
|
||||||
importScripts("pyodide/pyodide.js");
|
importScripts("pyodide/pyodide.js");
|
||||||
|
|
||||||
|
function data_to_js(thing) {
|
||||||
|
if (thing.toJs) {
|
||||||
|
pyproxies = [];
|
||||||
|
thing = thing.toJs({
|
||||||
|
pyproxies,
|
||||||
|
dict_converter: Object.fromEntries,
|
||||||
|
create_pyproxies: false,
|
||||||
|
});
|
||||||
|
for (p of pyproxies) {
|
||||||
|
p.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return thing;
|
||||||
|
}
|
||||||
|
|
||||||
const dingus_module = {
|
const dingus_module = {
|
||||||
post_grammar_status: function (message) {
|
post_grammar_status: function (message) {
|
||||||
console.log("Grammar Status:", message);
|
console.log("Grammar Status:", message);
|
||||||
|
|
@ -20,6 +35,25 @@ const dingus_module = {
|
||||||
console.log("Grammar Error:", error);
|
console.log("Grammar Error:", error);
|
||||||
postMessage({kind:"grammar_status", status: "error", message: error});
|
postMessage({kind:"grammar_status", status: "error", message: error});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
post_doc_parse: function(tree, errors) {
|
||||||
|
tree = data_to_js(tree);
|
||||||
|
errors = data_to_js(errors);
|
||||||
|
|
||||||
|
console.log("Doc parse:", tree, errors);
|
||||||
|
postMessage({
|
||||||
|
kind: "doc_status",
|
||||||
|
status: "ok",
|
||||||
|
message: "Parsed",
|
||||||
|
errors: errors,
|
||||||
|
tree: tree,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
post_doc_error: function(error) {
|
||||||
|
console.log("Doc Error:", error);
|
||||||
|
postMessage({kind:"doc_status", status: "error", message: error});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
async function setup_python() {
|
async function setup_python() {
|
||||||
|
|
@ -38,9 +72,13 @@ async function setup_python() {
|
||||||
pyodide.registerJsModule("dingus", dingus_module);
|
pyodide.registerJsModule("dingus", dingus_module);
|
||||||
|
|
||||||
pyodide.runPython(`
|
pyodide.runPython(`
|
||||||
|
import traceback
|
||||||
|
|
||||||
import dingus
|
import dingus
|
||||||
import parser
|
import parser
|
||||||
|
import parser.runtime as runtime
|
||||||
import pyodide.code
|
import pyodide.code
|
||||||
|
import pyodide.ffi as ffi
|
||||||
|
|
||||||
GRAMMAR_GLOBALS = {}
|
GRAMMAR_GLOBALS = {}
|
||||||
GRAMMAR = None
|
GRAMMAR = None
|
||||||
|
|
@ -81,7 +119,37 @@ def eval_grammar(code):
|
||||||
|
|
||||||
dingus.post_grammar_loaded(grammar.name)
|
dingus.post_grammar_loaded(grammar.name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
dingus.post_grammar_error(f"{e}")
|
dingus.post_grammar_error(f"{e}")
|
||||||
|
|
||||||
|
def tree_to_js(tree):
|
||||||
|
if tree is None:
|
||||||
|
return None
|
||||||
|
elif isinstance(tree, runtime.Tree):
|
||||||
|
return {
|
||||||
|
"kind": "tree",
|
||||||
|
"name": tree.name,
|
||||||
|
"start": tree.start,
|
||||||
|
"end": tree.end,
|
||||||
|
"children": [tree_to_js(child) for child in tree.children],
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
"kind": "token",
|
||||||
|
"start": tree.start,
|
||||||
|
"end": tree.end,
|
||||||
|
}
|
||||||
|
|
||||||
|
def eval_document(code):
|
||||||
|
global PARSE_TABLE
|
||||||
|
global LEXER
|
||||||
|
|
||||||
|
try:
|
||||||
|
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}")
|
||||||
`);
|
`);
|
||||||
|
|
||||||
dingus_module.post_grammar_status("Ready.");
|
dingus_module.post_grammar_status("Ready.");
|
||||||
|
|
@ -100,21 +168,27 @@ async function load_grammar_module(code) {
|
||||||
my_fn.destroy();
|
my_fn.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function parse_document(code) {
|
||||||
|
const pyodide = self.pyodide;
|
||||||
|
|
||||||
|
// console.log("Running...");
|
||||||
|
const my_fn = pyodide.globals.get("eval_document");
|
||||||
|
my_fn(code);
|
||||||
|
my_fn.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
self.onmessage = async function(event) {
|
self.onmessage = async function(event) {
|
||||||
await pyodide_promise;
|
await pyodide_promise;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { kind, data } = event.data;
|
const { kind, data } = event.data;
|
||||||
if (kind === "grammar_module") {
|
if (kind === "grammar_module") {
|
||||||
try {
|
await load_grammar_module(data);
|
||||||
await load_grammar_module(data);
|
} else if (kind === "document") {
|
||||||
} catch (e) {
|
await parse_document(data);
|
||||||
console.log("INTERNAL ERROR:", e.message);
|
|
||||||
postMessage({error: e.message});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (wtf) {
|
} catch (e) {
|
||||||
console.log("WTF?");
|
console.log("INTERNAL ERROR: ", e.message);
|
||||||
console.log(wtf);
|
postMessage({error: e.message});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue