api.chapi-ast-typescript.2.5.1.source-code.TypeScriptLexer.g4 Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 by Bart Kiers (original author) and Alexandre Vitorelli (contributor -> ported to CSharp)
* Copyright (c) 2017 by Ivan Kochurkin (Positive Technologies):
added ECMAScript 6 support, cleared and transformed to the universal grammar.
* Copyright (c) 2018 by Juan Alvarez (contributor -> ported to Go)
* Copyright (c) 2019 by Andrii Artiushok (contributor -> added TypeScript support)
* Copyright (c) 2022 by NumberFour AG (contributor -> overall restructuring and d.ts support)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
lexer grammar TypeScriptLexer;
options {
superClass=TypeScriptLexerBase;
}
channels { ERROR, JSDOC }
JsxComment: '{/*' .*? '*/}' -> channel(HIDDEN);
JSDocComment: '/**' .*? '*/' -> channel(JSDOC);
MultiLineComment: '/*' .*? '*/' -> channel(HIDDEN);
SingleLineComment: '//' ~[\r\n\u2028\u2029]* -> channel(HIDDEN);
RegularExpressionLiteral: '/' RegularExpressionFirstChar RegularExpressionChar* {this.IsRegexPossible()}? '/' IdentifierPart*;
OpenBracket: '[';
CloseBracket: ']';
OpenParen: '(';
CloseParen: ')';
OpenBrace: '{';
CloseBrace: '}' {this.popModeIfInTamplateString();};
SemiColon: ';';
Comma: ',';
Assign: '=';
QuestionMark: '?';
Colon: ':';
Ellipsis: '...';
Dot: '.';
PlusPlus: '++';
MinusMinus: '--';
Plus: '+';
Minus: '-';
BitNot: '~';
Not: '!';
Multiply: '*';
Divide: '/';
Modulus: '%';
//RightShiftArithmetic: '>''>'; // split up to allow nested generics
LeftShiftArithmetic: '<<';
//RightShiftLogical: '>''>''>'; // split up to allow nested generics
LessThan: '<';
MoreThan: '>';
LessThanEquals: '<=';
GreaterThanEquals: '>=';
Equals_: '==';
NotEquals: '!=';
IdentityEquals: '===';
IdentityNotEquals: '!==';
BitAnd: '&';
BitXOr: '^';
BitOr: '|';
And: '&&';
Or: '||';
MultiplyAssign: '*=';
DivideAssign: '/=';
ModulusAssign: '%=';
PlusAssign: '+=';
MinusAssign: '-=';
LeftShiftArithmeticAssign: '<<=';
RightShiftArithmeticAssign: '>>=';
RightShiftLogicalAssign: '>>>=';
BitAndAssign: '&=';
BitXorAssign: '^=';
BitOrAssign: '|=';
ARROW: '=>';
Hashtag: '#';
Lodash: '_'; // lodash
Dollar: '$'; // jquery
/// Null Literals
NullLiteral: 'null';
/// Undefined Literals
UndefinedLiteral: 'undefined';
/// Boolean Literals
BooleanLiteral: 'true'
| 'false';
/// Numeric Literals
DecimalLiteral: DecimalIntegerLiteral '.' [0-9] [0-9_]* ExponentPart?
| '.' [0-9] [0-9_]* ExponentPart?
| DecimalIntegerLiteral ExponentPart?
;
/// Numeric Literals
HexIntegerLiteral: '0' [xX] [0-9a-fA-F] HexDigit*;
OctalIntegerLiteral: '0' [0-7]+ {!this.IsStrictMode()}?;
OctalIntegerLiteral2: '0' [oO] [0-7] [_0-7]*;
BinaryIntegerLiteral: '0' [bB] [01] [_01]*;
BigHexIntegerLiteral: '0' [xX] [0-9a-fA-F] HexDigit* 'n';
BigOctalIntegerLiteral: '0' [oO] [0-7] [_0-7]* 'n';
BigBinaryIntegerLiteral: '0' [bB] [01] [_01]* 'n';
BigDecimalIntegerLiteral: DecimalIntegerLiteral 'n';
/// Keywords
/// Keywords
Break: 'break';
Do: 'do';
Instanceof: 'instanceof';
Typeof: 'typeof';
Unique: 'unique';
Keyof: 'keyof';
Case: 'case';
Else: 'else';
New: 'new';
Target: 'target';
Var: 'var';
Catch: 'catch';
Finally: 'finally';
Return: 'return';
Void: 'void';
Continue: 'continue';
For: 'for';
Switch: 'switch';
While: 'while';
Debugger: 'debugger';
Function: 'function';
This: 'this';
With: 'with';
Default: 'default';
If: 'if';
Throw: 'throw';
Delete: 'delete';
In: 'in';
Try: 'try';
As: 'as';
From: 'from';
ReadOnly: 'readonly';
Async: 'async';
Await: 'await';
/// Future Reserved Words
Class: 'class';
Enum: 'enum';
Extends: 'extends';
Super: 'super';
Const: 'const';
Export: 'export';
Import: 'import';
/// The following tokens are also considered to be FutureReservedWords
/// when parsing strict mode
Implements: 'implements' ;
Let: 'let' ;
Private: 'private';
Public: 'public' ;
Interface: 'interface' ;
Package: 'package' ;
Protected: 'protected' ;
Static: 'static' ;
Yield: 'yield' ;
//keywords:
Any : 'any';
Number: 'number';
Boolean: 'boolean';
String: 'string';
Symbol: 'symbol';
Of: 'of';
TypeAlias : 'type';
Get: 'get';
Set: 'set';
Constructor: 'constructor';
Namespace: 'namespace';
Global: 'global';
Require: 'require';
Module: 'module';
Declare: 'declare';
Abstract: 'abstract';
Is: 'is';
Infer: 'infer';
Never: 'never';
Unknown: 'unknown';
Asserts: 'asserts';
//
// Ext.2 Additions to 1.8: Decorators
//
At: '@';
/// Identifier Names and Identifiers
Identifier: IdentifierStart IdentifierPart*;
/// String Literals
StringLiteral: ('"' DoubleStringCharacter* '"'
| '\'' SingleStringCharacter* '\'')
;
BackTick: '`' {this.IncreaseTemplateDepth();} -> pushMode(TEMPLATE);
WhiteSpaces: [\t\u000B\u000C\u0020\u00A0]+ -> channel(HIDDEN);
LineTerminator: [\r\n\u2028\u2029] -> channel(HIDDEN);
/// Comments
HtmlComment: '' -> channel(HIDDEN);
CDataComment: '' -> channel(HIDDEN);
UnexpectedCharacter: . -> channel(ERROR);
mode TEMPLATE;
BackTickInside: '`' {this.DecreaseTemplateDepth();} -> type(BackTick), popMode;
TemplateStringStartExpression: '${' -> pushMode(DEFAULT_MODE);
TemplateStringAtom: EscapeTick | ~[`];
// Fragment rules
fragment EscapeTick
: '\u005C\u0060'
;
fragment DoubleStringCharacter
: ~["\\\r\n]
| '\\' EscapeSequence
| LineContinuation
;
fragment SingleStringCharacter
: ~['\\\r\n]
| '\\' EscapeSequence
| LineContinuation
;
fragment EscapeSequence
: CharacterEscapeSequence
| '0' // no digit ahead! TODO
| HexEscapeSequence
| UnicodeEscapeSequence
| ExtendedUnicodeEscapeSequence
;
fragment CharacterEscapeSequence
: SingleEscapeCharacter
| NonEscapeCharacter
;
fragment HexEscapeSequence
: 'x' HexDigit HexDigit
;
fragment UnicodeEscapeSequence
: 'u' HexDigit HexDigit HexDigit HexDigit
;
fragment ExtendedUnicodeEscapeSequence
: 'u' '{' HexDigit+ '}'
;
fragment SingleEscapeCharacter
: ['"\\bfnrtv]
;
fragment NonEscapeCharacter
: ~['"\\bfnrtv0-9xu\r\n]
;
fragment EscapeCharacter
: SingleEscapeCharacter
| [0-9]
| [xu]
;
fragment LineContinuation
: '\\' [\r\n\u2028\u2029]
;
fragment HexDigit
: [0-9a-fA-F]
;
fragment DecimalIntegerLiteral
: '0'
| [1-9] [0-9_]*
;
fragment ExponentPart
: [eE] [+-]? [0-9]+
;
fragment IdentifierPart
: IdentifierStart
| [\p{Mn}]
| [\p{Nd}]
| [\p{Pc}]
| '\u200C'
| '\u200D'
;
fragment IdentifierStart
: [\p{L}]
| [$_]
| '\\' UnicodeEscapeSequence
;
fragment RegularExpressionFirstChar
: ~[*\r\n\u2028\u2029\\/[]
| RegularExpressionBackslashSequence
| '[' RegularExpressionClassChar* ']'
;
fragment RegularExpressionChar
: ~[\r\n\u2028\u2029\\/[]
| RegularExpressionBackslashSequence
| '[' RegularExpressionClassChar* ']'
;
fragment RegularExpressionClassChar
: ~[\r\n\u2028\u2029\]\\]
| RegularExpressionBackslashSequence
;
fragment RegularExpressionBackslashSequence
: '\\' ~[\r\n\u2028\u2029]
;
//
// html tag declarations
//
mode TAG;
TagOpen
: LessThan -> pushMode(TAG)
;
TagClose
: MoreThan -> popMode
;
TagSlashClose
: '/>' -> popMode
;
TagSlash
: Divide
;
TagName
: TagNameStartChar TagNameChar*
;
// an attribute value may have spaces b/t the '=' and the value
AttributeValue
: [ ]* Attribute -> popMode
;
Attribute
: DoubleQuoteString
| SingleQuoteString
| AttributeChar
| HexChars
| DecChars
;
// Fragment rules
fragment AttributeChar
: '-'
| '_'
| '.'
| '/'
| '+'
| ','
| '?'
| '='
| ':'
| ';'
| '#'
| [0-9a-zA-Z]
;
fragment AttributeChars
: AttributeChar+ ' '?
;
fragment HexChars
: '#' [0-9a-fA-F]+
;
fragment DecChars
: [0-9]+ '%'?
;
fragment
TagNameStartChar
: [:a-zA-Z]
| '\u2070'..'\u218F'
| '\u2C00'..'\u2FEF'
| '\u3001'..'\uD7FF'
| '\uF900'..'\uFDCF'
| '\uFDF0'..'\uFFFD'
;
fragment
TagNameChar
: TagNameStartChar
| '-'
| '_'
| '.'
| Digit
| '\u00B7'
| '\u0300'..'\u036F'
| '\u203F'..'\u2040'
;
fragment DoubleQuoteString
: '"' ~[<"]* '"'
;
fragment SingleQuoteString
: '\'' ~[<']* '\''
;
fragment
Digit
: [0-9]
;