Hi all!
I’ve been building a parser that I want to use in a web application. I created the parser using Antlr4, I’ve tested it with node.js, everything seems to work fine.
To use in my web application, I’m trying to use webpack as described here: https://github.com/antlr/antlr4/blob/master/doc/javascript-target.md
I’m using the latest versions of antrl4 and webpack 5.
When I run webpack, it complains about all the ‘static’ properties that are found in the antrl4-generated classes, with messages like the following:
Module parse failed: Unexpected token (1840:27)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| export default class n1qlParser extends antlr4.Parser {
|
> static grammarFileName = "n1ql.g4";
| static literalNames = [ null, null, null, null, null, null, null, "'.'",
Does anybody out there know how to get webpack to work with antlr4-generated parsers?
Thanks,
-Eben
Alternately, is there some way to include the antlr runtime without using webpack? I tried installing the antlr4 npm package, and I get the error:TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../".
Instead I recommend that you switch to the antlr4ts runtime and use antlr4ts-cli to generate your files from the grammar. Both are still marked as being alpha, but I have used these packages for years already (e.g. in my vscode extension vscode-antlr4).
With that in place you can remove the webpack loader and generate the parser/lexer files as part of your build process.
sorry but I think this advice is misleading.The TS runtime leverages the JS runtime so any issue affecting the JS runtime also affects the TS one, you were just lucky not to bump into them (I didn't either).
The issue here has nothing to do with the runtime itself.Rather it relates to webpack 5, which is not currently supported.The best advice is to use webpack 4.
I thought the TS runtime followed the design of this PR: https://github.com/antlr/antlr4/pull/1256
I would be reluctant recommending unofficial runtime targets...
Thank you Mike,
I was able to install antlr4ts and generate my parser as typescript. However, I have one additional question: what target do you use when compiling the resulting parser with tsc?
When I tried “ES5” it compiled, but throws an error when I test the resulting code with node.js:
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
Yet when I try any higher target (ES6, ES2018, etc.), I get tsc compilation errors such as:
n1qlParser/n1qlParser.ts:23399:15 - error TS2339: Property 'getRuleContext' does not exist on type 'SavepointContext'.
23399 return this.getRuleContext(0, Savepoint_nameContext);
Thank you,
-Eben Haber
From: 'Mike Lischke' via antlr-discussion <antlr-di...@googlegroups.com>
Reply-To: "antlr-di...@googlegroups.com" <antlr-di...@googlegroups.com>
Date: Friday, April 16, 2021 at 12:16 AM
To: "'rtm...@googlemail.com' via antlr-discussion" <antlr-di...@googlegroups.com>
Subject: Re: [antlr-discussion] webpack doesn't like 'static' properties in antler-generated classes
|
EXTERNAL EMAIL: Use caution when opening attachments or clicking on links |
--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/antlr-discussion/imkUQ33T0ac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
antlr-discussi...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/antlr-discussion/A96331D5-4DDD-4711-A42C-2F2E90DC5D72%40googlemail.com.
I was able to install antlr4ts and generate my parser as typescript. However, I have one additional question: what target do you use when compiling the resulting parser with tsc?When I tried “ES5” it compiled, but throws an error when I test the resulting code with node.js:Object.defineProperty(exports, "__esModule", { value: true });^ReferenceError: exports is not defined
Yet when I try any higher target (ES6, ES2018, etc.), I get tsc compilation errors such as:n1qlParser/n1qlParser.ts:23399:15 - error TS2339: Property 'getRuleContext' does not exist on type 'SavepointContext'.23399 return this.getRuleContext(0, Savepoint_nameContext);