forked from kyr0/nodejs-parser-generator-based-on-antlr3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
36 lines (25 loc) · 1.27 KB
/
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
// Copyright (c) Aron Homberg, 2012; BSD licensed. All rights reserved.
var fs = require('fs');
// Import ANTLR3 JavaScript parser runtime, export org namespace to GLOBAL
GLOBAL.org = require('./runtime/lib/antlr3-all').runtime.org;
// Check CLI options
if (!process.argv[2] || !process.argv[3]) {
console.log('Usage: node parse.js $grammarName $fileToParse');
} else {
var lexerClassName = process.argv[2] + 'Lexer',
parserClassName = process.argv[2] + 'Parser';
// Import generated Lexer and Parser
GLOBAL[lexerClassName] = require('./parsers/' + process.argv[2] + '/' + lexerClassName + '.js');
GLOBAL[parserClassName] = require('./parsers/' + process.argv[2] + '/' + parserClassName + '.js');
// Read file to parse, parse and log the parser's state to the console
fs.readFile(process.argv[3], function (err, input) {
if (err) throw err;
// Read single file and use source code as input
var cstream = new org.antlr.runtime.ANTLRStringStream(String(input)),
lexer = new GLOBAL[lexerClassName](cstream),
tstream = new org.antlr.runtime.CommonTokenStream(lexer),
parser = new GLOBAL[parserClassName](tstream);
// Output the parser's state
console.log(parser.state);
});
}