How to correctly bundle antlr4 generated parsers with antlr4?

96 views
Skip to first unread message

Runqiu Hu

unread,
Jan 10, 2019, 5:54:31 AM1/10/19
to antlr-discussion

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.

  1. I have generated parser/lexer/listener file using antlr4 -Dlanguage=JavaScript LPMLN.g4
  2. I wrote a main.js file to refer to the parser/lexer/listener.
  3. I configured webpack.config.js file for bundling.
  4. I imported the bundle.js file in the application js file.

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.

Reply all
Reply to author
Forward
0 new messages