diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 index a93c1fbf..c3ff1c17 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4 @@ -355,7 +355,7 @@ constructSignature ; callSignature - : typeParameters? parameterBlock (':' (typePredicateWithOperatorTypeRef | typeRef))? + : typeParameters? '(' parameterList? ','? ')' (':' (typePredicateWithOperatorTypeRef | typeRef))? ; indexSignature @@ -876,6 +876,12 @@ singleExpression | Yield ({this.notLineTerminator()}? expressionSequence)? # YieldExpression | Await singleExpression # AwaitExpression + + // TODO: careful use those + | singleExpression '(' (argumentList ','?)? ')' # ArgumentsExpression + // RealtionExpression will have conflict + | singleExpression '<' typeArgumentList '>' '(' (argumentList ','?)? ')'# ArgumentsExpression + // respect precedence by order of sub-rules | singleExpression assignmentOperator singleExpression # AssignmentExpression | singleExpression '?' singleExpression ':' singleExpression # TernaryExpression @@ -919,8 +925,6 @@ singleExpression | templateStringLiteral # TemplateStringExpression | singleExpression As asExpression # CastAsExpression - // TODO: careful use those - | singleExpression typeArguments? '(' (argumentList ','?)? ')' # ArgumentsExpression | htmlElements # HtmlElementExpression ; diff --git a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt index c04c42a3..7eaf5adb 100644 --- a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt +++ b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptFullIdentListener.kt @@ -255,11 +255,11 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { return codeFunction } -// private fun buildImplements(typeList: TypeScriptParser.ClassOrInterfaceTypeListContext?): Array { -// return typeList?.typeReference()?.map { typeRefCtx -> -// typeRefCtx.typeName().text -// }?.toTypedArray() ?: arrayOf() -// } + private fun buildImplements(typeList: TypeScriptParser.ClassOrInterfaceTypeListContext?): Array { + return typeList?.parameterizedTypeRef()?.map { typeRefCtx -> + typeRefCtx.typeName().text + }?.toTypedArray() ?: arrayOf() + } override fun exitClassDeclaration(ctx: TypeScriptParser.ClassDeclarationContext?) { hasEnterClass = false @@ -278,15 +278,14 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { Position = buildPosition(ctx) ) -// if (ctx.interfaceExtendsClause() != null) { -// val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList()) -// currentNode.Extend = elements[0] -// } -// -// val objectTypeCtx = ctx.objectType() -// if (objectTypeCtx.typeBody() != null) { -// this.buildInterfaceBody(objectTypeCtx.typeBody().typeMemberList()) -// } + if (ctx.interfaceExtendsClause() != null) { + val elements = buildImplements(ctx.interfaceExtendsClause().classOrInterfaceTypeList()) + currentNode.Extend = elements[0] + } + + if (ctx.interfaceBody() != null) { + this.buildInterfaceBody(ctx.interfaceBody().interfaceMemberList()) + } nodeMap[nodeName] = currentNode } @@ -296,7 +295,7 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { } private fun buildInterfaceBody(typeMemberList: TypeScriptParser.InterfaceMemberListContext) { - typeMemberList?.interfaceMember()?.forEach { memberContext -> + typeMemberList.interfaceMember()?.forEach { memberContext -> when (val memberChild = memberContext.getChild(0)) { is TypeScriptParser.PropertySignatureContext -> { buildInterfacePropertySignature(memberChild) @@ -783,15 +782,15 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { callSignCtx: TypeScriptParser.CallSignatureContext, currentFunc: CodeFunction ) { -// if (callSignCtx.parameterList() != null) { -// val parameters = buildMethodParameters(callSignCtx.parameterList()) -// currentFunc.Parameters = parameters -// } -// -// if (callSignCtx.typeAnnotation() != null) { -// val returnType = buildReturnTypeByType(callSignCtx.typeAnnotation()) -// currentFunc.MultipleReturns += returnType -// } + if (callSignCtx.parameterList() != null) { + val parameters = buildMethodParameters(callSignCtx.parameterList()) + currentFunc.Parameters = parameters + } + + if (callSignCtx.typeRef() != null) { + val returnType = buildReturnTypeByTypeRef(callSignCtx.typeRef()!!) + currentFunc.MultipleReturns += returnType + } } private fun buildReturnTypeByType(typeAnnotationContext: TypeScriptParser.TypeAnnotationContext?): CodeProperty = @@ -799,6 +798,11 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() { TypeType = buildTypeAnnotation(typeAnnotationContext) ?: "", TypeValue = "" ) + private fun buildReturnTypeByTypeRef(typeRefContext: TypeScriptParser.TypeRefContext): CodeProperty = + CodeProperty( + TypeType = processRef(typeRefContext) ?: "", TypeValue = "" + ) + override fun enterExpressionStatement(ctx: TypeScriptParser.ExpressionStatementContext?) { if (ctx?.expressionSequence() == null) return