@ohm-js/wasm 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/build/ohmRuntime.wasm_sections.d.ts +39 -0
- package/dist/build/ohmRuntime.wasm_sections.js +47 -0
- package/dist/index.d.ts +4 -8
- package/dist/index.js +4 -2284
- package/dist/src/AstBuilder.d.ts +17 -0
- package/dist/src/AstBuilder.js +143 -0
- package/dist/src/Compiler.d.ts +195 -0
- package/dist/src/Compiler.js +1559 -0
- package/dist/src/assert.d.ts +3 -0
- package/dist/src/assert.js +9 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +56 -0
- package/dist/src/compat.d.ts +4 -0
- package/dist/src/compat.js +18 -0
- package/dist/src/ir.d.ts +121 -0
- package/dist/src/ir.js +297 -0
- package/dist/src/miniohm.d.ts +87 -0
- package/dist/src/miniohm.js +418 -0
- package/package.json +19 -5
- package/dist/cli.js +0 -2333
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CstNode, MatchResult } from './miniohm.ts';
|
|
2
|
+
export type AstMapping = Record<string, unknown>;
|
|
3
|
+
export declare class AstBuilder {
|
|
4
|
+
currNode?: CstNode;
|
|
5
|
+
private _mapping;
|
|
6
|
+
private _depth;
|
|
7
|
+
private _debug;
|
|
8
|
+
constructor(mapping: AstMapping, opts?: {
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
});
|
|
11
|
+
private _debugLog;
|
|
12
|
+
_visitTerminal(node: CstNode): string;
|
|
13
|
+
_visitNonterminal(node: CstNode): unknown;
|
|
14
|
+
_visitIter(node: CstNode): unknown;
|
|
15
|
+
toAst(nodeOrResult: MatchResult | CstNode): unknown;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=AstBuilder.d.ts.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { assert, checkNotNull } from "./assert.js";
|
|
2
|
+
function childAt(children, idx, ruleName, propName = '') {
|
|
3
|
+
if (idx > children.length) {
|
|
4
|
+
const path = propName ? `${ruleName}.${propName}` : ruleName;
|
|
5
|
+
throw new Error(`${path}: Child index ${idx} out of range`);
|
|
6
|
+
}
|
|
7
|
+
return children[idx];
|
|
8
|
+
}
|
|
9
|
+
export class AstBuilder {
|
|
10
|
+
currNode;
|
|
11
|
+
_mapping;
|
|
12
|
+
_depth = -1;
|
|
13
|
+
_debug = false;
|
|
14
|
+
constructor(mapping, opts = {}) {
|
|
15
|
+
const handleListOf = (child) => this.toAst(child);
|
|
16
|
+
const handleEmptyListOf = () => [];
|
|
17
|
+
const handleNonemptyListOf = (first, iterSepAndElem) => [
|
|
18
|
+
this.toAst(first),
|
|
19
|
+
...iterSepAndElem.map((_, elem) => this.toAst(elem)),
|
|
20
|
+
];
|
|
21
|
+
this._mapping = {
|
|
22
|
+
listOf: handleListOf,
|
|
23
|
+
ListOf: handleListOf,
|
|
24
|
+
emptyListOf: handleEmptyListOf,
|
|
25
|
+
EmptyListOf: handleEmptyListOf,
|
|
26
|
+
nonemptyListOf: handleNonemptyListOf,
|
|
27
|
+
NonemptyListOf: handleNonemptyListOf,
|
|
28
|
+
...mapping,
|
|
29
|
+
};
|
|
30
|
+
this._debug = opts.debug ?? false;
|
|
31
|
+
}
|
|
32
|
+
_debugLog(...data) {
|
|
33
|
+
if (this._debug)
|
|
34
|
+
console.log(' '.repeat(this._depth), ...data);
|
|
35
|
+
}
|
|
36
|
+
_visitTerminal(node) {
|
|
37
|
+
return node.sourceString;
|
|
38
|
+
}
|
|
39
|
+
_visitNonterminal(node) {
|
|
40
|
+
const { children, ruleName } = node;
|
|
41
|
+
const mapping = this._mapping;
|
|
42
|
+
this._debugLog(`> ${ruleName}`);
|
|
43
|
+
const dbgReturn = (val) => {
|
|
44
|
+
this._debugLog(`| ${ruleName} DONE`);
|
|
45
|
+
return val;
|
|
46
|
+
};
|
|
47
|
+
// without customization
|
|
48
|
+
if (!Object.hasOwn(mapping, ruleName)) {
|
|
49
|
+
// lexical rule
|
|
50
|
+
if (node.isLexical()) {
|
|
51
|
+
return node.sourceString;
|
|
52
|
+
}
|
|
53
|
+
// singular node (e.g. only surrounded by literals or lookaheads)
|
|
54
|
+
const realChildren = children.filter(c => !c.isTerminal());
|
|
55
|
+
if (realChildren.length === 1) {
|
|
56
|
+
return dbgReturn(this.toAst(realChildren[0]));
|
|
57
|
+
}
|
|
58
|
+
// rest: terms with multiple children
|
|
59
|
+
}
|
|
60
|
+
// direct forward
|
|
61
|
+
if (typeof mapping[ruleName] === 'number') {
|
|
62
|
+
const idx = mapping[ruleName];
|
|
63
|
+
return dbgReturn(this.toAst(childAt(children, idx, ruleName)));
|
|
64
|
+
}
|
|
65
|
+
assert(typeof mapping[ruleName] !== 'function', "shouldn't be possible");
|
|
66
|
+
// named/mapped children or unnamed children ('0', '1', '2', ...)
|
|
67
|
+
const propMap = mapping[ruleName] || children;
|
|
68
|
+
const ans = {
|
|
69
|
+
type: ruleName,
|
|
70
|
+
};
|
|
71
|
+
// eslint-disable-next-line guard-for-in
|
|
72
|
+
for (const prop in propMap) {
|
|
73
|
+
const mappedProp = mapping[ruleName] && mapping[ruleName][prop];
|
|
74
|
+
if (typeof mappedProp === 'number') {
|
|
75
|
+
// direct forward
|
|
76
|
+
ans[prop] = this.toAst(childAt(children, mappedProp, ruleName, prop));
|
|
77
|
+
}
|
|
78
|
+
else if (typeof mappedProp === 'string' ||
|
|
79
|
+
typeof mappedProp === 'boolean' ||
|
|
80
|
+
mappedProp === null) {
|
|
81
|
+
// primitive value
|
|
82
|
+
ans[prop] = mappedProp;
|
|
83
|
+
}
|
|
84
|
+
else if (typeof mappedProp === 'object' && mappedProp instanceof Number) {
|
|
85
|
+
// primitive number (must be unboxed)
|
|
86
|
+
ans[prop] = Number(mappedProp);
|
|
87
|
+
}
|
|
88
|
+
else if (typeof mappedProp === 'function') {
|
|
89
|
+
// computed value
|
|
90
|
+
ans[prop] = mappedProp.call(this, children);
|
|
91
|
+
}
|
|
92
|
+
else if (mappedProp === undefined) {
|
|
93
|
+
const child = children[Number(prop)];
|
|
94
|
+
if (child && !child.isTerminal()) {
|
|
95
|
+
ans[prop] = this.toAst(child);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// delete predefined 'type' properties, like 'type', if explicitly removed
|
|
99
|
+
delete ans[prop];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return dbgReturn(ans);
|
|
104
|
+
}
|
|
105
|
+
_visitIter(node) {
|
|
106
|
+
const { children } = node;
|
|
107
|
+
if (node.isOptional()) {
|
|
108
|
+
if (children.length === 0) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return this.toAst(children[0]);
|
|
112
|
+
}
|
|
113
|
+
return children.map(c => this.toAst(c));
|
|
114
|
+
}
|
|
115
|
+
toAst(nodeOrResult) {
|
|
116
|
+
let node = nodeOrResult;
|
|
117
|
+
if (typeof nodeOrResult.succeeded === 'function') {
|
|
118
|
+
const matchResult = nodeOrResult;
|
|
119
|
+
assert(matchResult.succeeded(), 'Cannot convert failed match result to AST');
|
|
120
|
+
node = checkNotNull(matchResult._cst);
|
|
121
|
+
}
|
|
122
|
+
let ans;
|
|
123
|
+
this._depth++;
|
|
124
|
+
if (node.isTerminal()) {
|
|
125
|
+
ans = this._visitTerminal(node);
|
|
126
|
+
}
|
|
127
|
+
else if (node.isIter()) {
|
|
128
|
+
ans = this._visitIter(node);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
assert(node.isNonterminal(), `Unknown node type: ${node._type}`);
|
|
132
|
+
this.currNode = node;
|
|
133
|
+
ans =
|
|
134
|
+
typeof this._mapping[node.ctorName] === 'function'
|
|
135
|
+
? this._mapping[node.ctorName].apply(this, node.children)
|
|
136
|
+
: this._visitNonterminal(node);
|
|
137
|
+
this.currNode = undefined;
|
|
138
|
+
}
|
|
139
|
+
this._depth--;
|
|
140
|
+
return ans;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=AstBuilder.js.map
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
export class Compiler {
|
|
2
|
+
constructor(grammar: any);
|
|
3
|
+
grammar: any;
|
|
4
|
+
importDecls: {
|
|
5
|
+
module: string;
|
|
6
|
+
name: string;
|
|
7
|
+
paramTypes: any[];
|
|
8
|
+
resultTypes: any[];
|
|
9
|
+
}[];
|
|
10
|
+
ruleIdByName: any;
|
|
11
|
+
_deferredRuleIds: Set<any>;
|
|
12
|
+
_maxMemoizedRuleId: number;
|
|
13
|
+
rules: Map<any, any> | undefined;
|
|
14
|
+
_nextLiftedId: number;
|
|
15
|
+
_lexContextStack: any[];
|
|
16
|
+
_applySpacesImplicit: ir.Apply;
|
|
17
|
+
importCount(): number;
|
|
18
|
+
ruleId(name: any): any;
|
|
19
|
+
_ensureRuleId(name: any, { notMemoized }?: {}): any;
|
|
20
|
+
_deferRuleId(name: any): void;
|
|
21
|
+
inLexicalContext(): any;
|
|
22
|
+
liftPExpr(exp: any, isSyntactic: any): any[];
|
|
23
|
+
ruleEvalFuncIdx(name: any): any;
|
|
24
|
+
getDebugImports(log: any): {};
|
|
25
|
+
normalize(): void;
|
|
26
|
+
compile(): Uint8Array<ArrayBuffer>;
|
|
27
|
+
typeMap: TypeMap | undefined;
|
|
28
|
+
asm: Assembler | undefined;
|
|
29
|
+
simplifyApplications(): void;
|
|
30
|
+
liftedTerminals: IndexedSet | undefined;
|
|
31
|
+
compileTerminalRule(name: any): any;
|
|
32
|
+
beginLexContext(initialVal: any): void;
|
|
33
|
+
endLexContext(): void;
|
|
34
|
+
compileRule(name: any): any;
|
|
35
|
+
specializeApplications(): void;
|
|
36
|
+
buildRuleNamesSection(ruleNames: any): any;
|
|
37
|
+
buildModule(typeMap: any, functionDecls: any): Uint8Array<ArrayBuffer>;
|
|
38
|
+
rewriteDebugLabels(decls: any): void;
|
|
39
|
+
functionDecls(): any[];
|
|
40
|
+
emitDispatch({ child: exp, patterns }: {
|
|
41
|
+
child: any;
|
|
42
|
+
patterns: any;
|
|
43
|
+
}): void;
|
|
44
|
+
emitPExpr(exp: any, { preHook, postHook }?: {}): void;
|
|
45
|
+
emitAlt(exp: any): void;
|
|
46
|
+
emitAny(): void;
|
|
47
|
+
emitApplyTerm({ terminalId }: {
|
|
48
|
+
terminalId: any;
|
|
49
|
+
}): void;
|
|
50
|
+
emitApplyGeneralized(exp: any): void;
|
|
51
|
+
emitApply(exp: any): void;
|
|
52
|
+
emitEnd(): void;
|
|
53
|
+
emitFail(): void;
|
|
54
|
+
emitLex({ child }: {
|
|
55
|
+
child: any;
|
|
56
|
+
}): void;
|
|
57
|
+
emitLookahead({ child }: {
|
|
58
|
+
child: any;
|
|
59
|
+
}): void;
|
|
60
|
+
emitNot({ child }: {
|
|
61
|
+
child: any;
|
|
62
|
+
}): void;
|
|
63
|
+
emitOpt({ child }: {
|
|
64
|
+
child: any;
|
|
65
|
+
}): void;
|
|
66
|
+
emitPlus(plusExp: any): void;
|
|
67
|
+
emitRange(exp: any): void;
|
|
68
|
+
emitSeq({ children }: {
|
|
69
|
+
children: any;
|
|
70
|
+
}): void;
|
|
71
|
+
maybeEmitSpaceSkipping(): void;
|
|
72
|
+
emitStar({ child }: {
|
|
73
|
+
child: any;
|
|
74
|
+
}, { reuseStackFrame }?: {}): void;
|
|
75
|
+
wrapTerminalLike(thunk: any): void;
|
|
76
|
+
emitCaseInsensitive({ value }: {
|
|
77
|
+
value: any;
|
|
78
|
+
}): void;
|
|
79
|
+
emitTerminal(exp: any): void;
|
|
80
|
+
emitUnicodeChar(exp: any): void;
|
|
81
|
+
}
|
|
82
|
+
export namespace Compiler {
|
|
83
|
+
export { WASM_PAGE_SIZE as STACK_START_OFFSET };
|
|
84
|
+
}
|
|
85
|
+
export namespace ConstantsForTesting {
|
|
86
|
+
let CST_NODE_SIZE_BYTES: any;
|
|
87
|
+
let MEMO_COL_SIZE_BYTES: any;
|
|
88
|
+
}
|
|
89
|
+
import * as ir from './ir.ts';
|
|
90
|
+
declare class TypeMap {
|
|
91
|
+
constructor(startIdx?: number);
|
|
92
|
+
_map: Map<any, any>;
|
|
93
|
+
_startIdx: number;
|
|
94
|
+
add(paramTypes: any, resultTypes: any): any;
|
|
95
|
+
addDecls(decls: any): void;
|
|
96
|
+
getIdx(paramTypes: any, resultTypes: any): any;
|
|
97
|
+
getIdxForDecl(decl: any): any;
|
|
98
|
+
getTypes(): any[];
|
|
99
|
+
}
|
|
100
|
+
declare class Assembler {
|
|
101
|
+
constructor(typeMap: any);
|
|
102
|
+
_globals: Map<any, any>;
|
|
103
|
+
_functionDecls: any[];
|
|
104
|
+
_blockStack: any[];
|
|
105
|
+
_code: any[];
|
|
106
|
+
_locals: Map<any, any> | undefined;
|
|
107
|
+
_typeMap: any;
|
|
108
|
+
addBlocktype(paramTypes: any, resultTypes: any): void;
|
|
109
|
+
blocktype(paramTypes: any, resultTypes: any): any;
|
|
110
|
+
doEmit(thunk: any): any[];
|
|
111
|
+
addGlobal(name: any, type: any, mut: any, initThunk: any): number;
|
|
112
|
+
addLocal(name: any, type: any): number;
|
|
113
|
+
addFunction(name: any, paramTypes: any, resultTypes: any, bodyFn: any): void;
|
|
114
|
+
globalidx(name: any): any;
|
|
115
|
+
localidx(name: any): any;
|
|
116
|
+
emit(...bytes: any[]): void;
|
|
117
|
+
block(bt: any, bodyThunk: any, label?: string): void;
|
|
118
|
+
_blockOnly(bt: any, label: any): void;
|
|
119
|
+
_endBlock(): void;
|
|
120
|
+
loop(bt: any, bodyThunk: any): void;
|
|
121
|
+
if(bt: any, bodyThunk: any): void;
|
|
122
|
+
ifElse(bt: any, thenThunk: any, elseThunk?: undefined): void;
|
|
123
|
+
ifFalse(bt: any, bodyThunk: any): void;
|
|
124
|
+
br(depth: any): void;
|
|
125
|
+
i32Add(): void;
|
|
126
|
+
i32Const(value: any): void;
|
|
127
|
+
i32Load(offset?: number): void;
|
|
128
|
+
i32Load8u(offset?: number): void;
|
|
129
|
+
i32Mul(): void;
|
|
130
|
+
i32Eq(): void;
|
|
131
|
+
i32Ne(): void;
|
|
132
|
+
i32Store(offset?: number): void;
|
|
133
|
+
i32Sub(): void;
|
|
134
|
+
globalGet(name: any): void;
|
|
135
|
+
globalSet(name: any): void;
|
|
136
|
+
localGet(name: any): void;
|
|
137
|
+
localSet(name: any): void;
|
|
138
|
+
localTee(name: any): void;
|
|
139
|
+
break(depth: any): void;
|
|
140
|
+
condBreak(depth: any): void;
|
|
141
|
+
continue(depth: any): void;
|
|
142
|
+
brTable(labels: any, defaultLabelidx: any): void;
|
|
143
|
+
return(): void;
|
|
144
|
+
switch(bt: any, discrimThunk: any, numCases: any, caseCb: any, defaultThunk: any): void;
|
|
145
|
+
refNull(valtype: any): void;
|
|
146
|
+
i32Inc(): void;
|
|
147
|
+
i32Dec(): void;
|
|
148
|
+
dup(): void;
|
|
149
|
+
currCharCode(): void;
|
|
150
|
+
nextCharCode(): void;
|
|
151
|
+
setRet(val: any): void;
|
|
152
|
+
pushStackFrame(saveThunk: any): void;
|
|
153
|
+
popStackFrame(): void;
|
|
154
|
+
savePos(): void;
|
|
155
|
+
getSavedPos(): void;
|
|
156
|
+
restorePos(): void;
|
|
157
|
+
saveNumBindings(): void;
|
|
158
|
+
getSavedNumBindings(): void;
|
|
159
|
+
restoreBindingsLength(): void;
|
|
160
|
+
saveFailurePos(): void;
|
|
161
|
+
restoreFailurePos(): void;
|
|
162
|
+
saveGlobalFailurePos(): void;
|
|
163
|
+
restoreGlobalFailurePos(): void;
|
|
164
|
+
updateGlobalFailurePos(): void;
|
|
165
|
+
updateLocalFailurePos(origPosThunk: any): void;
|
|
166
|
+
incPos(): void;
|
|
167
|
+
callPrebuiltFunc(name: any): void;
|
|
168
|
+
newIterNodeWithSavedPosAndBindings(arity: any, isOpt?: boolean): void;
|
|
169
|
+
newCaseInsensitiveNode(ruleId: any): void;
|
|
170
|
+
newTerminalNode(): void;
|
|
171
|
+
i32Max(aThunk: any, bThunk: any): void;
|
|
172
|
+
depthOf(label: any): number;
|
|
173
|
+
ruleEvalReturn(): void;
|
|
174
|
+
}
|
|
175
|
+
declare namespace Assembler {
|
|
176
|
+
export let ALIGN_1_BYTE: number;
|
|
177
|
+
export let ALIGN_4_BYTES: number;
|
|
178
|
+
export let CST_NODE_HEADER_SIZE_BYTES: number;
|
|
179
|
+
let MEMO_COL_SIZE_BYTES_1: number;
|
|
180
|
+
export { MEMO_COL_SIZE_BYTES_1 as MEMO_COL_SIZE_BYTES };
|
|
181
|
+
export let STACK_FRAME_SIZE_BYTES: number;
|
|
182
|
+
}
|
|
183
|
+
declare class IndexedSet {
|
|
184
|
+
_map: Map<any, any>;
|
|
185
|
+
add(item: any): any;
|
|
186
|
+
getIndex(item: any): any;
|
|
187
|
+
has(item: any): boolean;
|
|
188
|
+
get size(): number;
|
|
189
|
+
keys(): any[];
|
|
190
|
+
values(): any[];
|
|
191
|
+
[Symbol.iterator](): MapIterator<[any, any]>;
|
|
192
|
+
}
|
|
193
|
+
declare const WASM_PAGE_SIZE: number;
|
|
194
|
+
export {};
|
|
195
|
+
//# sourceMappingURL=Compiler.d.ts.map
|