Skip to content

Commit 2c75e64

Browse files
ericvergnaudhs-apotellNicksxsparrtKerze-Mann
authored
Typescript target (#4027)
* Fix CMake syntax for variable expansion When using variables to compare (like in if clause) the variable shouldn't be quoted. More details can be found at the link below: https://cmake.org/cmake/help/latest/command/if.html#variable-expansion Signed-off-by: HS <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * initial commit Signed-off-by: Eric Vergnaud <[email protected]> * renamed for clarity Signed-off-by: Eric Vergnaud <[email protected]> * renamed for clarity Signed-off-by: Eric Vergnaud <[email protected]> * able to locate antlr4 runtime using ts-node, missing types Signed-off-by: Eric Vergnaud <[email protected]> * progressing Signed-off-by: Eric Vergnaud <[email protected]> * able to 'run' a test. It fails but it compiles and resolves! Signed-off-by: Eric Vergnaud <[email protected]> * reflect refactored runtime Signed-off-by: Eric Vergnaud <[email protected]> * able to run RecursiveLexerRuleRefWithWildcardPlus_1 test locally Signed-off-by: Eric Vergnaud <[email protected]> * passes LexerExec tests in IntelliJ Signed-off-by: Eric Vergnaud <[email protected]> * make ATN private Signed-off-by: Eric Vergnaud <[email protected]> * ignore same tests as JavaScript Signed-off-by: Eric Vergnaud <[email protected]> * compiles Parser and Lexer bu local run fails Signed-off-by: Eric Vergnaud <[email protected]> * ParserExec.TokenOffset test successful in IntelliJ ! Signed-off-by: Eric Vergnaud <[email protected]> * Progressing, passing 131 of 348 tests Signed-off-by: Eric Vergnaud <[email protected]> * pass 327 tests out of 348 Signed-off-by: Eric Vergnaud <[email protected]> * more successful tests Signed-off-by: Eric Vergnaud <[email protected]> * 333 successful tests out of 348 Signed-off-by: Eric Vergnaud <[email protected]> * all tests pass except 7 caused by #3868 Signed-off-by: Eric Vergnaud <[email protected]> * update getting-started doc Signed-off-by: nicksxs <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * add blank github action file for hosted CI Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * update getting started document to say java 11 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Revert "update getting started document to say java 11" This reverts commit 1df58f7. Signed-off-by: Eric Vergnaud <[email protected]> * add C# book code links Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Add Jim/Ken to readme Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Update Swift Package to support static library Add static library distribution in SPM Signed-off-by: Hell_Ghost <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Update Package.swift Signed-off-by: Hell_Ghost [email protected] Signed-off-by: Hell_Ghost <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Add caching support for maven & dependencies. Also, include caching for cpp builds using actions/ccache. Builds are more reliable (avoids the archive.apache server which intermittently reports timeouts) and also significantly improves the overall builds times (down from 46 mins to 28 mins). The slowest part of the build now is the Windows+cpp builds because there is no reliable cache implementation yet. MacOS+cpp (65% cache hit) is also relatively slow compared to Ubuntu+cpp (99% cache hit). Signed-off-by: HS <[email protected]> Signed-off-by: Terence Parr <[email protected]> # Conflicts: # .github/workflows/hosted.yml Signed-off-by: Eric Vergnaud <[email protected]> * use snap to install go 1.19 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * grr...install snap Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * ugh. start snap Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * ugh. start snap Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * ugh. cant get snap to install go Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * try downloading golang with curl Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Issue #3823: Temporarily disable a few tests on CI The tests are currently failing. The underlying issues have been fixed on dev and so the builds will be turned back with the next release. Signed-off-by: HS <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * update actions status badge Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * update getting started document to say java 11 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Revert "update getting started document to say java 11" This reverts commit 3591ee0. Signed-off-by: Eric Vergnaud <[email protected]> * update getting-started doc Signed-off-by: nicksxs <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * make getValue visible to external profiler tools. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Fix #3508: Document the $parser attribute and its use in target-agnostic grammars. Signed-off-by: Ross Patterson <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Add accessor to IntervalSet for intervals Signed-off-by: James Taylor <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Remove libuuid dependency from C++ runtime libuuid and its headers are not referenced anywhere, so remove it. Signed-off-by: Bryan Tan <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Add `@SuppressWarnings("CheckReturnValue")` to prevent error_prone lib errors. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: Fixes for #3718 o Implement collections with generics to solve hash collisions o Fix type casting in LL start parser simulation optimization o General minor tidy ups Acknowledgements to @kaby76 for help with tracing errors Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3718 Revert accidental keyboard error in Java target Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3718 Correct DFAState index in Lexer ATN Simulator Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3718 Fix go runtime test runners With older versions of go, there was no good way to tell the compiler to use your local development copy of a particular package instead of the one installed in GOPATH/src/... However, we are now using modules, which allows us to tell the compiler that instead of a module downloaded to GOPATH/pkg, to use a local copy on disk. Hence this change removes the need to copy the whole of the go installation to a tempoorary location, then put the antlr go runtime in to the go installation as if it was part of the compiler. Hence the execution time for the go tests is now faster than before. This works because when the generated code is placed in the temporary location, we create a go.mod file for it, tell the module to replace the online module for the go runtime with the local copy on disk, then ro a go mod tidy to add the dependencies from the code (which avoids network access, so it is instant), which adds the ANTLR dependency itself (which is then replaced at compile time). All go runtime tests now pass. Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Rm remote github actions; hosted seems to work Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * install golang with curl; go was missing from dev Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: Rework of all Hash() and Equals() methods - implement generic collections - Implement new collections using generics that implement the functionality required by the Java runtime in a more idiomatic Go way. - Fix Hash() and Equals() for all objects in the runtime - Fix getConflictingAlts so that it behaves the same way as Java, using a new generic collection - Replaces the use of the array2DHashSet, which was causing unneeded memory allocations. Replaced with generic collection that allocates minimally (though, I think I can improve on that with a little analysis). Jim Idle - [email protected] Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3718 Correct DFAState index in Lexer ATN Simulator Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: Reduce initial memory allocations for collections - Many small collections are created at runtime, the default allocation for maps, even though small, still requires memory. Specifying a very small initial allocation prevents unnecesary allocations and has no measurable effect on performance. A small incremental change. Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3758 Allow for string being a keyword and fix go template to use escapedName - The go template was ignoring the use of escapedName in many places and was not consistenet with the Java version. - Added 'string' to the list of reserved words for the Go target Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3758 Add go.sum to the repo Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #3758 Ensure that standard runtime extensions are included in go.mod Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #2826 Go template is incorrect for dynamic scopes closes #2826 obviates PR #3101 Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #2016 - Generate correct iGo code for lists in a grammar, such as `label+=arg+` Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: Bump poms to use 4.11 Snapshot Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * turn off Golang test at circleci for now Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Replace smart-quote with single-quote in code examples Signed-off-by: Tim McCormack <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Augment error message during testing to include full cause of problem. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Augment error message during testing to include full cause of problem. (round 2 to avoid null ptr) Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Cpp: Link to threads library As detailed in #3708, it is necessary to link against the (p)threads library in order to be able to use std::call_once without producing linker errors. Since this function is frequently used in ANTLR's cpp version, this commit ensures that the respective library is always linked against in order to avoid this error, even if downstream users are not explicitly linking against an appropriate threads library. Fixes #3708 Signed-off-by: Robert Adam <[email protected]> Co-authored-by: Bryan Tan <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * add test for #2016 and fix java. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * ensure all targets have the appropriate argument list for the template causing the problem. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Fix other targets Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix format Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * add check that $args is a list Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * change made by @lingyv-li to fix bug in DART exposed by this test. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix AssertIsList in multiple targets. Go doesn't pass test and has no AssertIsList so I'm dropping that test from the Go test suite. How did a comment to the C++ runnerFor my future reference as to how to build things from the command line. Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * C++ gets an exception with this test so I'm turning it off. See #3845 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: #3840 Move Go to version v4.11.0 Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: #3840 Create the v4 version of the Go runtime Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: Create the v4 runtime layout for the Go runtime, ready for release tagging Note that the vast majority of the changes here are just copying the runtime file in to the /v4 subdirectory so that we can support legacy projects that use GOPATH only, as well as users that can use go modules. At a later release, we will delete the default path, and move the v4 subdirectory back to the top level. But, we cannot do that on this release. Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Reenable go tests on CircleCI Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Fold constants in generated code for all runtimes Go: getInlineTestSetWordSize 32 -> 64 Dart: get rid of BigInt Swift: optimize TestSetInline Python: fixes #3698 JavaScript: fixes #3699 Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Use int literals instead of refs for Python and JavaScript Update getMultiTokenAlternativeDescriptor test fixes #3703 Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * update release doc for Go version numbers Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * fix: #2016 Fix Go template list reference, go runtime and got test template for list labels Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * feat: Add a deprecation message to the existing v1 module Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Split tool and runtime tests for GitHub workflow Build only necessary modules for tests Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Remove not used methods from FileUtils (runtime tests) Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Update dependencies of antlr4-maven-plugin Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Update jUnit: 5.8.2 -> 5.9.0 Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Fixes #3733; update ST4 so it uses proper ANTLR 3 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * tweak doc Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Set to 4.11.0 not 4.11 in poms Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare release 4.11.0 Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare for next development iteration Signed-off-by: Eric Vergnaud <[email protected]> * Damn. java target said 4.10.2 not 4.11.0 Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * roll back to 4.11.0; made mistake Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * roll back to 4.11.0; made mistake Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare release antlr4-master-4.11.0 Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare for next development iteration Signed-off-by: Eric Vergnaud <[email protected]> * use build and twine to publish source and wheel Signed-off-by: Qijia Liu <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * tweak doc Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * tweak c++ build script to make Mac binaries with cmake/make Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * tweak release doc Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * tweak code / doc related to bad previous release Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare release 4.11.1 Signed-off-by: Eric Vergnaud <[email protected]> * [maven-release-plugin] prepare for next development iteration Signed-off-by: Eric Vergnaud <[email protected]> * clean up deploy c++ source script Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * cleanup code generation Signed-off-by: Eric Vergnaud <[email protected]> * don't initialize default param values twice Signed-off-by: Eric Vergnaud <[email protected]> * add missing field Signed-off-by: Eric Vergnaud <[email protected]> * update codegen template for 4.11.1 Signed-off-by: Eric Vergnaud <[email protected]> * support new param: Parser Signed-off-by: Eric Vergnaud <[email protected]> * fix template for 4.11 Signed-off-by: Eric Vergnaud <[email protected]> * default export Listener and Visitor Signed-off-by: Eric Vergnaud <[email protected]> * also default export parser and lexer Signed-off-by: Eric Vergnaud <[email protected]> * all tests pass except 7 caused by #3868 Signed-off-by: Eric Vergnaud <[email protected]> * fix issues Signed-off-by: Eric Vergnaud <[email protected]> * make it easy to break Signed-off-by: Eric Vergnaud <[email protected]> * fix #3868 Signed-off-by: Eric Vergnaud <[email protected]> * ALL TESTS PASS!!!! Signed-off-by: Eric Vergnaud <[email protected]> * cross fingers with CI Signed-off-by: Eric Vergnaud <[email protected]> * try fixing broken go tests Signed-off-by: Eric Vergnaud <[email protected]> * Try fix typescript CI Signed-off-by: Eric Vergnaud <[email protected]> * disable cpp for now Signed-off-by: Eric Vergnaud <[email protected]> * fix broken config Signed-off-by: Eric Vergnaud <[email protected]> * try fix macos gh build Signed-off-by: Eric Vergnaud <[email protected]> * improve speed by caching node_modules Signed-off-by: Eric Vergnaud <[email protected]> * no longer using ts-node Signed-off-by: Eric Vergnaud <[email protected]> * fix all tsc warnings Signed-off-by: Eric Vergnaud <[email protected]> * try fix MacOS CI Signed-off-by: Eric Vergnaud <[email protected]> * CI errors seem random, reactivate ubuntu Signed-off-by: Eric Vergnaud <[email protected]> * Disable node_modules caching, which seems to randomly fail in CI Signed-off-by: Eric Vergnaud <[email protected]> * don't delete symlink contents on windows (java bug with is SymbolicLink ?) Signed-off-by: Eric Vergnaud <[email protected]> * fix broken windows CI Signed-off-by: ERIC-WINDOWS\ericv <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * verify windows ci Signed-off-by: ERIC-WINDOWS\ericv <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> * Revert "verify windows ci" This reverts commit 770d821. Signed-off-by: Eric Vergnaud <[email protected]> * reinstate full CI Signed-off-by: Eric Vergnaud <[email protected]> * manually merged * manually merge * fix merge * fix broken template * add template for invoking context list * fix typo * fix test templates * Add code of conduct but with a different name since I do not like that name Signed-off-by: Terence Parr <[email protected]> * Update C# release instructions * Tweak code of conduct Signed-off-by: Terence Parr <[email protected]> * Bring back the Package.swift in the project's root Signed-off-by: Nikolay Edigaryev <[email protected]> * swift-target.md: fix SPM installation instructions Signed-off-by: Nikolay Edigaryev <[email protected]> * the scope (parser or lexer) in @parser::header was dropped, so keep track of it and only include @Header in Listener and Visitor code Signed-off-by: Eric Vergnaud <[email protected]> * drop workaround in favor of #3878 * drop cache usage since it fails in CI * #3878 was missing some scenarios * fix issue when deleting test folder * fix warnings * drop duplicate behavior * drop alien 'abstractRecognizer' property * drop alien property 'channels' * fix various codegen issues * change import * restore js extensions, see microsoft/TypeScript#50501 * use consistent inheritance * more API * more API stuff * fix typo * fix typescript exports * use ts-node to run typescript tests * webpack runtime before linking * fix exec paths on windows Signed-off-by: ERIC-WINDOWS\ericv <[email protected]> * fix failing tests * fix a few import issues * merge typescript-target with latest dev * runs Java and JavaScript tests after merging typescript-target * merge test template * skip unsupported test * fix template prototype * fix missing merge * bump typescript beta version after rebase * update docs * rollback unwanted changes Signed-off-by: HS <[email protected]> Signed-off-by: Eric Vergnaud <[email protected]> Signed-off-by: nicksxs <[email protected]> Signed-off-by: Terence Parr <[email protected]> Signed-off-by: Hell_Ghost <[email protected]> Signed-off-by: Hell_Ghost [email protected] Signed-off-by: Ross Patterson <[email protected]> Signed-off-by: James Taylor <[email protected]> Signed-off-by: Bryan Tan <[email protected]> Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Tim McCormack <[email protected]> Signed-off-by: Ivan Kochurkin <[email protected]> Signed-off-by: Qijia Liu <[email protected]> Signed-off-by: ERIC-WINDOWS\ericv <[email protected]> Signed-off-by: Nikolay Edigaryev <[email protected]> Co-authored-by: HS <[email protected]> Co-authored-by: nicksxs <[email protected]> Co-authored-by: Terence Parr <[email protected]> Co-authored-by: Hell_Ghost <[email protected]> Co-authored-by: Ross Patterson <[email protected]> Co-authored-by: James Taylor <[email protected]> Co-authored-by: Bryan Tan <[email protected]> Co-authored-by: Jim.Idle <[email protected]> Co-authored-by: Tim McCormack <[email protected]> Co-authored-by: Robert Adam <[email protected]> Co-authored-by: Bryan Tan <[email protected]> Co-authored-by: Ivan Kochurkin <[email protected]> Co-authored-by: Qijia Liu <[email protected]> Co-authored-by: Nikolay Edigaryev <[email protected]>
1 parent 31bdd52 commit 2c75e64

File tree

108 files changed

+3687
-1623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3687
-1623
lines changed

.github/workflows/hosted.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ jobs:
167167
go,
168168
java,
169169
javascript,
170+
typescript,
170171
php,
171172
python2,
172173
python3,
@@ -242,7 +243,7 @@ jobs:
242243
architecture: 'x64'
243244

244245
- name: Set up Node 14
245-
if: matrix.target == 'javascript'
246+
if: (matrix.target == 'javascript') || (matrix.target == 'typescript')
246247
uses: actions/[email protected]
247248
with:
248249
node-version: '14'

doc/javascript-target.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,13 @@ antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
195195
196196
## What about TypeScript?
197197
198-
We currently do not have a TypeScript target, but Sam Harwell is [working on a port](https://github.com/tunnelvisionlabs/antlr4ts). [Here](https://github.com/ChuckJonas/extract-interface-ts) is Section 4.3 of [ANTLR 4 book](http://a.co/5jUJYmh) converted to typescript.
198+
We have a [TypeScript target](typescript-target.md), based on the JavaScript target.
199199
200200
## How do I integrate my parser with ACE editor?
201201
202202
This specific task is described in this [dedicated page](ace-javascript-target.md).
203203
204204
## How can I learn more about ANTLR?
205-
206205
207206
Further information can be found from "The definitive ANTLR 4 reference" book.
208207

doc/targets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This page lists the available and upcoming ANTLR runtimes. Please note that you
66
* [C#](csharp-target.md)
77
* [Python](python-target.md) (2 and 3)
88
* [JavaScript](javascript-target.md)
9+
* [TypeScript](typescript-target.md)
910
* [Go](go-target.md)
1011
* [C++](cpp-target.md)
1112
* [Swift](swift-target.md)

doc/typescript-target.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# TypeScript
2+
3+
Antlr4 TypeScript runtime uses the JavaScript runtime and adds type files to it.
4+
This guarantees the same behaviour and performance across both target languages.
5+
Generated lexers, parsers, listeners and visitors are generated in TypeScript.
6+
7+
The runtime is built using TypeScript v4.8.3, node 16.17 and webpack 5.66.
8+
It may work with older versions but they have not been tested and they will not be supported.
9+
10+
11+
## How to create a TypeScript lexer or parser?
12+
13+
This is pretty much the same as creating a Java lexer or parser, except you need to specify the language target, for example:
14+
15+
```bash
16+
$ antlr4 -Dlanguage=TypeScript MyGrammar.g4
17+
```
18+
19+
For a full list of antlr4 tool options, please visit the [tool documentation page](tool-options.md).
20+
21+
## Where can I get the runtime?
22+
23+
Once you've generated the lexer and/or parser code, you need to download the runtime from [npm](https://www.npmjs.com/package/antlr4).
24+
25+
We will not document here how to refer to the runtime from your project, since this would differ a lot depending on your project type and IDE.
26+
27+
## How do I get the runtime in my browser?
28+
29+
The runtime is webpacked and sits in the dist folder. A .map file is also provided.
30+
31+
## How do I run the generated lexer and/or parser?
32+
33+
Let's suppose that your grammar is named, as above, "MyGrammar". Let's suppose this parser comprises a rule named "MyStartRule". The tool will have generated for you the following files:
34+
35+
* MyGrammarLexer.js
36+
* MyGrammarParser.js
37+
* MyGrammarListener.js (if you have not activated the -no-listener option)
38+
* MyGrammarVisitor.js (if you have activated the -visitor option)
39+
40+
There is no listener or visitor interface generated, instead the generated listener and visitor class methods are implemented using lambdas.
41+
42+
Now a fully functioning script might look like the following:
43+
44+
```typescript
45+
import { CharStream, CommonTokenStream } from 'antlr4';
46+
import MyGrammarLexer from './MyGrammarLexer';
47+
import MyGrammarParser from './MyGrammarParser';
48+
49+
const input = "your text to parse here"
50+
const chars = new CharStream(input); // replace this with a FileStream as required
51+
const lexer = new MyGrammarLexer(chars);
52+
const tokens = new CommonTokenStream(lexer);
53+
const parser = new MyGrammarParser(tokens);
54+
const tree = parser.MyStartRule();
55+
56+
```
57+
58+
Tha above program will work. But it won't be useful unless you do one of the following:
59+
60+
* you visit the parse tree using a custom listener
61+
* you visit the parse tree using a custom visitor
62+
* your grammar contains production code (like AntLR3)
63+
64+
(please note that production code is target specific, so you can't have multi target grammars that include production code)
65+
66+
## How do I create and run a visitor?
67+
68+
You need to create a custom visitor and use it to visit the parse tree, as follows:
69+
```typescript
70+
71+
import { ParserRuleContext } from 'antlr4';
72+
import MyGrammarVisitor from './MyGrammarVisitor';
73+
74+
class CustomVisitor extends MyGrammarVisitor {
75+
76+
visitChildren(ctx: ParserRuleContext) {
77+
if (!ctx) {
78+
return;
79+
}
80+
if (ctx.children) {
81+
return ctx.children.map(child => {
82+
if (child.children && child.children.length != 0) {
83+
return child.accept(this);
84+
} else {
85+
return child.getText();
86+
}
87+
});
88+
}
89+
}
90+
}
91+
92+
tree.accept(new Visitor());
93+
````
94+
95+
## How do I create and run a custom listener?
96+
97+
You need to create a custom listener and use it to visit the parse tree, as follows:
98+
99+
```typescript
100+
101+
import { ParseTreeWalker } from 'antlr4';
102+
import MyGrammarListener from './MyGrammarListener';
103+
104+
class MyTreeWalker extends MyGrammarListener {
105+
106+
exitMyStartRule = (ctx: MyStartRuleContext) => {
107+
console.log("In MyStartRule");
108+
};
109+
110+
}
111+
112+
const walker = new MyTreeWalker();
113+
ParseTreeWalker.DEFAULT.walk(walker, tree);
114+
115+
```
116+
117+
## How do I integrate my parser with ACE editor?
118+
119+
This specific task is described in this [dedicated page](ace-javascript-target.md).
120+
121+
## How can I learn more about ANTLR?
122+
123+
Further information can be found from "The definitive ANTLR 4 reference" book.
124+
125+
The TypeScript implementation of ANTLR is based on the JavaScript implementation, which is as close as possible to the Java one, so you shouldn't find it difficult to adapt the book's examples to TypeScript.

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/ParserErrors/ExtraneousInput.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Cpp
2626
CSharp
2727
Go
2828
JavaScript
29+
TypeScript
2930
PHP
3031
Python2
3132
Python3

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/Performance/DropLoopEntryBranchInLRRule_4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ between X1 and X2 or between X3 and X4
5454
Python2
5555
Python3
5656
JavaScript
57+
TypeScript
5758
PHP
5859

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/SemPredEvalParser/PredFromAltTestedInLoopBack_1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CSharp
3939
Dart
4040
Go
4141
JavaScript
42+
TypeScript
4243
PHP
4344
Python2
4445
Python3
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
FileStream,
3+
CommonTokenStream,
4+
DiagnosticErrorListener,
5+
Lexer,
6+
ParseTreeListener,
7+
ParseTreeWalker,
8+
RuleContext,
9+
ParserRuleContext,
10+
RuleNode
11+
} from 'antlr4';
12+
import <lexerName> from './<lexerName>.js';
13+
<if(parserName)>
14+
import <parserName> from './<parserName>.js';
15+
<if(useListener)>
16+
import <grammarName>Listener from './<grammarName>Listener.js';
17+
<endif>
18+
<if(useVisitor)>
19+
import <grammarName>Visitor from './<grammarName>Visitor.js';
20+
<endif>
21+
22+
class TreeShapeListener extends ParseTreeListener {
23+
enterEveryRule(ctx: ParserRuleContext) {
24+
for (let i = 0; i \< ctx.getChildCount(); i++) {
25+
const child = ctx.getChild(i) as RuleContext;
26+
const parent = child.parentCtx;
27+
if (parent!.ruleContext !== ctx || !(parent instanceof RuleNode)) {
28+
throw `Invalid parse tree shape detected.`;
29+
}
30+
}
31+
}
32+
}
33+
<endif>
34+
35+
function main(argv: string[]): void {
36+
const input = new FileStream(argv[2], true);
37+
const lexer = new <lexerName>(input);
38+
const stream = new CommonTokenStream(lexer);
39+
<if(parserName)>
40+
const parser = new <parserName>(stream);
41+
<if(showDiagnosticErrors)>
42+
parser.addErrorListener(new DiagnosticErrorListener());
43+
<endif>
44+
parser.buildParseTrees = true;
45+
parser.printer = {
46+
println : function(s: string) { console.log(s); },
47+
print : function(s: string) { process.stdout.write(s); }
48+
};
49+
const tree = parser.<parserStartRuleName>();
50+
ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);
51+
<else>
52+
stream.fill();
53+
for(let i=0; i\<stream.tokens.length; i++) {
54+
console.log(stream.tokens[i].toString());
55+
}
56+
<if(showDFA)>
57+
process.stdout.write(lexer._interp.decisionToDFA[Lexer.DEFAULT_MODE].toLexerString());
58+
<endif>
59+
<endif>
60+
}
61+
62+
main(process.argv);
63+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "module",
3+
"devDependencies": {
4+
"@types/node": "^18.0.5"
5+
},
6+
"dependencies": {
7+
"antlr4": "^4.10.1"
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ES2020",
4+
"moduleResolution": "node",
5+
"target": "ES6",
6+
"noImplicitAny": true,
7+
},
8+
"ts-node": {
9+
"esm": true
10+
}
11+
}

0 commit comments

Comments
 (0)