antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.3k stars 3.3k forks source link

Update the instructions for the TypeScript target #4108

Open kaby76 opened 1 year ago

kaby76 commented 1 year ago

The instructions for the new TypeScript target should be updated, as mentioned here and here. Ideally it should include a small example so folks like me, who don't use TypeScript that often, can get something working.

kaby76 commented 1 year ago

I cannot get the tsc compiler to import Recognizer. I don't know what may be wrong because I don't understand the scoping rules for TypeScript whatsoever. I can import the class definitions for the ones that are documented, https://github.com/antlr/antlr4/blob/c2bfa34a66e6449ff6fb2e388e17e2ff4b7e8e82/doc/typescript-target.md?plain=1#L45, and ErrorListener, but not the critical Recognizer class, which is used to create a custom error listener.

I think for all targets there really should be realistic examples provided, which go beyond the simple examples given in the doc. Many problems would have been discovered with the Go target if realistic examples were given.

Test.ts:6:10 - error TS2305: Module '"antlr4"' has no exported member 'Recognizer'.

6 import { Recognizer }  from 'antlr4';
           ~~~~~~~~~~

Test.js

// Generated from trgen 0.19.3

import { CharStream } from 'antlr4';
import { CommonTokenStream } from 'antlr4';
import { ErrorListener }  from 'antlr4';
import { Recognizer }  from 'antlr4';
import { RecognitionException }  from 'antlr4';
import { StringBuilder, emptyString, joinString, formatString, isNullOrWhiteSpace } from 'typescript-string-operations';
import { Timer, Time, TimerOptions } from 'timer-node';

class MyErrorListener<T> extends ErrorListener<T> {
    _quiet: boolean;
    _tee: boolean;
    _output: any;
    had_error: boolean;

    constructor(quiet: boolean, tee: boolean, output: any) {
        super();
        this._quiet = quiet;
        this._tee = tee;
        this._output = output;
        this.had_error = false;
    }

    syntaxError(recognizer: Recognizer<T>, offendingSymbol: T, line: number, column: number, msg: string, e: RecognitionException | undefined): void {
        this.had_error = true;
        if (this._tee) {
//            fs.writeSync(this._output, `line ${line}:${column} ${msg}\n`);
        }
        if (!this._quiet) {
            console.error(`line ${line}:${column} ${msg}`);
        }
    }
}

I am using the latest pre-release of the JavaScript/TypeScript code.

package.json

{
  "name": "i",
  "version": "1.0.0",
  "description": "",
  "main": "Test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "antlr4": "^4.12.0-beta.4",
    "buffer": "^6.0.3",
    "fs-extra": "^10.1.0",
    "timer-node": "^5.0.6",
    "typescript-string-operations": "^1.5.0"
  },
  "type": "module",
  "devDependencies": {
    "@types/node": "^18.13.0"
  },
  "scripts": {
    "build": "tsc -p tsconfig.json"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "ES2020",
    "moduleResolution": "node",
    "target": "ES6",
    "noImplicitAny": true,
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  }
}
$ tsc --version
Version 4.9.5
02/12-07:00:58 ~/issues/issue-2883/fix-issue-2883-2/abb/Generated-TypeScript
$ node --version
v16.16.0