1 /* 2 * Copyright (c) 2025 Shenzhen Kaihong Digital. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package antlr.typescript; 17 18 import antlr.ParseBaseListener; 19 import com.google.gson.Gson; 20 import com.google.gson.GsonBuilder; 21 import grammar.*; 22 import org.antlr.v4.runtime.tree.ParseTree; 23 import org.antlr.v4.runtime.ParserRuleContext; 24 import utils.Constants; 25 import utils.TsToken; 26 27 import java.util.List; 28 import java.util.concurrent.CopyOnWriteArrayList; 29 30 /** 31 * <h3>类名:该类用于xxx</h3> 32 * description typescript custom visitor 33 * 34 * @author Administrator 35 * date 2025-02-28 36 * @version 1.0 37 * @since 2025-02-28 38 */ 39 public class TypeScriptCustomListener extends TypeScriptParserBaseListener implements ParseBaseListener { 40 private final int currentLanguage = Constants.PARSE_TS_LANGUAGE; 41 private String currentToken = ""; 42 private GBaseObject currentObject; 43 private String currentIdentifier = ""; 44 private List<EnumObj> enumObjList; 45 private List<ClassObj> classObjList; 46 private List<FuncObj> funcObjList; 47 private List<StructObj> structObjList; 48 private List<TypeObj> typeObjList; 49 private List<UnionObj> unionObjList; 50 private List<InterfaceObject> interfaceObjList; 51 52 /** 53 * 构造函数 54 */ TypeScriptCustomListener()55 public TypeScriptCustomListener() { 56 enumObjList = new CopyOnWriteArrayList<>(); 57 classObjList = new CopyOnWriteArrayList<>(); 58 funcObjList = new CopyOnWriteArrayList<>(); 59 structObjList = new CopyOnWriteArrayList<>(); 60 typeObjList = new CopyOnWriteArrayList<>(); 61 unionObjList = new CopyOnWriteArrayList<>(); 62 interfaceObjList = new CopyOnWriteArrayList<>(); 63 } 64 65 /** 66 * 获取语言 67 * 68 * @return 语言 69 */ getCurrentLanguage()70 public int getCurrentLanguage() { 71 return currentLanguage; 72 } 73 74 /** 75 * 设置当前关键字 76 * 77 * @param currentToken 关键字 78 */ setCurrentToken(String currentToken)79 public void setCurrentToken(String currentToken) { 80 this.currentToken = currentToken; 81 } 82 83 /** 84 * 获取关键字 85 * 86 * @return 关键字 87 */ getCurrentToken()88 public String getCurrentToken() { 89 return currentToken; 90 } 91 92 /** 93 * 获取当前解析对象 94 * 95 * @return 解析对象 96 */ getCurrentObject()97 public GBaseObject getCurrentObject() { 98 return currentObject; 99 } 100 101 /** 102 * 设置当前解析对象 103 * 104 * @param currentObject 当前解析对象 105 */ setCurrentObject(GBaseObject currentObject)106 public void setCurrentObject(GBaseObject currentObject) { 107 this.currentObject = currentObject; 108 } 109 110 /** 111 * 获取类对象 112 * 113 * @return 对象 114 */ getClassObjList()115 public List<ClassObj> getClassObjList() { 116 return classObjList; 117 } 118 119 /** 120 * 设置类对象 121 * 122 * @param classObjList 类对象 123 */ setClassObjList(List<ClassObj> classObjList)124 public void setClassObjList(List<ClassObj> classObjList) { 125 this.classObjList = classObjList; 126 } 127 128 /** 129 * 获取枚举对象 130 * 131 * @return 枚举对象 132 */ getEnumObjList()133 public List<EnumObj> getEnumObjList() { 134 return enumObjList; 135 } 136 137 /** 138 * 枚举 139 * 140 * @param enumObjList 枚举 141 */ setEnumObjList(List<EnumObj> enumObjList)142 public void setEnumObjList(List<EnumObj> enumObjList) { 143 this.enumObjList = enumObjList; 144 } 145 146 /** 147 * 获取方法 148 * 149 * @return 方法 150 */ getFuncObjList()151 public List<FuncObj> getFuncObjList() { 152 return funcObjList; 153 } 154 155 /** 156 * 设置方法 157 * 158 * @param funcObjList 方法 159 */ setFuncObjList(List<FuncObj> funcObjList)160 public void setFuncObjList(List<FuncObj> funcObjList) { 161 this.funcObjList = funcObjList; 162 } 163 164 /** 165 * 获取结构体 166 * 167 * @return 结构体 168 */ getStructObjList()169 public List<StructObj> getStructObjList() { 170 return structObjList; 171 } 172 173 /** 174 * 设置结构体 175 * 176 * @param structObjList 结构体 177 */ setStructObjList(List<StructObj> structObjList)178 public void setStructObjList(List<StructObj> structObjList) { 179 this.structObjList = structObjList; 180 } 181 182 /** 183 * 获取接口 184 * 185 * @return 接口 186 */ getInterfaceObjList()187 public List<InterfaceObject> getInterfaceObjList() { 188 return interfaceObjList; 189 } 190 191 /** 192 * 设置接口 193 * 194 * @param interfaceObjList 接口 195 */ setInterfaceObjList(List<InterfaceObject> interfaceObjList)196 public void setInterfaceObjList(List<InterfaceObject> interfaceObjList) { 197 this.interfaceObjList = interfaceObjList; 198 } 199 200 /** 201 * 获取type 202 * 203 * @return type 204 */ getTypeObjList()205 public List<TypeObj> getTypeObjList() { 206 return typeObjList; 207 } 208 209 /** 210 * 设置 type 211 * 212 * @param typeObjList type 213 */ setTypeObjList(List<TypeObj> typeObjList)214 public void setTypeObjList(List<TypeObj> typeObjList) { 215 this.typeObjList = typeObjList; 216 } 217 218 /** 219 * 获取联合 220 * 221 * @return 联合 222 */ getUnionObjList()223 public List<UnionObj> getUnionObjList() { 224 return unionObjList; 225 } 226 227 /** 228 * 设置联合 229 * 230 * @param unionObjList 联合数组 231 */ setUnionObjList(List<UnionObj> unionObjList)232 public void setUnionObjList(List<UnionObj> unionObjList) { 233 this.unionObjList = unionObjList; 234 } 235 236 @Override enterFunctionType(TypeScriptParser.FunctionTypeContext ctx)237 public void enterFunctionType(TypeScriptParser.FunctionTypeContext ctx) { 238 super.enterFunctionType(ctx); 239 System.out.println("enterFunctionType: " + ctx.getText()); 240 } 241 242 @Override enterFunctionBody(TypeScriptParser.FunctionBodyContext ctx)243 public void enterFunctionBody(TypeScriptParser.FunctionBodyContext ctx) { 244 super.enterFunctionBody(ctx); 245 System.out.println("enterFunctionBody: " + ctx.getText()); 246 } 247 248 @Override enterUnion(TypeScriptParser.UnionContext ctx)249 public void enterUnion(TypeScriptParser.UnionContext ctx) { 250 super.enterUnion(ctx); 251 System.out.println("enterUnion: " + ctx.getText()); 252 } 253 254 @Override enterTypeAnnotation(TypeScriptParser.TypeAnnotationContext ctx)255 public void enterTypeAnnotation(TypeScriptParser.TypeAnnotationContext ctx) { 256 super.enterTypeAnnotation(ctx); 257 System.out.println("enterTypeAnnotation: " + ctx.getText()); 258 } 259 260 @Override enterPropertyName(TypeScriptParser.PropertyNameContext ctx)261 public void enterPropertyName(TypeScriptParser.PropertyNameContext ctx) { 262 super.enterPropertyName(ctx); 263 System.out.println("enterPropertyName: " + ctx.getText()); 264 } 265 266 @Override enterAnonymousFunction(TypeScriptParser.AnonymousFunctionContext ctx)267 public void enterAnonymousFunction(TypeScriptParser.AnonymousFunctionContext ctx) { 268 super.enterAnonymousFunction(ctx); 269 System.out.println("enterAnonymousFunction: " + ctx.getText()); 270 } 271 272 @Override enterIdentifier(TypeScriptParser.IdentifierContext ctx)273 public void enterIdentifier(TypeScriptParser.IdentifierContext ctx) { 274 super.enterIdentifier(ctx); 275 System.out.println("enterIdentifier: " + ctx.getText()); 276 this.currentIdentifier = ctx.getText(); 277 } 278 279 @Override enterIdentifierExpression(TypeScriptParser.IdentifierExpressionContext ctx)280 public void enterIdentifierExpression(TypeScriptParser.IdentifierExpressionContext ctx) { 281 super.enterIdentifierExpression(ctx); 282 System.out.println("enterIdentifierExpression: " + ctx.getText()); 283 } 284 285 @Override enterIdentifierName(TypeScriptParser.IdentifierNameContext ctx)286 public void enterIdentifierName(TypeScriptParser.IdentifierNameContext ctx) { 287 super.enterIdentifierName(ctx); 288 System.out.println("enterIdentifierName: " + ctx.getText()); 289 } 290 291 @Override enterVariableStatement(TypeScriptParser.VariableStatementContext ctx)292 public void enterVariableStatement(TypeScriptParser.VariableStatementContext ctx) { 293 super.enterVariableStatement(ctx); 294 System.out.println("enterVariableStatement: " + ctx.getText()); 295 } 296 297 @Override enterVariableDeclaration(TypeScriptParser.VariableDeclarationContext ctx)298 public void enterVariableDeclaration(TypeScriptParser.VariableDeclarationContext ctx) { 299 String varName = ctx.identifierOrKeyWord().getText(); 300 System.out.println("变量名: " + varName); 301 System.out.println("var : " + ctx.getText()); 302 String typeAnno = ctx.typeAnnotation() != null ? ctx.typeAnnotation().stop.getText() : ""; 303 System.out.println("type : " + typeAnno); 304 if (varName.equals(TsToken.TS_TOKEN_TYPE)) { 305 TypeObj to = new TypeObj(); 306 List<TypeScriptParser.SingleExpressionContext> secList = ctx.singleExpression(); 307 for (TypeScriptParser.SingleExpressionContext sec : secList) { 308 String value = sec.getText(); 309 System.out.println("single : " + value); 310 int cnt = sec.getChildCount(); 311 System.out.println("single child cnt: " + cnt); 312 if (cnt == 3) { 313 ParseTree pt = sec.getChild(0); 314 to.setName(pt.getText()); 315 316 ParseTree pt2 = sec.getChild(2); 317 to.addTypeValue(pt2.getText()); 318 } 319 for (int i = 0; i < cnt; i++) { 320 ParseTree pt = sec.getChild(i); 321 System.out.println("single child pt: " + pt.getText()); 322 } 323 } 324 this.typeObjList.add(to); 325 System.out.println("type: " + to.toJsonString()); 326 327 } else if (varName.equals(TsToken.TS_TOKEN_ABSTRACT)) { 328 int cnt = ctx.children.size(); 329 for (int i = 0; i < cnt; i++) { 330 ParseTree item = ctx.children.get(i); 331 System.out.println("item: " + item.getText()); 332 } 333 } 334 335 336 System.out.println("------------------------------"); 337 } 338 339 @Override enterExpressionStatement(TypeScriptParser.ExpressionStatementContext ctx)340 public void enterExpressionStatement(TypeScriptParser.ExpressionStatementContext ctx) { 341 super.enterExpressionStatement(ctx); 342 System.out.println("enterExpressionStatement: " + ctx.getText()); 343 } 344 345 @Override enterIdentifierOrPattern(TypeScriptParser.IdentifierOrPatternContext ctx)346 public void enterIdentifierOrPattern(TypeScriptParser.IdentifierOrPatternContext ctx) { 347 super.enterIdentifierOrPattern(ctx); 348 } 349 350 @Override enterObjectLiteralExpression(TypeScriptParser.ObjectLiteralExpressionContext ctx)351 public void enterObjectLiteralExpression(TypeScriptParser.ObjectLiteralExpressionContext ctx) { 352 super.enterObjectLiteralExpression(ctx); 353 ParserRuleContext prc = ctx.getParent(); 354 ParserRuleContext fprc = prc.getParent(); 355 String tokenStr = fprc.getStart().getText(); 356 if (tokenStr.equals(TsToken.TS_TOKEN_ENUM)) { 357 EnumObj eo = new EnumObj(); 358 eo.setName(ctx.getParent().getStart().getText()); 359 this.currentToken = TsToken.TS_TOKEN_ENUM; 360 this.currentObject = eo; 361 this.enumObjList.add(eo); 362 } 363 364 } 365 366 @Override enterLiteralExpression(TypeScriptParser.LiteralExpressionContext ctx)367 public void enterLiteralExpression(TypeScriptParser.LiteralExpressionContext ctx) { 368 super.enterLiteralExpression(ctx); 369 370 String memName = ctx.getParent().getStart().getText(); 371 String memValue = ctx.getParent().getStop().getText(); 372 if (this.currentToken.equals(TsToken.TS_TOKEN_ENUM)) { 373 if (this.currentObject instanceof EnumObj) { 374 EnumObj eo = (EnumObj) this.currentObject; 375 eo.addMemberItem(memName); 376 eo.addMemberValue(memValue); 377 int lastIndex = this.enumObjList.size() - 1; 378 this.enumObjList.set(lastIndex, eo); 379 System.out.println("enum: " + eo.toJsonString()); 380 } 381 } 382 383 } 384 385 @Override enterExpressionSequence(TypeScriptParser.ExpressionSequenceContext ctx)386 public void enterExpressionSequence(TypeScriptParser.ExpressionSequenceContext ctx) { 387 super.enterExpressionSequence(ctx); 388 389 } 390 391 @Override enterConstructorDeclaration(TypeScriptParser.ConstructorDeclarationContext ctx)392 public void enterConstructorDeclaration(TypeScriptParser.ConstructorDeclarationContext ctx) { 393 // 提取构造函数参数列表 394 String res = ctx.formalParameterList().getText(); 395 System.out.println("Construct: " + res); 396 TypeScriptParser.FormalParameterListContext fplc = ctx.formalParameterList(); 397 if (fplc == null || !(this.currentObject instanceof ClassObj co)) { 398 return; 399 } 400 401 int cnt = fplc.getChildCount(); 402 FuncObj fo = new FuncObj(); 403 fo.setName("constructor"); 404 fo.setRetValue("void"); 405 co.addFunc(fo); 406 for (int i = 0; i < cnt; i++) { 407 ParseTree pt = fplc.getChild(i); 408 if (!(pt instanceof TypeScriptParser.FormalParameterArgContext fpac)) { 409 continue; 410 } 411 412 String type = ""; 413 if (fpac.typeAnnotation() != null && fpac.typeAnnotation().stop != null) { 414 type = fpac.typeAnnotation().stop.getText(); 415 } 416 417 String name = ""; 418 if (fpac.assignable() != null) { 419 name = fpac.assignable().getText(); 420 } 421 422 if (type.isEmpty()) { 423 type = name; 424 } 425 fo.addParam(name, type); 426 } 427 428 } 429 430 @Override enterMethodProperty(TypeScriptParser.MethodPropertyContext ctx)431 public void enterMethodProperty(TypeScriptParser.MethodPropertyContext ctx) { 432 super.enterMethodProperty(ctx); 433 String res = ctx.toString(); 434 System.out.println("Method: " + res); 435 } 436 437 @Override enterFunctionDeclaration(TypeScriptParser.FunctionDeclarationContext ctx)438 public void enterFunctionDeclaration(TypeScriptParser.FunctionDeclarationContext ctx) { 439 super.enterFunctionDeclaration(ctx); 440 // 提取函数名、参数等信息 441 String funcName = ctx.identifier().getText(); 442 System.out.println("Function: " + funcName + " all: " + ctx.getText()); 443 444 String callSign = ctx.callSignature().getText(); 445 System.out.println("Function callSign: " + callSign); 446 String typeAnno = ctx.callSignature().typeAnnotation().stop.getText(); 447 System.out.println("Function typeAnno: " + typeAnno); 448 FuncObj fo = new FuncObj(); 449 fo.setName(funcName); 450 fo.setRetValue(typeAnno); 451 if (ctx.callSignature().parameterList() != null) { 452 List<TypeScriptParser.ParameterContext> plc = ctx.callSignature().parameterList().parameter(); 453 for (TypeScriptParser.ParameterContext pc : plc) { 454 System.out.println("Function param: " + pc.getText()); 455 TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); 456 String type = rpc.typeAnnotation().stop.getText(); 457 String name = rpc.identifierOrPattern().getText(); 458 System.out.println("Function type: " + type + " name: " + name); 459 fo.addParam(name, type); 460 } 461 } 462 System.out.println("--------------------" + fo.toJsonString()); 463 this.funcObjList.add(fo); 464 } 465 466 @Override enterClassDeclaration(TypeScriptParser.ClassDeclarationContext ctx)467 public void enterClassDeclaration(TypeScriptParser.ClassDeclarationContext ctx) { 468 super.enterClassDeclaration(ctx); 469 // 提取类名、方法、属性等信息 470 String className = ctx.identifier().getText(); 471 System.out.println("Class: " + className); 472 473 ClassObj co = new ClassObj(); 474 co.setName(className); 475 this.currentObject = co; 476 this.currentToken = TsToken.TS_TOKEN_CLASS; 477 this.classObjList.add(co); 478 479 // 获取修饰符(如public/abstract) 480 TypeScriptParser.DecoratorListContext dlc = ctx.decoratorList(); 481 if (dlc != null) { 482 System.out.println("Class decoratorList: " + dlc.getText()); 483 } 484 // 处理继承关系(extends/implements) 485 TypeScriptParser.ClassHeritageContext heritage = ctx.classHeritage(); 486 System.out.println("Class heritage: " + heritage.getText()); 487 } 488 489 @Override enterCallSignature(TypeScriptParser.CallSignatureContext ctx)490 public void enterCallSignature(TypeScriptParser.CallSignatureContext ctx) { 491 super.enterCallSignature(ctx); 492 System.out.println("enterCallSignature: " + ctx.getText()); 493 if (this.currentToken.equals(TsToken.TS_TOKEN_CLASS) && 494 (this.currentObject instanceof ClassObj co)) { 495 String typeName = ctx.typeAnnotation().stop.getText(); 496 FuncObj fo = new FuncObj(); 497 fo.setRetValue(typeName); 498 fo.setName(this.currentIdentifier); 499 TypeScriptParser.ParameterListContext plc = ctx.parameterList(); 500 int childCnt = plc.getChildCount(); 501 for (int i = 0; i < childCnt; i++) { 502 ParseTree pt = plc.getChild(i); 503 if (pt instanceof TypeScriptParser.ParameterContext pc) { 504 String paramName = pc.start.getText(); 505 String paramType = pc.stop.getText(); 506 fo.addParam(paramName, paramType); 507 } 508 } 509 co.addFunc(fo); 510 } 511 512 } 513 514 @Override enterTypeName(TypeScriptParser.TypeNameContext ctx)515 public void enterTypeName(TypeScriptParser.TypeNameContext ctx) { 516 super.enterTypeName(ctx); 517 System.out.println("enterTypeName: " + ctx.getText()); 518 } 519 520 @Override enterClassElementName(TypeScriptParser.ClassElementNameContext ctx)521 public void enterClassElementName(TypeScriptParser.ClassElementNameContext ctx) { 522 super.enterClassElementName(ctx); 523 System.out.println("enterClassElementName: " + ctx.getText()); 524 } 525 526 @Override enterClassOrInterfaceTypeList(TypeScriptParser.ClassOrInterfaceTypeListContext ctx)527 public void enterClassOrInterfaceTypeList(TypeScriptParser.ClassOrInterfaceTypeListContext ctx) { 528 super.enterClassOrInterfaceTypeList(ctx); 529 System.out.println("enterClassOrInterfaceTypeList: " + ctx.getText()); 530 } 531 532 @Override enterDeclaration(TypeScriptParser.DeclarationContext ctx)533 public void enterDeclaration(TypeScriptParser.DeclarationContext ctx) { 534 super.enterDeclaration(ctx); 535 System.out.println("enterDeclaration: " + ctx.getText()); 536 } 537 538 @Override enterClassExpression(TypeScriptParser.ClassExpressionContext ctx)539 public void enterClassExpression(TypeScriptParser.ClassExpressionContext ctx) { 540 super.enterClassExpression(ctx); 541 System.out.println("enterClassExpression: " + ctx.getText()); 542 543 int childCnt = ctx.getChildCount(); 544 if (childCnt > 2) { 545 546 ParseTree pt = ctx.getChild(1); 547 if (pt instanceof TypeScriptParser.IdentifierContext ic) { 548 ClassObj obj = new ClassObj(); 549 obj.setName(ic.start.getText()); 550 this.currentToken = TsToken.TS_TOKEN_CLASS; 551 this.currentObject = obj; 552 this.classObjList.add(obj); 553 } 554 } 555 } 556 557 @Override enterClassElement(TypeScriptParser.ClassElementContext ctx)558 public void enterClassElement(TypeScriptParser.ClassElementContext ctx) { 559 super.enterClassElement(ctx); 560 System.out.println("Class element: " + ctx.getText()); 561 562 TypeScriptParser.StatementContext sc = ctx.statement(); 563 if (sc != null) { 564 System.out.println("Class state: " + sc.getText()); 565 } 566 567 TypeScriptParser.PropertyMemberDeclarationContext pmdc = ctx.propertyMemberDeclaration(); 568 if (pmdc != null) { 569 System.out.println("Class property: " + pmdc.getText()); 570 } 571 } 572 573 @Override enterMethodDeclarationExpression(TypeScriptParser.MethodDeclarationExpressionContext ctx)574 public void enterMethodDeclarationExpression(TypeScriptParser.MethodDeclarationExpressionContext ctx) { 575 super.enterMethodDeclarationExpression(ctx); 576 System.out.println("Method property: " + ctx.getText()); 577 String propertyName = ctx.propertyName().getText(); 578 System.out.println("Method name: " + propertyName); 579 String callSign = ctx.callSignature().getText(); 580 System.out.println("Method callSign: " + callSign); 581 String typeAnno = ctx.callSignature().typeAnnotation().getText(); 582 TypeScriptParser.TypeAnnotationContext tac = ctx.callSignature().typeAnnotation(); 583 typeAnno = tac.stop.getText(); 584 System.out.println("Method typeAnno: " + typeAnno); 585 TypeScriptParser.ParameterListContext plc = ctx.callSignature().parameterList(); 586 587 FuncObj fo = new FuncObj(); 588 fo.setType(typeAnno); 589 fo.setName(propertyName); 590 591 if (plc != null) { 592 List<TypeScriptParser.ParameterContext> plcList = ctx.callSignature().parameterList().parameter(); 593 for (TypeScriptParser.ParameterContext pc : plcList) { 594 System.out.println("Method param: " + pc.getText()); 595 TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); 596 String ta = rpc.typeAnnotation().getText(); 597 String iop = rpc.identifierOrPattern().getText(); 598 System.out.println("Method type: " + ta + " name: " + iop); 599 fo.addParam(iop, ta); 600 } 601 } 602 603 if ((this.currentObject != null) && (this.currentObject instanceof ClassObj)) { 604 ClassObj co = (ClassObj) this.currentObject; 605 co.addFunc(fo); 606 607 int lastIndex = this.classObjList.size() - 1; 608 this.classObjList.set(lastIndex, co); 609 System.out.println("class: " + co.toJsonString()); 610 } 611 612 int cnt = ctx.getChildCount(); 613 System.out.println("Method param cnt: " + cnt); 614 } 615 616 @Override enterFunctionExpression(TypeScriptParser.FunctionExpressionContext ctx)617 public void enterFunctionExpression(TypeScriptParser.FunctionExpressionContext ctx) { 618 super.enterFunctionExpression(ctx); 619 System.out.println("enterFunctionExpression: " + ctx.getText()); 620 } 621 622 @Override enterGeneratorsFunctionExpression(TypeScriptParser.GeneratorsFunctionExpressionContext ctx)623 public void enterGeneratorsFunctionExpression(TypeScriptParser.GeneratorsFunctionExpressionContext ctx) { 624 super.enterGeneratorsFunctionExpression(ctx); 625 System.out.println("enterGeneratorsFunctionExpression: " + ctx.getText()); 626 } 627 628 @Override enterArrowFunctionDeclaration(TypeScriptParser.ArrowFunctionDeclarationContext ctx)629 public void enterArrowFunctionDeclaration(TypeScriptParser.ArrowFunctionDeclarationContext ctx) { 630 super.enterArrowFunctionDeclaration(ctx); 631 System.out.println("enterArrowFunctionDeclaration: " + ctx.getText()); 632 } 633 634 @Override enterArgument(TypeScriptParser.ArgumentContext ctx)635 public void enterArgument(TypeScriptParser.ArgumentContext ctx) { 636 super.enterArgument(ctx); 637 System.out.println("enterArgument: " + ctx.getText()); 638 } 639 640 @Override enterPropertyDeclarationExpression(TypeScriptParser.PropertyDeclarationExpressionContext ctx)641 public void enterPropertyDeclarationExpression(TypeScriptParser.PropertyDeclarationExpressionContext ctx) { 642 super.enterPropertyDeclarationExpression(ctx); 643 644 System.out.println("Property property: " + ctx.getText()); 645 String propertyName = ctx.propertyName().getText(); 646 String typeName = ctx.typeAnnotation().stop.getText(); 647 System.out.println("Property name: " + propertyName + " type: " + typeName); 648 if ((this.currentObject != null) && (this.currentObject instanceof ClassObj)) { 649 ClassObj co = (ClassObj) this.currentObject; 650 co.addParam(propertyName, typeName); 651 652 int lastIndex = this.classObjList.size() - 1; 653 this.classObjList.set(lastIndex, co); 654 System.out.println("class: " + co.toJsonString()); 655 } 656 } 657 658 @Override enterTypeAliasDeclaration(TypeScriptParser.TypeAliasDeclarationContext ctx)659 public void enterTypeAliasDeclaration(TypeScriptParser.TypeAliasDeclarationContext ctx) { 660 super.enterTypeAliasDeclaration(ctx); 661 String typeName = ctx.identifier().getText(); 662 System.out.println("Type: " + typeName); 663 TypeScriptParser.TypeParametersContext tpc = ctx.typeParameters(); 664 if (tpc != null) { 665 System.out.println("Type params: " + tpc.getText()); 666 } 667 TypeScriptParser.Type_Context typeContext = ctx.type_(); 668 if (typeContext != null) { 669 System.out.println("Type type_: " + typeContext.getText()); 670 671 TypeScriptParser.UnionOrIntersectionOrPrimaryTypeContext upt = 672 typeContext.unionOrIntersectionOrPrimaryType(); 673 if (upt != null) { 674 System.out.println("Type uoiop: " + upt.getText()); 675 } 676 TypeScriptParser.TypeGenericContext tgc = typeContext.typeGeneric(); 677 if (tgc != null) { 678 System.out.println("Type typeGeneric: " + tgc.getText()); 679 } 680 TypeScriptParser.ConstructorTypeContext ctc = typeContext.constructorType(); 681 if (ctc != null) { 682 System.out.println("Type constructorType: " + ctc.getText()); 683 } 684 TypeScriptParser.FunctionTypeContext ftc = typeContext.functionType(); 685 if (ftc != null) { 686 System.out.println("Type functionType: " + ftc.getText()); 687 } 688 } 689 690 System.out.println("-------------------"); 691 } 692 693 @Override enterEnumBody(TypeScriptParser.EnumBodyContext ctx)694 public void enterEnumBody(TypeScriptParser.EnumBodyContext ctx) { 695 super.enterEnumBody(ctx); 696 System.out.println("find Enum Body: "); 697 String enumName = ctx.getText(); 698 System.out.println("Enum: " + enumName); 699 } 700 701 @Override enterEnumMemberList(TypeScriptParser.EnumMemberListContext ctx)702 public void enterEnumMemberList(TypeScriptParser.EnumMemberListContext ctx) { 703 super.enterEnumMemberList(ctx); 704 List<TypeScriptParser. EnumMemberContext> memList = ctx.enumMember(); 705 for (TypeScriptParser.EnumMemberContext enumMemberContext : memList) { 706 String memName = enumMemberContext.getText(); 707 System.out.println("Enum mem: " + memName); 708 } 709 } 710 711 @Override enterEnumDeclaration(TypeScriptParser.EnumDeclarationContext ctx)712 public void enterEnumDeclaration(TypeScriptParser.EnumDeclarationContext ctx) { 713 super.enterEnumDeclaration(ctx); 714 System.out.println("find Enum Declare: "); 715 String res = ""; 716 String enumName = ctx.identifier().getText(); 717 res += "Enum: " + enumName; 718 System.out.println("Enum name: " + res); 719 720 List<TypeScriptParser.EnumMemberContext> members = ctx.enumBody().enumMemberList().enumMember(); 721 for (TypeScriptParser.EnumMemberContext member : members) { 722 res += " , " + member.getText(); 723 } 724 System.out.println("Enum: " + res); 725 } 726 727 @Override enterNamespaceDeclaration(TypeScriptParser.NamespaceDeclarationContext ctx)728 public void enterNamespaceDeclaration(TypeScriptParser.NamespaceDeclarationContext ctx) { 729 super.enterNamespaceDeclaration(ctx); 730 System.out.println("find namespace Declare: " + ctx.toString()); 731 } 732 733 @Override enterArguments(TypeScriptParser.ArgumentsContext ctx)734 public void enterArguments(TypeScriptParser.ArgumentsContext ctx) { 735 super.enterArguments(ctx); 736 System.out.println("enterArguments: " + ctx.toString()); 737 } 738 739 @Override enterClassExtendsClause(TypeScriptParser.ClassExtendsClauseContext ctx)740 public void enterClassExtendsClause(TypeScriptParser.ClassExtendsClauseContext ctx) { 741 super.enterClassExtendsClause(ctx); 742 System.out.println("enterClassExtendsClause: " + ctx.getText()); 743 } 744 745 @Override enterClassHeritage(TypeScriptParser.ClassHeritageContext ctx)746 public void enterClassHeritage(TypeScriptParser.ClassHeritageContext ctx) { 747 super.enterClassHeritage(ctx); 748 System.out.println("enterClassHeritage: " + ctx.getText()); 749 } 750 751 @Override enterEnumMember(TypeScriptParser.EnumMemberContext ctx)752 public void enterEnumMember(TypeScriptParser.EnumMemberContext ctx) { 753 super.enterEnumMember(ctx); 754 System.out.println("enterEnumMember: " + ctx.getText()); 755 } 756 757 @Override enterMethodSignature(TypeScriptParser.MethodSignatureContext ctx)758 public void enterMethodSignature(TypeScriptParser.MethodSignatureContext ctx) { 759 super.enterMethodSignature(ctx); 760 System.out.println("enterMethodSignature: " + ctx.getText()); 761 } 762 763 @Override enterAbstractMemberDeclaration(TypeScriptParser.AbstractMemberDeclarationContext ctx)764 public void enterAbstractMemberDeclaration(TypeScriptParser.AbstractMemberDeclarationContext ctx) { 765 super.enterAbstractMemberDeclaration(ctx); 766 System.out.println("find abstract member Declare: " + ctx.getText()); 767 TypeScriptParser.AbstractDeclarationContext adc = ctx.abstractDeclaration(); 768 TypeScriptParser.VariableStatementContext vsc = adc.variableStatement(); 769 if (vsc != null) { 770 TypeScriptParser.VariableDeclarationListContext vdl = vsc.variableDeclarationList(); 771 String paramName = vdl.start.getText(); 772 String paramType = vdl.stop.getText(); 773 774 if (this.currentObject instanceof ClassObj co) { 775 co.addParam(paramName, paramType); 776 } 777 } 778 779 } 780 781 @Override enterInterfaceDeclaration(TypeScriptParser.InterfaceDeclarationContext ctx)782 public void enterInterfaceDeclaration(TypeScriptParser.InterfaceDeclarationContext ctx) { 783 super.enterInterfaceDeclaration(ctx); 784 System.out.println("find interface Declare: " + ctx.getText()); 785 String interfaceName = ctx.identifier().getText(); 786 System.out.println("interface name: " + interfaceName); 787 TypeScriptParser.ObjectTypeContext otc = ctx.objectType(); 788 TypeScriptParser.TypeBodyContext tbc = otc.typeBody(); 789 TypeScriptParser.TypeMemberListContext tlc = tbc.typeMemberList(); 790 List<TypeScriptParser.TypeMemberContext> tmcList = tlc.typeMember(); 791 InterfaceObject io = new InterfaceObject(); 792 io.setName(interfaceName); 793 794 for (TypeScriptParser.TypeMemberContext tmc : tmcList) { 795 String callSign = tmc.callSignature().getText(); 796 System.out.println("interface callSign: " + callSign); 797 String typeAnno = tmc.callSignature().typeAnnotation().stop.getText(); 798 System.out.println("interface typeAnno: " + typeAnno); 799 FuncObj fo = new FuncObj(); 800 fo.setName(""); 801 fo.setRetValue(typeAnno); 802 List<TypeScriptParser.ParameterContext> plc = tmc.callSignature().parameterList().parameter(); 803 for (TypeScriptParser.ParameterContext pc : plc) { 804 System.out.println("interface param: " + pc.getText()); 805 TypeScriptParser. RequiredParameterContext rpc = pc.requiredParameter(); 806 String ta = rpc.typeAnnotation().stop.getText(); 807 String iop = rpc.identifierOrPattern().getText(); 808 System.out.println("interface type: " + ta + " name: " + iop); 809 fo.addParam(iop, ta); 810 } 811 io.addFunc(fo); 812 } 813 this.interfaceObjList.add(io); 814 this.currentObject = io; 815 this.currentToken = TsToken.TS_TOKEN_INTERFACE; 816 817 System.out.println("----------------" + io.toJsonString()); 818 } 819 820 @Override enterAbstractDeclaration(TypeScriptParser.AbstractDeclarationContext ctx)821 public void enterAbstractDeclaration(TypeScriptParser.AbstractDeclarationContext ctx) { 822 super.enterAbstractDeclaration(ctx); 823 System.out.println("find abstract Declare: " + ctx.getText()); 824 } 825 826 @Override enterExportDeclaration(TypeScriptParser.ExportDeclarationContext ctx)827 public void enterExportDeclaration(TypeScriptParser.ExportDeclarationContext ctx) { 828 super.enterExportDeclaration(ctx); 829 System.out.println("find export Declare: " + ctx.toString()); 830 } 831 832 @Override enterConstraint(TypeScriptParser.ConstraintContext ctx)833 public void enterConstraint(TypeScriptParser.ConstraintContext ctx) { 834 super.enterConstraint(ctx); 835 System.out.println("enter constraint: " + ctx.toString()); 836 } 837 838 @Override exitConstraint(TypeScriptParser.ConstraintContext ctx)839 public void exitConstraint(TypeScriptParser.ConstraintContext ctx) { 840 super.exitConstraint(ctx); 841 System.out.println("exit constraint: " + ctx.toString()); 842 } 843 844 @Override enterProgram(TypeScriptParser.ProgramContext ctx)845 public void enterProgram(TypeScriptParser.ProgramContext ctx) { 846 super.enterProgram(ctx); 847 System.out.println("enter Program: " + ctx.toString()); 848 } 849 850 @Override exitProgram(TypeScriptParser.ProgramContext ctx)851 public void exitProgram(TypeScriptParser.ProgramContext ctx) { 852 super.exitProgram(ctx); 853 System.out.println("exit Program: " + ctx.toString()); 854 } 855 856 @Override dump2JsonStr()857 public String dump2JsonStr() { 858 Gson gson = new GsonBuilder().setPrettyPrinting().create(); 859 return gson.toJson(this); 860 } 861 } 862