Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript target #4027

Merged
merged 180 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
180 commits
Select commit Hold shift + click to select a range
8347aff
Fix CMake syntax for variable expansion
hs-apotell Jul 6, 2022
de0efeb
initial commit
ericvergnaud Jul 17, 2022
e611332
renamed for clarity
ericvergnaud Jul 17, 2022
e8f59d6
renamed for clarity
ericvergnaud Jul 17, 2022
a4aa1fb
able to locate antlr4 runtime using ts-node, missing types
ericvergnaud Jul 17, 2022
6915202
progressing
ericvergnaud Jul 17, 2022
c0fd00f
able to 'run' a test. It fails but it compiles and resolves!
ericvergnaud Sep 3, 2022
b4f9e93
reflect refactored runtime
ericvergnaud Sep 3, 2022
971645f
able to run RecursiveLexerRuleRefWithWildcardPlus_1 test locally
ericvergnaud Sep 3, 2022
4c80ae1
passes LexerExec tests in IntelliJ
ericvergnaud Sep 3, 2022
16b121a
make ATN private
ericvergnaud Sep 3, 2022
009107c
ignore same tests as JavaScript
ericvergnaud Sep 3, 2022
66144f8
compiles Parser and Lexer bu local run fails
ericvergnaud Sep 4, 2022
484cc5a
ParserExec.TokenOffset test successful in IntelliJ !
ericvergnaud Sep 4, 2022
e34c664
Progressing, passing 131 of 348 tests
ericvergnaud Sep 5, 2022
9aca326
pass 327 tests out of 348
ericvergnaud Sep 5, 2022
a9b058f
more successful tests
ericvergnaud Sep 6, 2022
dcff0b0
333 successful tests out of 348
ericvergnaud Sep 6, 2022
06fd2d9
all tests pass except 7 caused by #3868
ericvergnaud Sep 6, 2022
0a5f10e
update getting-started doc
Nicksxs Jun 14, 2022
291d974
add blank github action file for hosted CI
parrt Jul 15, 2022
d7c6681
update getting started document to say java 11
parrt Jul 22, 2022
1fab453
Revert "update getting started document to say java 11"
parrt Jul 22, 2022
ac470cb
add C# book code links
parrt Jul 22, 2022
3fb98a3
Add Jim/Ken to readme
parrt Aug 13, 2022
20d6f35
Update Swift Package to support static library
HellGhost Apr 19, 2022
0d8116e
Update Package.swift
HellGhost Apr 20, 2022
cdfaeca
Add caching support for maven & dependencies. Also, include caching for
hs-apotell Aug 14, 2022
778bcc3
use snap to install go 1.19
parrt Aug 15, 2022
7298b4d
grr...install snap
parrt Aug 15, 2022
22bc7ba
ugh. start snap
parrt Aug 15, 2022
b8de845
ugh. start snap
parrt Aug 15, 2022
3ac6040
ugh. cant get snap to install go
parrt Aug 15, 2022
46f4f0d
try downloading golang with curl
parrt Aug 15, 2022
287750a
Issue #3823: Temporarily disable a few tests on CI
hs-apotell Aug 15, 2022
2c928d9
update actions status badge
parrt Sep 2, 2022
5aa550c
update getting started document to say java 11
parrt Jul 22, 2022
06be349
Revert "update getting started document to say java 11"
parrt Jul 22, 2022
7cf65fb
update getting-started doc
Nicksxs Jun 14, 2022
b992179
make getValue visible to external profiler tools.
parrt Jul 23, 2022
3cb4dc2
Fix #3508: Document the $parser attribute and its use in target-agnos…
RossPatterson Aug 1, 2022
543780e
Add accessor to IntervalSet for intervals
JamesRTaylor Aug 1, 2022
90eaf64
Remove libuuid dependency from C++ runtime
Technius Jul 14, 2022
4e64674
Add `@SuppressWarnings("CheckReturnValue")` to prevent error_prone li…
parrt Aug 9, 2022
ae9bc5f
fix: Fixes for https://github.com/antlr/antlr4/issues/3718
Aug 13, 2022
ae35acf
fix: #3718 Revert accidental keyboard error in Java target
Aug 14, 2022
3c0d902
fix: #3718 Correct DFAState index in Lexer ATN Simulator
Aug 15, 2022
3dc5810
fix: #3718 Fix go runtime test runners
Aug 15, 2022
a8f8441
Rm remote github actions; hosted seems to work
parrt Aug 13, 2022
e56dba2
install golang with curl; go was missing from dev
parrt Aug 15, 2022
9d90942
fix: Rework of all Hash() and Equals() methods - implement generic co…
Aug 20, 2022
1d253d3
fix: #3718 Correct DFAState index in Lexer ATN Simulator
Aug 15, 2022
d89bf4e
feat: Reduce initial memory allocations for collections
Aug 21, 2022
cf95233
fix: #3758 Allow for string being a keyword and fix go template to us…
Aug 22, 2022
a4399af
fix: #3758 Add go.sum to the repo
Aug 23, 2022
f3aa043
fix: #3758 Ensure that standard runtime extensions are included in go…
Aug 23, 2022
34f209b
fix: #2826 Go template is incorrect for dynamic scopes
Aug 22, 2022
e78a7f0
fix: #2016 - Generate correct iGo code for lists in a grammar, such a…
Aug 23, 2022
2737936
feat: Bump poms to use 4.11 Snapshot
Aug 23, 2022
049afb1
turn off Golang test at circleci for now
parrt Aug 26, 2022
ba58f09
Replace smart-quote with single-quote in code examples
timmc Aug 26, 2022
226e130
Augment error message during testing to include full cause of problem.
parrt Aug 27, 2022
0041270
Augment error message during testing to include full cause of problem…
parrt Aug 27, 2022
9c02642
Cpp: Link to threads library
Krzmbrzl Jul 18, 2022
e33c906
add test for #2016 and fix java.
parrt Aug 27, 2022
9a0be4b
ensure all targets have the appropriate argument list for the templat…
parrt Aug 27, 2022
2694548
Fix other targets
parrt Aug 27, 2022
534f1e2
fix format
parrt Aug 28, 2022
e5dd859
add check that $args is a list
parrt Aug 28, 2022
c6bf3ca
change made by @lingyv-li to fix bug in DART exposed by this test.
parrt Aug 28, 2022
f0b1cc6
fix AssertIsList in multiple targets. Go doesn't pass test and has no…
parrt Aug 28, 2022
23bf0d4
C++ gets an exception with this test so I'm turning it off. See https…
parrt Aug 28, 2022
40893e1
feat: #3840 Move Go to version v4.11.0
Aug 31, 2022
120c436
feat: #3840 Create the v4 version of the Go runtime
Aug 31, 2022
1f6b441
feat: Create the v4 runtime layout for the Go runtime, ready for rele…
Aug 31, 2022
cab9eba
Reenable go tests on CircleCI
KvanTTT Aug 30, 2022
ec4184d
Fold constants in generated code for all runtimes
KvanTTT Aug 30, 2022
44598d0
Use int literals instead of refs for Python and JavaScript
KvanTTT Aug 30, 2022
b6dd6ec
update release doc for Go version numbers
parrt Sep 2, 2022
cc5cc1a
fix: #2016 Fix Go template list reference, go runtime and got test te…
Aug 30, 2022
4853a32
feat: Add a deprecation message to the existing v1 module
Sep 1, 2022
94e9579
Split tool and runtime tests for GitHub workflow
KvanTTT Sep 1, 2022
2eb204c
Remove not used methods from FileUtils (runtime tests)
KvanTTT Sep 1, 2022
eeb203a
Update dependencies of antlr4-maven-plugin
KvanTTT Sep 1, 2022
b528b01
Update jUnit: 5.8.2 -> 5.9.0
KvanTTT Sep 2, 2022
b01e6c9
Fixes #3733; update ST4 so it uses proper ANTLR 3
parrt Sep 3, 2022
b0b939b
tweak doc
parrt Sep 4, 2022
4fa7d59
Set to 4.11.0 not 4.11 in poms
parrt Sep 4, 2022
e4dae63
[maven-release-plugin] prepare release 4.11.0
parrt Sep 4, 2022
d2dcdf0
[maven-release-plugin] prepare for next development iteration
parrt Sep 4, 2022
5e34cf2
Damn. java target said 4.10.2 not 4.11.0
parrt Sep 4, 2022
24c428f
roll back to 4.11.0; made mistake
parrt Sep 4, 2022
d9dd4e7
roll back to 4.11.0; made mistake
parrt Sep 4, 2022
a97d64f
[maven-release-plugin] prepare release antlr4-master-4.11.0
parrt Sep 4, 2022
2fbce07
[maven-release-plugin] prepare for next development iteration
parrt Sep 4, 2022
3ef1175
use build and twine to publish source and wheel
eagleoflqj Aug 5, 2022
3fd76cc
tweak doc
parrt Sep 4, 2022
3c03b13
tweak c++ build script to make Mac binaries with cmake/make
parrt Sep 4, 2022
1282aeb
tweak release doc
parrt Sep 4, 2022
2a339b1
tweak code / doc related to bad previous release
parrt Sep 4, 2022
cc6255d
[maven-release-plugin] prepare release 4.11.1
parrt Sep 4, 2022
247ea95
[maven-release-plugin] prepare for next development iteration
parrt Sep 4, 2022
4f51c8e
clean up deploy c++ source script
parrt Sep 4, 2022
7ecc8d4
cleanup code generation
ericvergnaud Sep 6, 2022
4436371
don't initialize default param values twice
ericvergnaud Sep 6, 2022
5ae181d
add missing field
ericvergnaud Sep 6, 2022
6606541
update codegen template for 4.11.1
ericvergnaud Sep 6, 2022
12bb4c0
support new param: Parser
ericvergnaud Sep 6, 2022
c686a23
fix template for 4.11
ericvergnaud Sep 6, 2022
53f37e6
default export Listener and Visitor
ericvergnaud Sep 6, 2022
3346121
also default export parser and lexer
ericvergnaud Sep 6, 2022
721dd27
all tests pass except 7 caused by #3868
ericvergnaud Sep 6, 2022
59a7cda
fix issues
ericvergnaud Sep 7, 2022
974606e
make it easy to break
ericvergnaud Sep 7, 2022
4cdda92
fix #3868
ericvergnaud Sep 7, 2022
3c177ad
ALL TESTS PASS!!!!
ericvergnaud Sep 7, 2022
adfea72
cross fingers with CI
ericvergnaud Sep 7, 2022
c9fda4c
try fixing broken go tests
ericvergnaud Sep 7, 2022
aab39f3
Try fix typescript CI
ericvergnaud Sep 7, 2022
3a2c80a
disable cpp for now
ericvergnaud Sep 7, 2022
b9155d5
fix broken config
ericvergnaud Sep 7, 2022
0385b0f
try fix macos gh build
ericvergnaud Sep 8, 2022
b569a8a
improve speed by caching node_modules
ericvergnaud Sep 8, 2022
b6f3a3e
no longer using ts-node
ericvergnaud Sep 8, 2022
46cd715
fix all tsc warnings
ericvergnaud Sep 8, 2022
d579281
try fix MacOS CI
ericvergnaud Sep 8, 2022
cfb7a07
CI errors seem random, reactivate ubuntu
ericvergnaud Sep 8, 2022
f7ca70f
Disable node_modules caching, which seems to randomly fail in CI
ericvergnaud Sep 9, 2022
cd42048
don't delete symlink contents on windows (java bug with is SymbolicLi…
ericvergnaud Sep 9, 2022
39f9187
fix broken windows CI
ericvergnaud Sep 9, 2022
d1ce99f
verify windows ci
ericvergnaud Sep 9, 2022
e0b49ff
Revert "verify windows ci"
ericvergnaud Sep 9, 2022
e46a957
reinstate full CI
ericvergnaud Sep 9, 2022
c120035
Merge branch 'dev' into typescript-target
ericvergnaud Sep 9, 2022
b267b88
manually merged
ericvergnaud Sep 9, 2022
994c1da
manually merge
ericvergnaud Sep 9, 2022
2196356
fix merge
ericvergnaud Sep 9, 2022
32d8324
fix broken template
ericvergnaud Sep 9, 2022
8459a10
add template for invoking context list
ericvergnaud Sep 9, 2022
ce4ef34
fix typo
ericvergnaud Sep 9, 2022
6243515
fix test templates
ericvergnaud Sep 9, 2022
dd6499b
Add code of conduct but with a different name since I do not like tha…
parrt Sep 10, 2022
13cade1
Update C# release instructions
ericvergnaud Sep 4, 2022
32b9ab1
Tweak code of conduct
parrt Sep 11, 2022
2056bdc
Bring back the Package.swift in the project's root
edigaryev Sep 6, 2022
f85941a
swift-target.md: fix SPM installation instructions
edigaryev Sep 6, 2022
0d9f564
the scope (parser or lexer) in @parser::header was dropped, so keep t…
ericvergnaud Sep 8, 2022
0ba2430
Merge commit 'aa1f1f12a846846fcb78544bdb2ad135471376db' into typescri…
ericvergnaud Sep 12, 2022
d47725c
drop workaround in favor of #3878
ericvergnaud Sep 12, 2022
c51aa8e
drop cache usage since it fails in CI
ericvergnaud Sep 12, 2022
3f53baf
#3878 was missing some scenarios
ericvergnaud Sep 12, 2022
4f68fd1
fix issue when deleting test folder
ericvergnaud Sep 12, 2022
8598898
fix warnings
ericvergnaud Sep 18, 2022
c77c6a4
drop duplicate behavior
ericvergnaud Sep 18, 2022
0ecd794
drop alien 'abstractRecognizer' property
ericvergnaud Sep 18, 2022
fba546f
drop alien property 'channels'
ericvergnaud Sep 18, 2022
b309992
fix various codegen issues
ericvergnaud Sep 18, 2022
7a41586
change import
ericvergnaud Sep 18, 2022
73aa072
restore js extensions, see https://github.com/microsoft/TypeScript/is…
ericvergnaud Sep 18, 2022
49208a0
use consistent inheritance
ericvergnaud Sep 18, 2022
475ce03
more API
ericvergnaud Sep 18, 2022
cfc617f
more API stuff
ericvergnaud Oct 6, 2022
8b56bd9
fix typo
ericvergnaud Oct 6, 2022
553d49d
fix typescript exports
ericvergnaud Oct 16, 2022
c89dcbd
use ts-node to run typescript tests
ericvergnaud Oct 16, 2022
c52f538
webpack runtime before linking
ericvergnaud Oct 16, 2022
1d350cb
fix exec paths on windows
ericvergnaud Oct 16, 2022
c4cf52b
fix failing tests
ericvergnaud Oct 17, 2022
5d4692e
fix a few import issues
ericvergnaud Oct 26, 2022
50405e3
merge typescript-target with latest dev
ericvergnaud Dec 18, 2022
b8c79ec
merge typescript-target with latest dev
ericvergnaud Dec 18, 2022
4c49d40
runs Java and JavaScript tests after merging typescript-target
ericvergnaud Dec 18, 2022
e075ac0
merge test template
ericvergnaud Dec 18, 2022
072a57b
skip unsupported test
ericvergnaud Dec 18, 2022
c03493f
fix template prototype
ericvergnaud Dec 18, 2022
f665f98
fix missing merge
ericvergnaud Dec 18, 2022
52c2095
bump typescript beta version after rebase
ericvergnaud Dec 18, 2022
fc53efe
update docs
ericvergnaud Dec 18, 2022
bed7b5d
Merge branch 'dev' into typescript-target
ericvergnaud Dec 20, 2022
c13d89e
rollback unwanted changes
ericvergnaud Dec 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/hosted.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ jobs:
go,
java,
javascript,
typescript,
php,
python2,
python3,
Expand Down Expand Up @@ -242,7 +243,7 @@ jobs:
architecture: 'x64'

- name: Set up Node 14
if: matrix.target == 'javascript'
if: (matrix.target == 'javascript') || (matrix.target == 'typescript')
uses: actions/[email protected]
with:
node-version: '14'
Expand Down
3 changes: 1 addition & 2 deletions doc/javascript-target.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,13 @@ antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);

## What about TypeScript?

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.
We have a [TypeScript target](typescript-target.md), based on the JavaScript target.

## How do I integrate my parser with ACE editor?

This specific task is described in this [dedicated page](ace-javascript-target.md).

## How can I learn more about ANTLR?


Further information can be found from "The definitive ANTLR 4 reference" book.

Expand Down
1 change: 1 addition & 0 deletions doc/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This page lists the available and upcoming ANTLR runtimes. Please note that you
* [C#](csharp-target.md)
* [Python](python-target.md) (2 and 3)
* [JavaScript](javascript-target.md)
* [TypeScript](typescript-target.md)
* [Go](go-target.md)
* [C++](cpp-target.md)
* [Swift](swift-target.md)
Expand Down
125 changes: 125 additions & 0 deletions doc/typescript-target.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# TypeScript

Antlr4 TypeScript runtime uses the JavaScript runtime and adds type files to it.
This guarantees the same behaviour and performance across both target languages.
Generated lexers, parsers, listeners and visitors are generated in TypeScript.

The runtime is built using TypeScript v4.8.3, node 16.17 and webpack 5.66.
It may work with older versions but they have not been tested and they will not be supported.


## How to create a TypeScript lexer or parser?

This is pretty much the same as creating a Java lexer or parser, except you need to specify the language target, for example:

```bash
$ antlr4 -Dlanguage=TypeScript MyGrammar.g4
```

For a full list of antlr4 tool options, please visit the [tool documentation page](tool-options.md).

## Where can I get the runtime?

Once you've generated the lexer and/or parser code, you need to download the runtime from [npm](https://www.npmjs.com/package/antlr4).

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.

## How do I get the runtime in my browser?

The runtime is webpacked and sits in the dist folder. A .map file is also provided.

## How do I run the generated lexer and/or parser?

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:

* MyGrammarLexer.js
* MyGrammarParser.js
* MyGrammarListener.js (if you have not activated the -no-listener option)
* MyGrammarVisitor.js (if you have activated the -visitor option)

There is no listener or visitor interface generated, instead the generated listener and visitor class methods are implemented using lambdas.

Now a fully functioning script might look like the following:

```typescript
import { CharStream, CommonTokenStream } from 'antlr4';
import MyGrammarLexer from './MyGrammarLexer';
import MyGrammarParser from './MyGrammarParser';

const input = "your text to parse here"
const chars = new CharStream(input); // replace this with a FileStream as required
const lexer = new MyGrammarLexer(chars);
const tokens = new CommonTokenStream(lexer);
const parser = new MyGrammarParser(tokens);
const tree = parser.MyStartRule();

```

Tha above program will work. But it won't be useful unless you do one of the following:

* you visit the parse tree using a custom listener
* you visit the parse tree using a custom visitor
* your grammar contains production code (like AntLR3)

(please note that production code is target specific, so you can't have multi target grammars that include production code)

## How do I create and run a visitor?

You need to create a custom visitor and use it to visit the parse tree, as follows:
```typescript

import { ParserRuleContext } from 'antlr4';
import MyGrammarVisitor from './MyGrammarVisitor';

class CustomVisitor extends MyGrammarVisitor {

visitChildren(ctx: ParserRuleContext) {
if (!ctx) {
return;
}
if (ctx.children) {
return ctx.children.map(child => {
if (child.children && child.children.length != 0) {
return child.accept(this);
} else {
return child.getText();
}
});
}
}
}

tree.accept(new Visitor());
````

## How do I create and run a custom listener?

You need to create a custom listener and use it to visit the parse tree, as follows:

```typescript

import { ParseTreeWalker } from 'antlr4';
import MyGrammarListener from './MyGrammarListener';

class MyTreeWalker extends MyGrammarListener {

exitMyStartRule = (ctx: MyStartRuleContext) => {
console.log("In MyStartRule");
};

}

const walker = new MyTreeWalker();
ParseTreeWalker.DEFAULT.walk(walker, tree);

```

## How do I integrate my parser with ACE editor?

This specific task is described in this [dedicated page](ace-javascript-target.md).

## How can I learn more about ANTLR?

Further information can be found from "The definitive ANTLR 4 reference" book.

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.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Cpp
CSharp
Go
JavaScript
TypeScript
PHP
Python2
Python3
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ between X1 and X2 or between X3 and X4
Python2
Python3
JavaScript
TypeScript
PHP

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CSharp
Dart
Go
JavaScript
TypeScript
PHP
Python2
Python3
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
FileStream,
CommonTokenStream,
DiagnosticErrorListener,
Lexer,
ParseTreeListener,
ParseTreeWalker,
RuleContext,
ParserRuleContext,
RuleNode
} from 'antlr4';
import <lexerName> from './<lexerName>.js';
<if(parserName)>
import <parserName> from './<parserName>.js';
<if(useListener)>
import <grammarName>Listener from './<grammarName>Listener.js';
<endif>
<if(useVisitor)>
import <grammarName>Visitor from './<grammarName>Visitor.js';
<endif>

class TreeShapeListener extends ParseTreeListener {
enterEveryRule(ctx: ParserRuleContext) {
for (let i = 0; i \< ctx.getChildCount(); i++) {
const child = ctx.getChild(i) as RuleContext;
const parent = child.parentCtx;
if (parent!.ruleContext !== ctx || !(parent instanceof RuleNode)) {
throw `Invalid parse tree shape detected.`;
}
}
}
}
<endif>

function main(argv: string[]): void {
const input = new FileStream(argv[2], true);
const lexer = new <lexerName>(input);
const stream = new CommonTokenStream(lexer);
<if(parserName)>
const parser = new <parserName>(stream);
<if(showDiagnosticErrors)>
parser.addErrorListener(new DiagnosticErrorListener());
<endif>
parser.buildParseTrees = true;
parser.printer = {
println : function(s: string) { console.log(s); },
print : function(s: string) { process.stdout.write(s); }
};
const tree = parser.<parserStartRuleName>();
ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);
<else>
stream.fill();
for(let i=0; i\<stream.tokens.length; i++) {
console.log(stream.tokens[i].toString());
}
<if(showDFA)>
process.stdout.write(lexer._interp.decisionToDFA[Lexer.DEFAULT_MODE].toLexerString());
<endif>
<endif>
}

main(process.argv);

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "module",
"devDependencies": {
"@types/node": "^18.0.5"
},
"dependencies": {
"antlr4": "^4.10.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "ES2020",
"moduleResolution": "node",
"target": "ES6",
"noImplicitAny": true,
},
"ts-node": {
"esm": true
}
}
Loading