Chapi is a common language data structure parser, which will parse different language to same JSON object.
Languages Stages (Welcome to PR your usage languages)
Features/Languages | Java | Python | Go | Kotlin | TypeScript | C | C# | Scala | C++ |
---|---|---|---|---|---|---|---|---|---|
syntax parse | ✅ | ✅ | ✅ | 🆕 | ✅ | 🆕 | 🆕 | 🆕 | 🆕 |
function call | ✅ | 🆕 | ✅ | ||||||
arch/package | ✅ | ||||||||
real world validate | ✅ | ||||||||
expression (TBD) |
Language Family wiki
Algol Family https://wiki.c2.com/?AlgolFamily
Languages | plan support | |
---|---|---|
C family | C#, Java, Go, C, C++, Objective-C, Rust, ... | C++, C, Java, C#, Rust? |
Functional | Scheme, Lisp, Clojure, Scala, ... | Scala |
Scripting | Lua, PHP, JavaScript, Python, Perl, Ruby, ... | Python, JavaScript |
Other | Fortran, Swift, Matlab, ... | Swift?, Fortran? |
PS: welcome to PR to send your projects
1add to dependencies
dependencies {
implementation 'com.phodal.chapi:chapi-application:0.0.7'
// or choose languages target
implementation 'com.phodal.chapi:chapi-ast-java:0.0.7'
implementation 'com.phodal.chapi:chapi-domain:0.0.7'
}
import chapi.domain.core.CodeCall
import chapi.domain.core.CodeDataStruct
import chapi.app.analyser
...
val nodes = ChapiAnalyser().analysisByPath(path.absolutePath)
...
examples Java source code:
package adapters.outbound.persistence.blog;
public class BlogPO implements PersistenceObject<Blog> {
@Override
public Blog toDomainModel() {
}
}
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": []
}
Syntax Parse Identify Rules:
brew install antlr
./scripts/compile-antlr.sh
// for multiple project analysis
code_project
code_module
// for package dependency analysis
code_package_info
code_dependency
// package or file as dependency analysis
code_package
code_container
// class-first or function-first
code_data_struct
code_function
// function or class detail
code_annotation
code_field
code_import
code_member
code_position
code_property
// method call information
code_call
环境准备:Intellij IDEA、JDK 11+
Clone 代码:git clone https://github.com/phodal/chapi
执行构建:./gradlew build
为了保证不易出现 bug,项目采用 TDD 的方式进行,即先编写对应的语法测试,然后实现代码。通过尽可能高的测试覆盖率,降低 bug 的出现。
项目主要由 domain + 各种语言的 AST + application 构建:
每个 AST 项目的入口是 xxAnalyser
,返回的是一个 CodeContainer,即代码容器。在非 C# 语言里,等同于 CodeFile,即代码文件。
CodeContainer 内对应的领域模型如下所示:
// class-first or function-first
code_data_struct // 类、struct、interface 等
code_function // 函数。如果是头等函数的语言(first-class function”),会用 NodeName = "default" 包在 code_data_struct 模型中
// function or class detail
code_annotation // 注解
code_field // 全局变量
code_import // 包依赖
code_member // 保留字段
code_position // 位置信息
code_property // 参数相关
// method call information
code_call // 函数调用,如 fmt.Println
通过 TDD 的方式一点点实现下面的功能(可以考虑按顺序),参照示例见 JavaFullIdentListenerTest.kt:
用于发布时,使用标准的 CHANGELOG.md
<type>: <message>
,示例:feat: <grammars> init python & go grammars Phodal Huang 2020/2/2, 5:01 PM
所有的 type 见:
Goal: source code data model for different language & different language family from Language support
@ 2020 A Phodal Huang's Idea. This code is distributed under the MPL license. See LICENSE
in this directory.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。