-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
main.js
65 lines (57 loc) · 2.12 KB
/
main.js
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
import ohmGrammar from '../dist/ohm-grammar.js';
import {buildGrammar} from './buildGrammar.js';
import * as common from './common.js';
import * as errors from './errors.js';
import {Grammar} from './Grammar.js';
import * as pexprs from './pexprs.js';
import * as util from './util.js';
// Late initialization for stuff that is bootstrapped.
import './semanticsDeferredInit.js'; // TODO: Clean this up.
Grammar.initApplicationParser(ohmGrammar, buildGrammar);
const isBuffer = obj =>
!!obj.constructor &&
typeof obj.constructor.isBuffer === 'function' &&
obj.constructor.isBuffer(obj);
function compileAndLoad(source, namespace) {
const m = ohmGrammar.match(source, 'Grammars');
if (m.failed()) {
throw errors.grammarSyntaxError(m);
}
return buildGrammar(m, namespace);
}
export function grammar(source, optNamespace) {
const ns = grammars(source, optNamespace);
// Ensure that the source contained no more than one grammar definition.
const grammarNames = Object.keys(ns);
if (grammarNames.length === 0) {
throw new Error('Missing grammar definition');
} else if (grammarNames.length > 1) {
const secondGrammar = ns[grammarNames[1]];
const interval = secondGrammar.source;
throw new Error(
util.getLineAndColumnMessage(interval.sourceString, interval.startIdx) +
'Found more than one grammar definition -- use ohm.grammars() instead.',
);
}
return ns[grammarNames[0]]; // Return the one and only grammar.
}
export function grammars(source, optNamespace) {
const ns = Object.create(optNamespace || {});
if (typeof source !== 'string') {
// For convenience, detect Node.js Buffer objects and automatically call toString().
if (isBuffer(source)) {
source = source.toString();
} else {
throw new TypeError(
'Expected string as first argument, got ' + common.unexpectedObjToString(source),
);
}
}
compileAndLoad(source, ns);
return ns;
}
export * from './main-kernel.js';
export {IndentationSensitive as ExperimentalIndentationSensitive} from './IndentationSensitive.js';
export {ohmGrammar};
export {pexprs};
export {version} from './version.js';