|
| 1 | +import { |
| 2 | + ArgumentListCtx, |
| 3 | + BaseJavaCstVisitorWithDefaults, |
| 4 | + BlockStatementsCtx, |
| 5 | + ConstructorDeclarationCstNode, |
| 6 | + ConstructorModifierCtx, |
| 7 | + FormalParameterCtx, |
| 8 | + FormalParameterListCtx, |
| 9 | + SimpleTypeNameCtx, |
| 10 | + UnqualifiedExplicitConstructorInvocationCtx, |
| 11 | + VariableArityParameterCtx, |
| 12 | + VariableDeclaratorIdCtx, |
| 13 | + VariableParaRegularParameterCtx |
| 14 | +} from 'java-parser' |
| 15 | + |
| 16 | +import { |
| 17 | + Identifier, |
| 18 | + FormalParameter, |
| 19 | + ConstructorModifier, |
| 20 | + ConstructorDeclaration |
| 21 | +} from '../types/classes' |
| 22 | +import { BlockStatement, ExplicitConstructorInvocation } from '../types/blocks-and-statements' |
| 23 | +import { Location } from '../types' |
| 24 | +import { BlockStatementExtractor } from './block-statement-extractor' |
| 25 | +import { TypeExtractor } from './type-extractor' |
| 26 | +import { ExpressionExtractor } from './expression-extractor' |
| 27 | + |
| 28 | +export class ConstructorExtractor extends BaseJavaCstVisitorWithDefaults { |
| 29 | + private modifier: Array<ConstructorModifier> = [] |
| 30 | + private identifier: Identifier |
| 31 | + private params: Array<FormalParameter> = [] |
| 32 | + private body: Array<BlockStatement> = [] |
| 33 | + private location: Location |
| 34 | + |
| 35 | + extract(cst: ConstructorDeclarationCstNode): ConstructorDeclaration { |
| 36 | + this.location = cst.location |
| 37 | + this.visit(cst) |
| 38 | + return { |
| 39 | + kind: 'ConstructorDeclaration', |
| 40 | + constructorModifier: this.modifier, |
| 41 | + constructorDeclarator: { |
| 42 | + identifier: this.identifier, |
| 43 | + formalParameterList: this.params |
| 44 | + }, |
| 45 | + constructorBody: { |
| 46 | + kind: 'Block', |
| 47 | + blockStatements: this.body, |
| 48 | + location: this.location |
| 49 | + }, |
| 50 | + location: this.location |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + constructorModifier(ctx: ConstructorModifierCtx) { |
| 55 | + const possibleModifiers = [ctx.Public, ctx.Protected, ctx.Private] |
| 56 | + .filter(x => x !== undefined) |
| 57 | + .map(x => (x ? x[0].image : x)) |
| 58 | + this.modifier.push(possibleModifiers[0] as ConstructorModifier) |
| 59 | + } |
| 60 | + |
| 61 | + simpleTypeName(ctx: SimpleTypeNameCtx) { |
| 62 | + this.identifier = ctx.Identifier[0].image |
| 63 | + } |
| 64 | + |
| 65 | + formalParameterList(ctx: FormalParameterListCtx) { |
| 66 | + this.params = ctx.formalParameter.map(p => this.visit(p)) |
| 67 | + } |
| 68 | + |
| 69 | + formalParameter(ctx: FormalParameterCtx) { |
| 70 | + if (ctx.variableParaRegularParameter) { |
| 71 | + return this.visit(ctx.variableParaRegularParameter) |
| 72 | + } /* if (ctx.variableArityParameter) */ else { |
| 73 | + return this.visit(ctx.variableArityParameter!) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + variableParaRegularParameter(ctx: VariableParaRegularParameterCtx) { |
| 78 | + const typeExtractor = new TypeExtractor() |
| 79 | + return { |
| 80 | + kind: 'FormalParameter', |
| 81 | + unannType: typeExtractor.extract(ctx.unannType[0]), |
| 82 | + identifier: this.visit(ctx.variableDeclaratorId) |
| 83 | + } as FormalParameter |
| 84 | + } |
| 85 | + |
| 86 | + variableArityParameter(ctx: VariableArityParameterCtx) { |
| 87 | + const typeExtractor = new TypeExtractor() |
| 88 | + return { |
| 89 | + kind: 'FormalParameter', |
| 90 | + unannType: typeExtractor.extract(ctx.unannType[0]), |
| 91 | + identifier: ctx.Identifier[0].image |
| 92 | + } as FormalParameter |
| 93 | + } |
| 94 | + |
| 95 | + variableDeclaratorId(ctx: VariableDeclaratorIdCtx) { |
| 96 | + return ctx.Identifier[0].image |
| 97 | + } |
| 98 | + |
| 99 | + blockStatements(ctx: BlockStatementsCtx) { |
| 100 | + ctx.blockStatement.forEach(x => { |
| 101 | + const blockStatementExtractor = new BlockStatementExtractor() |
| 102 | + this.body.push(blockStatementExtractor.extract(x)) |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + unqualifiedExplicitConstructorInvocation(ctx: UnqualifiedExplicitConstructorInvocationCtx) { |
| 107 | + const expConInv = { |
| 108 | + kind: 'ExplicitConstructorInvocation', |
| 109 | + thisOrSuper: ctx.This?.[0].image || ctx.Super?.[0].image, |
| 110 | + argumentList: ctx.argumentList ? this.visit(ctx.argumentList) : [], |
| 111 | + location: this.location |
| 112 | + } as ExplicitConstructorInvocation |
| 113 | + this.body.push(expConInv) |
| 114 | + } |
| 115 | + |
| 116 | + argumentList(ctx: ArgumentListCtx) { |
| 117 | + const expressionExtractor = new ExpressionExtractor() |
| 118 | + return ctx.expression.map(e => expressionExtractor.extract(e)) |
| 119 | + } |
| 120 | +} |
0 commit comments