How cloudwego/abcoder works
Project type and purpose
• ABCoder is a Go-based CLI SDK for AI-oriented code processing. It ingests source code in various languages, parses it into a universal AST (UniAST), and can emit source code back from that AST. It is neither a web app nor a microservice; it’s a command-line tool & library, focused on language-agnostic code analysis (parsing) and generation (writing).
High-level components
a. CLI entrypoint
• main.go: parses args (“parse” vs. “write”), dispatches to the parser or writer.
b. Universal AST layer
• lang/uniast: defines Node, AST structures, parser & writer interfaces.
c. Language-specific modules
• lang/golang/{parser,writer}: Go parser into UniAST and UniAST back to Go.
• lang/rust: Rust parser into UniAST (writer “coming soon”).
• lang/collect: batch collection & export utilities.
• lang/lsp: language server protocol support (for integration).
• lang/patch: AST patching utilities.
d. Utilities & logging
• lang/utils, lang/log: file handling, string utils, error wrapping, logging.
e. Tests & examples
• testdata/: sample Go and Rust repos for integration tests.
• *.go_test.go in each module.
f. Documentation
• docs/: architecture and UniAST spec in Chinese/English.
• README.md: overview, usage.
Relationships and interactions
• User invokes abcoder parse {language} {repo} (CLI).
→ main.go calls lang/{language}/parser to walk the repo (lang/utils_fs → lang/collect).
→ Parser emits a lang/uniast.AST structure.
→ main.go serializes AST to JSON and writes to stdout.
• User invokes abcoder write {language} ast.json.
→ main.go reads JSON into lang/uniast.AST.
→ Calls lang/{language}/writer to generate source files.
→ Writer uses lang/utils and lang/log to output code.
• lang/lsp.client or handler can be used by external LSP servers to operate on AST.
Architectural patterns & design principles
• Modular language adapters implementing common interfaces (parser/writer).
• Clean separation of concerns: CLI vs. core AST vs. language implementations.
• Interfaces in lang/uniast ensure new languages can plug in.
• Utilities centralized in lang/utils to avoid duplication.
• Tests co-located with modules for clear coverage.