-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
36 lines (31 loc) · 947 Bytes
/
parse.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
const nearley = require("nearley");
// const grammar = require("./json.js");
const grammar = require("./iSatPl.js");
const fs = require('mz/fs')
const path=require("path");
// Create a Parser object from our grammar.
async function main(){
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
const filename=process.argv[2];
const outputfile=path.basename(filename,'.isatplay')+".ast";
const code=(await fs.readFile(filename)).toString();
if(code=="sb:=1"){
console.log("matched");
}
else{
console.log("failed");
}
try {
console.log(code);
parser.feed(code);
const ast=parser.results[0];
await fs.writeFile(outputfile,JSON.stringify(ast,null))
console.log("result"+parser.results[0]);
console.log("o/p"+outputfile)
} catch (error) {
console.log(error.message);
}
}
main();
// parser.results is an array of possible parsings.
// [[[[ "foo" ],"\n" ]]]