forked from oguimbal/pgsql-ast-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nearley-loader.js
29 lines (27 loc) · 936 Bytes
/
nearley-loader.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
const fs = require('fs');
const path = require('path');
var nearley = require('nearley/lib/nearley.js');
var compile = require('nearley/lib/compile.js');
var generate = require('nearley/lib/generate.js');
var lint = require('nearley/lib/lint');
var rawGrammar = require('nearley/lib/nearley-language-bootstrapped.js');
var nearleyGrammar = nearley.Grammar.fromCompiled(rawGrammar);
module.exports = function (input) {
var parser = new nearley.Parser(nearleyGrammar);
console.log('Parsing nearley', this.resource);
parser.feed(input);
const opts = {
args: [this.resourcePath],
alreadycompiled: [],
}
var compilation = compile(parser.results[0], opts);
if (this.addDependency) {
for (const dep of opts.alreadycompiled) {
this.addDependency(dep);
}
}
lint(compilation, {});
const ret = generate(compilation, 'grammar');
// fs.writeFileSync(this.resource + '.compiled.ts', ret);
return ret;
}