|
| 1 | +import fs from 'fs'; |
| 2 | +import { |
| 3 | + ApexLexer, |
| 4 | + CommonTokenStream, |
| 5 | + ApexParser, |
| 6 | + CaseInsensitiveInputStream, |
| 7 | + ApexParserListener, |
| 8 | + ClassDeclarationContext, |
| 9 | + DotExpressionContext, |
| 10 | + VariableDeclaratorContext, |
| 11 | + CompilationUnitContext, |
| 12 | +} from '@apexdevtools/apex-parser'; |
| 13 | +import { CharStreams } from 'antlr4ts'; |
| 14 | +import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker'; |
| 15 | + |
| 16 | +export class ApexASTTraverser { |
| 17 | + public static parse(apexClass: string): CompilationUnitContext { |
| 18 | + const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(apexClass))); |
| 19 | + const tokens = new CommonTokenStream(lexer); |
| 20 | + const parser = new ApexParser(tokens); |
| 21 | + const context = parser.compilationUnit(); |
| 22 | + // parser.addParseListener(new interfaceVisitor() as ApexParserListener); |
| 23 | + ParseTreeWalker.DEFAULT.walk(new interfaceVisitor() as ApexParserListener, context); |
| 24 | + return context; |
| 25 | + } |
| 26 | + |
| 27 | + public static traverse(filePath: string): CompilationUnitContext { |
| 28 | + const fileContent = fs.readFileSync(filePath).toString(); |
| 29 | + const ast = this.parse(fileContent); |
| 30 | + return ast; |
| 31 | + } |
| 32 | +} |
| 33 | +export class interfaceVisitor implements ApexParserListener { |
| 34 | + private interfaceImplementations: string[] = []; |
| 35 | + |
| 36 | + public enterClassDeclaration(ctx: ClassDeclarationContext): void { |
| 37 | + this.interfaceImplementations.push(ctx.typeList().typeRef(0).typeName(0).id().Identifier().symbol.text); |
| 38 | + } |
| 39 | + public enterDotExpression(ctx: DotExpressionContext): void { |
| 40 | + // console.log('*********'); |
| 41 | + // console.log(ctx.expression().start.text); |
| 42 | + |
| 43 | + if (ctx.dotMethodCall()) { |
| 44 | + // console.log(ctx.dotMethodCall().anyId().Identifier().symbol.text); |
| 45 | + // ctx.dotMethodCall().expressionList().expression(1).children[0].children[0].children[0]; |
| 46 | + // console.log(ctx.dotMethodCall().expressionList().expression(1).children[0]); |
| 47 | + } |
| 48 | + // console.log('*********'); |
| 49 | + } |
| 50 | + public enterVariableDeclarator(ctx: VariableDeclaratorContext): void { |
| 51 | + if (ctx.id().Identifier().symbol.text === 'DRName') { |
| 52 | + // console.log(ctx.expression()); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +const filePath = '/Users/abhinavkumar2/company/plugin-omnistudio-migration-tool/test/FormulaParserService.cls'; |
| 58 | +ApexASTTraverser.traverse(filePath); |
| 59 | + |
| 60 | +// console.log(ast); |
0 commit comments