diff --git a/javascript/typescript/README.md b/javascript/typescript/README.md index 8364701067..c932264c56 100644 --- a/javascript/typescript/README.md +++ b/javascript/typescript/README.md @@ -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. diff --git a/javascript/typescript/TypeScript/TypeScriptLexerBase.ts b/javascript/typescript/TypeScript/TypeScriptLexerBase.ts index 8e7c258166..95a39176d8 100644 --- a/javascript/typescript/TypeScript/TypeScriptLexerBase.ts +++ b/javascript/typescript/TypeScript/TypeScriptLexerBase.ts @@ -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) diff --git a/javascript/typescript/TypeScript/TypeScriptParserBase.ts b/javascript/typescript/TypeScript/TypeScriptParserBase.ts index 1ae69ff0f6..38d734c7b4 100644 --- a/javascript/typescript/TypeScript/TypeScriptParserBase.ts +++ b/javascript/typescript/TypeScript/TypeScriptParserBase.ts @@ -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); @@ -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); } /** @@ -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; } @@ -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); } diff --git a/javascript/typescript/TypeScriptParser.g4 b/javascript/typescript/TypeScriptParser.g4 index 2f79fd7172..dcb2fa5336 100644 --- a/javascript/typescript/TypeScriptParser.g4 +++ b/javascript/typescript/TypeScriptParser.g4 @@ -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 @@ -160,7 +160,7 @@ typeMember ; arrayType - : primaryType {notLineTerminator()}? '[' ']' + : primaryType {this.notLineTerminator()}? '[' ']' ; tupleType diff --git a/javascript/typescript/desc.xml b/javascript/typescript/desc.xml index 26e6fa791d..86663b9642 100644 --- a/javascript/typescript/desc.xml +++ b/javascript/typescript/desc.xml @@ -1,5 +1,6 @@ ^4.7 - CSharp;Java + CSharp;Java;TypeScript + examples/*.ts diff --git a/javascript/typescript/example_not_work/Interface.ts b/javascript/typescript/examples/Interface.ts similarity index 100% rename from javascript/typescript/example_not_work/Interface.ts rename to javascript/typescript/examples/Interface.ts