dotlr
Version:
An LR(1) parser generator and visualizer created for educational purposes.
632 lines (567 loc) • 18.1 kB
JavaScript
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_export_0.get(idx);
wasm.__externref_table_dealloc(idx);
return value;
}
let WASM_VECTOR_LEN = 0;
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
let cachedTextEncoder = new lTextEncoder('utf-8');
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});
function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len, 1) >>> 0;
const mem = getUint8ArrayMemory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
function _assertClass(instance, klass) {
if (!(instance instanceof klass)) {
throw new Error(`expected instance of ${klass.name}`);
}
return instance.ptr;
}
let cachedDataViewMemory0 = null;
function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}
function getArrayJsValueFromWasm0(ptr, len) {
ptr = ptr >>> 0;
const mem = getDataViewMemory0();
const result = [];
for (let i = ptr; i < ptr + 4 * len; i += 4) {
result.push(wasm.__wbindgen_export_0.get(mem.getUint32(i, true)));
}
wasm.__externref_drop_slice(ptr, len);
return result;
}
const AutomatonFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_automaton_free(ptr >>> 0, 1));
/**
* LR(1) or LALR(1) automaton of a grammar.
*/
export class Automaton {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
AutomatonFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_automaton_free(ptr, 0);
}
}
const FirstTableFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_firsttable_free(ptr >>> 0, 1));
/**
* First table of the symbols in a grammar.
*/
export class FirstTable {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
FirstTableFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_firsttable_free(ptr, 0);
}
}
const FollowTableFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_followtable_free(ptr >>> 0, 1));
/**
* Follow table of the symbols in a grammar.
*/
export class FollowTable {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
FollowTableFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_followtable_free(ptr, 0);
}
}
const GrammarFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_grammar_free(ptr >>> 0, 1));
/**
* Grammar of a language.
*/
export class Grammar {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Grammar.prototype);
obj.__wbg_ptr = ptr;
GrammarFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
GrammarFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_grammar_free(ptr, 0);
}
/**
* Creates a grammar from a grammar string (WASM).
* @param {string} grammar_string
* @returns {Grammar}
*/
static parse_wasm(grammar_string) {
const ptr0 = passStringToWasm0(grammar_string, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.grammar_parse_wasm(ptr0, len0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return Grammar.__wrap(ret[0]);
}
/**
* Gets the symbols of the grammar (WASM).
* @returns {any}
*/
symbols_wasm() {
const ret = wasm.grammar_symbols_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the start symbol of the grammar (WASM).
* @returns {any}
*/
start_symbol_wasm() {
const ret = wasm.grammar_start_symbol_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the empty symbols of the grammar (WASM).
* @returns {any}
*/
rules_wasm() {
const ret = wasm.grammar_rules_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the constant tokens of the grammar (WASM).
* @returns {string}
*/
to_string_wasm() {
let deferred1_0;
let deferred1_1;
try {
const ret = wasm.grammar_to_string_wasm(this.__wbg_ptr);
deferred1_0 = ret[0];
deferred1_1 = ret[1];
return getStringFromWasm0(ret[0], ret[1]);
} finally {
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
}
}
/**
* Gets the regular expressions of the grammar (WASM).
* @returns {any}
*/
constant_tokens_wasm() {
const ret = wasm.grammar_constant_tokens_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the rules of the grammar (WASM).
* @returns {any}
*/
regular_expressions_wasm() {
const ret = wasm.grammar_regular_expressions_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Clones the grammar (WASM).
* @returns {Grammar}
*/
clone_wasm() {
const ret = wasm.grammar_clone_wasm(this.__wbg_ptr);
return Grammar.__wrap(ret);
}
}
const ParserFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_parser_free(ptr >>> 0, 1));
/**
* LR(1) or LALR(1) parser of a grammar.
*/
export class Parser {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Parser.prototype);
obj.__wbg_ptr = ptr;
ParserFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
ParserFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_parser_free(ptr, 0);
}
/**
* Crates an LR(1) parser of a grammar (WASM).
* @param {Grammar} grammar
* @returns {Parser}
*/
static new_wasm(grammar) {
_assertClass(grammar, Grammar);
var ptr0 = grammar.__destroy_into_raw();
const ret = wasm.parser_new_wasm(ptr0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return Parser.__wrap(ret[0]);
}
/**
* Crates an LALR(1) parser of a grammar (WASM).
* @param {Grammar} grammar
* @returns {Parser}
*/
static new_lalr_wasm(grammar) {
_assertClass(grammar, Grammar);
var ptr0 = grammar.__destroy_into_raw();
const ret = wasm.parser_new_lalr_wasm(ptr0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return Parser.__wrap(ret[0]);
}
/**
* Gets the first table of the symbols in the grammar of the parser (WASM).
* @returns {any}
*/
first_table_wasm() {
const ret = wasm.parser_first_table_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the follow table of the symbols in the grammar of the parser (WASM).
* @returns {any}
*/
follow_table_wasm() {
const ret = wasm.parser_follow_table_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the automaton of the grammar of the parser (WASM).
* @returns {any}
*/
automaton_wasm() {
const ret = wasm.parser_automaton_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the parsing tables of the parser (WASM).
* @returns {any}
*/
parsing_tables_wasm() {
const ret = wasm.parser_parsing_tables_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the action table of the parser (WASM).
* @returns {any}
*/
action_table_wasm() {
const ret = wasm.parser_action_table_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Gets the goto table of the parser (WASM).
* @returns {any}
*/
goto_table_wasm() {
const ret = wasm.parser_goto_table_wasm(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Tokenizes an input into a stream of tokens and their corresponding input slices (WASM).
* @param {string} input
* @returns {any}
*/
tokenize_wasm(input) {
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.parser_tokenize_wasm(this.__wbg_ptr, ptr0, len0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Parses a tokenized input (WASM).
* @param {string} input
* @returns {any}
*/
parse_wasm(input) {
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.parser_parse_wasm(this.__wbg_ptr, ptr0, len0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Traces the parsing of a tokenized input (WASM).
* @param {string} input
* @returns {any[]}
*/
trace_wasm(input) {
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.parser_trace_wasm(this.__wbg_ptr, ptr0, len0);
if (ret[3]) {
throw takeFromExternrefTable0(ret[2]);
}
var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
return v2;
}
}
const ParsingTablesFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_parsingtables_free(ptr >>> 0, 1));
/**
* Action and goto tables of a parser.
*/
export class ParsingTables {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
ParsingTablesFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_parsingtables_free(ptr, 0);
}
}
const WasmParserErrorFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_wasmparsererror_free(ptr >>> 0, 1));
/**
* Parser error of a parser tried to be constructed from a grammar (WASM).
*/
export class WasmParserError {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(WasmParserError.prototype);
obj.__wbg_ptr = ptr;
WasmParserErrorFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
WasmParserErrorFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_wasmparsererror_free(ptr, 0);
}
/**
* Prints the parser error to a string.
* @returns {string}
*/
to_string_wasm() {
let deferred1_0;
let deferred1_1;
try {
const ret = wasm.wasmparsererror_to_string_wasm(this.__wbg_ptr);
deferred1_0 = ret[0];
deferred1_1 = ret[1];
return getStringFromWasm0(ret[0], ret[1]);
} finally {
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
}
}
/**
* Serializes the parser error to a JavaScript value.
* @returns {any}
*/
serialize() {
const ret = wasm.wasmparsererror_serialize(this.__wbg_ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}
/**
* Converts the parser error to the conflicted parser if error was a conflict error.
* @returns {Parser}
*/
into_conflict_parser() {
const ptr = this.__destroy_into_raw();
const ret = wasm.wasmparsererror_into_conflict_parser(ptr);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return Parser.__wrap(ret[0]);
}
}
export function __wbindgen_is_string(arg0) {
const ret = typeof(arg0) === 'string';
return ret;
};
export function __wbindgen_error_new(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return ret;
};
export function __wbg_wasmparsererror_new(arg0) {
const ret = WasmParserError.__wrap(arg0);
return ret;
};
export function __wbindgen_string_new(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return ret;
};
export function __wbindgen_number_new(arg0) {
const ret = arg0;
return ret;
};
export function __wbindgen_bigint_from_u64(arg0) {
const ret = BigInt.asUintN(64, arg0);
return ret;
};
export function __wbg_set_f975102236d3c502(arg0, arg1, arg2) {
arg0[arg1] = arg2;
};
export function __wbg_new_034f913e7636e987() {
const ret = new Array();
return ret;
};
export function __wbg_new_7a87a0376e40533b() {
const ret = new Map();
return ret;
};
export function __wbg_new_e69b5f66fda8f13c() {
const ret = new Object();
return ret;
};
export function __wbg_set_425e70f7c64ac962(arg0, arg1, arg2) {
arg0[arg1 >>> 0] = arg2;
};
export function __wbg_set_277a63e77c89279f(arg0, arg1, arg2) {
const ret = arg0.set(arg1, arg2);
return ret;
};
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
;
};