I have been trying to use webpack to bundle my parser/lexer/listener .js files with antlr. I am not sure if I am doing it right. Because after getting the bundle.js file, I imported it with importScripts and the browser still claims that the variable is not defined.
antlr4 -Dlanguage=JavaScript LPMLN.g4
main.js:
var antlr4 = require('antlr4');
var LPMLNLexer = require('./LPMLNLexer').LPMLNLexer;
var LPMLNParser = require('./LPMLNParser').LPMLNParser;
var LPMLNListener = require('./LPMLNListener').LPMLNListener;
webpack.config.js:
module.exports = {
node: {
module: "empty",
net:"empty",
fs: "empty"
},
entry: './main',
output:{
path: __dirname + '/dist',
filename: 'bundle.js'
}
};
application-related code:
var validate = function(input) {
var lexer = new LPMLNLexer(chars);
var tokens = new CommonTokenStream(lexer);
var parser = new LPMLNParser(tokens);
var annotations = [];
var listener = new AnnotatingErrorListener(annotations)
parser.removeErrorListeners();
parser.addErrorListener(listener);
parser.parseMyRule();
return annotations;
};
bundle result:
Entrypoint main = bundle.js
[0] ./antlr4/Utils.js 10.9 KiB {0} [built]
[1] ./antlr4/Token.js 4.54 KiB {0} [built]
[2] ./antlr4/IntervalSet.js 7.4 KiB {0} [built]
[6] ./antlr4/PredictionContext.js 21.9 KiB {0} [built]
[12] ./antlr4/index.js 1.13 KiB {0} [built]
[15] ./antlr4/Lexer.js 10.2 KiB {0} [built]
[18] ./antlr4/ParserRuleContext.js 6.55 KiB {0} [built]
[19] ./antlr4/InputStream.js 3.22 KiB {0} [built]
[27] ./antlr4/polyfills/codepointat.js 1.6 KiB {0} [built]
[28] ./antlr4/polyfills/fromcodepoint.js 1.87 KiB {0} [built]
[31] ./LPMLNListener.js 7.88 KiB {0} [built]
[32] ./main.js 214 bytes {0} [built]
[33] ./antlr4/atn/index.js 534 bytes {0} [built]
[50] ./LPMLNLexer.js 15.8 KiB {0} [built]
[51] ./LPMLNParser.js 115 KiB {0} [built]
+ 37 hidden modules
Here is the structure of the bundle project:
I don't know if this is the right way to bundle the parser with antlr and how should I continue use the bundled file.