Having reached a frustrating impasse, I am seeking assistance with a perplexing issue. My attempt to integrate TypeScript with ANTLR4 has hit a snag, and despite exhaustive efforts, I am unable to pinpoint the root cause (with limited documentation available on the TS target for ANTLR...). The errors cannot find module antlr4ts
and
Property 'exitRule' does not exist on type...
continue to plague me. Any guidance or support in resolving this predicament would be immensely appreciated.
I have diligently followed the steps outlined on the GitHub page, adhering closely to their instructions, only to encounter issues at a critical juncture.
The grammar is relatively simple, starting with the parser rule r()
. The lexer/parser files are located in the primary directory (following antlr4ts execution). Despite generating some js files within an /output/
directory, I have attempted to streamline the project to its core components (even excluding an index.html
; relying solely on tsconfig.json, package.json,
, the grammar, and the generated grammar files).
One of the encountered errors:
Cannot find module 'antlr4ts'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
tsconfig.json:
{
"compilerOptions": {
"outDir": "output",
"target": "es6",
"lib": ["es6", "dom"]
},
"include": ["./*"],
"exclude": ["node_modules"]
}
package.json:
{
"name": "naturaldeductionthesistypescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"antlr4ts": "antlr4ts -listener MyGrammar.g4",
"compile-typescript": "tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"antlr4ts": "^0.5.0-alpha.4"
},
"devDependencies": {
"antlr4ts-cli": "^0.5.0-alpha.4",
"typescript": "^3.5.3"
}
}
main.ts
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
import { MyGrammarLexer } from 'MyGrammarLexer';
import { MyGrammarParser } from 'MyGrammarParser';
// Create the lexer and parser
let inputStream = new ANTLRInputStream("text");
let lexer = new MyGrammarLexer(inputStream);
let tokenStream = new CommonTokenStream(lexer);
let parser = new MyGrammarParser(tokenStream);
// Parse the input, where `compilationUnit` is whatever entry point you defined
let tree = parser.r();
Grammar
grammar MyGrammar;
r : 'hello' ID;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;