-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
25 lines (19 loc) · 756 Bytes
/
parse.js
File metadata and controls
25 lines (19 loc) · 756 Bytes
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
const { fs } = require('mz');
const nearley = require('nearley');
const edu = require('./edu.js');
async function main() {
const fileName = process.argv[2];
const input = (await fs.readFile(fileName)).toString();
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(edu));
parser.feed(input);
const parseResult = parser.results;
if (parseResult.length > 2) throw new Error('Error: Ambiguous code detected!');
else if (parseResult.length === 0) throw new Error('Error: Grammar not found!');
else {
const outputFileName = fileName.replace('.edu', '.ast');
const ast = parseResult[0];
await fs.writeFile(outputFileName, JSON.stringify(ast, null, ' '));
console.log(`Wrote to ${outputFileName}`);
}
}
main();