Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

CHAPI (Common Hierarchical Abstract Parser and Information Converter) streamlines code analysis by converting diverse language source code into a unified abstract model, simplifying cross-language development. Chapi 是一个通用层次抽象解析器与信息转换器,它可以将不同编程语言的源代码转换为统一的层次抽象模型。

License

NotificationsYou must be signed in to change notification settings

phodal/chapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chapi Logo

Chapi CIcodecovMaven Central

CHAPI (Common Hierarchical Abstract Parser and Information Converter) streamlines code analysis by converting diverselanguage source code into a unified abstract model, simplifying cross-language development. Chapi是一个通用层次抽象解析器与信息转换器,它可以将不同编程语言的源代码转换为统一的层次抽象模型。

Chapi => Cha Pi => Tea Pi => Tea π => 茶 π. See on inrefs:Tea if by sea, cha if by land.

Chapi (pronounce /tʃɑpi/) also pronounce XP in Chinese if you always call X in 叉.

language stages:

FeaturesJavaPythonGoKotlinTS/JSCC#ScalaC++Rust
http api decl🆕🆕🆕🆕🆕
syntax parse🆕🆕
function call🆕🆕
arch/package🆕🆕
real world

IDL stages:

FeaturesProtobufThrift
syntax parse
http api decl
arch/package
real world
  • ArchGuard - ArchGuard is an architecture workbench, also for architecturegovernance, which can analysis architecture in container, component, code level, create architecture fitnessfunctions,and analysis system dependencies.
  • UnitGen is a code fine-tuning data framework that generates data from yourexisting codebase.
  • ChocolateFactory is a cutting-edge LLM toolkit designed to empoweryou in creating your very own AI assistant.

PS: welcome to PR to send your projects

Language Information

language versions(tested):

  • Java: 8, 11, 17
  • TypeScript/JavaScript
  • Kotlin
  • Rust: v1.60.0
  • Python: 2, 3
// tier 1 languages":chapi-ast-java",":chapi-ast-typescript",// tier 1 model language":chapi-ast-protobuf",// tier 2 languages":chapi-ast-kotlin",":chapi-ast-go",":chapi-ast-python",":chapi-ast-scala",// tier 3 languages":chapi-ast-rust",":chapi-ast-csharp",":chapi-ast-c",":chapi-ast-cpp",// others":chapi-parser-toml",":chapi-parser-cmake",

Language Familywiki

Algol Familyhttps://wiki.c2.com/?AlgolFamily

Languagesplan support
C familyC#, Java, Go, C, C++, Objective-C, Rust, ...C++, C, Java, C#, Rust?
FunctionalScheme, Lisp, Clojure, Scala, ...Scala
ScriptingLua, PHP, JavaScript, Python, Perl, Ruby, ...Python, JavaScript
OtherFortran, Swift, Matlab, ...Swift?, Fortran?

Specify Rule

scan by twice. In order to success get:

  • find data struct in same package

TypeScript

  1. PackageName will use resolvePath, package ofsrc/grammar/blbla.ts is@.grammar
  2. Function in file will usedefault as DataStructure.Name
  3. export defaultObject in file will usedefault as FunctionName, and will belong todefault DataStructure

C# issues

C

We usehttps://github.com/shevek/jcpp to pre-process C code.

Kotlin

  • need to implementationwarpTargetFullType to got Class in same package.

Usage

  1. add todependencies
dependencies {    implementation 'com.phodal.chapi:chapi-ast-java:2.3.6'    implementation 'com.phodal.chapi:chapi-domain:2.3.6'}

Usage

importchapi.domain.core.CodeDataStructimportkotlinx.coroutines.asyncimportkotlinx.coroutines.awaitAllimportkotlinx.coroutines.runBlockingimportorg.archguard.scanner.core.sourcecode.LanguageSourceCodeAnalyserimportorg.archguard.scanner.core.sourcecode.SourceCodeContextimportjava.io.FileclassCSharpAnalyser(overridevalcontext:SourceCodeContext)privateval client= context.clientprivateval impl= chapi.ast.csharpast.CSharpAnalyser()funanalyse():List<CodeDataStruct>= runBlocking {    getFilesByPath(context.path) {        it.absolutePath.endsWith(".cs")    }        .map { async { analysisByFile(it) } }.awaitAll()        .flatten()        .also { client.saveDataStructure(it) }}funanalysisByFile(file:File):List<CodeDataStruct> {val codeContainer= impl.analysis(file.readContent(), file.name)return codeContainer.Containers.flatMap { container->        container.DataStructures.map {            it.apply {                it.Imports= codeContainer.Imports                it.FilePath= file.absolutePath            }        }    }}}

Examples

examples Java source code:

packageadapters.outbound.persistence.blog;publicclassBlogPOimplementsPersistenceObject<Blog> {@OverridepublicBlogtoDomainModel() {    }}

examples output

{"Imports": [],"Implements": ["PersistenceObject<Blog>"    ],"NodeName":"BlogPO","Extend":"","Type":"CLASS","FilePath":"","InOutProperties": [],"Functions": [        {"IsConstructor":false,"InnerFunctions": [],"Position": {"StartLine":6,"StartLinePosition":133,"StopLine":8,"StopLinePosition":145            },"Package":"","Name":"toDomainModel","MultipleReturns": [],"Annotations": [                {"Name":"Override","KeyValues": []                }            ],"Extension": {},"Override":false,"extensionMap": {},"Parameters": [],"InnerStructures": [],"ReturnType":"Blog","Modifiers": [],"FunctionCalls": []        }    ],"Annotations": [],"Extension": {},"Parameters": [],"Fields": [],"MultipleExtend": [],"InnerStructures": [],"Package":"adapters.outbound.persistence.blog","FunctionCalls": []}

Development

Syntax Parse Identify Rules:

  1. package name
  2. import name
  3. class / data struct
    1. struct name
    2. struct parameters
    3. function name
    4. return types
    5. function parameters
  4. function
    1. function name
    2. return types
    3. function parameters
  5. method call
    1. new instance call
    2. parameter call
    3. field call

Build Antlr Grammar

  1. setup Antlr:brew install antlr
  2. run compile:./scripts/compile-antlr.sh

Data Structures

// for multiple project analysiscode_projectcode_module// for package dependency analysiscode_package_infocode_dependency// package or file as dependency analysiscode_packagecode_container// class-first or function-firstcode_data_structcode_function// function or class detailcode_annotationcode_fieldcode_importcode_membercode_positioncode_property// method call informationcode_call

Development(Chinese Version)

Env:Intellij IDEA、JDK 11+

  1. Clone code:git clone https://github.com/phodal/chapi

  2. Build:./gradlew build

参与开发

为了保证不易出现 bug,项目采用 TDD 的方式进行,即先编写对应的语法测试,然后实现代码。通过尽可能高的测试覆盖率,降低 bug 的出现。

项目主要由 domain + 各种语言的 AST + application 构建:

  • domain,构建统一的代码模型
  • 各语言 AST
  • application,对外暴露的简易 API

每个 AST 项目的入口是xxAnalyser,返回的是一个 CodeContainer,即代码容器。在非 C# 语言里,等同于 CodeFile,即代码文件。

CodeContainer 内对应的领域模型如下所示:

// class-first or function-firstcode_data_struct // 类、struct、interface 等code_function    // 函数。如果是头等函数的语言(first-class function”),会用 NodeName = "default" 包在 code_data_struct 模型中// function or class detailcode_annotation  // 注解code_field       // 全局变量code_import      // 包依赖code_member      // 保留字段code_position    // 位置信息code_property    // 参数相关// method call informationcode_call        // 函数调用,如 fmt.Println

加入开发

  1. 寻找感兴趣的语言 / 添加新的语言 AST

通过 TDD的方式一点点实现下面的功能(可以考虑按顺序),参照示例见JavaFullIdentListenerTest.kt

  1. package name
  2. import name
  3. class / data struct
    1. struct name
    2. struct parameters
    3. function name
    4. return types
    5. function parameters
  4. function
    1. function name
    2. return types
    3. function parameters
  5. method call
    1. new instance call
    2. parameter call
    3. field call
    4. other calls...

提交信息格式

用于发布时,使用标准的CHANGELOG.md

<type>[Language]: <message>,示例:feat(java): <grammars> init python & go grammars Phodal Huang 2020/2/2, 5:01 PM

所有的 type 见:

  • build: 影响构建系统或外部依赖关系的更改(示例范围:gulp,broccoli,npm)
  • ci: 更改我们的持续集成文件和脚本(示例范围:Travis,Circle,BrowserStack,SauceLabs)
  • docs: 仅文档更改
  • feat: 一个新功能
  • fix: 修复错误
  • perf: 改进性能的代码更改
  • refactor: 代码更改,既不修复错误也不添加功能
  • style: 不影响代码含义的变化(空白,格式化,缺少分号等)
  • test: 添加缺失测试或更正现有测试

License

Phodal's Idea

@2020 APhodal Huang'sIdea. This code is distributed underthe MPL license. SeeLICENSE in this directory.

About

CHAPI (Common Hierarchical Abstract Parser and Information Converter) streamlines code analysis by converting diverse language source code into a unified abstract model, simplifying cross-language development. Chapi 是一个通用层次抽象解析器与信息转换器,它可以将不同编程语言的源代码转换为统一的层次抽象模型。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp