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] Fix #3314 -- base classes out of date, using obsolete antlr4ts fork. #4264

Merged
merged 7 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
79 changes: 15 additions & 64 deletions javascript/typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,27 @@
# TypeScript grammar

This TypeScript grammar does not exactly correspond to the TypeScript standard.
The main goal during developing was practical usage, performance, and clarity
(getting rid of duplicates).

## Universal Actions & Semantic Predicates

Some modern TypeScript syntax can not be handled with standard context-free
grammars, for example detection of the `get` keyword in getters and `get` identifiers
in other cases. Moreover, some parser options can be defined externally (`use strict`)
and should be considered during the parsing process.

For such complex syntax, [actions](https://github.com/antlr/antlr4/blob/master/doc/actions.md) and
[predicates](https://github.com/antlr/antlr4/blob/master/doc/predicates.md) are
used. This is a second grammar in repository that attempts to use **universal**
actions and predicates. At least, it works for **C#** and **Java** runtimes.

Consider the `getter` rule in grammar:

```ANTLR
getter
: Identifier{p("get")}? propertyName
;
```

Instruction `p("get")` stands for *get the previous token value and return a boolean
value as a result of comparison to "get" string*.

The **Java** runtime is described by the following code in [Java/TypeScriptLexerBase.java](Java/TypeScriptLexerBase.java)

```Java
protected boolean prev(String str) {
return _input.LT(-1).getText().equals(str);
}
```

The **C#** runtime, by Sam Harwell, is described by
[CSharp/TypeScriptParserBase.cs](CSharp/TypeScriptParserBase.cs)
## Authors

```CSharp
protected bool prev(string str)
{
return _input.LT(-1).Text.Equals(str);
}
```

Furthermore, the [`superClass`](https://github.com/antlr/antlr4/blob/master/doc/options.md)
lexer and parser grammar files options should be defined in the following manner:

```ANTLR
options {
tokenVocab=TypeScriptLexer;
superClass=TypeScriptBaseParser;
}
```

Runtimes super class names (`TypeScriptLexer`, `TypeScriptParser`) should be
the same, for correct parser generation.

## Syntax support

Based on [JavaScript grammar](https://github.com/loonydev/grammars-v4/tree/master/javascript) by [Positive Technologies](https://github.com/PositiveTechnologies)
* Andrii Artiushok (2019) - initial version

### TypeScript
## Description

See the [examples](examples) directory for test data files.
This TypeScript grammar does not exactly correspond to the TypeScript standard.
The main goal during developing was practical usage, performance, and clarity
(getting rid of duplicates).

## Main contributors
The syntax is based on [JavaScript grammar](https://github.com/loonydev/grammars-v4/tree/master/javascript)
by [Positive Technologies](https://github.com/PositiveTechnologies).

* Andrii Artiushok (2019) - initial version
## Links

* https://www.typescriptlang.org/
* https://github.com/Microsoft/TypeScript/blob/730f18955dc17068be33691f0fb0e0285ebbf9f5/doc/spec.md -- the abandoned specification.

## License

[MIT](https://opensource.org/licenses/MIT)

## Issues

* The grammar is very old and there are many ambiguities: primaryType Decision 11; singleExpression Decision 236; etc.
6 changes: 3 additions & 3 deletions javascript/typescript/TypeScript/TypeScriptLexerBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Lexer, Token, CharStream} from "antlr4ts";
import {TypeScriptLexer} from './TypeScriptLexer';
import {Lexer, Token, CharStream} from "antlr4";
import TypeScriptLexer from './TypeScriptLexer';

/**
* All lexer methods that used in grammar (IsStrictMode)
* should start with Upper Case Char similar to Lexer rules.
*/
export abstract class TypeScriptLexerBase extends Lexer {
export default abstract class TypeScriptLexerBase extends Lexer {
/**
* Stores values of nested modes. By default mode is strict or
* defined externally (useStrictDefault)
Expand Down
16 changes: 8 additions & 8 deletions javascript/typescript/TypeScript/TypeScriptParserBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Parser, Lexer, Token, TokenStream} from 'antlr4ts';
import {TypeScriptParser} from './TypeScriptParser';
import {Parser, Lexer, Token, TokenStream} from 'antlr4';
import TypeScriptParser from './TypeScriptParser';

/**
* All parser methods that used in grammar (p, prev, notLineTerminator, etc.)
* should start with lower case char similar to parser rules.
*/
export abstract class TypeScriptParserBase extends Parser {
export default abstract class TypeScriptParserBase extends Parser {

constructor(input: TokenStream) {
super(input);
Expand Down Expand Up @@ -68,12 +68,12 @@ export abstract class TypeScriptParserBase extends Parser {
private here(type:number):boolean {

// Get the token ahead of the current index.
const possibleIndexEosToken: number = this.currentToken.tokenIndex - 1;
const possibleIndexEosToken: number = this.getCurrentToken().tokenIndex - 1;
const ahead: Token = this._input.get(possibleIndexEosToken);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return (ahead.channel == Lexer.HIDDEN) && (ahead.type == type);
return (ahead.channel == Token.HIDDEN_CHANNEL) && (ahead.type == type);
}

/**
Expand All @@ -90,10 +90,10 @@ export abstract class TypeScriptParserBase extends Parser {
protected lineTerminatorAhead():boolean {

// Get the token ahead of the current index.
let possibleIndexEosToken: number = this.currentToken.tokenIndex - 1;
let possibleIndexEosToken: number = this.getCurrentToken().tokenIndex - 1;
let ahead: Token = this._input.get(possibleIndexEosToken);

if (ahead.channel != Lexer.HIDDEN) {
if (ahead.channel != Token.HIDDEN_CHANNEL) {
// We're only interested in tokens on the HIDDEN channel.
return false;
}
Expand All @@ -105,7 +105,7 @@ export abstract class TypeScriptParserBase extends Parser {

if (ahead.type == TypeScriptParser.WhiteSpaces) {
// Get the token ahead of the current whitespaces.
possibleIndexEosToken = this.currentToken.tokenIndex - 2;
possibleIndexEosToken = this.getCurrentToken().tokenIndex - 2;
ahead = this._input.get(possibleIndexEosToken);
}

Expand Down
4 changes: 2 additions & 2 deletions javascript/typescript/TypeScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ primaryType
| predefinedType # PredefinedPrimType
| typeReference # ReferencePrimType
| objectType # ObjectPrimType
| primaryType {notLineTerminator()}? '[' primaryType? ']' # ArrayPrimType
| primaryType {this.notLineTerminator()}? '[' primaryType? ']' # ArrayPrimType
| '[' tupleElementTypes ']' # TuplePrimType
| typeQuery # QueryPrimType
| This # ThisPrimType
Expand Down Expand Up @@ -160,7 +160,7 @@ typeMember
;

arrayType
: primaryType {notLineTerminator()}? '[' ']'
: primaryType {this.notLineTerminator()}? '[' ']'
;

tupleType
Expand Down
3 changes: 2 additions & 1 deletion javascript/typescript/desc.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
<antlr-version>^4.7</antlr-version>
<targets>CSharp;Java</targets>
<targets>CSharp;Java;TypeScript</targets>
<inputs>examples/*.ts</inputs>
</desc>
Loading