test: [ts] add java version for compare

上级 f194377a
package com.compare;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import pyantlr.PythonLexer;
import pyantlr.PythonParser;
import tsantlr.TypeScriptLexer;
import tsantlr.TypeScriptParser;
public class TsIdentApp {
static void processString(String inputStr) {
CharStream stream = CharStreams.fromString(inputStr);;
TypeScriptLexer lexer = new TypeScriptLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
TypeScriptParser parser = new TypeScriptParser(tokens);
TypeScriptParser.InitializerContext tree = parser.initializer();
TsIdentListener listener = new TsIdentListener();
ParseTreeWalker.DEFAULT.walk(listener, tree);
}
}
package com.compare;
import tsantlr.TypeScriptParser;
import tsantlr.TypeScriptParserBaseListener;
public class TsIdentListener extends TypeScriptParserBaseListener {
@Override
public void enterClassDeclaration(TypeScriptParser.ClassDeclarationContext ctx) {
super.enterClassDeclaration(ctx);
}
}
package tsantlr;
import org.antlr.v4.runtime.*;
import java.util.Stack;
/**
* All lexer methods that used in grammar (IsStrictMode)
* should start with Upper Case Char similar to Lexer rules.
*/
public abstract class TypeScriptBaseLexer extends Lexer
{
/**
* Stores values of nested modes. By default mode is strict or
* defined externally (useStrictDefault)
*/
private Stack<Boolean> scopeStrictModes = new Stack<Boolean>();
private Token lastToken = null;
/**
* Default value of strict mode
* Can be defined externally by setUseStrictDefault
*/
private boolean useStrictDefault = false;
/**
* Current value of strict mode
* Can be defined during parsing, see StringFunctions.js and StringGlobal.js samples
*/
private boolean useStrictCurrent = false;
public TypeScriptBaseLexer(CharStream input) {
super(input);
}
public boolean getStrictDefault() {
return useStrictDefault;
}
public void setUseStrictDefault(boolean value) {
useStrictDefault = value;
useStrictCurrent = value;
}
public boolean IsStrictMode() {
return useStrictCurrent;
}
/**
* Return the next token from the character stream and records this last
* token in case it resides on the default channel. This recorded token
* is used to determine when the lexer could possibly match a regex
* literal. Also changes scopeStrictModes stack if tokenize special
* string 'use strict';
*
* @return the next token from the character stream.
*/
@Override
public Token nextToken() {
Token next = super.nextToken();
if (next.getChannel() == Token.DEFAULT_CHANNEL) {
// Keep track of the last token on the default channel.
this.lastToken = next;
}
return next;
}
protected void ProcessOpenBrace()
{
useStrictCurrent = scopeStrictModes.size() > 0 && scopeStrictModes.peek() ? true : useStrictDefault;
scopeStrictModes.push(useStrictCurrent);
}
protected void ProcessCloseBrace()
{
useStrictCurrent = scopeStrictModes.size() > 0 ? scopeStrictModes.pop() : useStrictDefault;
}
protected void ProcessStringLiteral()
{
if (lastToken == null || lastToken.getType() == TypeScriptLexer.OpenBrace)
{
String text = getText();
if (text.equals("\"use strict\"") || text.equals("'use strict'"))
{
if (scopeStrictModes.size() > 0)
scopeStrictModes.pop();
useStrictCurrent = true;
scopeStrictModes.push(useStrictCurrent);
}
}
}
/**
* Returns {@code true} if the lexer can match a regex literal.
*/
protected boolean IsRegexPossible() {
if (this.lastToken == null) {
// No token has been produced yet: at the start of the input,
// no division is possible, so a regex literal _is_ possible.
return true;
}
switch (this.lastToken.getType()) {
case TypeScriptLexer.Identifier:
case TypeScriptLexer.NullLiteral:
case TypeScriptLexer.BooleanLiteral:
case TypeScriptLexer.This:
case TypeScriptLexer.CloseBracket:
case TypeScriptLexer.CloseParen:
case TypeScriptLexer.OctalIntegerLiteral:
case TypeScriptLexer.DecimalLiteral:
case TypeScriptLexer.HexIntegerLiteral:
case TypeScriptLexer.StringLiteral:
case TypeScriptLexer.PlusPlus:
case TypeScriptLexer.MinusMinus:
// After any of the tokens above, no regex literal can follow.
return false;
default:
// In all other cases, a regex literal _is_ possible.
return true;
}
}
}
package tsantlr;
import org.antlr.v4.runtime.*;
/**
* All parser methods that used in grammar (p, prev, notLineTerminator, etc.)
* should start with lower case char similar to parser rules.
*/
public abstract class TypeScriptBaseParser extends Parser
{
public TypeScriptBaseParser(TokenStream input) {
super(input);
}
/**
* Short form for prev(String str)
*/
protected boolean p(String str) {
return prev(str);
}
/**
* Whether the previous token value equals to @param str
*/
protected boolean prev(String str) {
return _input.LT(-1).getText().equals(str);
}
/**
* Short form for next(String str)
*/
protected boolean n(String str) {
return next(str);
}
/**
* Whether the next token value equals to @param str
*/
protected boolean next(String str) {
return _input.LT(1).getText().equals(str);
}
protected boolean notLineTerminator() {
return !here(TypeScriptParser.LineTerminator);
}
protected boolean notOpenBraceAndNotFunction() {
int nextTokenType = _input.LT(1).getType();
return nextTokenType != TypeScriptParser.OpenBrace && nextTokenType != TypeScriptParser.Function;
}
protected boolean closeBrace() {
return _input.LT(1).getType() == TypeScriptParser.CloseBrace;
}
/**
* Returns {@code true} iff on the current index of the parser's
* token stream a token of the given {@code type} exists on the
* {@code HIDDEN} channel.
*
* @param type
* the type of the token on the {@code HIDDEN} channel
* to check.
*
* @return {@code true} iff on the current index of the parser's
* token stream a token of the given {@code type} exists on the
* {@code HIDDEN} channel.
*/
private boolean here(final int type) {
// Get the token ahead of the current index.
int possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 1;
Token ahead = _input.get(possibleIndexEosToken);
// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return (ahead.getChannel() == Lexer.HIDDEN) && (ahead.getType() == type);
}
/**
* Returns {@code true} iff on the current index of the parser's
* token stream a token exists on the {@code HIDDEN} channel which
* either is a line terminator, or is a multi line comment that
* contains a line terminator.
*
* @return {@code true} iff on the current index of the parser's
* token stream a token exists on the {@code HIDDEN} channel which
* either is a line terminator, or is a multi line comment that
* contains a line terminator.
*/
protected boolean lineTerminatorAhead() {
// Get the token ahead of the current index.
int possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 1;
Token ahead = _input.get(possibleIndexEosToken);
if (ahead.getChannel() != Lexer.HIDDEN) {
// We're only interested in tokens on the HIDDEN channel.
return false;
}
if (ahead.getType() == TypeScriptParser.LineTerminator) {
// There is definitely a line terminator ahead.
return true;
}
if (ahead.getType() == TypeScriptParser.WhiteSpaces) {
// Get the token ahead of the current whitespaces.
possibleIndexEosToken = this.getCurrentToken().getTokenIndex() - 2;
ahead = _input.get(possibleIndexEosToken);
}
// Get the token's text and type.
String text = ahead.getText();
int type = ahead.getType();
// Check if the token is, or contains a line terminator.
return (type == TypeScriptParser.MultiLineComment && (text.contains("\r") || text.contains("\n"))) ||
(type == TypeScriptParser.LineTerminator);
}
}
HashBangLine=1
MultiLineComment=2
SingleLineComment=3
RegularExpressionLiteral=4
OpenBracket=5
CloseBracket=6
OpenParen=7
CloseParen=8
OpenBrace=9
CloseBrace=10
SemiColon=11
Comma=12
Assign=13
QuestionMark=14
Colon=15
Ellipsis=16
Dot=17
PlusPlus=18
MinusMinus=19
Plus=20
Minus=21
BitNot=22
Not=23
Multiply=24
Lodash=25
Dollar=26
Divide=27
Modulus=28
Power=29
NullCoalesce=30
Hashtag=31
RightShiftArithmetic=32
LeftShiftArithmetic=33
RightShiftLogical=34
LessThan=35
MoreThan=36
LessThanEquals=37
GreaterThanEquals=38
Equals_=39
NotEquals=40
IdentityEquals=41
IdentityNotEquals=42
BitAnd=43
BitXOr=44
BitOr=45
And=46
Or=47
MultiplyAssign=48
DivideAssign=49
ModulusAssign=50
PlusAssign=51
MinusAssign=52
LeftShiftArithmeticAssign=53
RightShiftArithmeticAssign=54
RightShiftLogicalAssign=55
BitAndAssign=56
BitXorAssign=57
BitOrAssign=58
ARROW=59
PowerAssign=60
NullLiteral=61
BooleanLiteral=62
DecimalLiteral=63
HexIntegerLiteral=64
OctalIntegerLiteral=65
OctalIntegerLiteral2=66
BinaryIntegerLiteral=67
Break=68
Do=69
Instanceof=70
Typeof=71
Case=72
Else=73
New=74
Var=75
Catch=76
Finally=77
Return=78
Void=79
Continue=80
For=81
Switch=82
While=83
Debugger=84
Function=85
This=86
With=87
Default=88
If=89
Throw=90
Delete=91
In=92
Try=93
As=94
From=95
ReadOnly=96
Async=97
Class=98
Enum=99
Extends=100
Super=101
Const=102
Export=103
Import=104
Await=105
Implements=106
Let=107
Private=108
Public=109
Interface=110
Package=111
Protected=112
Static=113
Yield=114
ANY=115
NUMBER=116
BOOLEAN=117
STRING=118
SYMBOL=119
Type=120
Get=121
Set=122
Constructor=123
Namespace=124
Require=125
Module=126
Declare=127
Abstract=128
Is=129
At=130
Identifier=131
StringLiteral=132
TemplateStringLiteral=133
WhiteSpaces=134
LineTerminator=135
HtmlComment=136
CDataComment=137
UnexpectedCharacter=138
'['=5
']'=6
'('=7
')'=8
'{'=9
'}'=10
';'=11
','=12
'='=13
'?'=14
':'=15
'...'=16
'.'=17
'++'=18
'--'=19
'+'=20
'-'=21
'~'=22
'!'=23
'*'=24
'_'=25
'$'=26
'/'=27
'%'=28
'**'=29
'??'=30
'#'=31
'>>'=32
'<<'=33
'>>>'=34
'<'=35
'>'=36
'<='=37
'>='=38
'=='=39
'!='=40
'==='=41
'!=='=42
'&'=43
'^'=44
'|'=45
'&&'=46
'||'=47
'*='=48
'/='=49
'%='=50
'+='=51
'-='=52
'<<='=53
'>>='=54
'>>>='=55
'&='=56
'^='=57
'|='=58
'=>'=59
'**='=60
'null'=61
'break'=68
'do'=69
'instanceof'=70
'typeof'=71
'case'=72
'else'=73
'new'=74
'var'=75
'catch'=76
'finally'=77
'return'=78
'void'=79
'continue'=80
'for'=81
'switch'=82
'while'=83
'debugger'=84
'function'=85
'this'=86
'with'=87
'default'=88
'if'=89
'throw'=90
'delete'=91
'in'=92
'try'=93
'as'=94
'from'=95
'readonly'=96
'async'=97
'class'=98
'enum'=99
'extends'=100
'super'=101
'const'=102
'export'=103
'import'=104
'await'=105
'implements'=106
'let'=107
'private'=108
'public'=109
'interface'=110
'package'=111
'protected'=112
'static'=113
'yield'=114
'any'=115
'number'=116
'boolean'=117
'string'=118
'symbol'=119
'type'=120
'get '=121
'set '=122
'constructor'=123
'namespace'=124
'require'=125
'module'=126
'declare'=127
'abstract'=128
'is'=129
'@'=130
HashBangLine=1
MultiLineComment=2
SingleLineComment=3
RegularExpressionLiteral=4
OpenBracket=5
CloseBracket=6
OpenParen=7
CloseParen=8
OpenBrace=9
CloseBrace=10
SemiColon=11
Comma=12
Assign=13
QuestionMark=14
Colon=15
Ellipsis=16
Dot=17
PlusPlus=18
MinusMinus=19
Plus=20
Minus=21
BitNot=22
Not=23
Multiply=24
Lodash=25
Dollar=26
Divide=27
Modulus=28
Power=29
NullCoalesce=30
Hashtag=31
RightShiftArithmetic=32
LeftShiftArithmetic=33
RightShiftLogical=34
LessThan=35
MoreThan=36
LessThanEquals=37
GreaterThanEquals=38
Equals_=39
NotEquals=40
IdentityEquals=41
IdentityNotEquals=42
BitAnd=43
BitXOr=44
BitOr=45
And=46
Or=47
MultiplyAssign=48
DivideAssign=49
ModulusAssign=50
PlusAssign=51
MinusAssign=52
LeftShiftArithmeticAssign=53
RightShiftArithmeticAssign=54
RightShiftLogicalAssign=55
BitAndAssign=56
BitXorAssign=57
BitOrAssign=58
ARROW=59
PowerAssign=60
NullLiteral=61
BooleanLiteral=62
DecimalLiteral=63
HexIntegerLiteral=64
OctalIntegerLiteral=65
OctalIntegerLiteral2=66
BinaryIntegerLiteral=67
Break=68
Do=69
Instanceof=70
Typeof=71
Case=72
Else=73
New=74
Var=75
Catch=76
Finally=77
Return=78
Void=79
Continue=80
For=81
Switch=82
While=83
Debugger=84
Function=85
This=86
With=87
Default=88
If=89
Throw=90
Delete=91
In=92
Try=93
As=94
From=95
ReadOnly=96
Async=97
Class=98
Enum=99
Extends=100
Super=101
Const=102
Export=103
Import=104
Await=105
Implements=106
Let=107
Private=108
Public=109
Interface=110
Package=111
Protected=112
Static=113
Yield=114
ANY=115
NUMBER=116
BOOLEAN=117
STRING=118
SYMBOL=119
Type=120
Get=121
Set=122
Constructor=123
Namespace=124
Require=125
Module=126
Declare=127
Abstract=128
Is=129
At=130
Identifier=131
StringLiteral=132
TemplateStringLiteral=133
WhiteSpaces=134
LineTerminator=135
HtmlComment=136
CDataComment=137
UnexpectedCharacter=138
'['=5
']'=6
'('=7
')'=8
'{'=9
'}'=10
';'=11
','=12
'='=13
'?'=14
':'=15
'...'=16
'.'=17
'++'=18
'--'=19
'+'=20
'-'=21
'~'=22
'!'=23
'*'=24
'_'=25
'$'=26
'/'=27
'%'=28
'**'=29
'??'=30
'#'=31
'>>'=32
'<<'=33
'>>>'=34
'<'=35
'>'=36
'<='=37
'>='=38
'=='=39
'!='=40
'==='=41
'!=='=42
'&'=43
'^'=44
'|'=45
'&&'=46
'||'=47
'*='=48
'/='=49
'%='=50
'+='=51
'-='=52
'<<='=53
'>>='=54
'>>>='=55
'&='=56
'^='=57
'|='=58
'=>'=59
'**='=60
'null'=61
'break'=68
'do'=69
'instanceof'=70
'typeof'=71
'case'=72
'else'=73
'new'=74
'var'=75
'catch'=76
'finally'=77
'return'=78
'void'=79
'continue'=80
'for'=81
'switch'=82
'while'=83
'debugger'=84
'function'=85
'this'=86
'with'=87
'default'=88
'if'=89
'throw'=90
'delete'=91
'in'=92
'try'=93
'as'=94
'from'=95
'readonly'=96
'async'=97
'class'=98
'enum'=99
'extends'=100
'super'=101
'const'=102
'export'=103
'import'=104
'await'=105
'implements'=106
'let'=107
'private'=108
'public'=109
'interface'=110
'package'=111
'protected'=112
'static'=113
'yield'=114
'any'=115
'number'=116
'boolean'=117
'string'=118
'symbol'=119
'type'=120
'get '=121
'set '=122
'constructor'=123
'namespace'=124
'require'=125
'module'=126
'declare'=127
'abstract'=128
'is'=129
'@'=130
package com.compare;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class TsIdentAppTest {
@Test
void shouldHandleField() {
String str = "console.log('a')";
TsIdentApp.processString(str);
}
}
......@@ -24,3 +24,6 @@ antlr -Dlanguage=Go -listener PythonParser.g4 -o ../python
#antlr -Dlanguage=Java -listener PythonLexer.g4 -o ../compare/java
#antlr -Dlanguage=Java -listener PythonParser.g4 -o ../compare/java
#antlr -Dlanguage=Java -listener TypeScriptLexer.g4 -o ../compare/src/main/java/tsantlr
#antlr -Dlanguage=Java -listener TypeScriptParser.g4 -o ../compare/src/main/java/tsantlr
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册