← Back to README | API Reference | Language Guide →
This document explains the public Go package APIs used by the Carv CLI and tooling integrations.
source -> lexer -> parser -> types -> codegen
Typical flow in tools:
- Create a lexer from source text.
- Parse into an AST program.
- Run type checking and inspect structured issues.
- Generate C (
pkg/codegen) and compile it with a C compiler.
Purpose: tokenize source with line/column tracking.
Key API:
lexer.New(input string) *Lexer(*Lexer).NextToken() lexer.Tokenlexer.LookupIdent(string) lexer.TokenType
Usage pattern:
l := lexer.New(src)
for tok := l.NextToken(); tok.Type != lexer.TOKEN_EOF; tok = l.NextToken() {
// consume tokens
}Purpose: syntax analysis with Pratt expression parsing + recursive descent statements/declarations.
Key API:
parser.New(l *lexer.Lexer) *Parser(*Parser).ParseProgram() *ast.Program(*Parser).Errors() []string
Usage pattern:
p := parser.New(lexer.New(src))
prog := p.ParseProgram()
if len(p.Errors()) > 0 {
// handle syntax errors
}Purpose: semantic analysis (typing, ownership/borrow checks, interface validation, async checks).
Key API:
types.NewChecker() *types.Checker(*Checker).Check(program *ast.Program) *types.CheckResult(*Checker).ErrorIssues() []types.CheckIssue(*Checker).WarningIssues() []types.CheckIssue
Structured diagnostics:
CheckIssue{Line, Column, Kind, Message}
Design notes:
- Ownership/borrow/interface/async rules are intentionally separated into focused files.
Errors()/Warnings()remain available as formatted string compatibility helpers.
Purpose: emit C99 code from typed AST.
Key API:
codegen.NewCGenerator() *codegen.CGenerator(*CGenerator).Generate(program *ast.Program) string
Design notes:
- Preserves ownership semantics through generated move/drop/clone logic.
- Lowers async functions to frame structs + poll state machines.
- Lowers interfaces to vtables and fat pointers.
Purpose: module resolution/loading and project configuration (carv.toml).
Key API:
module.NewLoader(basePath string) *module.Loader(*Loader).Load(importPath string, fromFile string) (*module.Module, error)module.LoadConfig(dir string) (*module.Config, error)(*Config).Save(dir string) errormodule.FindProjectRoot(startDir string) (string, error)module.DefaultConfig(name string) *module.Configmodule.LoadLock(dir string) (*module.LockFile, error)module.SaveLock(dir string, lf *module.LockFile) error
Data types:
Config— full carv.toml structure (package info, dependencies, build config, scripts)Dependency— version, git URL, branch, tag, or local pathLockFile/LockedPackage— pinned dependency versions with source and revision
Design notes:
- Supports relative imports, project-local imports, built-in modules, and external packages from
carv_modules/. - Lock files enable reproducible builds by pinning exact git revisions.
Purpose: semantic version parsing and constraint matching for the package manager.
Key API:
semver.ParseVersion(s string) (Version, error)semver.ParseConstraint(s string) (Constraint, error)semver.ParseConstraints(s string) (Constraints, error)(Constraint).Matches(v Version) bool(Constraints).Matches(v Version) boolsemver.Satisfies(versionStr, constraintStr string) (bool, error)
Supported constraints:
- Comparison:
=,!=,>,<,>=,<= - Caret:
^1.2.3(compatible with 1.x.x) - Tilde:
~1.2.3(approximately 1.2.x) - Wildcard:
*(any version) - Compound:
>=1.0.0, <2.0.0
Purpose: transitive dependency resolution with cycle detection and GitHub tag resolution.
Key API:
resolver.New(rootDir string) *Resolver(*Resolver).Resolve(cfg *module.Config) ([]*ResolvedDep, error)(*Resolver).LockFile() *module.LockFileresolver.PrintTree(deps []*ResolvedDep, prefix string)
Data types:
ResolvedDep— resolved dependency with name, version, source, revision, and transitive deps
Design notes:
- Resolves git dependencies by fetching tags via
git ls-remoteand matching against semver constraints. - Path-based dependencies are symlinked (with copy fallback).
- Cycle detection via visiting set prevents infinite recursion.
Purpose: Language Server Protocol implementation for editor integration.
Key API:
lsp.RunServer()— starts the LSP server on stdin/stdout
Protocol: LSP 3.16 via opa-oz/glsp, stdio transport.
Provided capabilities:
textDocument/diagnostic— type errors and warningstextDocument/hover— type information for symbolstextDocument/definition— go-to-definition using tracked symbol positionstextDocument/completion— keyword and symbol suggestions
← Back to README | API Reference | Language Guide →