antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

[Bug] SyntaxError antlr4 does not provide an export named 'DecisionState' when use typescript esm import

Open lisonge opened this issue 1 month ago • 2 comments

First of all thank you for your great open source work

  1. set tsconfig.json https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax to true
  2. compile https://github.com/antlr/grammars-v4/blob/master/java/java/JavaParser.g4 to JavaParser.ts
// Generated from ./src/grammar/java/JavaParser.g4 by ANTLR 4.13.2
// noinspection ES6UnusedImports,JSUnusedGlobalSymbols,JSUnusedLocalSymbols

import {
  ATN,
  ATNDeserializer,
  DecisionState, /// error
  DFA,
  FailedPredicateException,
  NoViableAltException,
  ParserATNSimulator,
  ParserRuleContext,
  PredictionContextCache,
  RecognitionException,
  RuleContext,
  TerminalNode,
  Token,
  TokenStream, // error
} from 'antlr4';
import JavaParserListener from './JavaParserListener.js';
// for running tests with parameters, TODO: discuss strategy for typed parameters in CI
// eslint-disable-next-line no-unused-vars
// type int = number;

import JavaParserBase from './JavaParserBase.js';
Image

here is my solution

import {
  ATN,
  ATNDeserializer,
-  DecisionState,
+  type DecisionState,
  DFA,
  FailedPredicateException,
  NoViableAltException,
  ParserATNSimulator,
  ParserRuleContext,
  PredictionContextCache,
  RecognitionException,
  RuleContext,
  TerminalNode,
  Token,
- TokenStream,
+ type TokenStream,
} from 'antlr4';

or just set verbatimModuleSyntax to false


Is it possible to determine, when converting JavaParser.g4 to TypeScript, that when a type identifier is used solely as a type marker, its import statement can be changed to a type-only import statement?

lisonge avatar Nov 22 '25 10:11 lisonge

NB: There is no port of the java/java/ grammar yet. I wrote the ports for CSharp and Java only. See the desc.xml.

kaby76 avatar Nov 22 '25 11:11 kaby76

Actually, for the antlr4 JavaScript module, its actual exports are fully known. It is only necessary to determine whether the identifier generated for export is in this list of string constants, and then update these variables to type-only imports. There is no need to actually analyze whether they are used as types or expressions during usage.

// just run it in your browser devtools
console.log(Object.keys(await import('https://cdn.jsdelivr.net/npm/antlr4')))
/*
[
    "ATN",
    "ATNDeserializer",
    "BailErrorStrategy",
    "CharStream",
    "CharStreams",
    "CommonToken",
    "CommonTokenStream",
    "DFA",
    "DefaultErrorStrategy",
    "DiagnosticErrorListener",
    "ErrorListener",
    "FailedPredicateException",
    "InputMismatchException",
    "InputStream",
    "Interval",
    "IntervalSet",
    "LL1Analyzer",
    "Lexer",
    "LexerATNSimulator",
    "NoViableAltException",
    "ParseTreeListener",
    "ParseTreeVisitor",
    "ParseTreeWalker",
    "Parser",
    "ParserATNSimulator",
    "ParserRuleContext",
    "PredictionContextCache",
    "PredictionMode",
    "RecognitionException",
    "RuleContext",
    "RuleNode",
    "TerminalNode",
    "Token",
    "TokenStreamRewriter",
    "arrayToString",
    "default"
]
*/

lisonge avatar Dec 09 '25 03:12 lisonge