{"id":13495504,"url":"https://github.com/phodal/chapi","last_synced_at":"2025-05-15T20:03:38.341Z","repository":{"id":41874429,"uuid":"237559200","full_name":"phodal/chapi","owner":"phodal","description":"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 是一个通用层次抽象解析器与信息转换器，它可以将不同编程语言的源代码转换为统一的层次抽象模型。","archived":false,"fork":false,"pushed_at":"2024-11-26T12:04:25.000Z","size":4838,"stargazers_count":294,"open_issues_count":3,"forks_count":46,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-13T12:08:56.248Z","etag":null,"topics":["meta-analysis","meta-data","meta-language","meta-model","meta-programming","modeling"],"latest_commit_sha":null,"homepage":"https://chapi.phodal.com/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phodal.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-01T04:03:53.000Z","updated_at":"2025-05-04T13:08:57.000Z","dependencies_parsed_at":"2022-09-11T17:02:57.247Z","dependency_job_id":"d561ea73-55e9-48c6-afb0-da5166a757ce","html_url":"https://github.com/phodal/chapi","commit_stats":{"total_commits":1381,"total_committers":7,"mean_commits":"197.28571428571428","dds":0.0354815351194786,"last_synced_commit":"b2bdf95496300b60e4259bc2251c9e5bbcba070d"},"previous_names":["phodal/chapi","modernizing/chapi"],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phodal%2Fchapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phodal%2Fchapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phodal%2Fchapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phodal%2Fchapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phodal","download_url":"https://codeload.github.com/phodal/chapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414493,"owners_count":22067271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["meta-analysis","meta-data","meta-language","meta-model","meta-programming","modeling"],"created_at":"2024-07-31T19:01:35.375Z","updated_at":"2025-05-15T20:03:37.623Z","avatar_url":"https://github.com/phodal.png","language":"Kotlin","readme":"# Chapi\n\n\u003cimg src=\"docs/logo.svg\" width=\"100\" height=\"100\" alt=\"Chapi Logo\"\u003e\n\n[![Chapi CI](https://github.com/modernizing/chapi/actions/workflows/ci.yml/badge.svg)](https://github.com/modernizing/chapi/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/phodal/chapi/graph/badge.svg?token=sdB5L3xbRv)](https://codecov.io/gh/phodal/chapi)\n[![Maven Central](https://img.shields.io/maven-central/v/com.phodal.chapi/chapi-domain)](https://central.sonatype.com/artifact/com.phodal.chapi/chapi-domain)\n\n\u003e CHAPI (Common Hierarchical Abstract Parser and Information Converter) streamlines code analysis by converting diverse\n\u003e language source code into a unified abstract model, simplifying cross-language development. Chapi\n\u003e 是一个通用层次抽象解析器与信息转换器，它可以将不同编程语言的源代码转换为统一的层次抽象模型。\n\nChapi =\u003e Cha Pi =\u003e Tea Pi =\u003e Tea π =\u003e 茶 π. See on in\nrefs: [Tea if by sea, cha if by land](https://qz.com/1176962/map-how-the-word-tea-spread-over-land-and-sea-to-conquer-the-world/).\n\nChapi (pronounce /tʃɑpi/) also pronounce XP in Chinese if you always call X in 叉.\n\nlanguage stages:\n\n| Features      | Java | Python | Go | Kotlin | TS/JS | C  | C# | Scala | C++ | Rust |\n|---------------|------|--------|----|--------|-------|----|----|-------|-----|------|\n| http api decl | ✅    | 🆕     | 🆕 | ✅      | ✅     | 🆕 | 🆕 |       | ✅   | 🆕   |\n| syntax parse  | ✅    | ✅      | ✅  | ✅      | ✅     | ✅  | 🆕 | ✅     | 🆕  | ✅    |\n| function call | ✅    | ✅      | 🆕 | ✅      | ✅     | 🆕 |    |       |     | ✅    |\n| arch/package  | ✅    |        |    | ✅      | ✅     | 🆕 |    | ✅     | ✅   | 🆕   |\n| real world    | ✅    |        |    | ✅      | ✅     |    |    |       |     |      |\n\nIDL stages:\n\n| Features      | Protobuf | Thrift | \n|---------------|----------|--------|\n| syntax parse  | ✅        | ✅      |\n| http api decl | ✅        | ✅      |\n| arch/package  | ✅        |        |\n| real world    | ✅        |        | \n\n- [ArchGuard](https://github.com/archguard/archguard) - ArchGuard is an architecture workbench, also for architecture\n  governance, which can analysis architecture in container, component, code level, create architecture fitness\n  functions,\n  and analysis system dependencies.\n- [UnitGen](https://github.com/unit-mesh/unit-gen) is a code fine-tuning data framework that generates data from your\n  existing codebase.\n- [ChocolateFactory](https://github.com/unit-mesh/chocolate-factory) is a cutting-edge LLM toolkit designed to empower\n  you in creating your very own AI assistant.\n\nPS: welcome to PR to send your projects\n\n## Language Information\n\nlanguage versions（tested）:\n\n- Java: 8, 11, 17\n- TypeScript/JavaScript\n- Kotlin\n- Rust: v1.60.0\n- Python: 2, 3\n\n```goovy\n// tier 1 languages\n\":chapi-ast-java\",\n\":chapi-ast-typescript\",\n\n// tier 1 model language\n\":chapi-ast-protobuf\",\n\n// tier 2 languages\n\":chapi-ast-kotlin\",\n\":chapi-ast-go\",\n\":chapi-ast-python\",\n\":chapi-ast-scala\",\n\n// tier 3 languages\n\":chapi-ast-rust\",\n\":chapi-ast-csharp\",\n\":chapi-ast-c\",\n\":chapi-ast-cpp\",\n\n// others\n\":chapi-parser-toml\",\n\":chapi-parser-cmake\",\n```\n\nLanguage Family [wiki](https://en.wikipedia.org/wiki/First-class_function)\n\nAlgol Family [https://wiki.c2.com/?AlgolFamily](https://wiki.c2.com/?AlgolFamily)\n\n|            | Languages                                     | plan support            |\n|------------|-----------------------------------------------|-------------------------|\n| C family   | C#, Java, Go, C, C++, Objective-C, Rust, ...  | C++, C, Java, C#, Rust? |\n| Functional | Scheme, Lisp, Clojure, Scala, ...             | Scala                   |\n| Scripting  | Lua, PHP, JavaScript, Python, Perl, Ruby, ... | Python, JavaScript      |\n| Other      | Fortran, Swift, Matlab, ...                   | Swift?, Fortran?        |\n\n## Specify Rule\n\n**scan by twice**. In order to success get:\n\n- find data struct in same package\n\n### TypeScript\n\n1. PackageName will use resolvePath, package of `src/grammar/blbla.ts` is `@.grammar`\n2. Function in file will use `default` as DataStructure.Name\n3. export default `Object` in file will use `default` as FunctionName, and will belong to `default` DataStructure\n\n### C# issues\n\n- interpolated_string parse\n  issue：[official grammar](https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Generated/CSharp.Generated.g4)\n  ，Antlr issues: [https://github.com/antlr/grammars-v4/issues/1146](https://github.com/antlr/grammars-v4/issues/1146)\n- import analysis support\n    - in C#, import `namespace` can use call in namespace\n\n### C \n\nWe use [https://github.com/shevek/jcpp](https://github.com/shevek/jcpp) to pre-process C code.\n\n### Kotlin\n\n- need to implementation `warpTargetFullType` to got Class in same package.\n\n## Usage\n\n1. add to ``dependencies``\n\n```\ndependencies {\n    implementation 'com.phodal.chapi:chapi-ast-java:2.3.6'\n    implementation 'com.phodal.chapi:chapi-domain:2.3.6'\n}\n```\n\n### Usage\n\n```kotlin\nimport chapi.domain.core.CodeDataStruct\nimport kotlinx.coroutines.async\nimport kotlinx.coroutines.awaitAll\nimport kotlinx.coroutines.runBlocking\nimport org.archguard.scanner.core.sourcecode.LanguageSourceCodeAnalyser\nimport org.archguard.scanner.core.sourcecode.SourceCodeContext\nimport java.io.File\n\nclass CSharpAnalyser(override val context: SourceCodeContext)\n\nprivate val client = context.client\nprivate val impl = chapi.ast.csharpast.CSharpAnalyser()\n\nfun analyse(): List\u003cCodeDataStruct\u003e = runBlocking {\n    getFilesByPath(context.path) {\n        it.absolutePath.endsWith(\".cs\")\n    }\n        .map { async { analysisByFile(it) } }.awaitAll()\n        .flatten()\n        .also { client.saveDataStructure(it) }\n}\n\nfun analysisByFile(file: File): List\u003cCodeDataStruct\u003e {\n    val codeContainer = impl.analysis(file.readContent(), file.name)\n    return codeContainer.Containers.flatMap { container -\u003e\n        container.DataStructures.map {\n            it.apply {\n                it.Imports = codeContainer.Imports\n                it.FilePath = file.absolutePath\n            }\n        }\n    }\n}\n}\n\n```\n\n## Examples\n\nexamples Java source code:\n\n```java\npackage adapters.outbound.persistence.blog;\n\npublic class BlogPO implements PersistenceObject\u003cBlog\u003e {\n    @Override\n    public Blog toDomainModel() {\n\n    }\n}\n```\n\nexamples output\n\n```json\n{\n    \"Imports\": [],\n    \"Implements\": [\n        \"PersistenceObject\u003cBlog\u003e\"\n    ],\n    \"NodeName\": \"BlogPO\",\n    \"Extend\": \"\",\n    \"Type\": \"CLASS\",\n    \"FilePath\": \"\",\n    \"InOutProperties\": [],\n    \"Functions\": [\n        {\n            \"IsConstructor\": false,\n            \"InnerFunctions\": [],\n            \"Position\": {\n                \"StartLine\": 6,\n                \"StartLinePosition\": 133,\n                \"StopLine\": 8,\n                \"StopLinePosition\": 145\n            },\n            \"Package\": \"\",\n            \"Name\": \"toDomainModel\",\n            \"MultipleReturns\": [],\n            \"Annotations\": [\n                {\n                    \"Name\": \"Override\",\n                    \"KeyValues\": []\n                }\n            ],\n            \"Extension\": {},\n            \"Override\": false,\n            \"extensionMap\": {},\n            \"Parameters\": [],\n            \"InnerStructures\": [],\n            \"ReturnType\": \"Blog\",\n            \"Modifiers\": [],\n            \"FunctionCalls\": []\n        }\n    ],\n    \"Annotations\": [],\n    \"Extension\": {},\n    \"Parameters\": [],\n    \"Fields\": [],\n    \"MultipleExtend\": [],\n    \"InnerStructures\": [],\n    \"Package\": \"adapters.outbound.persistence.blog\",\n    \"FunctionCalls\": []\n}\n```\n\n## Development\n\nSyntax Parse Identify Rules:\n\n1. package name\n2. import name\n3. class / data struct\n    1. struct name\n    2. struct parameters\n    3. function name\n    4. return types\n    5. function parameters\n4. function\n    1. function name\n    2. return types\n    3. function parameters\n5. method call\n    1. new instance call\n    2. parameter call\n    3. field call\n\n### Build Antlr Grammar\n\n1. setup Antlr: `brew install antlr`\n2. run compile: `./scripts/compile-antlr.sh`\n\n### Data Structures\n\n```\n// for multiple project analysis\ncode_project\ncode_module\n\n// for package dependency analysis\ncode_package_info\ncode_dependency\n\n// package or file as dependency analysis\ncode_package\ncode_container\n\n// class-first or function-first\ncode_data_struct\ncode_function\n\n// function or class detail\ncode_annotation\ncode_field\ncode_import\ncode_member\ncode_position\ncode_property\n\n// method call information\ncode_call\n```\n\n## Development（Chinese Version）\n\nEnv：Intellij IDEA、JDK 11+\n\n1. Clone code：``git clone https://github.com/phodal/chapi``\n\n2. Build：``./gradlew build``\n\n### 参与开发\n\n为了保证不易出现 bug，项目采用 TDD 的方式进行，即先编写对应的语法测试，然后实现代码。通过尽可能高的测试覆盖率，降低 bug 的出现。\n\n项目主要由 domain + 各种语言的 AST + application 构建：\n\n- domain，构建统一的代码模型\n- 各语言 AST\n- application，对外暴露的简易 API\n\n每个 AST 项目的入口是 ``xxAnalyser``，返回的是一个 CodeContainer，即代码容器。在非 C# 语言里，等同于 CodeFile，即代码文件。\n\nCodeContainer 内对应的领域模型如下所示：\n\n```\n// class-first or function-first\ncode_data_struct // 类、struct、interface 等\ncode_function    // 函数。如果是头等函数的语言（first-class function”），会用 NodeName = \"default\" 包在 code_data_struct 模型中\n\n// function or class detail\ncode_annotation  // 注解\ncode_field       // 全局变量\ncode_import      // 包依赖\ncode_member      // 保留字段\ncode_position    // 位置信息\ncode_property    // 参数相关\n\n// method call information\ncode_call        // 函数调用，如 fmt.Println\n```\n\n### 加入开发\n\n1. 寻找感兴趣的语言 / 添加新的语言 AST\n\n通过 TDD\n的方式一点点实现下面的功能（可以考虑按顺序），参照示例见 [JavaFullIdentListenerTest.kt](https://github.com/phodal/chapi/blob/master/chapi-ast-java/src/test/kotlin/chapi/ast/javaast/JavaFullIdentListenerTest.kt)：\n\n1. package name\n2. import name\n3. class / data struct\n    1. struct name\n    2. struct parameters\n    3. function name\n    4. return types\n    5. function parameters\n4. function\n    1. function name\n    2. return types\n    3. function parameters\n5. method call\n    1. new instance call\n    2. parameter call\n    3. field call\n    4. other calls...\n\n### 提交信息格式\n\n用于发布时，使用标准的 [CHANGELOG.md](CHANGELOG.md)\n\n`\u003ctype\u003e[Language]: \u003cmessage\u003e`，示例：`feat(java): \u003cgrammars\u003e init python \u0026 go grammars Phodal Huang 2020/2/2, 5:01 PM`\n\n所有的 type 见：\n\n- build: 影响构建系统或外部依赖关系的更改（示例范围：gulp，broccoli，npm）\n- ci: 更改我们的持续集成文件和脚本（示例范围：Travis，Circle，BrowserStack，SauceLabs）\n- docs: 仅文档更改\n- feat: 一个新功能\n- fix: 修复错误\n- perf: 改进性能的代码更改\n- refactor: 代码更改，既不修复错误也不添加功能\n- style: 不影响代码含义的变化（空白，格式化，缺少分号等）\n- test: 添加缺失测试或更正现有测试\n\nLicense\n---\n\n[![Phodal's Idea](http://brand.phodal.com/shields/idea-small.svg)](http://ideas.phodal.com/)\n\n@2020 A [Phodal Huang](https://www.phodal.com)'s [Idea](http://github.com/phodal/ideas). This code is distributed under\nthe MPL license. See `LICENSE` in this directory.\n\n","funding_links":[],"categories":["Kotlin","Everything as Code"],"sub_categories":["Coding"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphodal%2Fchapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphodal%2Fchapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphodal%2Fchapi/lists"}