-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (157 loc) · 4.61 KB
/
Copy pathindex.js
File metadata and controls
183 lines (157 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* global process, TextDecoder, TextEncoder, WebAssembly */
const WASM_PAGE_SIZE = 64 * 1024;
const INPUT_BUFFER_OFFSET = WASM_PAGE_SIZE;
function assert(cond, msg) {
if (!cond) {
throw new Error(msg ?? 'assertion failed');
}
}
export class WasmMatcher {
constructor() {
this._instance = undefined;
this._imports = {
// System-level AssemblyScript imports.
env: {
abort() {
throw new Error('abort');
},
},
// For imports from ohmRuntime.ts.
ohmRuntime: {
printI32(val) {
// eslint-disable-next-line no-console
console.log(val);
},
isRuleSyntactic: ruleId => {
// TODO: Precompute this for all rules, and encode it in the module?
const name = this._ruleNames[ruleId];
assert(!!name);
return name[0] === name[0].toUpperCase();
},
fillInputBuffer: this._fillInputBuffer.bind(this),
},
};
this._ruleIds = new Map();
this._ruleNames = [];
}
_extractRuleIds(module) {
const sections = WebAssembly.Module.customSections(module, 'ruleNames');
if (sections.length === 0) {
throw new Error('No ruleNames section found in module');
}
const data = new Uint8Array(sections[0]);
const dataView = new DataView(data.buffer);
let offset = 0;
const parseU32 = () => {
// Quick 'n dirty ULeb128 parsing, assuming no more than 2 bytes.
const b1 = dataView.getUint8(offset++);
let value = b1 & 0x7f;
if (b1 & 0x80) {
const b2 = dataView.getUint8(offset++);
assert((b2 & 0x80) === 0, 'Expected max two bytes');
value |= (b2 & 0x7f) << 7;
}
return value;
};
const decoder = new TextDecoder('utf-8');
const numEntries = parseU32();
for (let i = 0; i < numEntries; i++) {
const stringLen = parseU32();
const bytes = data.slice(offset, offset + stringLen);
offset += stringLen;
const name = decoder.decode(bytes);
this._ruleIds.set(name, i);
this._ruleNames.push(name);
}
}
async _instantiate(source, debugImports = {}) {
const {module, instance} = await WebAssembly.instantiate(source, {
...this._imports,
debug: debugImports,
});
this._instance = instance;
this._extractRuleIds(module);
return this;
}
static async fromBytes(source) {
return new WasmMatcher()._instantiate(source);
}
getInput() {
return this._input;
}
setInput(str) {
if (this._input !== str) {
// this.replaceInputRange(0, this._input.length, str);
this._input = str;
}
return this;
}
replaceInputRange(startIdx, endIdx, str) {
throw new Error('Not implemented');
}
match() {
if (process.env.OHM_DEBUG === '1') debugger; // eslint-disable-line no-debugger
return this._instance.exports.match(0);
}
getMemorySizeBytes() {
return this._instance.exports.memory.buffer.byteLength;
}
getCstRoot() {
const {buffer} = this._instance.exports.memory;
const addr = this._instance.exports.getCstRoot();
return new CstNode(this._ruleNames, new DataView(buffer), addr);
}
_fillInputBuffer(offset, maxLen) {
const encoder = new TextEncoder();
const {memory} = this._instance.exports;
const buf = new Uint8Array(memory.buffer, INPUT_BUFFER_OFFSET + offset);
const {read, written} = encoder.encodeInto(this._input.substring(this._pos), buf);
assert(written < 64 * 1024, 'Input too long');
this._pos += read;
buf[written] = 0xff; // Mark end of input with an invalid UTF-8 character.
return written;
}
getRightmostFailurePosition() {
return this._instance.exports.rightmostFailurePos.value;
}
}
class CstNode {
constructor(ruleNames, dataView, offset) {
this._ruleNames = ruleNames;
this._view = dataView;
this._base = offset;
}
isNonterminal() {
return this.type >= 0;
}
isTerminal() {
return this._type === -1;
}
isIter() {
return this._type === -2;
}
get ruleName() {
const id = this._view.getInt32(this._base + 8, true);
return this._ruleNames[id];
}
get count() {
return this._view.getUint32(this._base, true);
}
get matchLength() {
return this._view.getUint32(this._base + 4, true);
}
get _type() {
const t = this._view.getInt32(this._base + 8, true);
return t < 0 ? t : 0;
}
get children() {
const children = [];
for (let i = 0; i < this.count; i++) {
const slotOffset = this._base + 16 + i * 4;
children.push(
new CstNode(this._ruleNames, this._view, this._view.getUint32(slotOffset, true)),
);
}
return children;
}
}