mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-11-22 17:30:49 +08:00
v0.5: CBM tree-sitter engine, 59 vendored language grammars
- Replace Go-binding tree-sitter with vendored C grammars compiled via CGo (internal/cbm/); eliminates all Go module/linker fragility - Add 32 new languages: Clojure, CMake, COBOL, Common Lisp, CUDA, Elm, Emacs Lisp, Fortran, F#, GLSL, GraphQL, INI, JSON, Julia, Makefile, Markdown, Meson, Nix, Protobuf, Svelte, Verilog, Vim Script, Vue, XML (plus C, C#, Erlang, SQL, YAML quality improvements) - Adaptive worker concurrency and mmap prefetch for faster indexing - Graph buffer: batched SQLite writes for 3-5x throughput improvement - Fix cancellation test: use 50ms timeout (pipeline now indexes 212-file Erlang repo in ~400ms, well under the old 2s deadline) - Fix isTestFunction: add "Test" prefix for C#, "test" prefix for Scala
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26"
|
||||
|
||||
- uses: golangci/golangci-lint-action@v7
|
||||
with:
|
||||
version: v2.10
|
||||
args: --timeout=5m
|
||||
|
||||
test-unix:
|
||||
needs: [lint]
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
- os: ubuntu-24.04-arm
|
||||
goos: linux
|
||||
goarch: arm64
|
||||
- os: macos-14
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
- os: macos-15-intel
|
||||
goos: darwin
|
||||
goarch: amd64
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26"
|
||||
|
||||
- name: Run tests
|
||||
run: go test ./...
|
||||
|
||||
- name: Build binary
|
||||
run: |
|
||||
CGO_ENABLED=1 go build \
|
||||
-o codebase-memory-mcp \
|
||||
./cmd/codebase-memory-mcp/
|
||||
|
||||
- name: Smoke test
|
||||
run: bash scripts/smoke-test.sh ./codebase-memory-mcp
|
||||
|
||||
test-windows:
|
||||
needs: [lint]
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26"
|
||||
|
||||
- uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: UCRT64
|
||||
path-type: inherit
|
||||
install: mingw-w64-ucrt-x86_64-gcc
|
||||
|
||||
- name: Run tests
|
||||
shell: msys2 {0}
|
||||
run: go test ./...
|
||||
|
||||
- name: Build binary
|
||||
shell: msys2 {0}
|
||||
run: |
|
||||
CGO_ENABLED=1 CC=gcc go build \
|
||||
-o codebase-memory-mcp.exe \
|
||||
./cmd/codebase-memory-mcp/
|
||||
|
||||
- name: Smoke test
|
||||
shell: msys2 {0}
|
||||
run: bash scripts/smoke-test.sh ./codebase-memory-mcp.exe
|
||||
@@ -70,6 +70,9 @@ jobs:
|
||||
-o codebase-memory-mcp \
|
||||
./cmd/codebase-memory-mcp/
|
||||
|
||||
- name: Smoke test
|
||||
run: bash scripts/smoke-test.sh ./codebase-memory-mcp
|
||||
|
||||
- name: Create archive
|
||||
run: tar -czf codebase-memory-mcp-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz codebase-memory-mcp
|
||||
|
||||
@@ -109,6 +112,10 @@ jobs:
|
||||
-o codebase-memory-mcp.exe \
|
||||
./cmd/codebase-memory-mcp/
|
||||
|
||||
- name: Smoke test
|
||||
shell: msys2 {0}
|
||||
run: bash scripts/smoke-test.sh ./codebase-memory-mcp.exe
|
||||
|
||||
- name: Create archive
|
||||
shell: pwsh
|
||||
run: Compress-Archive -Path codebase-memory-mcp.exe -DestinationPath codebase-memory-mcp-windows-amd64.zip
|
||||
|
||||
@@ -56,6 +56,7 @@ linters:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
- internal/cbm
|
||||
issues:
|
||||
max-issues-per-linter: 0
|
||||
max-same-issues: 0
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"mcpServers": {
|
||||
"codebase-memory-mcp": {
|
||||
"command": "/Users/martinvogel/.local/bin/codebase-memory-mcp",
|
||||
"args": []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ Parses source code with [tree-sitter](https://tree-sitter.github.io/tree-sitter/
|
||||
|
||||
## Features
|
||||
|
||||
- **35 languages**: Python, Go, JavaScript, TypeScript, TSX, Rust, Java, C++, C#, C, PHP, Lua, Scala, Kotlin, Ruby, Bash, Zig, Elixir, Haskell, OCaml, Objective-C, Swift, Dart, Perl, Groovy, Erlang, R, HTML, CSS, SCSS, YAML, TOML, HCL, SQL, Dockerfile
|
||||
- **59 languages**: Python, Go, JavaScript, TypeScript, TSX, Rust, Java, C++, C#, C, PHP, Lua, Scala, Kotlin, Ruby, Bash, Zig, Elixir, Haskell, OCaml, Objective-C, Swift, Dart, Perl, Groovy, Erlang, R, Clojure, F#, Julia, Vim Script, Nix, Common Lisp, Elm, Fortran, CUDA, COBOL, Verilog, Emacs Lisp, HTML, CSS, SCSS, YAML, TOML, HCL, SQL, Dockerfile, JSON, XML, Markdown, Makefile, CMake, Protobuf, GraphQL, Vue, Svelte, Meson, GLSL, INI
|
||||
- **Architecture overview**: `get_architecture` returns languages, packages, entry points, routes, hotspots, boundaries, layers, and clusters in a single call — instant codebase orientation
|
||||
- **Architecture Decision Records**: `manage_adr` persists architectural decisions (PURPOSE, STACK, ARCHITECTURE, PATTERNS, TRADEOFFS, PHILOSOPHY) across sessions with section filtering and validation
|
||||
- **Louvain community detection**: Discovers hidden functional modules across packages by clustering CALLS, HTTP_CALLS, and ASYNC_CALLS edges
|
||||
@@ -57,7 +57,7 @@ Claude Code formats and explains the results.
|
||||
|
||||
**Why no built-in LLM?** Other code graph tools embed an LLM to translate natural language into graph queries. This means extra API keys, extra cost per query, and another model to configure. With MCP, the AI assistant you're already talking to *is* the query translator — no duplication needed.
|
||||
|
||||
**Token efficiency**: Compared to having an AI agent grep through your codebase file by file, graph queries return precise results in a single tool call. In benchmarks across 35 real-world repos (78 to 49K nodes), five structural queries consumed ~3,400 tokens via codebase-memory-mcp versus ~412,000 tokens via file-by-file exploration — a **99.2% reduction**.
|
||||
**Token efficiency**: Compared to having an AI agent grep through your codebase file by file, graph queries return precise results in a single tool call. In benchmarks across 35 real-world repos (78 to 49K nodes), five structural queries consumed ~3,400 tokens via codebase-memory-mcp versus ~412,000 tokens via file-by-file exploration — a **99.2% reduction**. All 59 supported languages use the same efficient graph backend.
|
||||
|
||||
## Performance
|
||||
|
||||
@@ -663,13 +663,14 @@ make install # go install
|
||||
|
||||
## Language Benchmark
|
||||
|
||||
Benchmarked against 35 real open-source repositories (78 to 49K nodes). 12 standardized questions per language, up to 5 retry attempts each. Grading: PASS (1.0) / PARTIAL (0.5) / FAIL (0.0). Overall: **91.8%** weighted score.
|
||||
59 languages supported. Benchmarked against 35 real open-source repositories (78 to 49K nodes). 12 standardized questions per language, up to 5 retry attempts each. Grading: PASS (1.0) / PARTIAL (0.5) / FAIL (0.0). Overall: **91.8%** weighted score across benchmarked languages.
|
||||
|
||||
| Tier | Score | Languages |
|
||||
|------|-------|-----------|
|
||||
| **Tier 1 — Excellent** | >= 90% | Lua, Kotlin, C++, Perl, Objective-C, Groovy, C, Bash, Zig, Swift, CSS, YAML, TOML, HTML, SCSS, HCL, Dockerfile |
|
||||
| **Tier 2 — Good** | 75–89% | Python, TypeScript, TSX, Go, Rust, Java, R, Dart, JavaScript, Erlang, Elixir, Scala, Ruby, PHP, C#, SQL |
|
||||
| **Tier 3 — Functional** | < 75% | OCaml (72%), Haskell (62%) |
|
||||
| **Not yet benchmarked** | — | Clojure, F#, Julia, Vim Script, Nix, Common Lisp, Elm, Fortran, CUDA, COBOL, Verilog, Emacs Lisp, JSON, XML, Markdown, Makefile, CMake, Protobuf, GraphQL, Vue, Svelte, Meson, GLSL, INI |
|
||||
|
||||
**Stress test**: Linux kernel `drivers/net/ethernet/intel/` — 20K nodes, 67K edges, 129K-char deep traces, zero timeouts.
|
||||
|
||||
@@ -681,7 +682,7 @@ See [`BENCHMARK.md`](BENCHMARK.md) for the full 35-language benchmark with per-q
|
||||
cmd/codebase-memory-mcp/ Entry point (MCP stdio server + CLI mode + install/update commands)
|
||||
internal/
|
||||
store/ SQLite graph storage (nodes, edges, traversal, search, architecture, Louvain clustering)
|
||||
lang/ Language specs (35 languages, tree-sitter node types)
|
||||
lang/ Language specs (59 languages, tree-sitter node types)
|
||||
parser/ Tree-sitter grammar loading and AST parsing
|
||||
pipeline/ Multi-pass indexing (structure → definitions → calls → HTTP links → communities → tests)
|
||||
httplink/ Cross-service HTTP route/call-site matching
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/DeusData/codebase-memory-mcp/internal/lang"
|
||||
"github.com/DeusData/codebase-memory-mcp/internal/parser"
|
||||
|
||||
tree_sitter "github.com/tree-sitter/go-tree-sitter"
|
||||
)
|
||||
|
||||
func printAST(node *tree_sitter.Node, source []byte, indent int) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
prefix := ""
|
||||
for i := 0; i < indent; i++ {
|
||||
prefix += " "
|
||||
}
|
||||
parentKind := "nil"
|
||||
if node.Parent() != nil {
|
||||
parentKind = node.Parent().Kind()
|
||||
}
|
||||
text := string(source[node.StartByte():node.EndByte()])
|
||||
if len(text) > 60 {
|
||||
text = text[:60] + "..."
|
||||
}
|
||||
fmt.Printf("%s%s (parent=%s) %q\n", prefix, node.Kind(), parentKind, text)
|
||||
for i := uint(0); i < node.ChildCount(); i++ {
|
||||
printAST(node.Child(i), source, indent+1)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Test Go - check if var groups have wrapping
|
||||
goCode := []byte("package main\n\nvar globalVar = 42\n\nvar (\n\ta = 1\n\tb = 2\n)\n")
|
||||
fmt.Println("=== GO AST ===")
|
||||
tree, err := parser.Parse(lang.Go, goCode)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
if tree != nil {
|
||||
printAST(tree.RootNode(), goCode, 0)
|
||||
tree.Close()
|
||||
}
|
||||
|
||||
// Test Rust
|
||||
rustCode := []byte("pub static X: i32 = 5;\nconst Y: &str = \"hello\";\n")
|
||||
fmt.Println("\n=== RUST AST ===")
|
||||
tree2, err := parser.Parse(lang.Rust, rustCode)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
if tree2 != nil {
|
||||
printAST(tree2.RootNode(), rustCode, 0)
|
||||
tree2.Close()
|
||||
}
|
||||
|
||||
// Test Python decorated function
|
||||
pyCode := []byte("@app.route('/api')\ndef handler():\n pass\n")
|
||||
fmt.Println("\n=== PYTHON DECORATED FUNC ===")
|
||||
tree3, err := parser.Parse(lang.Python, pyCode)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
if tree3 != nil {
|
||||
printAST(tree3.RootNode(), pyCode, 0)
|
||||
tree3.Close()
|
||||
}
|
||||
|
||||
// Test Python with type annotation assignment
|
||||
pyCode2 := []byte("x: int = 5\nlogger: Logger = get_logger()\n")
|
||||
fmt.Println("\n=== PYTHON TYPE ANNOTATED ASSIGNMENT ===")
|
||||
tree4, err := parser.Parse(lang.Python, pyCode2)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
if tree4 != nil {
|
||||
printAST(tree4.RootNode(), pyCode2, 0)
|
||||
tree4.Close()
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -3,43 +3,8 @@ module github.com/DeusData/codebase-memory-mcp
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
github.com/DerekStride/tree-sitter-sql v0.0.0
|
||||
github.com/UserNobody14/tree-sitter-dart v0.0.0
|
||||
github.com/alex-pinkus/tree-sitter-swift v0.0.0
|
||||
github.com/camdencheek/tree-sitter-dockerfile v0.0.0
|
||||
github.com/mattn/go-sqlite3 v1.14.34
|
||||
github.com/modelcontextprotocol/go-sdk v1.4.0
|
||||
github.com/murtaza64/tree-sitter-groovy v0.0.0
|
||||
github.com/r-lib/tree-sitter-r v0.0.0
|
||||
github.com/tree-sitter-grammars/tree-sitter-hcl v1.2.0
|
||||
github.com/tree-sitter-grammars/tree-sitter-kotlin v1.1.0
|
||||
github.com/tree-sitter-grammars/tree-sitter-lua v0.4.1
|
||||
github.com/tree-sitter-grammars/tree-sitter-objc v1.2.1-0.20250131075517-181a81b8f23a
|
||||
github.com/tree-sitter-grammars/tree-sitter-scss v0.0.0
|
||||
github.com/tree-sitter-grammars/tree-sitter-toml v0.7.0
|
||||
github.com/tree-sitter-grammars/tree-sitter-yaml v0.7.2
|
||||
github.com/tree-sitter-grammars/tree-sitter-zig v1.1.2
|
||||
github.com/tree-sitter/go-tree-sitter v0.25.0
|
||||
github.com/tree-sitter/tree-sitter-bash v0.25.1
|
||||
github.com/tree-sitter/tree-sitter-c v0.24.1
|
||||
github.com/tree-sitter/tree-sitter-c-sharp v0.23.1
|
||||
github.com/tree-sitter/tree-sitter-cpp v0.23.4
|
||||
github.com/tree-sitter/tree-sitter-css v0.25.0
|
||||
github.com/tree-sitter/tree-sitter-elixir v0.0.0
|
||||
github.com/tree-sitter/tree-sitter-erlang v0.0.0
|
||||
github.com/tree-sitter/tree-sitter-go v0.25.0
|
||||
github.com/tree-sitter/tree-sitter-haskell v0.0.0
|
||||
github.com/tree-sitter/tree-sitter-html v0.23.2
|
||||
github.com/tree-sitter/tree-sitter-java v0.23.5
|
||||
github.com/tree-sitter/tree-sitter-javascript v0.25.0
|
||||
github.com/tree-sitter/tree-sitter-ocaml v0.24.2
|
||||
github.com/tree-sitter/tree-sitter-perl v0.0.0
|
||||
github.com/tree-sitter/tree-sitter-php v0.24.2
|
||||
github.com/tree-sitter/tree-sitter-python v0.25.0
|
||||
github.com/tree-sitter/tree-sitter-ruby v0.23.1
|
||||
github.com/tree-sitter/tree-sitter-rust v0.24.0
|
||||
github.com/tree-sitter/tree-sitter-scala v0.24.0
|
||||
github.com/tree-sitter/tree-sitter-typescript v0.23.2
|
||||
github.com/zeebo/xxh3 v1.1.0
|
||||
golang.org/x/sync v0.19.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
@@ -48,36 +13,9 @@ require (
|
||||
require (
|
||||
github.com/google/jsonschema-go v0.4.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/mattn/go-pointer v0.0.1 // indirect
|
||||
github.com/segmentio/asm v1.1.3 // indirect
|
||||
github.com/segmentio/encoding v0.5.3 // indirect
|
||||
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 // indirect
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||
golang.org/x/oauth2 v0.34.0 // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/tools v0.41.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/tree-sitter/go-tree-sitter => github.com/DeusData/go-tree-sitter v0.26.0-deusdata.1
|
||||
|
||||
replace github.com/tree-sitter/tree-sitter-elixir => github.com/elixir-lang/tree-sitter-elixir v0.3.4
|
||||
|
||||
replace github.com/tree-sitter/tree-sitter-haskell => github.com/tree-sitter-grammars/tree-sitter-haskell v0.0.0-20251125091915-7fa19f195803
|
||||
|
||||
replace github.com/UserNobody14/tree-sitter-dart => github.com/DeusData/tree-sitter-dart v0.0.1-deusdata.1
|
||||
|
||||
replace github.com/DerekStride/tree-sitter-sql => github.com/DeusData/tree-sitter-sql v0.0.2-deusdata.1
|
||||
|
||||
replace github.com/tree-sitter/tree-sitter-perl => github.com/DeusData/tree-sitter-perl v0.0.4-deusdata.1
|
||||
|
||||
replace github.com/murtaza64/tree-sitter-groovy => github.com/DeusData/tree-sitter-groovy v0.0.2-deusdata.1
|
||||
|
||||
replace github.com/tree-sitter-grammars/tree-sitter-scss => github.com/DeusData/tree-sitter-scss v0.0.2-deusdata.1
|
||||
|
||||
replace github.com/r-lib/tree-sitter-r => github.com/DeusData/tree-sitter-r v0.0.1-deusdata.1
|
||||
|
||||
replace github.com/tree-sitter/tree-sitter-erlang => github.com/DeusData/tree-sitter-erlang v0.0.1-deusdata.1
|
||||
|
||||
replace github.com/alex-pinkus/tree-sitter-swift => github.com/DeusData/tree-sitter-swift v0.0.3-deusdata.1
|
||||
|
||||
replace github.com/camdencheek/tree-sitter-dockerfile => github.com/DeusData/tree-sitter-dockerfile v0.0.1-deusdata.1
|
||||
|
||||
@@ -1,126 +1,31 @@
|
||||
github.com/DeusData/go-tree-sitter v0.26.0-deusdata.1 h1:/cTwrXEE/3t6HYOvnHzgSZpO8MOwxFFFlz65Cc9xf9U=
|
||||
github.com/DeusData/go-tree-sitter v0.26.0-deusdata.1/go.mod h1:r77ig7BikoZhHrrsjAnv8RqGti5rtSyvDHPzgTPsUuU=
|
||||
github.com/DeusData/tree-sitter-dart v0.0.1-deusdata.1 h1:G8ZJ9glIj73w6fUV9+INR6fGLNi3udrRjs7Bd8yIVAc=
|
||||
github.com/DeusData/tree-sitter-dart v0.0.1-deusdata.1/go.mod h1:gaimGqOZqVTIc7JSlnsUqjXSjAYisTq7nhzAR5VQ0Rw=
|
||||
github.com/DeusData/tree-sitter-dockerfile v0.0.1-deusdata.1 h1:WU/rFyOW5PiDHbvsiC2B8Y/r0f6ttfdZOA+HM62Nsyw=
|
||||
github.com/DeusData/tree-sitter-dockerfile v0.0.1-deusdata.1/go.mod h1:pc+Ka90MBUzYW0uqj0djhsC/jooRI39T6bqN4+vUaag=
|
||||
github.com/DeusData/tree-sitter-erlang v0.0.1-deusdata.1 h1:HhK7QUh57lSOz+tPztIpoKthVIj+P6+EBQh+dg2smao=
|
||||
github.com/DeusData/tree-sitter-erlang v0.0.1-deusdata.1/go.mod h1:BeJG7Ezjd3yHpmBcc4Gjw8xkXQz3BDrQdPr7c46z3MA=
|
||||
github.com/DeusData/tree-sitter-groovy v0.0.2-deusdata.1 h1:eawA+AystZ+aKSJqjKOSo7YHdTy2u9Q1+443MGZgYJA=
|
||||
github.com/DeusData/tree-sitter-groovy v0.0.2-deusdata.1/go.mod h1:YWGloyOrnz4RXXlyAu0MmLf3qkpoYwfZAJ9Hn658Ywo=
|
||||
github.com/DeusData/tree-sitter-perl v0.0.4-deusdata.1 h1:4HNDSdmGBeMI+cQF/MYlDkGpbvh7dNHZCg2bx2LjJqo=
|
||||
github.com/DeusData/tree-sitter-perl v0.0.4-deusdata.1/go.mod h1:mUpbTt2dUb6i1ckLX98gXNyCRdjOpHlk7MQqSA37T1A=
|
||||
github.com/DeusData/tree-sitter-r v0.0.1-deusdata.1 h1:pyTuUUefQpPKQVdJ7159g6xehzi/2xzgpALWNgwt4FY=
|
||||
github.com/DeusData/tree-sitter-r v0.0.1-deusdata.1/go.mod h1:EiQTehwt8ojqpEcjZHKJmU26zijUuHkmwFi6Rot/GGQ=
|
||||
github.com/DeusData/tree-sitter-scss v0.0.2-deusdata.1 h1:JgA+OjWTaQqN09QKvYxDo1WuZvDZ4AHt2dUJuZRjiYc=
|
||||
github.com/DeusData/tree-sitter-scss v0.0.2-deusdata.1/go.mod h1:Fi256VkrezhT/uu/vZ7dJVHthET6S8PcZ2YNq/Bz3wE=
|
||||
github.com/DeusData/tree-sitter-sql v0.0.2-deusdata.1 h1:+6yX51GXklTXWRVs5TGkhaWlS7MRjwtjTGUs9j89grg=
|
||||
github.com/DeusData/tree-sitter-sql v0.0.2-deusdata.1/go.mod h1:tKhfNbTiFmw3xaK1QSZjCPlOXhPkcf168z/FOpsBDCQ=
|
||||
github.com/DeusData/tree-sitter-swift v0.0.3-deusdata.1 h1:QB0FpUDRIUZ/dQCrCx65idtPaHnvgSXzkM9NzETDmqY=
|
||||
github.com/DeusData/tree-sitter-swift v0.0.3-deusdata.1/go.mod h1:nMZLtsEtko+0F2g09G5Tm16uRdDoczLZDE5O207ruYQ=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/elixir-lang/tree-sitter-elixir v0.3.4 h1:M0Wc3XXEJyGpVELOzxqhV069bBsRAQyGdehWw/G+nWk=
|
||||
github.com/elixir-lang/tree-sitter-elixir v0.3.4/go.mod h1:wNBVf64kzvhSbZ8ojVtBF1jRiqGY0lsuK5Kx/60s6Z0=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8=
|
||||
github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
|
||||
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
|
||||
github.com/mattn/go-sqlite3 v1.14.34 h1:3NtcvcUnFBPsuRcno8pUtupspG/GM+9nZ88zgJcp6Zk=
|
||||
github.com/mattn/go-sqlite3 v1.14.34/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modelcontextprotocol/go-sdk v1.3.1 h1:TfqtNKOIWN4Z1oqmPAiWDC2Jq7K9OdJaooe0teoXASI=
|
||||
github.com/modelcontextprotocol/go-sdk v1.3.1/go.mod h1:DgVX498dMD8UJlseK1S5i1T4tFz2fkBk4xogC3D15nw=
|
||||
github.com/modelcontextprotocol/go-sdk v1.4.0 h1:u0kr8lbJc1oBcawK7Df+/ajNMpIDFE41OEPxdeTLOn8=
|
||||
github.com/modelcontextprotocol/go-sdk v1.4.0/go.mod h1:Nxc2n+n/GdCebUaqCOhTetptS17SXXNu9IfNTaLDi1E=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
|
||||
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
|
||||
github.com/segmentio/encoding v0.5.3 h1:OjMgICtcSFuNvQCdwqMCv9Tg7lEOXGwm1J5RPQccx6w=
|
||||
github.com/segmentio/encoding v0.5.3/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=
|
||||
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
|
||||
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tree-sitter-grammars/tree-sitter-haskell v0.0.0-20251125091915-7fa19f195803 h1:qGglJu/VLlh+LvkvOK26WHK3YL+wHbpJERiPpQhWst8=
|
||||
github.com/tree-sitter-grammars/tree-sitter-haskell v0.0.0-20251125091915-7fa19f195803/go.mod h1:NWY55NC9F0agDcuCKOkPqAQWZQETS3cUjO+T+a21IIc=
|
||||
github.com/tree-sitter-grammars/tree-sitter-hcl v1.2.0 h1:jl3v597Dii91OHcHAUrTQaSEK7oODNh6yK8z4H5xXFA=
|
||||
github.com/tree-sitter-grammars/tree-sitter-hcl v1.2.0/go.mod h1:NzEB+a6rpv14H4tCrCfzhTorCyvYYb3T9+o/e4d0pdU=
|
||||
github.com/tree-sitter-grammars/tree-sitter-kotlin v1.1.0 h1:SWIUDASa+WPhDDem1U5IJpYwQEezkqXrUI61OcnORzM=
|
||||
github.com/tree-sitter-grammars/tree-sitter-kotlin v1.1.0/go.mod h1:eH+flFf3QOa9c9BY9g3Bz02F7zTq30kGIG3cgB0lSlI=
|
||||
github.com/tree-sitter-grammars/tree-sitter-lua v0.4.1 h1:DOxLDVTEmkLuoIHXJ65h5oyHcEK89mn9DOhlnPQRAmQ=
|
||||
github.com/tree-sitter-grammars/tree-sitter-lua v0.4.1/go.mod h1:hIOfn+lxpU4SRrtejLVrU2+8SAoRwNC01m3XaR/Cw0A=
|
||||
github.com/tree-sitter-grammars/tree-sitter-objc v1.2.1-0.20250131075517-181a81b8f23a h1:nUAwJerIz8+ndhTYW1i2tpIwFcKX8Y65EkVHq5T+WyM=
|
||||
github.com/tree-sitter-grammars/tree-sitter-objc v1.2.1-0.20250131075517-181a81b8f23a/go.mod h1:3WHVwcdAgzNBRoNnFqo5NFFpX9Im21tMlnwA9mIiDK8=
|
||||
github.com/tree-sitter-grammars/tree-sitter-toml v0.7.0 h1:VQOyz+wSHzNZJ7gUMHAWVEP4Xt/YBZkeL7iurBv/9xc=
|
||||
github.com/tree-sitter-grammars/tree-sitter-toml v0.7.0/go.mod h1:hagl3VSOuQdsaDpGY13oGMz2cLK2tUDAf4mNNW2pDBQ=
|
||||
github.com/tree-sitter-grammars/tree-sitter-yaml v0.7.2 h1:Mmr9LYGTdr+8iD3ZEK1+j2FKZtY1Jt3y5Wq9dedgwfs=
|
||||
github.com/tree-sitter-grammars/tree-sitter-yaml v0.7.2/go.mod h1:tOZ8GmiIfLySKNOX58+PV2mvjFJdfciksfwotcELKbg=
|
||||
github.com/tree-sitter-grammars/tree-sitter-zig v1.1.2 h1:j8JARutysdxMwBEfVLGx9us7cdSzD1TTui/pPLGCFDk=
|
||||
github.com/tree-sitter-grammars/tree-sitter-zig v1.1.2/go.mod h1:ekWQEqj2e/gQal396f5rKJ6L14/a4bMPMqSRzmf8OZE=
|
||||
github.com/tree-sitter/tree-sitter-bash v0.25.1 h1:ZD3MK4oDB5lAsFztqbdcyYEd24pxDtx3g9UOWA062rE=
|
||||
github.com/tree-sitter/tree-sitter-bash v0.25.1/go.mod h1:AksQ6zE+sP9hnp7mKTMT7Q+CwpthV7VGQLXvweVXz9U=
|
||||
github.com/tree-sitter/tree-sitter-c v0.24.1 h1:GV9DjvIV6uYe3W/JBKMFwE4hJcRxzRDq63llxNFHOkY=
|
||||
github.com/tree-sitter/tree-sitter-c v0.24.1/go.mod h1:/SpJlv2BuiCgFA5xvtgukFGi51WxctByPUGDxPl60fc=
|
||||
github.com/tree-sitter/tree-sitter-c-sharp v0.23.1 h1:ddG6osP34sMieVNN6lu5ZG/3N8Wn+67+43BmipqidyM=
|
||||
github.com/tree-sitter/tree-sitter-c-sharp v0.23.1/go.mod h1:H7/aFm5vR1A8Yn5VIOfLWPdlKuJsMgZ5eDmaJdv8bY0=
|
||||
github.com/tree-sitter/tree-sitter-cpp v0.23.4 h1:LaWZsiqQKvR65yHgKmnaqA+uz6tlDJTJFCyFIeZU/8w=
|
||||
github.com/tree-sitter/tree-sitter-cpp v0.23.4/go.mod h1:doqNW64BriC7WBCQ1klf0KmJpdEvfxyXtoEybnBo6v8=
|
||||
github.com/tree-sitter/tree-sitter-css v0.25.0 h1:S5NbzhdZ5LE5V474wmdg+7NthmLjIg5v4wbyewMpziw=
|
||||
github.com/tree-sitter/tree-sitter-css v0.25.0/go.mod h1:0Z46XCb3L16nVOVw0Lhb43pzloUG/4T6E/pAOE62fEw=
|
||||
github.com/tree-sitter/tree-sitter-embedded-template v0.23.2 h1:nFkkH6Sbe56EXLmZBqHHcamTpmz3TId97I16EnGy4rg=
|
||||
github.com/tree-sitter/tree-sitter-embedded-template v0.23.2/go.mod h1:HNPOhN0qF3hWluYLdxWs5WbzP/iE4aaRVPMsdxuzIaQ=
|
||||
github.com/tree-sitter/tree-sitter-go v0.25.0 h1:cEB0Q3LHgZtS+ECHx9wcP7AwzoOddJFQCVmytX42cVU=
|
||||
github.com/tree-sitter/tree-sitter-go v0.25.0/go.mod h1:Jrx8QqYN0v7npv1fJRH1AznddllYiCMUChtVjxPK040=
|
||||
github.com/tree-sitter/tree-sitter-html v0.23.2 h1:1UYDV+Yd05GGRhVnTcbP58GkKLSHHZwVaN+lBZV11Lc=
|
||||
github.com/tree-sitter/tree-sitter-html v0.23.2/go.mod h1:gpUv/dG3Xl/eebqgeYeFMt+JLOY9cgFinb/Nw08a9og=
|
||||
github.com/tree-sitter/tree-sitter-java v0.23.5 h1:J9YeMGMwXYlKSP3K4Us8CitC6hjtMjqpeOf2GGo6tig=
|
||||
github.com/tree-sitter/tree-sitter-java v0.23.5/go.mod h1:NRKlI8+EznxA7t1Yt3xtraPk1Wzqh3GAIC46wxvc320=
|
||||
github.com/tree-sitter/tree-sitter-javascript v0.25.0 h1:ZkWETb66/w8cc13yhfnNuHOLDQWl3BnKlH6f9AdR88c=
|
||||
github.com/tree-sitter/tree-sitter-javascript v0.25.0/go.mod h1:lmGD1EJdCA+v0S1u2fFgepMg/opzSg/4pgFym2FPGAs=
|
||||
github.com/tree-sitter/tree-sitter-json v0.24.8 h1:tV5rMkihgtiOe14a9LHfDY5kzTl5GNUYe6carZBn0fQ=
|
||||
github.com/tree-sitter/tree-sitter-json v0.24.8/go.mod h1:F351KK0KGvCaYbZ5zxwx/gWWvZhIDl0eMtn+1r+gQbo=
|
||||
github.com/tree-sitter/tree-sitter-ocaml v0.24.2 h1:8tK5RFs0WjO1LvdW+qA6N7oYmLvanaDmPU3Ww7hPq+8=
|
||||
github.com/tree-sitter/tree-sitter-ocaml v0.24.2/go.mod h1:18SxqyGRpOHl8CmxfC2oTs5mXRDveWhtCnprYBAT0oc=
|
||||
github.com/tree-sitter/tree-sitter-php v0.24.2 h1:yy+COnaaHUNDTKODfNbHhVRD4mQpFELTnBK9+EhpO+w=
|
||||
github.com/tree-sitter/tree-sitter-php v0.24.2/go.mod h1:cEzabPRy4doSxaP9CF9u4FXc3L9Q3Mek3XPVCfqJQRw=
|
||||
github.com/tree-sitter/tree-sitter-python v0.25.0 h1:O6XD9v8U1LOcRc3cNj9nM7XufrtEBezE6VrpRrHZDf0=
|
||||
github.com/tree-sitter/tree-sitter-python v0.25.0/go.mod h1:cpdthSy/Yoa28aJFBscFHlGiU+cnSiSh1kuDVtI8YeM=
|
||||
github.com/tree-sitter/tree-sitter-ruby v0.23.1 h1:T/NKHUA+iVbHM440hFx+lzVOzS4dV6z8Qw8ai+72bYo=
|
||||
github.com/tree-sitter/tree-sitter-ruby v0.23.1/go.mod h1:kUS4kCCQloFcdX6sdpr8p6r2rogbM6ZjTox5ZOQy8cA=
|
||||
github.com/tree-sitter/tree-sitter-rust v0.24.0 h1:nr3ga5ThXyPR5n/DiMq4Zh3e8pMR+sfzk088QE809+g=
|
||||
github.com/tree-sitter/tree-sitter-rust v0.24.0/go.mod h1:hfeGWic9BAfgTrc7Xf6FaOAguCFJRo3RBbs7QJ6D7MI=
|
||||
github.com/tree-sitter/tree-sitter-scala v0.24.0 h1:F8UcZQdNQSkOGtkW8tUsFrqifOVXzmzJ19/JSbB+X3E=
|
||||
github.com/tree-sitter/tree-sitter-scala v0.24.0/go.mod h1:BmDV0f9rgsnGuG9QtKXQZnqJvECyR9fM8wVg984ulBo=
|
||||
github.com/tree-sitter/tree-sitter-typescript v0.23.2 h1:/Odvphn18PniVixb9e97X0DbNVsU6Qocv9mfkyzdXwU=
|
||||
github.com/tree-sitter/tree-sitter-typescript v0.23.2/go.mod h1:zjzMXT/Ulffel2xfOcAkQQkiAkmgnbtPGlFQw/5X4xA=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
|
||||
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
|
||||
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
|
||||
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
|
||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
||||
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
||||
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
||||
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
|
||||
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "arena.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void cbm_arena_init(CBMArena* a) {
|
||||
memset(a, 0, sizeof(*a));
|
||||
a->block_size = CBM_ARENA_DEFAULT_BLOCK_SIZE;
|
||||
a->blocks[0] = (char*)malloc(a->block_size);
|
||||
if (a->blocks[0]) {
|
||||
a->nblocks = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int arena_grow(CBMArena* a, size_t min_size) {
|
||||
if (a->nblocks >= CBM_ARENA_MAX_BLOCKS) {
|
||||
return 0;
|
||||
}
|
||||
size_t new_size = a->block_size * 2;
|
||||
if (new_size < min_size) {
|
||||
new_size = min_size;
|
||||
}
|
||||
char* block = (char*)malloc(new_size);
|
||||
if (!block) {
|
||||
return 0;
|
||||
}
|
||||
a->blocks[a->nblocks] = block;
|
||||
a->nblocks++;
|
||||
a->block_size = new_size;
|
||||
a->used = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void* cbm_arena_alloc(CBMArena* a, size_t n) {
|
||||
if (n == 0) {
|
||||
return NULL;
|
||||
}
|
||||
// 8-byte alignment
|
||||
n = (n + 7) & ~(size_t)7;
|
||||
|
||||
if (a->nblocks == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->used + n > a->block_size) {
|
||||
if (!arena_grow(a, n)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
char* ptr = a->blocks[a->nblocks - 1] + a->used;
|
||||
a->used += n;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char* cbm_arena_strdup(CBMArena* a, const char* s) {
|
||||
if (!s) return NULL;
|
||||
size_t len = strlen(s);
|
||||
char* dst = (char*)cbm_arena_alloc(a, len + 1);
|
||||
if (dst) {
|
||||
memcpy(dst, s, len + 1);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
char* cbm_arena_strndup(CBMArena* a, const char* s, size_t len) {
|
||||
if (!s) return NULL;
|
||||
char* dst = (char*)cbm_arena_alloc(a, len + 1);
|
||||
if (dst) {
|
||||
memcpy(dst, s, len);
|
||||
dst[len] = '\0';
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
char* cbm_arena_sprintf(CBMArena* a, const char* fmt, ...) {
|
||||
// First pass: compute length
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int needed = vsnprintf(NULL, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (needed < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* dst = (char*)cbm_arena_alloc(a, (size_t)needed + 1);
|
||||
if (!dst) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(args, fmt);
|
||||
vsnprintf(dst, (size_t)needed + 1, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void cbm_arena_destroy(CBMArena* a) {
|
||||
for (int i = 0; i < a->nblocks; i++) {
|
||||
free(a->blocks[i]);
|
||||
}
|
||||
memset(a, 0, sizeof(*a));
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef CBM_ARENA_H
|
||||
#define CBM_ARENA_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
// CBMArena is a simple bump allocator that allocates from fixed-size blocks.
|
||||
// All memory is freed at once via cbm_arena_destroy(). Individual frees are not
|
||||
// supported — this is by design for per-file extraction where all data has the
|
||||
// same lifetime.
|
||||
#define CBM_ARENA_MAX_BLOCKS 256
|
||||
#define CBM_ARENA_DEFAULT_BLOCK_SIZE (64 * 1024) // 64KB initial
|
||||
|
||||
typedef struct {
|
||||
char* blocks[CBM_ARENA_MAX_BLOCKS];
|
||||
int nblocks;
|
||||
size_t block_size;
|
||||
size_t used; // bytes used in current block
|
||||
} CBMArena;
|
||||
|
||||
// Initialize an arena with the default block size.
|
||||
void cbm_arena_init(CBMArena* a);
|
||||
|
||||
// Allocate n bytes from the arena. Returns NULL on OOM or block exhaustion.
|
||||
// All returned pointers are 8-byte aligned.
|
||||
void* cbm_arena_alloc(CBMArena* a, size_t n);
|
||||
|
||||
// Duplicate a string into arena memory. Returns arena-owned copy.
|
||||
char* cbm_arena_strdup(CBMArena* a, const char* s);
|
||||
|
||||
// Duplicate a string of known length into arena memory. NUL-terminates.
|
||||
char* cbm_arena_strndup(CBMArena* a, const char* s, size_t len);
|
||||
|
||||
// sprintf into arena memory. Returns arena-owned string.
|
||||
char* cbm_arena_sprintf(CBMArena* a, const char* fmt, ...)
|
||||
__attribute__((format(printf, 2, 3)));
|
||||
|
||||
// Free all blocks. Arena is invalid after this call.
|
||||
void cbm_arena_destroy(CBMArena* a);
|
||||
|
||||
#endif // CBM_ARENA_H
|
||||
@@ -0,0 +1,281 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
// Atomic counters for profiling parse vs extraction time (nanoseconds).
|
||||
// Accessed from multiple threads; using _Atomic for safe accumulation.
|
||||
#include <stdatomic.h>
|
||||
static _Atomic uint64_t total_parse_ns = 0;
|
||||
static _Atomic uint64_t total_extract_ns = 0;
|
||||
static _Atomic uint64_t total_files = 0;
|
||||
|
||||
static uint64_t now_ns(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
|
||||
}
|
||||
|
||||
// cbm_get_profile returns accumulated parse/extract times and file count.
|
||||
void cbm_get_profile(uint64_t* parse_ns, uint64_t* extract_ns, uint64_t* files) {
|
||||
*parse_ns = atomic_load(&total_parse_ns);
|
||||
*extract_ns = atomic_load(&total_extract_ns);
|
||||
*files = atomic_load(&total_files);
|
||||
}
|
||||
|
||||
// cbm_reset_profile zeros the profiling counters.
|
||||
void cbm_reset_profile(void) {
|
||||
atomic_store(&total_parse_ns, 0);
|
||||
atomic_store(&total_extract_ns, 0);
|
||||
atomic_store(&total_files, 0);
|
||||
}
|
||||
|
||||
// --- Growable array push functions ---
|
||||
|
||||
#define GROW_ARRAY(arr, arena, type) do { \
|
||||
if ((arr)->count >= (arr)->cap) { \
|
||||
int new_cap = (arr)->cap == 0 ? 32 : (arr)->cap * 2; \
|
||||
type* new_items = (type*)cbm_arena_alloc(arena, (size_t)new_cap * sizeof(type)); \
|
||||
if (!new_items) return; \
|
||||
if ((arr)->items && (arr)->count > 0) { \
|
||||
memcpy(new_items, (arr)->items, (size_t)(arr)->count * sizeof(type)); \
|
||||
} \
|
||||
(arr)->items = new_items; \
|
||||
(arr)->cap = new_cap; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
void cbm_defs_push(CBMDefArray* arr, CBMArena* a, CBMDefinition def) {
|
||||
GROW_ARRAY(arr, a, CBMDefinition);
|
||||
arr->items[arr->count++] = def;
|
||||
}
|
||||
|
||||
void cbm_calls_push(CBMCallArray* arr, CBMArena* a, CBMCall call) {
|
||||
GROW_ARRAY(arr, a, CBMCall);
|
||||
arr->items[arr->count++] = call;
|
||||
}
|
||||
|
||||
void cbm_imports_push(CBMImportArray* arr, CBMArena* a, CBMImport imp) {
|
||||
GROW_ARRAY(arr, a, CBMImport);
|
||||
arr->items[arr->count++] = imp;
|
||||
}
|
||||
|
||||
void cbm_usages_push(CBMUsageArray* arr, CBMArena* a, CBMUsage usage) {
|
||||
GROW_ARRAY(arr, a, CBMUsage);
|
||||
arr->items[arr->count++] = usage;
|
||||
}
|
||||
|
||||
void cbm_throws_push(CBMThrowArray* arr, CBMArena* a, CBMThrow thr) {
|
||||
GROW_ARRAY(arr, a, CBMThrow);
|
||||
arr->items[arr->count++] = thr;
|
||||
}
|
||||
|
||||
void cbm_rw_push(CBMRWArray* arr, CBMArena* a, CBMReadWrite rw) {
|
||||
GROW_ARRAY(arr, a, CBMReadWrite);
|
||||
arr->items[arr->count++] = rw;
|
||||
}
|
||||
|
||||
void cbm_typerefs_push(CBMTypeRefArray* arr, CBMArena* a, CBMTypeRef tr) {
|
||||
GROW_ARRAY(arr, a, CBMTypeRef);
|
||||
arr->items[arr->count++] = tr;
|
||||
}
|
||||
|
||||
void cbm_envaccess_push(CBMEnvAccessArray* arr, CBMArena* a, CBMEnvAccess ea) {
|
||||
GROW_ARRAY(arr, a, CBMEnvAccess);
|
||||
arr->items[arr->count++] = ea;
|
||||
}
|
||||
|
||||
void cbm_typeassign_push(CBMTypeAssignArray* arr, CBMArena* a, CBMTypeAssign ta) {
|
||||
GROW_ARRAY(arr, a, CBMTypeAssign);
|
||||
arr->items[arr->count++] = ta;
|
||||
}
|
||||
|
||||
void cbm_impltrait_push(CBMImplTraitArray* arr, CBMArena* a, CBMImplTrait it) {
|
||||
GROW_ARRAY(arr, a, CBMImplTrait);
|
||||
arr->items[arr->count++] = it;
|
||||
}
|
||||
|
||||
// --- String input reader (for parse_with_options) ---
|
||||
|
||||
typedef struct {
|
||||
const char* string;
|
||||
uint32_t length;
|
||||
} CBMStringInput;
|
||||
|
||||
static const char* cbm_string_read(void* payload, uint32_t byte, TSPoint point, uint32_t* bytes_read) {
|
||||
(void)point;
|
||||
CBMStringInput* self = (CBMStringInput*)payload;
|
||||
if (byte >= self->length) {
|
||||
*bytes_read = 0;
|
||||
return "";
|
||||
}
|
||||
*bytes_read = self->length - byte;
|
||||
return self->string + byte;
|
||||
}
|
||||
|
||||
// --- Parse timeout callback ---
|
||||
|
||||
static bool cbm_timeout_cb(TSParseState* state) {
|
||||
uint64_t deadline = *(uint64_t*)state->payload;
|
||||
return now_ns() > deadline;
|
||||
}
|
||||
|
||||
// --- Thread-local parser pool ---
|
||||
// TSParser is not thread-safe, but can be reused across files on the same thread.
|
||||
// We keep one parser per thread, and just switch language as needed.
|
||||
// This avoids ~70K ts_parser_new()/ts_parser_delete() cycles on large repos.
|
||||
|
||||
static _Thread_local TSParser* tl_parser = NULL;
|
||||
static _Thread_local CBMLanguage tl_parser_lang = CBM_LANG_COUNT; // invalid sentinel
|
||||
|
||||
// Get or create a thread-local parser configured for the given language.
|
||||
static TSParser* get_thread_parser(const TSLanguage* ts_lang, CBMLanguage lang) {
|
||||
if (!tl_parser) {
|
||||
tl_parser = ts_parser_new();
|
||||
if (!tl_parser) return NULL;
|
||||
tl_parser_lang = CBM_LANG_COUNT;
|
||||
}
|
||||
if (tl_parser_lang != lang) {
|
||||
ts_parser_set_language(tl_parser, ts_lang);
|
||||
tl_parser_lang = lang;
|
||||
}
|
||||
return tl_parser;
|
||||
}
|
||||
|
||||
// --- Init/Shutdown ---
|
||||
|
||||
static int cbm_initialized = 0;
|
||||
|
||||
int cbm_init(void) {
|
||||
if (cbm_initialized) return 0;
|
||||
cbm_initialized = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cbm_shutdown(void) {
|
||||
// Clean up thread-local parser for the calling thread.
|
||||
// Note: other threads' TLS parsers are freed when those threads exit.
|
||||
if (tl_parser) {
|
||||
ts_parser_delete(tl_parser);
|
||||
tl_parser = NULL;
|
||||
tl_parser_lang = CBM_LANG_COUNT;
|
||||
}
|
||||
cbm_initialized = 0;
|
||||
}
|
||||
|
||||
// --- Main extraction function ---
|
||||
|
||||
CBMFileResult* cbm_extract_file(
|
||||
const char* source, int source_len,
|
||||
CBMLanguage language,
|
||||
const char* project, const char* rel_path,
|
||||
int64_t timeout_micros
|
||||
) {
|
||||
// Allocate result on heap (arena inside for all string data)
|
||||
CBMFileResult* result = (CBMFileResult*)calloc(1, sizeof(CBMFileResult));
|
||||
if (!result) return NULL;
|
||||
|
||||
cbm_arena_init(&result->arena);
|
||||
CBMArena* a = &result->arena;
|
||||
|
||||
// Get language spec
|
||||
const CBMLangSpec* spec = cbm_lang_spec(language);
|
||||
if (!spec) {
|
||||
result->has_error = true;
|
||||
result->error_msg = cbm_arena_strdup(a, "unsupported language");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get tree-sitter language
|
||||
const TSLanguage* ts_lang = cbm_ts_language(language);
|
||||
if (!ts_lang) {
|
||||
result->has_error = true;
|
||||
result->error_msg = cbm_arena_strdup(a, "no tree-sitter grammar");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get thread-local parser (reused across files on same thread)
|
||||
TSParser* parser = get_thread_parser(ts_lang, language);
|
||||
if (!parser) {
|
||||
result->has_error = true;
|
||||
result->error_msg = cbm_arena_strdup(a, "parser alloc failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Reset parser state from any previous parse (cancellation flags etc.)
|
||||
ts_parser_reset(parser);
|
||||
|
||||
uint64_t t0 = now_ns();
|
||||
|
||||
// Build string input + timeout options for parse_with_options
|
||||
CBMStringInput str_input = {source, (uint32_t)source_len};
|
||||
TSInput ts_input = {
|
||||
&str_input,
|
||||
cbm_string_read,
|
||||
TSInputEncodingUTF8,
|
||||
NULL,
|
||||
};
|
||||
|
||||
uint64_t deadline_ns = 0;
|
||||
TSParseOptions opts = {0};
|
||||
if (timeout_micros > 0) {
|
||||
deadline_ns = t0 + (uint64_t)timeout_micros * 1000ULL;
|
||||
opts.payload = &deadline_ns;
|
||||
opts.progress_callback = cbm_timeout_cb;
|
||||
}
|
||||
|
||||
TSTree* tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts);
|
||||
uint64_t t1 = now_ns();
|
||||
|
||||
if (!tree) {
|
||||
result->has_error = true;
|
||||
result->error_msg = cbm_arena_strdup(a, timeout_micros > 0 ? "parse timeout" : "parse failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
TSNode root = ts_tree_root_node(tree);
|
||||
|
||||
// Compute module QN
|
||||
result->module_qn = cbm_fqn_module(a, project, rel_path);
|
||||
result->is_test_file = cbm_is_test_file(rel_path, language);
|
||||
|
||||
// Build extraction context
|
||||
CBMExtractCtx ctx = {
|
||||
.arena = a,
|
||||
.result = result,
|
||||
.source = source,
|
||||
.source_len = source_len,
|
||||
.language = language,
|
||||
.project = project,
|
||||
.rel_path = rel_path,
|
||||
.module_qn = result->module_qn,
|
||||
.root = root,
|
||||
};
|
||||
|
||||
// Run extractors: defs + imports use separate walks (unique recursion patterns),
|
||||
// then a single unified cursor walk handles the remaining 7 extractors.
|
||||
cbm_extract_definitions(&ctx);
|
||||
cbm_extract_imports(&ctx);
|
||||
cbm_extract_unified(&ctx);
|
||||
|
||||
uint64_t t2 = now_ns();
|
||||
|
||||
result->imports_count = result->imports.count;
|
||||
|
||||
// Accumulate profiling counters
|
||||
atomic_fetch_add(&total_parse_ns, t1 - t0);
|
||||
atomic_fetch_add(&total_extract_ns, t2 - t1);
|
||||
atomic_fetch_add(&total_files, 1);
|
||||
|
||||
ts_tree_delete(tree);
|
||||
return result;
|
||||
}
|
||||
|
||||
void cbm_free_result(CBMFileResult* result) {
|
||||
if (!result) return;
|
||||
cbm_arena_destroy(&result->arena);
|
||||
free(result);
|
||||
}
|
||||
@@ -0,0 +1,419 @@
|
||||
package cbm
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -std=c11 -O2 -Wall -Ivendored/ts_runtime/include
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"github.com/DeusData/codebase-memory-mcp/internal/lang"
|
||||
)
|
||||
|
||||
// FileResult holds the extraction results from one file.
|
||||
type FileResult struct {
|
||||
Definitions []Definition
|
||||
Calls []Call
|
||||
Imports []Import
|
||||
Usages []Usage
|
||||
Throws []Throw
|
||||
ReadWrites []ReadWrite
|
||||
TypeRefs []TypeRef
|
||||
EnvAccesses []EnvAccess
|
||||
TypeAssigns []TypeAssign
|
||||
ImplTraits []ImplTrait
|
||||
|
||||
ModuleQN string
|
||||
IsTestFile bool
|
||||
ImportCount int
|
||||
}
|
||||
|
||||
// ImplTrait represents a Rust `impl Trait for Struct` pair.
|
||||
type ImplTrait struct {
|
||||
TraitName string
|
||||
StructName string
|
||||
}
|
||||
|
||||
// Definition represents a function/class/variable/module extracted from source.
|
||||
type Definition struct {
|
||||
Name string
|
||||
QualifiedName string
|
||||
Label string // "Function", "Method", "Class", "Variable", "Module", "Interface", "Enum", "Type"
|
||||
FilePath string
|
||||
StartLine int
|
||||
EndLine int
|
||||
Signature string
|
||||
ReturnType string
|
||||
Receiver string
|
||||
Docstring string
|
||||
ParentClass string
|
||||
Decorators []string
|
||||
BaseClasses []string
|
||||
ParamTypes []string
|
||||
Complexity int
|
||||
Lines int
|
||||
IsExported bool
|
||||
IsAbstract bool
|
||||
IsTest bool
|
||||
IsEntryPoint bool
|
||||
}
|
||||
|
||||
// Call represents a raw call site (callee name + enclosing function).
|
||||
type Call struct {
|
||||
CalleeName string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
// Import represents a local name -> module path mapping.
|
||||
type Import struct {
|
||||
LocalName string
|
||||
ModulePath string
|
||||
}
|
||||
|
||||
// Usage represents a reference to an identifier (not a call or import).
|
||||
type Usage struct {
|
||||
RefName string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
// Throw represents a throw/raise statement.
|
||||
type Throw struct {
|
||||
ExceptionName string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
// ReadWrite represents a variable read or write.
|
||||
type ReadWrite struct {
|
||||
VarName string
|
||||
EnclosingFuncQN string
|
||||
IsWrite bool
|
||||
}
|
||||
|
||||
// TypeRef represents a type reference in a function signature or body.
|
||||
type TypeRef struct {
|
||||
TypeName string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
// EnvAccess represents an environment variable access.
|
||||
type EnvAccess struct {
|
||||
EnvKey string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
// TypeAssign represents a variable assignment where RHS is a constructor call.
|
||||
type TypeAssign struct {
|
||||
VarName string
|
||||
TypeName string
|
||||
EnclosingFuncQN string
|
||||
}
|
||||
|
||||
var initOnce sync.Once
|
||||
|
||||
// Init initializes the C library. Safe to call multiple times.
|
||||
func Init() {
|
||||
initOnce.Do(func() {
|
||||
C.cbm_init()
|
||||
})
|
||||
}
|
||||
|
||||
// Shutdown cleans up the C library.
|
||||
func Shutdown() {
|
||||
C.cbm_shutdown()
|
||||
}
|
||||
|
||||
// ProfileStats holds accumulated profiling data from C extraction.
|
||||
type ProfileStats struct {
|
||||
ParseNs uint64
|
||||
ExtractNs uint64
|
||||
Files uint64
|
||||
}
|
||||
|
||||
// GetProfile returns accumulated parse/extraction timing and resets counters.
|
||||
func GetProfile() ProfileStats {
|
||||
var p, e, f C.uint64_t
|
||||
C.cbm_get_profile(&p, &e, &f)
|
||||
C.cbm_reset_profile()
|
||||
return ProfileStats{ParseNs: uint64(p), ExtractNs: uint64(e), Files: uint64(f)}
|
||||
}
|
||||
|
||||
// languageToC maps Go lang.Language to CBMLanguage enum.
|
||||
var languageToC = map[lang.Language]C.CBMLanguage{
|
||||
lang.Go: C.CBM_LANG_GO,
|
||||
lang.Python: C.CBM_LANG_PYTHON,
|
||||
lang.JavaScript: C.CBM_LANG_JAVASCRIPT,
|
||||
lang.TypeScript: C.CBM_LANG_TYPESCRIPT,
|
||||
lang.TSX: C.CBM_LANG_TSX,
|
||||
lang.Rust: C.CBM_LANG_RUST,
|
||||
lang.Java: C.CBM_LANG_JAVA,
|
||||
lang.CPP: C.CBM_LANG_CPP,
|
||||
lang.CSharp: C.CBM_LANG_CSHARP,
|
||||
lang.PHP: C.CBM_LANG_PHP,
|
||||
lang.Lua: C.CBM_LANG_LUA,
|
||||
lang.Scala: C.CBM_LANG_SCALA,
|
||||
lang.Kotlin: C.CBM_LANG_KOTLIN,
|
||||
lang.Ruby: C.CBM_LANG_RUBY,
|
||||
lang.C: C.CBM_LANG_C,
|
||||
lang.Bash: C.CBM_LANG_BASH,
|
||||
lang.Zig: C.CBM_LANG_ZIG,
|
||||
lang.Elixir: C.CBM_LANG_ELIXIR,
|
||||
lang.Haskell: C.CBM_LANG_HASKELL,
|
||||
lang.OCaml: C.CBM_LANG_OCAML,
|
||||
lang.ObjectiveC: C.CBM_LANG_OBJC,
|
||||
lang.Swift: C.CBM_LANG_SWIFT,
|
||||
lang.Dart: C.CBM_LANG_DART,
|
||||
lang.Perl: C.CBM_LANG_PERL,
|
||||
lang.Groovy: C.CBM_LANG_GROOVY,
|
||||
lang.Erlang: C.CBM_LANG_ERLANG,
|
||||
lang.R: C.CBM_LANG_R,
|
||||
lang.HTML: C.CBM_LANG_HTML,
|
||||
lang.CSS: C.CBM_LANG_CSS,
|
||||
lang.SCSS: C.CBM_LANG_SCSS,
|
||||
lang.YAML: C.CBM_LANG_YAML,
|
||||
lang.TOML: C.CBM_LANG_TOML,
|
||||
lang.HCL: C.CBM_LANG_HCL,
|
||||
lang.SQL: C.CBM_LANG_SQL,
|
||||
lang.Dockerfile: C.CBM_LANG_DOCKERFILE,
|
||||
// New languages (v0.5 expansion)
|
||||
lang.Clojure: C.CBM_LANG_CLOJURE,
|
||||
lang.FSharp: C.CBM_LANG_FSHARP,
|
||||
lang.Julia: C.CBM_LANG_JULIA,
|
||||
lang.VimScript: C.CBM_LANG_VIMSCRIPT,
|
||||
lang.Nix: C.CBM_LANG_NIX,
|
||||
lang.CommonLisp: C.CBM_LANG_COMMONLISP,
|
||||
lang.Elm: C.CBM_LANG_ELM,
|
||||
lang.Fortran: C.CBM_LANG_FORTRAN,
|
||||
lang.CUDA: C.CBM_LANG_CUDA,
|
||||
lang.COBOL: C.CBM_LANG_COBOL,
|
||||
lang.Verilog: C.CBM_LANG_VERILOG,
|
||||
lang.EmacsLisp: C.CBM_LANG_EMACSLISP,
|
||||
lang.JSON: C.CBM_LANG_JSON,
|
||||
lang.XML: C.CBM_LANG_XML,
|
||||
lang.Markdown: C.CBM_LANG_MARKDOWN,
|
||||
lang.Makefile: C.CBM_LANG_MAKEFILE,
|
||||
lang.CMake: C.CBM_LANG_CMAKE,
|
||||
lang.Protobuf: C.CBM_LANG_PROTOBUF,
|
||||
lang.GraphQL: C.CBM_LANG_GRAPHQL,
|
||||
lang.Vue: C.CBM_LANG_VUE,
|
||||
lang.Svelte: C.CBM_LANG_SVELTE,
|
||||
lang.Meson: C.CBM_LANG_MESON,
|
||||
lang.GLSL: C.CBM_LANG_GLSL,
|
||||
lang.INI: C.CBM_LANG_INI,
|
||||
}
|
||||
|
||||
// ParseTimeoutMicros is the default per-file parse timeout (10 seconds).
|
||||
const ParseTimeoutMicros = 10_000_000
|
||||
|
||||
// ExtractFile runs the C extraction library on one file.
|
||||
// source is the file content, language is the programming language,
|
||||
// project and relPath are used for qualified name computation.
|
||||
func ExtractFile(source []byte, language lang.Language, project, relPath string) (*FileResult, error) {
|
||||
Init()
|
||||
|
||||
if len(source) == 0 {
|
||||
return &FileResult{}, nil
|
||||
}
|
||||
|
||||
cLang, ok := languageToC[language]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cbm: unsupported language %q", language)
|
||||
}
|
||||
|
||||
cSource := (*C.char)(unsafe.Pointer(&source[0]))
|
||||
cSourceLen := C.int(len(source))
|
||||
cProject := C.CString(project)
|
||||
cRelPath := C.CString(relPath)
|
||||
defer C.free(unsafe.Pointer(cProject))
|
||||
defer C.free(unsafe.Pointer(cRelPath))
|
||||
|
||||
result := C.cbm_extract_file(cSource, cSourceLen, cLang, cProject, cRelPath, C.int64_t(ParseTimeoutMicros))
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("cbm: extraction returned nil")
|
||||
}
|
||||
defer C.cbm_free_result(result)
|
||||
|
||||
if result.has_error {
|
||||
return nil, fmt.Errorf("cbm: %s", C.GoString(result.error_msg))
|
||||
}
|
||||
|
||||
return convertResult(result), nil
|
||||
}
|
||||
|
||||
func convertResult(r *C.CBMFileResult) *FileResult {
|
||||
fr := &FileResult{
|
||||
ModuleQN: C.GoString(r.module_qn),
|
||||
IsTestFile: bool(r.is_test_file),
|
||||
ImportCount: int(r.imports_count),
|
||||
}
|
||||
|
||||
// Definitions
|
||||
if r.defs.count > 0 {
|
||||
fr.Definitions = make([]Definition, r.defs.count)
|
||||
defs := unsafe.Slice(r.defs.items, r.defs.count)
|
||||
for i := range defs {
|
||||
d := &defs[i]
|
||||
fr.Definitions[i] = Definition{
|
||||
Name: C.GoString(d.name),
|
||||
QualifiedName: C.GoString(d.qualified_name),
|
||||
Label: C.GoString(d.label),
|
||||
FilePath: C.GoString(d.file_path),
|
||||
StartLine: int(d.start_line),
|
||||
EndLine: int(d.end_line),
|
||||
Signature: goStringOrEmpty(d.signature),
|
||||
ReturnType: goStringOrEmpty(d.return_type),
|
||||
Receiver: goStringOrEmpty(d.receiver),
|
||||
Docstring: goStringOrEmpty(d.docstring),
|
||||
ParentClass: goStringOrEmpty(d.parent_class),
|
||||
Decorators: goStringSlice(d.decorators),
|
||||
BaseClasses: goStringSlice(d.base_classes),
|
||||
ParamTypes: goStringSlice(d.param_types),
|
||||
Complexity: int(d.complexity),
|
||||
Lines: int(d.lines),
|
||||
IsExported: bool(d.is_exported),
|
||||
IsAbstract: bool(d.is_abstract),
|
||||
IsTest: bool(d.is_test),
|
||||
IsEntryPoint: bool(d.is_entry_point),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calls
|
||||
if r.calls.count > 0 {
|
||||
fr.Calls = make([]Call, r.calls.count)
|
||||
calls := unsafe.Slice(r.calls.items, r.calls.count)
|
||||
for i, c := range calls {
|
||||
fr.Calls[i] = Call{
|
||||
CalleeName: C.GoString(c.callee_name),
|
||||
EnclosingFuncQN: C.GoString(c.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Imports
|
||||
if r.imports.count > 0 {
|
||||
fr.Imports = make([]Import, r.imports.count)
|
||||
imports := unsafe.Slice(r.imports.items, r.imports.count)
|
||||
for i, imp := range imports {
|
||||
fr.Imports[i] = Import{
|
||||
LocalName: C.GoString(imp.local_name),
|
||||
ModulePath: C.GoString(imp.module_path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Usages
|
||||
if r.usages.count > 0 {
|
||||
fr.Usages = make([]Usage, r.usages.count)
|
||||
usages := unsafe.Slice(r.usages.items, r.usages.count)
|
||||
for i, u := range usages {
|
||||
fr.Usages[i] = Usage{
|
||||
RefName: C.GoString(u.ref_name),
|
||||
EnclosingFuncQN: C.GoString(u.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Throws
|
||||
if r.throws.count > 0 {
|
||||
fr.Throws = make([]Throw, r.throws.count)
|
||||
throws := unsafe.Slice(r.throws.items, r.throws.count)
|
||||
for i, t := range throws {
|
||||
fr.Throws[i] = Throw{
|
||||
ExceptionName: C.GoString(t.exception_name),
|
||||
EnclosingFuncQN: C.GoString(t.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadWrites
|
||||
if r.rw.count > 0 {
|
||||
fr.ReadWrites = make([]ReadWrite, r.rw.count)
|
||||
rws := unsafe.Slice(r.rw.items, r.rw.count)
|
||||
for i, rw := range rws {
|
||||
fr.ReadWrites[i] = ReadWrite{
|
||||
VarName: C.GoString(rw.var_name),
|
||||
EnclosingFuncQN: C.GoString(rw.enclosing_func_qn),
|
||||
IsWrite: bool(rw.is_write),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TypeRefs
|
||||
if r.type_refs.count > 0 {
|
||||
fr.TypeRefs = make([]TypeRef, r.type_refs.count)
|
||||
refs := unsafe.Slice(r.type_refs.items, r.type_refs.count)
|
||||
for i, tr := range refs {
|
||||
fr.TypeRefs[i] = TypeRef{
|
||||
TypeName: C.GoString(tr.type_name),
|
||||
EnclosingFuncQN: C.GoString(tr.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnvAccesses
|
||||
if r.env_accesses.count > 0 {
|
||||
fr.EnvAccesses = make([]EnvAccess, r.env_accesses.count)
|
||||
envs := unsafe.Slice(r.env_accesses.items, r.env_accesses.count)
|
||||
for i, ea := range envs {
|
||||
fr.EnvAccesses[i] = EnvAccess{
|
||||
EnvKey: C.GoString(ea.env_key),
|
||||
EnclosingFuncQN: C.GoString(ea.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TypeAssigns
|
||||
if r.type_assigns.count > 0 {
|
||||
fr.TypeAssigns = make([]TypeAssign, r.type_assigns.count)
|
||||
assigns := unsafe.Slice(r.type_assigns.items, r.type_assigns.count)
|
||||
for i, ta := range assigns {
|
||||
fr.TypeAssigns[i] = TypeAssign{
|
||||
VarName: C.GoString(ta.var_name),
|
||||
TypeName: C.GoString(ta.type_name),
|
||||
EnclosingFuncQN: C.GoString(ta.enclosing_func_qn),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ImplTraits (Rust)
|
||||
if r.impl_traits.count > 0 {
|
||||
fr.ImplTraits = make([]ImplTrait, r.impl_traits.count)
|
||||
traits := unsafe.Slice(r.impl_traits.items, r.impl_traits.count)
|
||||
for i, it := range traits {
|
||||
fr.ImplTraits[i] = ImplTrait{
|
||||
TraitName: C.GoString(it.trait_name),
|
||||
StructName: C.GoString(it.struct_name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fr
|
||||
}
|
||||
|
||||
func goStringOrEmpty(s *C.char) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return C.GoString(s)
|
||||
}
|
||||
|
||||
func goStringSlice(arr **C.char) []string {
|
||||
if arr == nil {
|
||||
return nil
|
||||
}
|
||||
var result []string
|
||||
for i := 0; ; i++ {
|
||||
ptr := *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + uintptr(i)*unsafe.Sizeof(arr)))
|
||||
if ptr == nil {
|
||||
break
|
||||
}
|
||||
result = append(result, C.GoString(ptr))
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
#ifndef CBM_H
|
||||
#define CBM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "arena.h"
|
||||
#include "tree_sitter/api.h"
|
||||
|
||||
// Language enum mirrors lang.Language in Go.
|
||||
// Order must match lang_specs.c tables.
|
||||
typedef enum {
|
||||
CBM_LANG_GO = 0,
|
||||
CBM_LANG_PYTHON,
|
||||
CBM_LANG_JAVASCRIPT,
|
||||
CBM_LANG_TYPESCRIPT,
|
||||
CBM_LANG_TSX,
|
||||
CBM_LANG_RUST,
|
||||
CBM_LANG_JAVA,
|
||||
CBM_LANG_CPP,
|
||||
CBM_LANG_CSHARP,
|
||||
CBM_LANG_PHP,
|
||||
CBM_LANG_LUA,
|
||||
CBM_LANG_SCALA,
|
||||
CBM_LANG_KOTLIN,
|
||||
CBM_LANG_RUBY,
|
||||
CBM_LANG_C,
|
||||
CBM_LANG_BASH,
|
||||
CBM_LANG_ZIG,
|
||||
CBM_LANG_ELIXIR,
|
||||
CBM_LANG_HASKELL,
|
||||
CBM_LANG_OCAML,
|
||||
CBM_LANG_OBJC,
|
||||
CBM_LANG_SWIFT,
|
||||
CBM_LANG_DART,
|
||||
CBM_LANG_PERL,
|
||||
CBM_LANG_GROOVY,
|
||||
CBM_LANG_ERLANG,
|
||||
CBM_LANG_R,
|
||||
CBM_LANG_HTML,
|
||||
CBM_LANG_CSS,
|
||||
CBM_LANG_SCSS,
|
||||
CBM_LANG_YAML,
|
||||
CBM_LANG_TOML,
|
||||
CBM_LANG_HCL,
|
||||
CBM_LANG_SQL,
|
||||
CBM_LANG_DOCKERFILE,
|
||||
// New languages (v0.5 expansion)
|
||||
CBM_LANG_CLOJURE,
|
||||
CBM_LANG_FSHARP,
|
||||
CBM_LANG_JULIA,
|
||||
CBM_LANG_VIMSCRIPT,
|
||||
CBM_LANG_NIX,
|
||||
CBM_LANG_COMMONLISP,
|
||||
CBM_LANG_ELM,
|
||||
CBM_LANG_FORTRAN,
|
||||
CBM_LANG_CUDA,
|
||||
CBM_LANG_COBOL,
|
||||
CBM_LANG_VERILOG,
|
||||
CBM_LANG_EMACSLISP,
|
||||
CBM_LANG_JSON,
|
||||
CBM_LANG_XML,
|
||||
CBM_LANG_MARKDOWN,
|
||||
CBM_LANG_MAKEFILE,
|
||||
CBM_LANG_CMAKE,
|
||||
CBM_LANG_PROTOBUF,
|
||||
CBM_LANG_GRAPHQL,
|
||||
CBM_LANG_VUE,
|
||||
CBM_LANG_SVELTE,
|
||||
CBM_LANG_MESON,
|
||||
CBM_LANG_GLSL,
|
||||
CBM_LANG_INI,
|
||||
CBM_LANG_COUNT
|
||||
} CBMLanguage;
|
||||
|
||||
// --- Extraction result structs ---
|
||||
|
||||
typedef struct {
|
||||
const char* name; // short name
|
||||
const char* qualified_name; // project.path.name
|
||||
const char* label; // "Function", "Method", "Class", "Variable", "Module"
|
||||
const char* file_path; // relative path
|
||||
uint32_t start_line;
|
||||
uint32_t end_line;
|
||||
const char* signature; // parameter text (NULL if none)
|
||||
const char* return_type; // return type text (NULL if none)
|
||||
const char* receiver; // Go method receiver (NULL if none)
|
||||
const char* docstring; // leading doc comment (NULL if none)
|
||||
const char* parent_class; // enclosing class QN for methods (NULL if none)
|
||||
const char** decorators; // NULL-terminated array (NULL if none)
|
||||
const char** base_classes; // NULL-terminated array (NULL if none)
|
||||
const char** param_types; // NULL-terminated array (NULL if none)
|
||||
const char** return_types; // NULL-terminated array (NULL if none)
|
||||
int complexity; // cyclomatic complexity
|
||||
int lines; // body line count
|
||||
bool is_exported;
|
||||
bool is_abstract;
|
||||
bool is_test;
|
||||
bool is_entry_point;
|
||||
} CBMDefinition;
|
||||
|
||||
typedef struct {
|
||||
const char* callee_name; // raw callee text ("pkg.Func", "foo")
|
||||
const char* enclosing_func_qn; // QN of enclosing function (or module QN)
|
||||
} CBMCall;
|
||||
|
||||
typedef struct {
|
||||
const char* local_name; // local alias or name
|
||||
const char* module_path; // resolved module path / QN
|
||||
} CBMImport;
|
||||
|
||||
typedef struct {
|
||||
const char* ref_name; // referenced identifier
|
||||
const char* enclosing_func_qn; // QN of enclosing function (or module QN)
|
||||
} CBMUsage;
|
||||
|
||||
typedef struct {
|
||||
const char* exception_name; // exception class/type name
|
||||
const char* enclosing_func_qn; // QN of enclosing function
|
||||
} CBMThrow;
|
||||
|
||||
typedef struct {
|
||||
const char* var_name; // variable name
|
||||
const char* enclosing_func_qn; // QN of enclosing function
|
||||
bool is_write; // true = write, false = read
|
||||
} CBMReadWrite;
|
||||
|
||||
typedef struct {
|
||||
const char* type_name; // referenced type/class name
|
||||
const char* enclosing_func_qn; // QN of enclosing function
|
||||
} CBMTypeRef;
|
||||
|
||||
typedef struct {
|
||||
const char* env_key; // environment variable key
|
||||
const char* enclosing_func_qn; // QN of enclosing function
|
||||
} CBMEnvAccess;
|
||||
|
||||
typedef struct {
|
||||
const char* var_name; // variable being assigned
|
||||
const char* type_name; // class/type name of RHS constructor
|
||||
const char* enclosing_func_qn; // QN of enclosing function
|
||||
} CBMTypeAssign;
|
||||
|
||||
// Rust: impl Trait for Struct
|
||||
typedef struct {
|
||||
const char* trait_name; // trait name (raw text)
|
||||
const char* struct_name; // struct/type name (raw text)
|
||||
} CBMImplTrait;
|
||||
|
||||
// Growable arrays used during extraction.
|
||||
typedef struct {
|
||||
CBMDefinition* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMDefArray;
|
||||
|
||||
typedef struct {
|
||||
CBMCall* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMCallArray;
|
||||
|
||||
typedef struct {
|
||||
CBMImport* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMImportArray;
|
||||
|
||||
typedef struct {
|
||||
CBMUsage* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMUsageArray;
|
||||
|
||||
typedef struct {
|
||||
CBMThrow* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMThrowArray;
|
||||
|
||||
typedef struct {
|
||||
CBMReadWrite* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMRWArray;
|
||||
|
||||
typedef struct {
|
||||
CBMTypeRef* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMTypeRefArray;
|
||||
|
||||
typedef struct {
|
||||
CBMEnvAccess* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMEnvAccessArray;
|
||||
|
||||
typedef struct {
|
||||
CBMTypeAssign* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMTypeAssignArray;
|
||||
|
||||
typedef struct {
|
||||
CBMImplTrait* items;
|
||||
int count;
|
||||
int cap;
|
||||
} CBMImplTraitArray;
|
||||
|
||||
// Full extraction result for one file.
|
||||
typedef struct {
|
||||
CBMArena arena; // owns all string memory
|
||||
|
||||
CBMDefArray defs;
|
||||
CBMCallArray calls;
|
||||
CBMImportArray imports;
|
||||
CBMUsageArray usages;
|
||||
CBMThrowArray throws;
|
||||
CBMRWArray rw;
|
||||
CBMTypeRefArray type_refs;
|
||||
CBMEnvAccessArray env_accesses;
|
||||
CBMTypeAssignArray type_assigns;
|
||||
CBMImplTraitArray impl_traits; // Rust: impl Trait for Struct pairs
|
||||
|
||||
const char* module_qn; // module qualified name
|
||||
const char** exports; // NULL-terminated (NULL if none)
|
||||
const char** constants; // NULL-terminated (NULL if none)
|
||||
const char** global_vars; // NULL-terminated (NULL if none)
|
||||
const char** macros; // NULL-terminated, C/C++ only (NULL if none)
|
||||
|
||||
bool has_error;
|
||||
const char* error_msg;
|
||||
bool is_test_file;
|
||||
int imports_count;
|
||||
} CBMFileResult;
|
||||
|
||||
// --- Enclosing function cache ---
|
||||
// Avoids repeated parent-chain walks for nodes within the same function body.
|
||||
// Each entry records a function's byte range and its precomputed QN.
|
||||
#define EFC_SIZE 64 // power of 2 for fast modulo
|
||||
|
||||
typedef struct {
|
||||
uint32_t start_byte;
|
||||
uint32_t end_byte;
|
||||
const char* qn;
|
||||
} EFCEntry;
|
||||
|
||||
typedef struct {
|
||||
EFCEntry entries[EFC_SIZE];
|
||||
int count;
|
||||
} EFCache;
|
||||
|
||||
// --- Extraction context passed to sub-extractors ---
|
||||
|
||||
typedef struct {
|
||||
CBMArena* arena;
|
||||
CBMFileResult* result;
|
||||
const char* source;
|
||||
int source_len;
|
||||
CBMLanguage language;
|
||||
const char* project;
|
||||
const char* rel_path;
|
||||
const char* module_qn;
|
||||
TSNode root;
|
||||
EFCache ef_cache; // enclosing function cache
|
||||
} CBMExtractCtx;
|
||||
|
||||
// --- Public API ---
|
||||
|
||||
// Initialize the library. Call once at startup. Returns 0 on success.
|
||||
int cbm_init(void);
|
||||
|
||||
// Extract all data from one file. Caller must call cbm_free_result().
|
||||
// source must remain valid for the duration of the call.
|
||||
// timeout_micros: per-file parse timeout in microseconds (0 = no timeout).
|
||||
CBMFileResult* cbm_extract_file(
|
||||
const char* source, int source_len,
|
||||
CBMLanguage language,
|
||||
const char* project, const char* rel_path,
|
||||
int64_t timeout_micros
|
||||
);
|
||||
|
||||
// Free all memory associated with a result.
|
||||
void cbm_free_result(CBMFileResult* result);
|
||||
|
||||
// Shutdown the library. Call once at exit.
|
||||
void cbm_shutdown(void);
|
||||
|
||||
// Profiling: get accumulated parse/extraction times and file count.
|
||||
void cbm_get_profile(uint64_t* parse_ns, uint64_t* extract_ns, uint64_t* files);
|
||||
void cbm_reset_profile(void);
|
||||
|
||||
// --- Internal helpers used by extractors ---
|
||||
|
||||
// Growable array push functions (arena-allocated, no individual free needed).
|
||||
void cbm_defs_push(CBMDefArray* arr, CBMArena* a, CBMDefinition def);
|
||||
void cbm_calls_push(CBMCallArray* arr, CBMArena* a, CBMCall call);
|
||||
void cbm_imports_push(CBMImportArray* arr, CBMArena* a, CBMImport imp);
|
||||
void cbm_usages_push(CBMUsageArray* arr, CBMArena* a, CBMUsage usage);
|
||||
void cbm_throws_push(CBMThrowArray* arr, CBMArena* a, CBMThrow thr);
|
||||
void cbm_rw_push(CBMRWArray* arr, CBMArena* a, CBMReadWrite rw);
|
||||
void cbm_typerefs_push(CBMTypeRefArray* arr, CBMArena* a, CBMTypeRef tr);
|
||||
void cbm_envaccess_push(CBMEnvAccessArray* arr, CBMArena* a, CBMEnvAccess ea);
|
||||
void cbm_typeassign_push(CBMTypeAssignArray* arr, CBMArena* a, CBMTypeAssign ta);
|
||||
void cbm_impltrait_push(CBMImplTraitArray* arr, CBMArena* a, CBMImplTrait it);
|
||||
|
||||
// --- Sub-extractor entry points ---
|
||||
|
||||
void cbm_extract_definitions(CBMExtractCtx* ctx);
|
||||
void cbm_extract_calls(CBMExtractCtx* ctx);
|
||||
void cbm_extract_imports(CBMExtractCtx* ctx);
|
||||
void cbm_extract_usages(CBMExtractCtx* ctx);
|
||||
void cbm_extract_semantic(CBMExtractCtx* ctx);
|
||||
void cbm_extract_type_refs(CBMExtractCtx* ctx);
|
||||
void cbm_extract_env_accesses(CBMExtractCtx* ctx);
|
||||
void cbm_extract_type_assigns(CBMExtractCtx* ctx);
|
||||
|
||||
// Single-pass unified extraction (replaces the 7 calls above except defs+imports).
|
||||
void cbm_extract_unified(CBMExtractCtx* ctx);
|
||||
|
||||
#endif // CBM_H
|
||||
@@ -0,0 +1,78 @@
|
||||
package cbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/DeusData/codebase-memory-mcp/internal/lang"
|
||||
)
|
||||
|
||||
func TestPythonDocstring(t *testing.T) {
|
||||
source := []byte("def compute(x, y):\n \"\"\"Compute the sum of x and y.\"\"\"\n return x + y\n")
|
||||
result, err := ExtractFile(source, lang.Python, "test", "test.py")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Defs: %d\n", len(result.Definitions))
|
||||
for _, d := range result.Definitions {
|
||||
fmt.Printf(" Name=%q Label=%q QN=%q Doc=%q Sig=%q\n", d.Name, d.Label, d.QualifiedName, d.Docstring, d.Signature)
|
||||
}
|
||||
if len(result.Definitions) == 0 {
|
||||
t.Fatal("no definitions extracted")
|
||||
}
|
||||
found := false
|
||||
for _, d := range result.Definitions {
|
||||
if d.Name == "compute" {
|
||||
found = true
|
||||
if d.Docstring == "" {
|
||||
t.Error("docstring is empty for compute")
|
||||
}
|
||||
t.Logf("docstring: %q", d.Docstring)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("compute function not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoFunctionExtraction(t *testing.T) {
|
||||
source := []byte(`package main
|
||||
|
||||
// Greet returns a greeting.
|
||||
func Greet(name string) string {
|
||||
return "Hello, " + name
|
||||
}
|
||||
|
||||
func main() {
|
||||
Greet("world")
|
||||
}
|
||||
`)
|
||||
result, err := ExtractFile(source, lang.Go, "test", "main.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Defs: %d, Calls: %d, Imports: %d\n", len(result.Definitions), len(result.Calls), len(result.Imports))
|
||||
for _, d := range result.Definitions {
|
||||
fmt.Printf(" Name=%q Label=%q QN=%q Sig=%q Doc=%q\n", d.Name, d.Label, d.QualifiedName, d.Signature, d.Docstring)
|
||||
}
|
||||
for _, c := range result.Calls {
|
||||
fmt.Printf(" Call: callee=%q enclosing=%q\n", c.CalleeName, c.EnclosingFuncQN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSArrowFunction(t *testing.T) {
|
||||
source := []byte(`const greet = (name) => {
|
||||
return "Hello " + name;
|
||||
};
|
||||
|
||||
const result = greet("world");
|
||||
`)
|
||||
result, err := ExtractFile(source, lang.JavaScript, "test", "app.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Defs: %d\n", len(result.Definitions))
|
||||
for _, d := range result.Definitions {
|
||||
fmt.Printf(" Name=%q Label=%q QN=%q\n", d.Name, d.Label, d.QualifiedName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Forward declarations
|
||||
static void walk_calls(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec);
|
||||
static char* extract_callee_name(CBMArena* a, TSNode node, const char* source, CBMLanguage lang);
|
||||
static void extract_jsx_refs(CBMExtractCtx* ctx, TSNode node);
|
||||
|
||||
// Extract callee name from a call node
|
||||
static char* extract_callee_name(CBMArena* a, TSNode node, const char* source, CBMLanguage lang) {
|
||||
// Try "function" field (most languages: call_expression, etc.)
|
||||
TSNode func_node = ts_node_child_by_field_name(node, "function", 8);
|
||||
if (!ts_node_is_null(func_node)) {
|
||||
const char* fk = ts_node_type(func_node);
|
||||
if (strcmp(fk, "identifier") == 0 ||
|
||||
strcmp(fk, "simple_identifier") == 0 ||
|
||||
strcmp(fk, "selector_expression") == 0 ||
|
||||
strcmp(fk, "attribute") == 0 ||
|
||||
strcmp(fk, "member_expression") == 0 ||
|
||||
strcmp(fk, "field_expression") == 0 ||
|
||||
strcmp(fk, "dot") == 0 ||
|
||||
strcmp(fk, "function") == 0 ||
|
||||
strcmp(fk, "dotted_identifier") == 0 ||
|
||||
strcmp(fk, "member_access_expression") == 0 ||
|
||||
strcmp(fk, "scoped_identifier") == 0 ||
|
||||
strcmp(fk, "qualified_identifier") == 0) {
|
||||
return cbm_node_text(a, func_node, source);
|
||||
}
|
||||
}
|
||||
|
||||
// Try "name" field (Java method_invocation)
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
char* name = cbm_node_text(a, name_node, source);
|
||||
// For Java: prepend object if present
|
||||
TSNode obj = ts_node_child_by_field_name(node, "object", 6);
|
||||
if (!ts_node_is_null(obj) && name) {
|
||||
char* obj_text = cbm_node_text(a, obj, source);
|
||||
if (obj_text && obj_text[0]) {
|
||||
return cbm_arena_sprintf(a, "%s.%s", obj_text, name);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// Ruby: "method" + "receiver" fields
|
||||
TSNode method_node = ts_node_child_by_field_name(node, "method", 6);
|
||||
if (!ts_node_is_null(method_node)) {
|
||||
char* method = cbm_node_text(a, method_node, source);
|
||||
TSNode recv = ts_node_child_by_field_name(node, "receiver", 8);
|
||||
if (!ts_node_is_null(recv) && method) {
|
||||
char* recv_text = cbm_node_text(a, recv, source);
|
||||
if (recv_text && recv_text[0]) {
|
||||
return cbm_arena_sprintf(a, "%s.%s", recv_text, method);
|
||||
}
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
// ObjC message_expression: [receiver message]
|
||||
if (lang == CBM_LANG_OBJC && strcmp(ts_node_type(node), "message_expression") == 0) {
|
||||
TSNode selector = ts_node_child_by_field_name(node, "selector", 8);
|
||||
if (!ts_node_is_null(selector)) {
|
||||
return cbm_node_text(a, selector, source);
|
||||
}
|
||||
}
|
||||
|
||||
// Erlang: call -> first child is module:function or just function
|
||||
if (lang == CBM_LANG_ERLANG && strcmp(ts_node_type(node), "call") == 0) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
return cbm_node_text(a, ts_node_child(node, 0), source);
|
||||
}
|
||||
}
|
||||
|
||||
// Haskell/OCaml: application_expression, infix, apply
|
||||
if (lang == CBM_LANG_HASKELL || lang == CBM_LANG_OCAML) {
|
||||
const char* nk = ts_node_type(node);
|
||||
if (strcmp(nk, "apply") == 0 || strcmp(nk, "application_expression") == 0) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
TSNode callee = ts_node_child(node, 0);
|
||||
if (strcmp(ts_node_type(callee), "identifier") == 0 ||
|
||||
strcmp(ts_node_type(callee), "variable") == 0 ||
|
||||
strcmp(ts_node_type(callee), "constructor") == 0 ||
|
||||
strcmp(ts_node_type(callee), "value_path") == 0) {
|
||||
return cbm_node_text(a, callee, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strcmp(nk, "infix") == 0 || strcmp(nk, "infix_expression") == 0) {
|
||||
TSNode op = ts_node_child_by_field_name(node, "operator", 8);
|
||||
if (!ts_node_is_null(op)) {
|
||||
return cbm_node_text(a, op, source);
|
||||
}
|
||||
// Fallback: second child is usually the operator
|
||||
if (ts_node_child_count(node) >= 3) {
|
||||
return cbm_node_text(a, ts_node_child(node, 1), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Elixir: first child of call is the function name
|
||||
if (lang == CBM_LANG_ELIXIR && strcmp(ts_node_type(node), "call") == 0) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
TSNode first = ts_node_child(node, 0);
|
||||
const char* fk = ts_node_type(first);
|
||||
if (strcmp(fk, "identifier") == 0 || strcmp(fk, "dot") == 0) {
|
||||
return cbm_node_text(a, first, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Perl: various call expression types
|
||||
if (lang == CBM_LANG_PERL) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
return cbm_node_text(a, ts_node_child(node, 0), source);
|
||||
}
|
||||
}
|
||||
|
||||
// PHP: function_call_expression, member_call_expression, etc.
|
||||
if (lang == CBM_LANG_PHP) {
|
||||
func_node = ts_node_child_by_field_name(node, "function", 8);
|
||||
if (ts_node_is_null(func_node)) {
|
||||
func_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
}
|
||||
if (!ts_node_is_null(func_node)) {
|
||||
return cbm_node_text(a, func_node, source);
|
||||
}
|
||||
}
|
||||
|
||||
// Kotlin: navigation_expression, call_expression
|
||||
if (lang == CBM_LANG_KOTLIN) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
return cbm_node_text(a, ts_node_child(node, 0), source);
|
||||
}
|
||||
}
|
||||
|
||||
// Generic fallback: first child
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
TSNode first = ts_node_child(node, 0);
|
||||
if (strcmp(ts_node_type(first), "identifier") == 0) {
|
||||
return cbm_node_text(a, first, source);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Walk AST for call nodes
|
||||
static void walk_calls(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (cbm_kind_in_set(node, spec->call_node_types)) {
|
||||
char* callee = extract_callee_name(ctx->arena, node, ctx->source, ctx->language);
|
||||
if (callee && callee[0]) {
|
||||
// Skip keywords
|
||||
if (!cbm_is_keyword(callee, ctx->language)) {
|
||||
CBMCall call;
|
||||
call.callee_name = callee;
|
||||
call.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
|
||||
}
|
||||
}
|
||||
// Don't recurse into call arguments for nested calls — the walk handles that
|
||||
}
|
||||
|
||||
// JSX component refs (TSX/JSX)
|
||||
if (ctx->language == CBM_LANG_TSX || ctx->language == CBM_LANG_JAVASCRIPT) {
|
||||
if (strcmp(kind, "jsx_self_closing_element") == 0 ||
|
||||
strcmp(kind, "jsx_opening_element") == 0) {
|
||||
extract_jsx_refs(ctx, node);
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_calls(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract JSX component references (uppercase = component, lowercase = HTML)
|
||||
static void extract_jsx_refs(CBMExtractCtx* ctx, TSNode node) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(name_node)) return;
|
||||
|
||||
char* name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (!name || !name[0]) return;
|
||||
|
||||
// Only uppercase names are components
|
||||
if (name[0] < 'A' || name[0] > 'Z') return;
|
||||
|
||||
CBMCall call;
|
||||
call.callee_name = name;
|
||||
call.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
|
||||
}
|
||||
|
||||
void cbm_extract_calls(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec || !spec->call_node_types || !spec->call_node_types[0]) return;
|
||||
|
||||
walk_calls(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// --- Unified handler: called once per node by the cursor walk ---
|
||||
|
||||
void handle_calls(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
if (!spec->call_node_types || !spec->call_node_types[0]) return;
|
||||
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (cbm_kind_in_set(node, spec->call_node_types)) {
|
||||
char* callee = extract_callee_name(ctx->arena, node, ctx->source, ctx->language);
|
||||
if (callee && callee[0] && !cbm_is_keyword(callee, ctx->language)) {
|
||||
CBMCall call;
|
||||
call.callee_name = callee;
|
||||
call.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
|
||||
}
|
||||
}
|
||||
|
||||
// JSX component refs (TSX/JSX)
|
||||
if (ctx->language == CBM_LANG_TSX || ctx->language == CBM_LANG_JAVASCRIPT) {
|
||||
if (strcmp(kind, "jsx_self_closing_element") == 0 ||
|
||||
strcmp(kind, "jsx_opening_element") == 0) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
char* name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (name && name[0] >= 'A' && name[0] <= 'Z') {
|
||||
CBMCall call;
|
||||
call.callee_name = name;
|
||||
call.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,165 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Unquote a string literal: "foo" -> foo, 'foo' -> foo
|
||||
static const char* unquote(CBMArena* a, const char* s) {
|
||||
if (!s || !s[0]) return NULL;
|
||||
// Trim whitespace
|
||||
while (*s == ' ' || *s == '\t') s++;
|
||||
size_t len = strlen(s);
|
||||
if (len >= 2) {
|
||||
char first = s[0], last = s[len-1];
|
||||
if ((first == '"' && last == '"') || (first == '\'' && last == '\'') ||
|
||||
(first == '`' && last == '`')) {
|
||||
return cbm_arena_strndup(a, s + 1, len - 2);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// Extract env key from a function call like os.Getenv("KEY").
|
||||
static const char* extract_env_key_from_call(CBMExtractCtx* ctx, TSNode node,
|
||||
const CBMLangSpec* spec) {
|
||||
if (!spec->env_access_functions || !spec->env_access_functions[0]) return NULL;
|
||||
|
||||
TSNode func_node = ts_node_child_by_field_name(node, "function", 8);
|
||||
if (ts_node_is_null(func_node)) return NULL;
|
||||
char* callee = cbm_node_text(ctx->arena, func_node, ctx->source);
|
||||
if (!callee) return NULL;
|
||||
|
||||
// Check if callee matches any env access function
|
||||
bool match = false;
|
||||
for (const char** ef = spec->env_access_functions; *ef; ef++) {
|
||||
if (strcmp(callee, *ef) == 0) { match = true; break; }
|
||||
}
|
||||
if (!match) return NULL;
|
||||
|
||||
// Get first argument (the env key)
|
||||
TSNode args = ts_node_child_by_field_name(node, "arguments", 9);
|
||||
if (ts_node_is_null(args)) return NULL;
|
||||
// Find first named child (skip parentheses)
|
||||
for (uint32_t i = 0; i < ts_node_child_count(args); i++) {
|
||||
TSNode child = ts_node_child(args, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "(") == 0 || strcmp(ck, ")") == 0 || strcmp(ck, ",") == 0) continue;
|
||||
char* arg_text = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
return unquote(ctx->arena, arg_text);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Extract env key from member access like process.env.KEY or os.environ["KEY"].
|
||||
static const char* extract_env_key_from_member(CBMExtractCtx* ctx, TSNode node,
|
||||
const CBMLangSpec* spec) {
|
||||
if (!spec->env_access_member_patterns || !spec->env_access_member_patterns[0]) return NULL;
|
||||
|
||||
char* text = cbm_node_text(ctx->arena, node, ctx->source);
|
||||
if (!text || !text[0]) return NULL;
|
||||
|
||||
for (const char** pat = spec->env_access_member_patterns; *pat; pat++) {
|
||||
size_t plen = strlen(*pat);
|
||||
|
||||
// Dot access: pattern.KEY
|
||||
if (strncmp(text, *pat, plen) == 0 && text[plen] == '.') {
|
||||
const char* key = text + plen + 1;
|
||||
// Validate: no further dots/brackets
|
||||
if (key[0] && !strchr(key, '.') && !strchr(key, '[')) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
// Subscript: pattern["KEY"]
|
||||
if (strncmp(text, *pat, plen) == 0 && text[plen] == '[') {
|
||||
const char* inner = text + plen + 1;
|
||||
size_t ilen = strlen(inner);
|
||||
if (ilen > 0 && inner[ilen-1] == ']') {
|
||||
char* bracket_content = cbm_arena_strndup(ctx->arena, inner, ilen - 1);
|
||||
return unquote(ctx->arena, bracket_content);
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check if an env key name looks like an environment variable (uppercase + underscores).
|
||||
static bool is_env_var_name(const char* s) {
|
||||
if (!s || strlen(s) < 2) return false;
|
||||
bool has_upper = false;
|
||||
for (const char* p = s; *p; p++) {
|
||||
if (*p >= 'A' && *p <= 'Z') has_upper = true;
|
||||
else if (*p == '_' || (*p >= '0' && *p <= '9')) { /* ok */ }
|
||||
else return false;
|
||||
}
|
||||
return has_upper;
|
||||
}
|
||||
|
||||
static void walk_env_accesses(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
const char* kind = ts_node_type(node);
|
||||
const char* env_key = NULL;
|
||||
|
||||
// Check call nodes
|
||||
if (cbm_kind_in_set(node, spec->call_node_types)) {
|
||||
env_key = extract_env_key_from_call(ctx, node, spec);
|
||||
}
|
||||
// Check member/subscript access
|
||||
else if (strcmp(kind, "member_expression") == 0 || strcmp(kind, "subscript") == 0 ||
|
||||
strcmp(kind, "attribute") == 0) {
|
||||
env_key = extract_env_key_from_member(ctx, node, spec);
|
||||
}
|
||||
|
||||
if (env_key && env_key[0] && is_env_var_name(env_key)) {
|
||||
CBMEnvAccess ea;
|
||||
ea.env_key = env_key;
|
||||
ea.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_envaccess_push(&ctx->result->env_accesses, ctx->arena, ea);
|
||||
// Don't recurse into this node's children (avoid double-counting)
|
||||
return;
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_env_accesses(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
void cbm_extract_env_accesses(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
bool has_funcs = spec->env_access_functions && spec->env_access_functions[0];
|
||||
bool has_members = spec->env_access_member_patterns && spec->env_access_member_patterns[0];
|
||||
if (!has_funcs && !has_members) return;
|
||||
|
||||
walk_env_accesses(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// --- Unified handler ---
|
||||
|
||||
void handle_env_accesses(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
bool has_funcs = spec->env_access_functions && spec->env_access_functions[0];
|
||||
bool has_members = spec->env_access_member_patterns && spec->env_access_member_patterns[0];
|
||||
if (!has_funcs && !has_members) return;
|
||||
|
||||
const char* kind = ts_node_type(node);
|
||||
const char* env_key = NULL;
|
||||
|
||||
if (has_funcs && spec->call_node_types && cbm_kind_in_set(node, spec->call_node_types)) {
|
||||
env_key = extract_env_key_from_call(ctx, node, spec);
|
||||
} else if (has_members &&
|
||||
(strcmp(kind, "member_expression") == 0 || strcmp(kind, "subscript") == 0 ||
|
||||
strcmp(kind, "attribute") == 0)) {
|
||||
env_key = extract_env_key_from_member(ctx, node, spec);
|
||||
}
|
||||
|
||||
if (env_key && env_key[0] && is_env_var_name(env_key)) {
|
||||
CBMEnvAccess ea;
|
||||
ea.env_key = env_key;
|
||||
ea.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_envaccess_push(&ctx->result->env_accesses, ctx->arena, ea);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,615 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Forward declarations
|
||||
static void parse_go_imports(CBMExtractCtx* ctx);
|
||||
static void parse_python_imports(CBMExtractCtx* ctx);
|
||||
static void parse_es_imports(CBMExtractCtx* ctx);
|
||||
static void parse_java_imports(CBMExtractCtx* ctx);
|
||||
static void parse_rust_imports(CBMExtractCtx* ctx);
|
||||
static void parse_c_imports(CBMExtractCtx* ctx);
|
||||
static void parse_ruby_imports(CBMExtractCtx* ctx);
|
||||
static void parse_lua_imports(CBMExtractCtx* ctx);
|
||||
static void parse_generic_imports(CBMExtractCtx* ctx, const char* node_type);
|
||||
|
||||
// Helper: strip quotes from a string literal
|
||||
static char* strip_quotes(CBMArena* a, const char* s) {
|
||||
if (!s) return NULL;
|
||||
size_t len = strlen(s);
|
||||
if (len >= 2 && (s[0] == '"' || s[0] == '\'') && s[len-1] == s[0]) {
|
||||
return cbm_arena_strndup(a, s + 1, len - 2);
|
||||
}
|
||||
return cbm_arena_strdup(a, s);
|
||||
}
|
||||
|
||||
// Helper: get last path component as local name
|
||||
static const char* path_last(CBMArena* a, const char* path) {
|
||||
if (!path) return NULL;
|
||||
const char* last = strrchr(path, '/');
|
||||
if (last) return cbm_arena_strdup(a, last + 1);
|
||||
last = strrchr(path, '.');
|
||||
if (last) return cbm_arena_strdup(a, last + 1);
|
||||
return path;
|
||||
}
|
||||
|
||||
// --- Go imports ---
|
||||
// import_declaration -> import_spec_list -> import_spec -> (name, path)
|
||||
|
||||
static void parse_go_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t root_count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < root_count; i++) {
|
||||
TSNode decl = ts_node_child(ctx->root, i);
|
||||
if (strcmp(ts_node_type(decl), "import_declaration") != 0) continue;
|
||||
|
||||
uint32_t dc = ts_node_child_count(decl);
|
||||
for (uint32_t j = 0; j < dc; j++) {
|
||||
TSNode child = ts_node_child(decl, j);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "import_spec") != 0 && strcmp(ck, "import_spec_list") != 0) continue;
|
||||
|
||||
if (strcmp(ck, "import_spec") == 0) {
|
||||
TSNode path_node = ts_node_child_by_field_name(child, "path", 4);
|
||||
if (ts_node_is_null(path_node)) continue;
|
||||
char* path = strip_quotes(a, cbm_node_text(a, path_node, ctx->source));
|
||||
if (!path || !path[0]) continue;
|
||||
|
||||
TSNode name_node = ts_node_child_by_field_name(child, "name", 4);
|
||||
const char* local_name;
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
local_name = cbm_node_text(a, name_node, ctx->source);
|
||||
} else {
|
||||
local_name = path_last(a, path);
|
||||
}
|
||||
|
||||
CBMImport imp = {.local_name = local_name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
} else {
|
||||
// import_spec_list: contains multiple import_spec children
|
||||
uint32_t sc = ts_node_child_count(child);
|
||||
for (uint32_t k = 0; k < sc; k++) {
|
||||
TSNode spec = ts_node_child(child, k);
|
||||
if (strcmp(ts_node_type(spec), "import_spec") != 0) continue;
|
||||
|
||||
TSNode path_node = ts_node_child_by_field_name(spec, "path", 4);
|
||||
if (ts_node_is_null(path_node)) continue;
|
||||
char* path = strip_quotes(a, cbm_node_text(a, path_node, ctx->source));
|
||||
if (!path || !path[0]) continue;
|
||||
|
||||
TSNode name_node = ts_node_child_by_field_name(spec, "name", 4);
|
||||
const char* local_name;
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
local_name = cbm_node_text(a, name_node, ctx->source);
|
||||
} else {
|
||||
local_name = path_last(a, path);
|
||||
}
|
||||
|
||||
CBMImport imp = {.local_name = local_name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Python imports ---
|
||||
// import_statement: import X, import X as Y
|
||||
// import_from_statement: from X import Y, from X import Y as Z
|
||||
|
||||
static void parse_python_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (strcmp(kind, "import_statement") == 0) {
|
||||
// import X [as Y]
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(name_node)) {
|
||||
// Try children: dotted_name nodes
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode child = ts_node_child(node, j);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "dotted_name") == 0 || strcmp(ck, "identifier") == 0) {
|
||||
char* mod = cbm_node_text(a, child, ctx->source);
|
||||
if (mod && mod[0]) {
|
||||
const char* local = path_last(a, mod);
|
||||
CBMImport imp = {.local_name = local, .module_path = mod};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
} else if (strcmp(ck, "aliased_import") == 0) {
|
||||
TSNode mod_node = ts_node_child_by_field_name(child, "name", 4);
|
||||
TSNode alias_node = ts_node_child_by_field_name(child, "alias", 5);
|
||||
if (!ts_node_is_null(mod_node)) {
|
||||
char* mod = cbm_node_text(a, mod_node, ctx->source);
|
||||
const char* local = !ts_node_is_null(alias_node) ?
|
||||
cbm_node_text(a, alias_node, ctx->source) : path_last(a, mod);
|
||||
CBMImport imp = {.local_name = local, .module_path = mod};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
char* mod = cbm_node_text(a, name_node, ctx->source);
|
||||
if (mod && mod[0]) {
|
||||
CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(kind, "import_from_statement") == 0) {
|
||||
// from X import Y [as Z]
|
||||
TSNode module_node = ts_node_child_by_field_name(node, "module_name", 11);
|
||||
if (ts_node_is_null(module_node)) {
|
||||
// Try alternative field names
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode c = ts_node_child(node, j);
|
||||
if (strcmp(ts_node_type(c), "dotted_name") == 0 ||
|
||||
strcmp(ts_node_type(c), "relative_import") == 0) {
|
||||
module_node = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
char* mod_path = ts_node_is_null(module_node) ? NULL :
|
||||
cbm_node_text(a, module_node, ctx->source);
|
||||
|
||||
// Find imported names
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode child = ts_node_child(node, j);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "identifier") == 0 || strcmp(ck, "dotted_name") == 0) {
|
||||
// Skip the module name node
|
||||
if (!ts_node_is_null(module_node) &&
|
||||
ts_node_start_byte(child) == ts_node_start_byte(module_node)) continue;
|
||||
char* name = cbm_node_text(a, child, ctx->source);
|
||||
if (name && name[0]) {
|
||||
const char* full = mod_path ?
|
||||
cbm_arena_sprintf(a, "%s.%s", mod_path, name) : name;
|
||||
CBMImport imp = {.local_name = name, .module_path = full};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
} else if (strcmp(ck, "aliased_import") == 0) {
|
||||
TSNode name_n = ts_node_child_by_field_name(child, "name", 4);
|
||||
TSNode alias_n = ts_node_child_by_field_name(child, "alias", 5);
|
||||
if (!ts_node_is_null(name_n)) {
|
||||
char* name = cbm_node_text(a, name_n, ctx->source);
|
||||
const char* local = !ts_node_is_null(alias_n) ?
|
||||
cbm_node_text(a, alias_n, ctx->source) : name;
|
||||
const char* full = mod_path ?
|
||||
cbm_arena_sprintf(a, "%s.%s", mod_path, name) : name;
|
||||
CBMImport imp = {.local_name = local, .module_path = full};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- ES module imports (JS/TS/TSX) ---
|
||||
// import X from "Y"; import {A, B} from "Y"; import * as X from "Y"
|
||||
// const X = require("Y")
|
||||
|
||||
static void walk_es_imports(CBMExtractCtx* ctx, TSNode node) {
|
||||
CBMArena* a = ctx->arena;
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (strcmp(kind, "import_statement") == 0) {
|
||||
TSNode source_node = ts_node_child_by_field_name(node, "source", 6);
|
||||
if (ts_node_is_null(source_node)) {
|
||||
// Try last string child
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (int j = (int)nc - 1; j >= 0; j--) {
|
||||
TSNode c = ts_node_child(node, (uint32_t)j);
|
||||
const char* ck = ts_node_type(c);
|
||||
if (strcmp(ck, "string") == 0 || strcmp(ck, "string_literal") == 0) {
|
||||
source_node = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ts_node_is_null(source_node)) goto recurse;
|
||||
|
||||
char* path = strip_quotes(a, cbm_node_text(a, source_node, ctx->source));
|
||||
if (!path || !path[0]) goto recurse;
|
||||
|
||||
// Default import, namespace import, named imports
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
bool found = false;
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode child = ts_node_child(node, j);
|
||||
const char* ck = ts_node_type(child);
|
||||
|
||||
if (strcmp(ck, "identifier") == 0) {
|
||||
// Default import: import X from "Y"
|
||||
char* name = cbm_node_text(a, child, ctx->source);
|
||||
CBMImport imp = {.local_name = name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
found = true;
|
||||
} else if (strcmp(ck, "import_clause") == 0) {
|
||||
// Walk into import clause for named/default imports
|
||||
uint32_t cc = ts_node_child_count(child);
|
||||
for (uint32_t k = 0; k < cc; k++) {
|
||||
TSNode sub = ts_node_child(child, k);
|
||||
const char* sk = ts_node_type(sub);
|
||||
if (strcmp(sk, "identifier") == 0) {
|
||||
char* name = cbm_node_text(a, sub, ctx->source);
|
||||
CBMImport imp = {.local_name = name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
found = true;
|
||||
} else if (strcmp(sk, "namespace_import") == 0) {
|
||||
TSNode as_name = ts_node_child_by_field_name(sub, "name", 4);
|
||||
if (ts_node_is_null(as_name) && ts_node_child_count(sub) > 0) {
|
||||
as_name = ts_node_child(sub, ts_node_child_count(sub) - 1);
|
||||
}
|
||||
if (!ts_node_is_null(as_name)) {
|
||||
char* name = cbm_node_text(a, as_name, ctx->source);
|
||||
CBMImport imp = {.local_name = name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
found = true;
|
||||
}
|
||||
} else if (strcmp(sk, "named_imports") == 0) {
|
||||
uint32_t nc2 = ts_node_child_count(sub);
|
||||
for (uint32_t m = 0; m < nc2; m++) {
|
||||
TSNode imp_spec = ts_node_child(sub, m);
|
||||
if (strcmp(ts_node_type(imp_spec), "import_specifier") == 0) {
|
||||
TSNode local = ts_node_child_by_field_name(imp_spec, "alias", 5);
|
||||
TSNode orig = ts_node_child_by_field_name(imp_spec, "name", 4);
|
||||
if (ts_node_is_null(orig) && ts_node_child_count(imp_spec) > 0) {
|
||||
orig = ts_node_child(imp_spec, 0);
|
||||
}
|
||||
if (!ts_node_is_null(orig)) {
|
||||
char* local_name = !ts_node_is_null(local) ?
|
||||
cbm_node_text(a, local, ctx->source) :
|
||||
cbm_node_text(a, orig, ctx->source);
|
||||
CBMImport imp = {.local_name = local_name, .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
// Side-effect import: import "Y"
|
||||
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
recurse:
|
||||
;
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_es_imports(ctx, ts_node_child(node, i));
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_es_imports(CBMExtractCtx* ctx) {
|
||||
walk_es_imports(ctx, ctx->root);
|
||||
}
|
||||
|
||||
// --- Java imports ---
|
||||
// import_declaration -> scoped_identifier
|
||||
|
||||
static void parse_java_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
if (strcmp(ts_node_type(node), "import_declaration") != 0) continue;
|
||||
|
||||
// Get the full import path (skip "import" and "static" keywords)
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode child = ts_node_child(node, j);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "scoped_identifier") == 0 || strcmp(ck, "identifier") == 0) {
|
||||
char* path = cbm_node_text(a, child, ctx->source);
|
||||
if (path && path[0]) {
|
||||
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Rust imports ---
|
||||
// use_declaration -> use_list or scoped_use_list
|
||||
|
||||
static void parse_rust_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
if (strcmp(ts_node_type(node), "use_declaration") != 0) continue;
|
||||
|
||||
char* full = cbm_node_text(a, node, ctx->source);
|
||||
if (!full) continue;
|
||||
// Strip "use " prefix and trailing ";"
|
||||
if (strncmp(full, "use ", 4) == 0) full += 4;
|
||||
size_t len = strlen(full);
|
||||
if (len > 0 && full[len-1] == ';') full[len-1] = '\0';
|
||||
|
||||
CBMImport imp = {.local_name = path_last(a, full), .module_path = full};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
|
||||
// --- C/C++ imports ---
|
||||
// preproc_include -> path or string_literal
|
||||
|
||||
static void parse_c_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
const char* kind = ts_node_type(node);
|
||||
if (strcmp(kind, "preproc_include") != 0 && strcmp(kind, "preproc_import") != 0) continue;
|
||||
|
||||
TSNode path_node = ts_node_child_by_field_name(node, "path", 4);
|
||||
if (ts_node_is_null(path_node)) {
|
||||
// Try system_lib_string or string_literal
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t j = 0; j < nc; j++) {
|
||||
TSNode c = ts_node_child(node, j);
|
||||
const char* ck = ts_node_type(c);
|
||||
if (strcmp(ck, "string_literal") == 0 || strcmp(ck, "system_lib_string") == 0) {
|
||||
path_node = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ts_node_is_null(path_node)) continue;
|
||||
|
||||
char* path = strip_quotes(a, cbm_node_text(a, path_node, ctx->source));
|
||||
// Also strip angle brackets
|
||||
if (path && path[0] == '<') {
|
||||
size_t len = strlen(path);
|
||||
if (len > 1 && path[len-1] == '>') {
|
||||
path = cbm_arena_strndup(a, path + 1, len - 2);
|
||||
}
|
||||
}
|
||||
if (!path || !path[0]) continue;
|
||||
|
||||
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Ruby imports ---
|
||||
// call nodes: require("X"), require_relative("X")
|
||||
|
||||
static void parse_ruby_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
// Walk for call nodes with "require" or "require_relative"
|
||||
// Simple: walk top-level children
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
const char* kind = ts_node_type(node);
|
||||
if (strcmp(kind, "call") != 0 && strcmp(kind, "command_call") != 0) continue;
|
||||
|
||||
TSNode method = ts_node_child_by_field_name(node, "method", 6);
|
||||
if (ts_node_is_null(method) && ts_node_child_count(node) > 0) {
|
||||
method = ts_node_child(node, 0);
|
||||
}
|
||||
if (ts_node_is_null(method)) continue;
|
||||
|
||||
char* name = cbm_node_text(a, method, ctx->source);
|
||||
if (!name || (strcmp(name, "require") != 0 && strcmp(name, "require_relative") != 0)) continue;
|
||||
|
||||
// Get the argument (string literal)
|
||||
TSNode args = ts_node_child_by_field_name(node, "arguments", 9);
|
||||
if (ts_node_is_null(args)) {
|
||||
// Try second child
|
||||
if (ts_node_child_count(node) > 1) args = ts_node_child(node, 1);
|
||||
}
|
||||
if (ts_node_is_null(args)) continue;
|
||||
|
||||
// Find string literal in args
|
||||
char* arg_text = NULL;
|
||||
uint32_t ac = ts_node_child_count(args);
|
||||
for (uint32_t j = 0; j < ac; j++) {
|
||||
TSNode c = ts_node_child(args, j);
|
||||
const char* ck = ts_node_type(c);
|
||||
if (strcmp(ck, "string") == 0 || strcmp(ck, "string_literal") == 0) {
|
||||
arg_text = strip_quotes(a, cbm_node_text(a, c, ctx->source));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!arg_text) {
|
||||
arg_text = strip_quotes(a, cbm_node_text(a, args, ctx->source));
|
||||
}
|
||||
if (!arg_text || !arg_text[0]) continue;
|
||||
|
||||
CBMImport imp = {.local_name = path_last(a, arg_text), .module_path = arg_text};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Lua imports ---
|
||||
// function_call: require("X")
|
||||
|
||||
static void parse_lua_imports(CBMExtractCtx* ctx) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
// Lua: local X = require("Y") → assignment_statement or variable_declaration
|
||||
// containing function_call(require, "Y")
|
||||
char* text = cbm_node_text(a, node, ctx->source);
|
||||
if (!text) continue;
|
||||
if (strstr(text, "require") == NULL) continue;
|
||||
|
||||
// Simple extraction: find require("...") pattern in node text
|
||||
const char* req = strstr(text, "require");
|
||||
if (!req) continue;
|
||||
|
||||
// Find the string argument
|
||||
const char* open = strchr(req, '(');
|
||||
if (!open) open = strchr(req, '"');
|
||||
if (!open) open = strchr(req, '\'');
|
||||
if (!open) continue;
|
||||
|
||||
const char* q1 = strchr(open, '"');
|
||||
const char* q2 = strchr(open, '\'');
|
||||
if (!q1 && !q2) continue;
|
||||
const char* start = q1 && (!q2 || q1 < q2) ? q1 : q2;
|
||||
char delim = *start;
|
||||
start++;
|
||||
const char* end = strchr(start, delim);
|
||||
if (!end) continue;
|
||||
|
||||
char* mod = cbm_arena_strndup(a, start, (size_t)(end - start));
|
||||
CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Generic import parsing for languages with simple import_declaration ---
|
||||
|
||||
static void parse_generic_imports(CBMExtractCtx* ctx, const char* node_type) {
|
||||
CBMArena* a = ctx->arena;
|
||||
|
||||
uint32_t count = ts_node_child_count(ctx->root);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode node = ts_node_child(ctx->root, i);
|
||||
if (strcmp(ts_node_type(node), node_type) != 0) continue;
|
||||
|
||||
// Try to find a path/source/module field
|
||||
static const char* path_fields[] = {"path","source","module","name",NULL};
|
||||
for (const char** f = path_fields; *f; f++) {
|
||||
TSNode path_node = ts_node_child_by_field_name(node, *f, (uint32_t)strlen(*f));
|
||||
if (!ts_node_is_null(path_node)) {
|
||||
char* path = strip_quotes(a, cbm_node_text(a, path_node, ctx->source));
|
||||
if (path && path[0]) {
|
||||
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: use full node text minus keyword
|
||||
if (ctx->result->imports.count == 0) {
|
||||
char* text = cbm_node_text(a, node, ctx->source);
|
||||
if (text && text[0]) {
|
||||
// Strip leading keyword
|
||||
char* space = strchr(text, ' ');
|
||||
if (space) text = space + 1;
|
||||
// Strip trailing semicolon
|
||||
size_t len = strlen(text);
|
||||
if (len > 0 && text[len-1] == ';') text[len-1] = '\0';
|
||||
if (text[0]) {
|
||||
CBMImport imp = {.local_name = path_last(a, text), .module_path = text};
|
||||
cbm_imports_push(&ctx->result->imports, a, imp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Main dispatch ---
|
||||
|
||||
void cbm_extract_imports(CBMExtractCtx* ctx) {
|
||||
switch (ctx->language) {
|
||||
case CBM_LANG_GO:
|
||||
parse_go_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_PYTHON:
|
||||
parse_python_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_JAVASCRIPT:
|
||||
case CBM_LANG_TYPESCRIPT:
|
||||
case CBM_LANG_TSX:
|
||||
parse_es_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_JAVA:
|
||||
parse_java_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_KOTLIN:
|
||||
parse_generic_imports(ctx, "import");
|
||||
break;
|
||||
case CBM_LANG_SCALA:
|
||||
parse_generic_imports(ctx, "import_declaration");
|
||||
break;
|
||||
case CBM_LANG_CSHARP:
|
||||
parse_generic_imports(ctx, "using_directive");
|
||||
break;
|
||||
case CBM_LANG_RUST:
|
||||
parse_rust_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_C:
|
||||
case CBM_LANG_CPP:
|
||||
case CBM_LANG_OBJC:
|
||||
parse_c_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_PHP:
|
||||
// PHP uses require/include calls, similar to Ruby
|
||||
parse_generic_imports(ctx, "expression_statement");
|
||||
break;
|
||||
case CBM_LANG_RUBY:
|
||||
parse_ruby_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_LUA:
|
||||
parse_lua_imports(ctx);
|
||||
break;
|
||||
case CBM_LANG_ELIXIR:
|
||||
// Elixir: import/use/alias/require are call nodes
|
||||
parse_generic_imports(ctx, "call");
|
||||
break;
|
||||
case CBM_LANG_BASH:
|
||||
// source/. commands
|
||||
parse_generic_imports(ctx, "command");
|
||||
break;
|
||||
case CBM_LANG_ZIG:
|
||||
parse_generic_imports(ctx, "builtin_function");
|
||||
break;
|
||||
case CBM_LANG_ERLANG:
|
||||
parse_generic_imports(ctx, "module_attribute");
|
||||
break;
|
||||
case CBM_LANG_HASKELL:
|
||||
parse_generic_imports(ctx, "import");
|
||||
break;
|
||||
case CBM_LANG_OCAML:
|
||||
parse_generic_imports(ctx, "open_module");
|
||||
break;
|
||||
case CBM_LANG_CSS:
|
||||
case CBM_LANG_SCSS:
|
||||
parse_generic_imports(ctx, "import_statement");
|
||||
break;
|
||||
case CBM_LANG_PERL:
|
||||
parse_generic_imports(ctx, "use_statement");
|
||||
break;
|
||||
case CBM_LANG_GROOVY:
|
||||
parse_generic_imports(ctx, "groovy_import");
|
||||
break;
|
||||
case CBM_LANG_SWIFT:
|
||||
case CBM_LANG_DART:
|
||||
parse_generic_imports(ctx, "import_declaration");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// --- Throw/Raise extraction ---
|
||||
|
||||
static void walk_throws(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (cbm_kind_in_set(node, spec->throw_node_types)) {
|
||||
// Extract the exception name
|
||||
char* exc_name = NULL;
|
||||
|
||||
// Python raise: raise_statement -> first child is the exception
|
||||
// Java throw: throw_statement -> first expression child
|
||||
// JS throw: throw_statement -> first expression child
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
// Skip keywords
|
||||
if (strcmp(ck, "raise") == 0 || strcmp(ck, "throw") == 0) continue;
|
||||
// Skip semicolons and other punctuation
|
||||
if (ck[0] == ';' || ck[0] == '(' || ck[0] == ')') continue;
|
||||
|
||||
if (strcmp(ck, "identifier") == 0 || strcmp(ck, "type_identifier") == 0) {
|
||||
exc_name = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
} else if (strcmp(ck, "call") == 0 || strcmp(ck, "call_expression") == 0 ||
|
||||
strcmp(ck, "new_expression") == 0 || strcmp(ck, "object_creation_expression") == 0 ||
|
||||
strcmp(ck, "instance_expression") == 0) {
|
||||
// Call/new: get the function/type name
|
||||
TSNode fn = ts_node_child_by_field_name(child, "function", 8);
|
||||
if (ts_node_is_null(fn)) fn = ts_node_child_by_field_name(child, "constructor", 11);
|
||||
if (ts_node_is_null(fn)) fn = ts_node_child_by_field_name(child, "type", 4);
|
||||
// Fallback: first named child (skips 'new' keyword)
|
||||
if (ts_node_is_null(fn) && ts_node_named_child_count(child) > 0) {
|
||||
fn = ts_node_named_child(child, 0);
|
||||
}
|
||||
if (!ts_node_is_null(fn)) {
|
||||
exc_name = cbm_node_text(ctx->arena, fn, ctx->source);
|
||||
}
|
||||
} else {
|
||||
// Fallback: use full expression text
|
||||
exc_name = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (exc_name && exc_name[0]) {
|
||||
// Truncate long exception names
|
||||
if (strlen(exc_name) > 100) exc_name[100] = '\0';
|
||||
|
||||
CBMThrow thr;
|
||||
thr.exception_name = exc_name;
|
||||
thr.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_throws_push(&ctx->result->throws, ctx->arena, thr);
|
||||
}
|
||||
}
|
||||
|
||||
// Java: check for throws clause on function declarations
|
||||
if (spec->throws_clause_field && spec->throws_clause_field[0]) {
|
||||
if (strcmp(kind, "method_declaration") == 0 || strcmp(kind, "constructor_declaration") == 0) {
|
||||
TSNode throws_clause = ts_node_child_by_field_name(node, spec->throws_clause_field,
|
||||
(uint32_t)strlen(spec->throws_clause_field));
|
||||
if (!ts_node_is_null(throws_clause)) {
|
||||
// Extract each exception type from the throws clause
|
||||
uint32_t nc = ts_node_child_count(throws_clause);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(throws_clause, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "type_identifier") == 0 || strcmp(ck, "identifier") == 0 ||
|
||||
strcmp(ck, "scoped_type_identifier") == 0) {
|
||||
char* exc = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
if (exc && exc[0]) {
|
||||
const char* func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
CBMThrow thr = {.exception_name = exc, .enclosing_func_qn = func_qn};
|
||||
cbm_throws_push(&ctx->result->throws, ctx->arena, thr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_throws(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Read/Write detection ---
|
||||
|
||||
static void walk_readwrites(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
// Check if this is an assignment node
|
||||
if (cbm_kind_in_set(node, spec->assignment_node_types)) {
|
||||
// Left side is a write, right side contains reads
|
||||
TSNode left = ts_node_child_by_field_name(node, "left", 4);
|
||||
if (ts_node_is_null(left)) {
|
||||
// Try first child as left-hand side
|
||||
if (ts_node_child_count(node) > 0) left = ts_node_child(node, 0);
|
||||
}
|
||||
|
||||
if (!ts_node_is_null(left)) {
|
||||
const char* lk = ts_node_type(left);
|
||||
if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
|
||||
char* name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
|
||||
CBMReadWrite rw;
|
||||
rw.var_name = name;
|
||||
rw.is_write = true;
|
||||
rw.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_rw_push(&ctx->result->rw, ctx->arena, rw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_readwrites(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
void cbm_extract_semantic(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
// Throws
|
||||
if (spec->throw_node_types && spec->throw_node_types[0]) {
|
||||
walk_throws(ctx, ctx->root, spec);
|
||||
} else if (spec->throws_clause_field && spec->throws_clause_field[0]) {
|
||||
walk_throws(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// Reads/Writes
|
||||
if (spec->assignment_node_types && spec->assignment_node_types[0]) {
|
||||
walk_readwrites(ctx, ctx->root, spec);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Unified handlers ---
|
||||
|
||||
void handle_throws(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
bool has_throws = spec->throw_node_types && spec->throw_node_types[0];
|
||||
bool has_clause = spec->throws_clause_field && spec->throws_clause_field[0];
|
||||
if (!has_throws && !has_clause) return;
|
||||
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
if (has_throws && cbm_kind_in_set(node, spec->throw_node_types)) {
|
||||
char* exc_name = NULL;
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "raise") == 0 || strcmp(ck, "throw") == 0) continue;
|
||||
if (ck[0] == ';' || ck[0] == '(' || ck[0] == ')') continue;
|
||||
|
||||
if (strcmp(ck, "identifier") == 0 || strcmp(ck, "type_identifier") == 0) {
|
||||
exc_name = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
} else if (strcmp(ck, "call") == 0 || strcmp(ck, "call_expression") == 0 ||
|
||||
strcmp(ck, "new_expression") == 0 || strcmp(ck, "object_creation_expression") == 0 ||
|
||||
strcmp(ck, "instance_expression") == 0) {
|
||||
TSNode fn = ts_node_child_by_field_name(child, "function", 8);
|
||||
if (ts_node_is_null(fn)) fn = ts_node_child_by_field_name(child, "constructor", 11);
|
||||
if (ts_node_is_null(fn)) fn = ts_node_child_by_field_name(child, "type", 4);
|
||||
if (ts_node_is_null(fn) && ts_node_named_child_count(child) > 0) {
|
||||
fn = ts_node_named_child(child, 0);
|
||||
}
|
||||
if (!ts_node_is_null(fn)) {
|
||||
exc_name = cbm_node_text(ctx->arena, fn, ctx->source);
|
||||
}
|
||||
} else {
|
||||
exc_name = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (exc_name && exc_name[0]) {
|
||||
if (strlen(exc_name) > 100) exc_name[100] = '\0';
|
||||
CBMThrow thr;
|
||||
thr.exception_name = exc_name;
|
||||
thr.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_throws_push(&ctx->result->throws, ctx->arena, thr);
|
||||
}
|
||||
}
|
||||
|
||||
// Java: throws clause on method/constructor declarations
|
||||
if (has_clause) {
|
||||
if (strcmp(kind, "method_declaration") == 0 || strcmp(kind, "constructor_declaration") == 0) {
|
||||
TSNode throws_clause = ts_node_child_by_field_name(node, spec->throws_clause_field,
|
||||
(uint32_t)strlen(spec->throws_clause_field));
|
||||
if (!ts_node_is_null(throws_clause)) {
|
||||
uint32_t nc = ts_node_child_count(throws_clause);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(throws_clause, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "type_identifier") == 0 || strcmp(ck, "identifier") == 0 ||
|
||||
strcmp(ck, "scoped_type_identifier") == 0) {
|
||||
char* exc = cbm_node_text(ctx->arena, child, ctx->source);
|
||||
if (exc && exc[0]) {
|
||||
CBMThrow thr = {.exception_name = exc,
|
||||
.enclosing_func_qn = state->enclosing_func_qn};
|
||||
cbm_throws_push(&ctx->result->throws, ctx->arena, thr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handle_readwrites(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
if (!spec->assignment_node_types || !spec->assignment_node_types[0]) return;
|
||||
|
||||
if (cbm_kind_in_set(node, spec->assignment_node_types)) {
|
||||
TSNode left = ts_node_child_by_field_name(node, "left", 4);
|
||||
if (ts_node_is_null(left)) {
|
||||
if (ts_node_child_count(node) > 0) left = ts_node_child(node, 0);
|
||||
}
|
||||
|
||||
if (!ts_node_is_null(left)) {
|
||||
const char* lk = ts_node_type(left);
|
||||
if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
|
||||
char* name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
|
||||
CBMReadWrite rw;
|
||||
rw.var_name = name;
|
||||
rw.is_write = true;
|
||||
rw.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_rw_push(&ctx->result->rw, ctx->arena, rw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Extract class/type name from a constructor expression.
|
||||
// e.g., new Foo() -> "Foo", Foo() -> "Foo" (if uppercase), Foo{} -> "Foo"
|
||||
static const char* extract_constructor_type(CBMArena* a, TSNode rhs, const char* source, CBMLanguage lang) {
|
||||
const char* kind = ts_node_type(rhs);
|
||||
|
||||
// new_expression / object_creation_expression -> type field or first child
|
||||
if (strcmp(kind, "new_expression") == 0 || strcmp(kind, "object_creation_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(rhs, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
const char* tk = ts_node_type(type_node);
|
||||
if (strcmp(tk, "type_identifier") == 0 || strcmp(tk, "identifier") == 0 ||
|
||||
strcmp(tk, "simple_identifier") == 0) {
|
||||
return cbm_node_text(a, type_node, source);
|
||||
}
|
||||
// generic_type: get base
|
||||
if (strcmp(tk, "generic_type") == 0 && ts_node_child_count(type_node) > 0) {
|
||||
return cbm_node_text(a, ts_node_child(type_node, 0), source);
|
||||
}
|
||||
return cbm_node_text(a, type_node, source);
|
||||
}
|
||||
// Fallback: first named child
|
||||
for (uint32_t i = 0; i < ts_node_child_count(rhs); i++) {
|
||||
TSNode child = ts_node_child(rhs, i);
|
||||
const char* ck = ts_node_type(child);
|
||||
if (strcmp(ck, "identifier") == 0 || strcmp(ck, "type_identifier") == 0 ||
|
||||
strcmp(ck, "simple_identifier") == 0) {
|
||||
return cbm_node_text(a, child, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// call_expression where function is an uppercase identifier (Python/Kotlin/Scala class construction)
|
||||
if (strcmp(kind, "call") == 0 || strcmp(kind, "call_expression") == 0) {
|
||||
TSNode func = ts_node_child_by_field_name(rhs, "function", 8);
|
||||
if (ts_node_is_null(func) && ts_node_child_count(rhs) > 0) {
|
||||
func = ts_node_child(rhs, 0);
|
||||
}
|
||||
if (!ts_node_is_null(func)) {
|
||||
char* fname = cbm_node_text(a, func, source);
|
||||
if (fname && fname[0] >= 'A' && fname[0] <= 'Z') {
|
||||
return fname;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Go composite_literal: Type{...}
|
||||
if (strcmp(kind, "composite_literal") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(rhs, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
return cbm_node_text(a, type_node, source);
|
||||
}
|
||||
}
|
||||
|
||||
// Rust: Type::new() or Type { ... }
|
||||
if (lang == CBM_LANG_RUST) {
|
||||
if (strcmp(kind, "struct_expression") == 0) {
|
||||
TSNode name = ts_node_child_by_field_name(rhs, "name", 4);
|
||||
if (!ts_node_is_null(name)) return cbm_node_text(a, name, source);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Walk AST for assignment patterns where RHS is a constructor call.
|
||||
static void walk_type_assigns(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
// Assignment: var = Constructor()
|
||||
if (cbm_kind_in_set(node, spec->assignment_node_types)) {
|
||||
TSNode left = ts_node_child_by_field_name(node, "left", 4);
|
||||
TSNode right = ts_node_child_by_field_name(node, "right", 5);
|
||||
if (ts_node_is_null(right)) right = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(left) && !ts_node_is_null(right)) {
|
||||
const char* lk = ts_node_type(left);
|
||||
if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, right, ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Variable declarations with constructor RHS
|
||||
// Go: short_var_declaration, var_spec
|
||||
// JS/TS: variable_declarator (const x = new Foo())
|
||||
// Python: assignment already handled above
|
||||
// Rust: let_declaration
|
||||
if (strcmp(kind, "short_var_declaration") == 0 || strcmp(kind, "var_spec") == 0) {
|
||||
TSNode left = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(left)) left = ts_node_child_by_field_name(node, "left", 4);
|
||||
TSNode right = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (ts_node_is_null(right)) right = ts_node_child_by_field_name(node, "right", 5);
|
||||
if (!ts_node_is_null(left) && !ts_node_is_null(right)) {
|
||||
char* var_name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, right, ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(kind, "variable_declarator") == 0) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
TSNode value_node = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(name_node) && !ts_node_is_null(value_node)) {
|
||||
const char* nk = ts_node_type(name_node);
|
||||
if (strcmp(nk, "identifier") == 0 || strcmp(nk, "simple_identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, value_node, ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(kind, "let_declaration") == 0 && ctx->language == CBM_LANG_RUST) {
|
||||
TSNode pat = ts_node_child_by_field_name(node, "pattern", 7);
|
||||
TSNode val = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(pat) && !ts_node_is_null(val)) {
|
||||
if (strcmp(ts_node_type(pat), "identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, pat, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, val, ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_type_assigns(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
void cbm_extract_type_assigns(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
walk_type_assigns(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// --- Unified handler ---
|
||||
|
||||
void handle_type_assigns(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
// Assignment: var = Constructor()
|
||||
if (spec->assignment_node_types && cbm_kind_in_set(node, spec->assignment_node_types)) {
|
||||
TSNode left = ts_node_child_by_field_name(node, "left", 4);
|
||||
TSNode right = ts_node_child_by_field_name(node, "right", 5);
|
||||
if (ts_node_is_null(right)) right = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(left) && !ts_node_is_null(right)) {
|
||||
const char* lk = ts_node_type(left);
|
||||
if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, right,
|
||||
ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Go: short_var_declaration, var_spec
|
||||
if (strcmp(kind, "short_var_declaration") == 0 || strcmp(kind, "var_spec") == 0) {
|
||||
TSNode left = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(left)) left = ts_node_child_by_field_name(node, "left", 4);
|
||||
TSNode right = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (ts_node_is_null(right)) right = ts_node_child_by_field_name(node, "right", 5);
|
||||
if (!ts_node_is_null(left) && !ts_node_is_null(right)) {
|
||||
char* var_name = cbm_node_text(ctx->arena, left, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, right,
|
||||
ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JS/TS: variable_declarator
|
||||
if (strcmp(kind, "variable_declarator") == 0) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
TSNode value_node = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(name_node) && !ts_node_is_null(value_node)) {
|
||||
const char* nk = ts_node_type(name_node);
|
||||
if (strcmp(nk, "identifier") == 0 || strcmp(nk, "simple_identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, value_node,
|
||||
ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rust: let_declaration
|
||||
if (strcmp(kind, "let_declaration") == 0 && ctx->language == CBM_LANG_RUST) {
|
||||
TSNode pat = ts_node_child_by_field_name(node, "pattern", 7);
|
||||
TSNode val = ts_node_child_by_field_name(node, "value", 5);
|
||||
if (!ts_node_is_null(pat) && !ts_node_is_null(val)) {
|
||||
if (strcmp(ts_node_type(pat), "identifier") == 0) {
|
||||
char* var_name = cbm_node_text(ctx->arena, pat, ctx->source);
|
||||
const char* type_name = extract_constructor_type(ctx->arena, val,
|
||||
ctx->source, ctx->language);
|
||||
if (var_name && var_name[0] && type_name && type_name[0]) {
|
||||
CBMTypeAssign ta;
|
||||
ta.var_name = var_name;
|
||||
ta.type_name = type_name;
|
||||
ta.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_typeassign_push(&ctx->result->type_assigns, ctx->arena, ta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Builtin types that should not generate USES_TYPE edges.
|
||||
static bool is_builtin_type(const char* name) {
|
||||
if (!name || !name[0]) return true;
|
||||
if (strlen(name) <= 1) return true;
|
||||
|
||||
// Common primitives
|
||||
if (strcmp(name, "int") == 0 || strcmp(name, "string") == 0 ||
|
||||
strcmp(name, "bool") == 0 || strcmp(name, "float") == 0 ||
|
||||
strcmp(name, "float32") == 0 || strcmp(name, "float64") == 0 ||
|
||||
strcmp(name, "int8") == 0 || strcmp(name, "int16") == 0 ||
|
||||
strcmp(name, "int32") == 0 || strcmp(name, "int64") == 0 ||
|
||||
strcmp(name, "uint") == 0 || strcmp(name, "uint8") == 0 ||
|
||||
strcmp(name, "uint16") == 0 || strcmp(name, "uint32") == 0 ||
|
||||
strcmp(name, "uint64") == 0 || strcmp(name, "uintptr") == 0 ||
|
||||
strcmp(name, "byte") == 0 || strcmp(name, "rune") == 0 ||
|
||||
strcmp(name, "void") == 0 || strcmp(name, "char") == 0 ||
|
||||
strcmp(name, "double") == 0 || strcmp(name, "long") == 0 ||
|
||||
strcmp(name, "short") == 0 || strcmp(name, "unsigned") == 0 ||
|
||||
strcmp(name, "error") == 0 || strcmp(name, "any") == 0 ||
|
||||
strcmp(name, "interface") == 0 || strcmp(name, "object") == 0 ||
|
||||
strcmp(name, "Object") == 0 || strcmp(name, "None") == 0 ||
|
||||
strcmp(name, "nil") == 0 || strcmp(name, "null") == 0 ||
|
||||
strcmp(name, "undefined") == 0 || strcmp(name, "number") == 0 ||
|
||||
strcmp(name, "boolean") == 0 || strcmp(name, "str") == 0 ||
|
||||
strcmp(name, "dict") == 0 || strcmp(name, "list") == 0 ||
|
||||
strcmp(name, "tuple") == 0 || strcmp(name, "set") == 0 ||
|
||||
strcmp(name, "complex128") == 0 || strcmp(name, "complex64") == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Strip pointer/reference/slice/optional markers from a type name.
|
||||
static const char* clean_type_name(CBMArena* a, const char* name) {
|
||||
if (!name || !name[0]) return name;
|
||||
// Skip leading *, &, [], ?
|
||||
while (*name == '*' || *name == '&' || *name == '?' || *name == '[' || *name == ']') name++;
|
||||
if (!*name) return NULL;
|
||||
// Take only the base type before any < or [
|
||||
size_t len = strlen(name);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (name[i] == '<' || name[i] == '[') {
|
||||
return cbm_arena_strndup(a, name, i);
|
||||
}
|
||||
}
|
||||
// Strip trailing ? (optionals)
|
||||
if (len > 0 && name[len-1] == '?') {
|
||||
return cbm_arena_strndup(a, name, len-1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// Extract type name from a type annotation node.
|
||||
static const char* extract_type_text(CBMArena* a, TSNode node, const char* source) {
|
||||
const char* kind = ts_node_type(node);
|
||||
// For type_identifier / identifier, just get text
|
||||
if (strcmp(kind, "type_identifier") == 0 || strcmp(kind, "identifier") == 0 ||
|
||||
strcmp(kind, "simple_identifier") == 0 || strcmp(kind, "name") == 0) {
|
||||
return cbm_node_text(a, node, source);
|
||||
}
|
||||
// For generic types, get the base type
|
||||
if (strcmp(kind, "generic_type") == 0 || strcmp(kind, "parameterized_type") == 0) {
|
||||
if (ts_node_child_count(node) > 0) {
|
||||
return cbm_node_text(a, ts_node_child(node, 0), source);
|
||||
}
|
||||
}
|
||||
// For pointer/reference/slice types
|
||||
if (strcmp(kind, "pointer_type") == 0 || strcmp(kind, "reference_type") == 0 ||
|
||||
strcmp(kind, "slice_type") == 0 || strcmp(kind, "array_type") == 0) {
|
||||
// Get the element type
|
||||
TSNode elem = ts_node_child_by_field_name(node, "element", 7);
|
||||
if (!ts_node_is_null(elem)) return extract_type_text(a, elem, source);
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) return extract_type_text(a, type_node, source);
|
||||
}
|
||||
// Fallback: full text cleaned
|
||||
return clean_type_name(a, cbm_node_text(a, node, source));
|
||||
}
|
||||
|
||||
// Add a type reference for a function.
|
||||
static void add_type_ref(CBMExtractCtx* ctx, const char* type_name, const char* func_qn) {
|
||||
if (!type_name || !type_name[0]) return;
|
||||
type_name = clean_type_name(ctx->arena, type_name);
|
||||
if (!type_name || !type_name[0]) return;
|
||||
if (is_builtin_type(type_name)) return;
|
||||
|
||||
CBMTypeRef tr;
|
||||
tr.type_name = type_name;
|
||||
tr.enclosing_func_qn = func_qn;
|
||||
cbm_typerefs_push(&ctx->result->type_refs, ctx->arena, tr);
|
||||
}
|
||||
|
||||
// Extract parameter types from a parameters/formal_parameters node.
|
||||
static void extract_param_type_refs(CBMExtractCtx* ctx, TSNode params, const char* func_qn) {
|
||||
uint32_t count = ts_node_child_count(params);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode child = ts_node_child(params, i);
|
||||
TSNode type_node = ts_node_child_by_field_name(child, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
const char* tname = extract_type_text(ctx->arena, type_node, ctx->source);
|
||||
add_type_ref(ctx, tname, func_qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract return type references.
|
||||
static void extract_return_type_refs(CBMExtractCtx* ctx, TSNode func_node, const char* func_qn) {
|
||||
const char* fields[] = {"result", "return_type", "type", NULL};
|
||||
for (const char** f = fields; *f; f++) {
|
||||
TSNode rt = ts_node_child_by_field_name(func_node, *f, (uint32_t)strlen(*f));
|
||||
if (!ts_node_is_null(rt)) {
|
||||
const char* tname = extract_type_text(ctx->arena, rt, ctx->source);
|
||||
add_type_ref(ctx, tname, func_qn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Walk function body for type references (casts, type assertions, local var types, generics).
|
||||
static void walk_body_type_refs(CBMExtractCtx* ctx, TSNode node, const char* func_qn) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
switch (ctx->language) {
|
||||
case CBM_LANG_GO:
|
||||
if (strcmp(kind, "var_spec") == 0 || strcmp(kind, "type_assertion") == 0 ||
|
||||
strcmp(kind, "type_conversion_expression") == 0 || strcmp(kind, "composite_literal") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_TYPESCRIPT: case CBM_LANG_TSX:
|
||||
if (strcmp(kind, "as_expression") == 0 || strcmp(kind, "satisfies_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
} else if (strcmp(kind, "type_arguments") == 0) {
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_identifier") == 0 ||
|
||||
strcmp(ts_node_type(child), "identifier") == 0) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, child, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(kind, "variable_declarator") == 0) {
|
||||
// Check for type_annotation child
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_annotation") == 0) {
|
||||
if (ts_node_child_count(child) > 0) {
|
||||
TSNode inner = ts_node_child(child, ts_node_child_count(child) - 1);
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, inner, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_JAVA:
|
||||
if (strcmp(kind, "local_variable_declaration") == 0 || strcmp(kind, "cast_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
} else if (strcmp(kind, "generic_type") == 0) {
|
||||
// Extract type arguments
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_arguments") == 0) {
|
||||
uint32_t nta = ts_node_child_count(child);
|
||||
for (uint32_t j = 0; j < nta; j++) {
|
||||
TSNode ta = ts_node_child(child, j);
|
||||
if (strcmp(ts_node_type(ta), "type_identifier") == 0) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, ta, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_PYTHON:
|
||||
if (strcmp(kind, "assignment") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_RUST:
|
||||
if (strcmp(kind, "let_declaration") == 0 || strcmp(kind, "type_cast_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Recurse
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_body_type_refs(ctx, ts_node_child(node, i), func_qn);
|
||||
}
|
||||
}
|
||||
|
||||
// Walk AST for function nodes, extract type references from signatures and bodies.
|
||||
static void walk_type_refs(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
if (!spec->function_node_types || !spec->function_node_types[0]) return;
|
||||
|
||||
if (cbm_kind_in_set(node, spec->function_node_types)) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(name_node)) goto recurse;
|
||||
char* func_name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (!func_name || !func_name[0]) goto recurse;
|
||||
|
||||
const char* func_qn = cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, func_name);
|
||||
|
||||
// Signature types
|
||||
TSNode params = ts_node_child_by_field_name(node, "parameters", 10);
|
||||
if (!ts_node_is_null(params)) {
|
||||
extract_param_type_refs(ctx, params, func_qn);
|
||||
}
|
||||
extract_return_type_refs(ctx, node, func_qn);
|
||||
|
||||
// Body types
|
||||
TSNode body = ts_node_child_by_field_name(node, "body", 4);
|
||||
if (ts_node_is_null(body)) body = ts_node_child_by_field_name(node, "block", 5);
|
||||
if (!ts_node_is_null(body)) {
|
||||
walk_body_type_refs(ctx, body, func_qn);
|
||||
}
|
||||
return; // Don't recurse into function internals again
|
||||
}
|
||||
|
||||
recurse:
|
||||
;
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_type_refs(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
void cbm_extract_type_refs(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
walk_type_refs(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// --- Unified handler ---
|
||||
// For function nodes: extract signature type refs (params + return).
|
||||
// For body-level type-bearing nodes: extract body type refs.
|
||||
// The cursor visits both, so this single handler replaces the old
|
||||
// walk_type_refs + walk_body_type_refs split.
|
||||
|
||||
void handle_type_refs(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
if (!spec->function_node_types || !spec->function_node_types[0]) return;
|
||||
|
||||
// Function signature: extract param and return type refs
|
||||
if (cbm_kind_in_set(node, spec->function_node_types)) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(name_node)) return;
|
||||
char* func_name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (!func_name || !func_name[0]) return;
|
||||
|
||||
const char* func_qn;
|
||||
if (state->enclosing_class_qn) {
|
||||
func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, func_name);
|
||||
} else {
|
||||
func_qn = cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, func_name);
|
||||
}
|
||||
|
||||
TSNode params = ts_node_child_by_field_name(node, "parameters", 10);
|
||||
if (!ts_node_is_null(params)) {
|
||||
extract_param_type_refs(ctx, params, func_qn);
|
||||
}
|
||||
extract_return_type_refs(ctx, node, func_qn);
|
||||
return;
|
||||
}
|
||||
|
||||
// Body type refs: use enclosing_func_qn from state
|
||||
const char* kind = ts_node_type(node);
|
||||
const char* func_qn = state->enclosing_func_qn;
|
||||
|
||||
switch (ctx->language) {
|
||||
case CBM_LANG_GO:
|
||||
if (strcmp(kind, "var_spec") == 0 || strcmp(kind, "type_assertion") == 0 ||
|
||||
strcmp(kind, "type_conversion_expression") == 0 || strcmp(kind, "composite_literal") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_TYPESCRIPT: case CBM_LANG_TSX:
|
||||
if (strcmp(kind, "as_expression") == 0 || strcmp(kind, "satisfies_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
} else if (strcmp(kind, "type_arguments") == 0) {
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_identifier") == 0 ||
|
||||
strcmp(ts_node_type(child), "identifier") == 0) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, child, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(kind, "variable_declarator") == 0) {
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_annotation") == 0) {
|
||||
if (ts_node_child_count(child) > 0) {
|
||||
TSNode inner = ts_node_child(child, ts_node_child_count(child) - 1);
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, inner, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_JAVA:
|
||||
if (strcmp(kind, "local_variable_declaration") == 0 || strcmp(kind, "cast_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
} else if (strcmp(kind, "generic_type") == 0) {
|
||||
uint32_t nc = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
if (strcmp(ts_node_type(child), "type_arguments") == 0) {
|
||||
uint32_t nta = ts_node_child_count(child);
|
||||
for (uint32_t j = 0; j < nta; j++) {
|
||||
TSNode ta = ts_node_child(child, j);
|
||||
if (strcmp(ts_node_type(ta), "type_identifier") == 0) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, ta, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_PYTHON:
|
||||
if (strcmp(kind, "assignment") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, cbm_node_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CBM_LANG_RUST:
|
||||
if (strcmp(kind, "let_declaration") == 0 || strcmp(kind, "type_cast_expression") == 0) {
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
add_type_ref(ctx, extract_type_text(ctx->arena, type_node, ctx->source), func_qn);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
#include "extract_unified.h"
|
||||
#include "helpers.h"
|
||||
#include <string.h>
|
||||
|
||||
// --- Scope stack management ---
|
||||
|
||||
static void push_scope(WalkState* state, uint8_t kind, uint32_t depth, const char* qn) {
|
||||
if (state->scope_top >= MAX_SCOPES) return;
|
||||
state->scopes[state->scope_top].kind = kind;
|
||||
state->scopes[state->scope_top].depth = depth;
|
||||
state->scopes[state->scope_top].qn = qn;
|
||||
state->scope_top++;
|
||||
}
|
||||
|
||||
// Pop scopes that we've ascended out of (depth >= current cursor depth).
|
||||
static void pop_expired_scopes(WalkState* state, uint32_t cur_depth) {
|
||||
while (state->scope_top > 0 &&
|
||||
state->scopes[state->scope_top - 1].depth >= cur_depth) {
|
||||
state->scope_top--;
|
||||
}
|
||||
}
|
||||
|
||||
// Recompute state flags from the current scope stack.
|
||||
static void recompute_state(WalkState* state, const char* module_qn) {
|
||||
state->enclosing_func_qn = module_qn;
|
||||
state->enclosing_class_qn = NULL;
|
||||
state->inside_call = false;
|
||||
state->inside_import = false;
|
||||
|
||||
for (int i = 0; i < state->scope_top; i++) {
|
||||
switch (state->scopes[i].kind) {
|
||||
case SCOPE_FUNC:
|
||||
state->enclosing_func_qn = state->scopes[i].qn;
|
||||
break;
|
||||
case SCOPE_CLASS:
|
||||
state->enclosing_class_qn = state->scopes[i].qn;
|
||||
break;
|
||||
case SCOPE_CALL:
|
||||
state->inside_call = true;
|
||||
break;
|
||||
case SCOPE_IMPORT:
|
||||
state->inside_import = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute function QN for scope tracking (mirrors cbm_enclosing_func_qn logic).
|
||||
static const char* compute_func_qn(CBMExtractCtx* ctx, TSNode node,
|
||||
const CBMLangSpec* spec, WalkState* state) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
|
||||
// Arrow function: name from parent variable_declarator
|
||||
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "arrow_function") == 0) {
|
||||
TSNode parent = ts_node_parent(node);
|
||||
if (!ts_node_is_null(parent) &&
|
||||
strcmp(ts_node_type(parent), "variable_declarator") == 0) {
|
||||
name_node = ts_node_child_by_field_name(parent, "name", 4);
|
||||
}
|
||||
}
|
||||
|
||||
if (ts_node_is_null(name_node)) return NULL;
|
||||
|
||||
char* name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (!name || !name[0]) return NULL;
|
||||
|
||||
if (state->enclosing_class_qn) {
|
||||
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name);
|
||||
}
|
||||
return cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, name);
|
||||
}
|
||||
|
||||
// Compute class QN for scope tracking.
|
||||
static const char* compute_class_qn(CBMExtractCtx* ctx, TSNode node) {
|
||||
TSNode name_node = ts_node_child_by_field_name(node, "name", 4);
|
||||
if (ts_node_is_null(name_node)) return NULL;
|
||||
|
||||
char* name = cbm_node_text(ctx->arena, name_node, ctx->source);
|
||||
if (!name || !name[0]) return NULL;
|
||||
|
||||
return cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, name);
|
||||
}
|
||||
|
||||
// --- Main unified cursor walk ---
|
||||
|
||||
void cbm_extract_unified(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
|
||||
WalkState state;
|
||||
memset(&state, 0, sizeof(state));
|
||||
|
||||
uint32_t depth = 0;
|
||||
|
||||
for (;;) {
|
||||
TSNode node = ts_tree_cursor_current_node(&cursor);
|
||||
|
||||
// 1. Pop scopes we've ascended out of
|
||||
pop_expired_scopes(&state, depth);
|
||||
|
||||
// 2. Recompute state from remaining scopes
|
||||
recompute_state(&state, ctx->module_qn);
|
||||
|
||||
// 3. Dispatch to all handlers
|
||||
handle_calls(ctx, node, spec, &state);
|
||||
handle_usages(ctx, node, spec, &state);
|
||||
handle_throws(ctx, node, spec, &state);
|
||||
handle_readwrites(ctx, node, spec, &state);
|
||||
handle_type_refs(ctx, node, spec, &state);
|
||||
handle_env_accesses(ctx, node, spec, &state);
|
||||
handle_type_assigns(ctx, node, spec, &state);
|
||||
|
||||
// 4. Push scope markers for boundary nodes
|
||||
if (spec->function_node_types && cbm_kind_in_set(node, spec->function_node_types)) {
|
||||
const char* fqn = compute_func_qn(ctx, node, spec, &state);
|
||||
if (fqn) push_scope(&state, SCOPE_FUNC, depth, fqn);
|
||||
} else if (spec->class_node_types && cbm_kind_in_set(node, spec->class_node_types)) {
|
||||
const char* cqn = compute_class_qn(ctx, node);
|
||||
if (cqn) push_scope(&state, SCOPE_CLASS, depth, cqn);
|
||||
} else if (ctx->language == CBM_LANG_RUST &&
|
||||
strcmp(ts_node_type(node), "impl_item") == 0) {
|
||||
// Rust impl block acts as class scope for methods
|
||||
TSNode type_node = ts_node_child_by_field_name(node, "type", 4);
|
||||
if (!ts_node_is_null(type_node)) {
|
||||
char* type_name = cbm_node_text(ctx->arena, type_node, ctx->source);
|
||||
if (type_name && type_name[0]) {
|
||||
const char* tqn = cbm_fqn_compute(ctx->arena, ctx->project,
|
||||
ctx->rel_path, type_name);
|
||||
push_scope(&state, SCOPE_CLASS, depth, tqn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (spec->call_node_types && cbm_kind_in_set(node, spec->call_node_types)) {
|
||||
push_scope(&state, SCOPE_CALL, depth, NULL);
|
||||
}
|
||||
if (spec->import_node_types && cbm_kind_in_set(node, spec->import_node_types)) {
|
||||
push_scope(&state, SCOPE_IMPORT, depth, NULL);
|
||||
}
|
||||
|
||||
// 5. Advance cursor: DFS order
|
||||
if (ts_tree_cursor_goto_first_child(&cursor)) {
|
||||
depth++;
|
||||
continue;
|
||||
}
|
||||
if (ts_tree_cursor_goto_next_sibling(&cursor)) {
|
||||
continue;
|
||||
}
|
||||
// Ascend until we find a sibling
|
||||
bool found = false;
|
||||
while (ts_tree_cursor_goto_parent(&cursor)) {
|
||||
depth--;
|
||||
if (ts_tree_cursor_goto_next_sibling(&cursor)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) break;
|
||||
}
|
||||
|
||||
ts_tree_cursor_delete(&cursor);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef CBM_EXTRACT_UNIFIED_H
|
||||
#define CBM_EXTRACT_UNIFIED_H
|
||||
|
||||
#include "cbm.h"
|
||||
#include "lang_specs.h"
|
||||
|
||||
// Scope kinds for the walk state stack.
|
||||
#define SCOPE_FUNC 1
|
||||
#define SCOPE_CLASS 2
|
||||
#define SCOPE_CALL 3
|
||||
#define SCOPE_IMPORT 4
|
||||
|
||||
#define MAX_SCOPES 64
|
||||
|
||||
// WalkState tracks scope context during the unified cursor walk.
|
||||
// Replaces parent-chain walks for enclosing_func_qn, inside_call, etc.
|
||||
typedef struct {
|
||||
const char* enclosing_func_qn; // current function QN (module_qn at top level)
|
||||
const char* enclosing_class_qn; // current class QN (NULL outside class)
|
||||
bool inside_call; // within a call_node_types subtree
|
||||
bool inside_import; // within an import_node_types subtree
|
||||
|
||||
struct {
|
||||
const char* qn;
|
||||
uint32_t depth;
|
||||
uint8_t kind;
|
||||
} scopes[MAX_SCOPES];
|
||||
int scope_top;
|
||||
} WalkState;
|
||||
|
||||
// Per-node handler prototypes. Each is called once per node during the
|
||||
// unified cursor walk, replacing the old recursive walk_* functions.
|
||||
void handle_calls(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_usages(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_throws(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_readwrites(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_type_refs(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_env_accesses(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
void handle_type_assigns(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state);
|
||||
|
||||
// Single-pass extraction using TSTreeCursor. Visits every node once,
|
||||
// dispatching to all handlers per node. Replaces the 7 separate walk_*
|
||||
// functions for calls/usages/throws/readwrites/type_refs/env_accesses/type_assigns.
|
||||
// Definitions and imports stay as separate passes (different recursion patterns).
|
||||
void cbm_extract_unified(CBMExtractCtx* ctx);
|
||||
|
||||
#endif // CBM_EXTRACT_UNIFIED_H
|
||||
@@ -0,0 +1,142 @@
|
||||
#include "cbm.h"
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include "extract_unified.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Forward declaration
|
||||
static void walk_usages(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec);
|
||||
|
||||
// Check if a node is inside a call expression (to avoid double-counting as usage)
|
||||
static bool is_inside_call(TSNode node, const CBMLangSpec* spec) {
|
||||
TSNode cur = ts_node_parent(node);
|
||||
int depth = 0;
|
||||
while (!ts_node_is_null(cur) && depth < 10) {
|
||||
if (cbm_kind_in_set(cur, spec->call_node_types)) return true;
|
||||
cur = ts_node_parent(cur);
|
||||
depth++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if a node is inside an import statement
|
||||
static bool is_inside_import(TSNode node, const CBMLangSpec* spec) {
|
||||
if (!spec->import_node_types || !spec->import_node_types[0]) return false;
|
||||
TSNode cur = ts_node_parent(node);
|
||||
int depth = 0;
|
||||
while (!ts_node_is_null(cur) && depth < 10) {
|
||||
if (cbm_kind_in_set(cur, spec->import_node_types)) return true;
|
||||
cur = ts_node_parent(cur);
|
||||
depth++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is this an identifier-like node that represents a reference?
|
||||
static bool is_reference_node(TSNode node, CBMLanguage lang) {
|
||||
const char* kind = ts_node_type(node);
|
||||
|
||||
// Common identifier types across languages
|
||||
if (strcmp(kind, "identifier") == 0 ||
|
||||
strcmp(kind, "simple_identifier") == 0 ||
|
||||
strcmp(kind, "type_identifier") == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Language-specific reference types
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO:
|
||||
return strcmp(kind, "field_identifier") == 0 ||
|
||||
strcmp(kind, "package_identifier") == 0;
|
||||
case CBM_LANG_PYTHON:
|
||||
return strcmp(kind, "attribute") == 0;
|
||||
case CBM_LANG_RUST:
|
||||
return strcmp(kind, "field_identifier") == 0 ||
|
||||
strcmp(kind, "scoped_identifier") == 0;
|
||||
case CBM_LANG_HASKELL:
|
||||
return strcmp(kind, "variable") == 0 ||
|
||||
strcmp(kind, "constructor") == 0;
|
||||
case CBM_LANG_OCAML:
|
||||
return strcmp(kind, "value_path") == 0 ||
|
||||
strcmp(kind, "constructor_path") == 0;
|
||||
case CBM_LANG_ERLANG:
|
||||
return strcmp(kind, "atom") == 0 ||
|
||||
strcmp(kind, "var") == 0;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void walk_usages(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec) {
|
||||
if (is_reference_node(node, ctx->language)) {
|
||||
// Skip if inside a call (already counted as CALLS edge)
|
||||
if (is_inside_call(node, spec)) goto recurse;
|
||||
// Skip if inside an import
|
||||
if (is_inside_import(node, spec)) goto recurse;
|
||||
// Skip if it's a definition name (left side of assignment, function name)
|
||||
TSNode parent = ts_node_parent(node);
|
||||
if (!ts_node_is_null(parent)) {
|
||||
// Check if this is the "name" field of the parent
|
||||
TSNode name_field = ts_node_child_by_field_name(parent, "name", 4);
|
||||
if (!ts_node_is_null(name_field) &&
|
||||
ts_node_start_byte(name_field) == ts_node_start_byte(node) &&
|
||||
ts_node_end_byte(name_field) == ts_node_end_byte(node)) {
|
||||
goto recurse;
|
||||
}
|
||||
}
|
||||
|
||||
char* name = cbm_node_text(ctx->arena, node, ctx->source);
|
||||
if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
|
||||
CBMUsage usage;
|
||||
usage.ref_name = name;
|
||||
usage.enclosing_func_qn = cbm_enclosing_func_qn_cached(ctx, node);
|
||||
cbm_usages_push(&ctx->result->usages, ctx->arena, usage);
|
||||
}
|
||||
}
|
||||
|
||||
recurse:
|
||||
;
|
||||
uint32_t count = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
walk_usages(ctx, ts_node_child(node, i), spec);
|
||||
}
|
||||
}
|
||||
|
||||
void cbm_extract_usages(CBMExtractCtx* ctx) {
|
||||
const CBMLangSpec* spec = cbm_lang_spec(ctx->language);
|
||||
if (!spec) return;
|
||||
|
||||
walk_usages(ctx, ctx->root, spec);
|
||||
}
|
||||
|
||||
// --- Unified handler: called once per node by the cursor walk ---
|
||||
// Uses WalkState flags instead of parent-chain walks for O(1) context checks.
|
||||
|
||||
void handle_usages(CBMExtractCtx* ctx, TSNode node, const CBMLangSpec* spec, WalkState* state) {
|
||||
if (!is_reference_node(node, ctx->language)) return;
|
||||
|
||||
// Skip if inside a call (already counted as CALLS edge) — O(1) via state
|
||||
if (state->inside_call) return;
|
||||
// Skip if inside an import
|
||||
if (state->inside_import) return;
|
||||
|
||||
// Skip if it's a definition name (left side of assignment, function name)
|
||||
TSNode parent = ts_node_parent(node);
|
||||
if (!ts_node_is_null(parent)) {
|
||||
TSNode name_field = ts_node_child_by_field_name(parent, "name", 4);
|
||||
if (!ts_node_is_null(name_field) &&
|
||||
ts_node_start_byte(name_field) == ts_node_start_byte(node) &&
|
||||
ts_node_end_byte(name_field) == ts_node_end_byte(node)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
char* name = cbm_node_text(ctx->arena, node, ctx->source);
|
||||
if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
|
||||
CBMUsage usage;
|
||||
usage.ref_name = name;
|
||||
usage.enclosing_func_qn = state->enclosing_func_qn;
|
||||
cbm_usages_push(&ctx->result->usages, ctx->arena, usage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: bash
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/bash/parser.c"
|
||||
#include "vendored/grammars/bash/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: c
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/c/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: c_sharp
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/c_sharp/parser.c"
|
||||
#include "vendored/grammars/c_sharp/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: clojure
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/clojure/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: cmake
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/cmake/parser.c"
|
||||
#include "vendored/grammars/cmake/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: cobol
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/cobol/parser.c"
|
||||
#include "vendored/grammars/cobol/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: commonlisp
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/commonlisp/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: cpp
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/cpp/parser.c"
|
||||
#include "vendored/grammars/cpp/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: css
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/css/parser.c"
|
||||
#include "vendored/grammars/css/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: cuda
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/cuda/parser.c"
|
||||
#include "vendored/grammars/cuda/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: dart
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/dart/parser.c"
|
||||
#include "vendored/grammars/dart/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: dockerfile
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/dockerfile/parser.c"
|
||||
#include "vendored/grammars/dockerfile/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: elisp (Emacs Lisp)
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/elisp/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: elixir
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/elixir/parser.c"
|
||||
#include "vendored/grammars/elixir/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: elm
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/elm/parser.c"
|
||||
#include "vendored/grammars/elm/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: erlang
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/erlang/parser.c"
|
||||
#include "vendored/grammars/erlang/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: fortran
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/fortran/parser.c"
|
||||
#include "vendored/grammars/fortran/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: fsharp
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/fsharp/parser.c"
|
||||
#include "vendored/grammars/fsharp/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: glsl
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/glsl/parser.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: go
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/go/parser.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: graphql
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/graphql/parser.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: groovy
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/groovy/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: haskell
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/haskell/parser.c"
|
||||
#include "vendored/grammars/haskell/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: hcl
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/hcl/parser.c"
|
||||
#include "vendored/grammars/hcl/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: html
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/html/parser.c"
|
||||
#include "vendored/grammars/html/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: ini
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/ini/parser.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: java
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/java/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: javascript
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/javascript/parser.c"
|
||||
#include "vendored/grammars/javascript/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: json
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/json/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: julia
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/julia/parser.c"
|
||||
#include "vendored/grammars/julia/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: kotlin
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/kotlin/parser.c"
|
||||
#include "vendored/grammars/kotlin/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: lua
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/lua/parser.c"
|
||||
#include "vendored/grammars/lua/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: make (Makefile)
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/make/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: markdown
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/markdown/parser.c"
|
||||
#include "vendored/grammars/markdown/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: meson
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/meson/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: nix
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/nix/parser.c"
|
||||
#include "vendored/grammars/nix/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: objc
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/objc/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: ocaml
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/ocaml/parser.c"
|
||||
#include "vendored/grammars/ocaml/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: perl
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/perl/parser.c"
|
||||
#include "vendored/grammars/perl/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: php
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/php/parser.c"
|
||||
#include "vendored/grammars/php/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: protobuf
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/protobuf/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: python
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/python/parser.c"
|
||||
#include "vendored/grammars/python/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: r
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/r/parser.c"
|
||||
#include "vendored/grammars/r/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: ruby
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/ruby/parser.c"
|
||||
#include "vendored/grammars/ruby/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: rust
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/rust/parser.c"
|
||||
#include "vendored/grammars/rust/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: scala
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/scala/parser.c"
|
||||
#include "vendored/grammars/scala/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: scss
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/scss/parser.c"
|
||||
#include "vendored/grammars/scss/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: sql
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/sql/parser.c"
|
||||
#include "vendored/grammars/sql/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: svelte
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/svelte/parser.c"
|
||||
#include "vendored/grammars/svelte/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: swift
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/swift/parser.c"
|
||||
#include "vendored/grammars/swift/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: toml
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/toml/parser.c"
|
||||
#include "vendored/grammars/toml/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: tsx
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/tsx/parser.c"
|
||||
#include "vendored/grammars/tsx/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: typescript
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/typescript/parser.c"
|
||||
#include "vendored/grammars/typescript/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: verilog
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/verilog/parser.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: vim
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/vim/parser.c"
|
||||
#include "vendored/grammars/vim/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: vue
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/vue/parser.c"
|
||||
#include "vendored/grammars/vue/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: xml
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/xml/parser.c"
|
||||
#include "vendored/grammars/xml/scanner.c"
|
||||
@@ -0,0 +1,4 @@
|
||||
// Vendored tree-sitter grammar: yaml
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/yaml/parser.c"
|
||||
#include "vendored/grammars/yaml/scanner.c"
|
||||
@@ -0,0 +1,3 @@
|
||||
// Vendored tree-sitter grammar: zig
|
||||
// Each grammar compiled as separate unit (conflicting static symbols).
|
||||
#include "vendored/grammars/zig/parser.c"
|
||||
@@ -0,0 +1,623 @@
|
||||
#include "helpers.h"
|
||||
#include "lang_specs.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// --- Node text extraction ---
|
||||
|
||||
char* cbm_node_text(CBMArena* a, TSNode node, const char* source) {
|
||||
uint32_t start = ts_node_start_byte(node);
|
||||
uint32_t end = ts_node_end_byte(node);
|
||||
if (end <= start) return cbm_arena_strdup(a, "");
|
||||
return cbm_arena_strndup(a, source + start, end - start);
|
||||
}
|
||||
|
||||
// --- Keyword sets per language ---
|
||||
|
||||
static const char* go_keywords[] = {
|
||||
"break","case","chan","const","continue","default","defer","else",
|
||||
"fallthrough","for","func","go","goto","if","import","interface",
|
||||
"map","package","range","return","select","struct","switch","type","var",
|
||||
"true","false","nil","iota","append","cap","close","complex","copy",
|
||||
"delete","imag","len","make","new","panic","print","println","real","recover",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char* python_keywords[] = {
|
||||
"False","None","True","and","as","assert","async","await","break",
|
||||
"class","continue","def","del","elif","else","except","finally",
|
||||
"for","from","global","if","import","in","is","lambda","nonlocal",
|
||||
"not","or","pass","raise","return","try","while","with","yield",
|
||||
"self","cls","__init__","__name__","__main__","super",
|
||||
"print","len","range","enumerate","zip","map","filter","type",
|
||||
"int","str","float","bool","list","dict","set","tuple","bytes",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char* js_keywords[] = {
|
||||
"break","case","catch","class","const","continue","debugger","default",
|
||||
"delete","do","else","export","extends","false","finally","for",
|
||||
"function","if","import","in","instanceof","let","new","null",
|
||||
"return","super","switch","this","throw","true","try","typeof",
|
||||
"undefined","var","void","while","with","yield","async","await",
|
||||
"of","static","get","set","from","as","constructor","prototype",
|
||||
"console","window","document","process","module","exports","require",
|
||||
"Array","Object","String","Number","Boolean","Symbol","Map","Set",
|
||||
"Promise","Error","RegExp","Date","Math","JSON","parseInt","parseFloat",
|
||||
"setTimeout","setInterval","clearTimeout","clearInterval",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char* rust_keywords[] = {
|
||||
"as","async","await","break","const","continue","crate","dyn","else",
|
||||
"enum","extern","false","fn","for","if","impl","in","let","loop",
|
||||
"match","mod","move","mut","pub","ref","return","self","Self","static",
|
||||
"struct","super","trait","true","type","unsafe","use","where","while",
|
||||
"abstract","become","box","do","final","macro","override","priv",
|
||||
"try","typeof","unsized","virtual","yield",
|
||||
"Some","None","Ok","Err","Vec","String","Box","Rc","Arc","Option","Result",
|
||||
"println","eprintln","format","write","writeln","print","eprint","panic",
|
||||
"assert","assert_eq","assert_ne","debug_assert","todo","unimplemented",
|
||||
"cfg","derive","test","allow","deny","warn","forbid","deprecated",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char* java_keywords[] = {
|
||||
"abstract","assert","boolean","break","byte","case","catch","char",
|
||||
"class","const","continue","default","do","double","else","enum",
|
||||
"extends","false","final","finally","float","for","goto","if",
|
||||
"implements","import","instanceof","int","interface","long","native",
|
||||
"new","null","package","private","protected","public","return",
|
||||
"short","static","strictfp","super","switch","synchronized","this",
|
||||
"throw","throws","transient","true","try","void","volatile","while",
|
||||
"var","record","sealed","permits","yield",
|
||||
"System","String","Integer","Long","Double","Float","Boolean","Object",
|
||||
"List","Map","Set","Optional","Stream","Arrays","Collections",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char* generic_keywords[] = {
|
||||
"true","false","null","nil","None","undefined","void",
|
||||
"if","else","for","while","do","switch","case","default",
|
||||
"break","continue","return","throw","try","catch","finally",
|
||||
"class","struct","enum","interface","trait","impl",
|
||||
"import","export","package","module","use","require","include",
|
||||
"new","delete","this","self","super",
|
||||
"public","private","protected","static","const","var","let",
|
||||
"function","def","fn","func","fun","proc","sub","method",
|
||||
"async","await","yield",
|
||||
NULL
|
||||
};
|
||||
|
||||
bool cbm_is_keyword(const char* name, CBMLanguage lang) {
|
||||
if (!name || !name[0]) return true;
|
||||
|
||||
const char** keywords;
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO: keywords = go_keywords; break;
|
||||
case CBM_LANG_PYTHON: keywords = python_keywords; break;
|
||||
case CBM_LANG_JAVASCRIPT:
|
||||
case CBM_LANG_TYPESCRIPT:
|
||||
case CBM_LANG_TSX: keywords = js_keywords; break;
|
||||
case CBM_LANG_RUST: keywords = rust_keywords; break;
|
||||
case CBM_LANG_JAVA:
|
||||
case CBM_LANG_KOTLIN:
|
||||
case CBM_LANG_SCALA: keywords = java_keywords; break;
|
||||
default: keywords = generic_keywords; break;
|
||||
}
|
||||
|
||||
for (const char** kw = keywords; *kw; kw++) {
|
||||
if (strcmp(name, *kw) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// --- Export detection ---
|
||||
|
||||
bool cbm_is_exported(const char* name, CBMLanguage lang) {
|
||||
if (!name || !name[0]) return false;
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO:
|
||||
return name[0] >= 'A' && name[0] <= 'Z';
|
||||
case CBM_LANG_PYTHON:
|
||||
return name[0] != '_';
|
||||
case CBM_LANG_JAVA:
|
||||
case CBM_LANG_CSHARP:
|
||||
case CBM_LANG_KOTLIN:
|
||||
return name[0] >= 'A' && name[0] <= 'Z';
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Test file detection ---
|
||||
|
||||
static bool has_suffix(const char* str, const char* suffix) {
|
||||
size_t slen = strlen(str);
|
||||
size_t xlen = strlen(suffix);
|
||||
if (xlen > slen) return false;
|
||||
return strcmp(str + slen - xlen, suffix) == 0;
|
||||
}
|
||||
|
||||
static bool has_prefix(const char* str, const char* prefix) {
|
||||
return strncmp(str, prefix, strlen(prefix)) == 0;
|
||||
}
|
||||
|
||||
// Extract basename from path
|
||||
static const char* path_basename(const char* path) {
|
||||
const char* last = strrchr(path, '/');
|
||||
return last ? last + 1 : path;
|
||||
}
|
||||
|
||||
// Strip extension from basename
|
||||
static void strip_ext(const char* base, char* buf, size_t buflen) {
|
||||
const char* dot = strrchr(base, '.');
|
||||
if (dot && dot != base) {
|
||||
size_t len = (size_t)(dot - base);
|
||||
if (len >= buflen) len = buflen - 1;
|
||||
memcpy(buf, base, len);
|
||||
buf[len] = '\0';
|
||||
} else {
|
||||
snprintf(buf, buflen, "%s", base);
|
||||
}
|
||||
}
|
||||
|
||||
bool cbm_is_test_file(const char* rel_path, CBMLanguage lang) {
|
||||
if (!rel_path) return false;
|
||||
const char* base = path_basename(rel_path);
|
||||
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO:
|
||||
return has_suffix(base, "_test.go");
|
||||
case CBM_LANG_PYTHON:
|
||||
return has_prefix(base, "test_") || has_suffix(base, "_test.py");
|
||||
case CBM_LANG_JAVASCRIPT:
|
||||
case CBM_LANG_TYPESCRIPT:
|
||||
case CBM_LANG_TSX: {
|
||||
char noext[256];
|
||||
strip_ext(base, noext, sizeof(noext));
|
||||
return has_suffix(noext, ".test") || has_suffix(noext, ".spec") ||
|
||||
has_suffix(noext, "_test") || has_suffix(noext, "_spec") ||
|
||||
has_prefix(base, "test_");
|
||||
}
|
||||
case CBM_LANG_JAVA:
|
||||
case CBM_LANG_KOTLIN:
|
||||
case CBM_LANG_SCALA:
|
||||
return has_suffix(base, "Test.java") || has_suffix(base, "Tests.java") ||
|
||||
has_suffix(base, "Spec.java") || has_suffix(base, "Test.kt") ||
|
||||
has_suffix(base, "Spec.kt") || has_suffix(base, "Test.scala") ||
|
||||
has_suffix(base, "Spec.scala");
|
||||
case CBM_LANG_RUST:
|
||||
// Rust tests are typically mod tests inside the file, but test files too
|
||||
return has_suffix(base, "_test.rs") || has_prefix(base, "test_");
|
||||
case CBM_LANG_RUBY:
|
||||
return has_suffix(base, "_test.rb") || has_suffix(base, "_spec.rb") ||
|
||||
has_prefix(base, "test_");
|
||||
case CBM_LANG_PHP:
|
||||
return has_suffix(base, "Test.php");
|
||||
case CBM_LANG_CSHARP:
|
||||
return has_suffix(base, "Tests.cs") || has_suffix(base, "Test.cs");
|
||||
case CBM_LANG_CPP:
|
||||
case CBM_LANG_C:
|
||||
return has_suffix(base, "_test.c") || has_suffix(base, "_test.cc") ||
|
||||
has_suffix(base, "_test.cpp") || has_prefix(base, "test_");
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// --- AST traversal helpers ---
|
||||
|
||||
TSNode cbm_find_child_by_kind(TSNode parent, const char* kind) {
|
||||
uint32_t count = ts_node_child_count(parent);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode child = ts_node_child(parent, i);
|
||||
if (strcmp(ts_node_type(child), kind) == 0) {
|
||||
return child;
|
||||
}
|
||||
}
|
||||
TSNode null_node = {0};
|
||||
return null_node;
|
||||
}
|
||||
|
||||
bool cbm_kind_in_set(TSNode node, const char** types) {
|
||||
if (!types) return false;
|
||||
const char* kind = ts_node_type(node);
|
||||
for (const char** t = types; *t; t++) {
|
||||
if (strcmp(kind, *t) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cbm_has_ancestor_kind(TSNode node, const char* kind, int max_depth) {
|
||||
TSNode cur = node;
|
||||
for (int i = 0; i < max_depth; i++) {
|
||||
TSNode parent = ts_node_parent(cur);
|
||||
if (ts_node_is_null(parent)) return false;
|
||||
if (strcmp(ts_node_type(parent), kind) == 0) return true;
|
||||
cur = parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recursive branching count
|
||||
static int count_branching_rec(TSNode node, const char** types) {
|
||||
int count = 0;
|
||||
const char* kind = ts_node_type(node);
|
||||
for (const char** t = types; *t; t++) {
|
||||
if (strcmp(kind, *t) == 0) {
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
uint32_t n = ts_node_child_count(node);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
count += count_branching_rec(ts_node_child(node, i), types);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int cbm_count_branching(TSNode node, const char** branching_types) {
|
||||
if (!branching_types) return 0;
|
||||
return count_branching_rec(node, branching_types);
|
||||
}
|
||||
|
||||
// --- Enclosing function detection ---
|
||||
|
||||
// Language-specific function node types for parent-chain walk
|
||||
static const char* func_kinds_go[] = {"function_declaration","method_declaration",NULL};
|
||||
static const char* func_kinds_python[] = {"function_definition",NULL};
|
||||
static const char* func_kinds_js[] = {"function_declaration","method_definition","arrow_function","function_expression",NULL};
|
||||
static const char* func_kinds_rust[] = {"function_item",NULL};
|
||||
static const char* func_kinds_java[] = {"method_declaration","constructor_declaration",NULL};
|
||||
static const char* func_kinds_cpp[] = {"function_definition",NULL};
|
||||
static const char* func_kinds_ruby[] = {"method","singleton_method",NULL};
|
||||
static const char* func_kinds_php[] = {"function_definition","method_declaration",NULL};
|
||||
static const char* func_kinds_lua[] = {"function_declaration","function_definition",NULL};
|
||||
static const char* func_kinds_scala[] = {"function_definition",NULL};
|
||||
static const char* func_kinds_kotlin[] = {"function_declaration",NULL};
|
||||
static const char* func_kinds_elixir[] = {"call",NULL}; // def/defp are call nodes
|
||||
static const char* func_kinds_haskell[] = {"function","value_definition",NULL};
|
||||
static const char* func_kinds_ocaml[] = {"value_definition","let_binding",NULL};
|
||||
static const char* func_kinds_zig[] = {"function_declaration","test_declaration",NULL};
|
||||
static const char* func_kinds_bash[] = {"function_definition",NULL};
|
||||
static const char* func_kinds_erlang[] = {"function_clause",NULL};
|
||||
static const char* func_kinds_csharp[] = {"method_declaration","constructor_declaration",NULL};
|
||||
static const char* func_kinds_generic[] = {"function_declaration","function_definition","method_declaration","method_definition",NULL};
|
||||
|
||||
static const char** func_kinds_for_lang(CBMLanguage lang) {
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO: return func_kinds_go;
|
||||
case CBM_LANG_PYTHON: return func_kinds_python;
|
||||
case CBM_LANG_JAVASCRIPT:
|
||||
case CBM_LANG_TYPESCRIPT:
|
||||
case CBM_LANG_TSX: return func_kinds_js;
|
||||
case CBM_LANG_RUST: return func_kinds_rust;
|
||||
case CBM_LANG_JAVA: return func_kinds_java;
|
||||
case CBM_LANG_CPP:
|
||||
case CBM_LANG_C: return func_kinds_cpp;
|
||||
case CBM_LANG_RUBY: return func_kinds_ruby;
|
||||
case CBM_LANG_PHP: return func_kinds_php;
|
||||
case CBM_LANG_LUA: return func_kinds_lua;
|
||||
case CBM_LANG_SCALA: return func_kinds_scala;
|
||||
case CBM_LANG_KOTLIN: return func_kinds_kotlin;
|
||||
case CBM_LANG_ELIXIR: return func_kinds_elixir;
|
||||
case CBM_LANG_HASKELL: return func_kinds_haskell;
|
||||
case CBM_LANG_OCAML: return func_kinds_ocaml;
|
||||
case CBM_LANG_ZIG: return func_kinds_zig;
|
||||
case CBM_LANG_BASH: return func_kinds_bash;
|
||||
case CBM_LANG_ERLANG: return func_kinds_erlang;
|
||||
case CBM_LANG_CSHARP: return func_kinds_csharp;
|
||||
default: return func_kinds_generic;
|
||||
}
|
||||
}
|
||||
|
||||
TSNode cbm_find_enclosing_func(TSNode node, CBMLanguage lang) {
|
||||
const char** kinds = func_kinds_for_lang(lang);
|
||||
TSNode cur = node;
|
||||
for (;;) {
|
||||
TSNode parent = ts_node_parent(cur);
|
||||
if (ts_node_is_null(parent)) break;
|
||||
const char* pk = ts_node_type(parent);
|
||||
for (const char** k = kinds; *k; k++) {
|
||||
if (strcmp(pk, *k) == 0) return parent;
|
||||
}
|
||||
cur = parent;
|
||||
}
|
||||
TSNode null_node = {0};
|
||||
return null_node;
|
||||
}
|
||||
|
||||
// Get the name of a function node (basic: try "name" field)
|
||||
static const char* func_node_name(CBMArena* a, TSNode func_node, const char* source, CBMLanguage lang) {
|
||||
TSNode name_node = ts_node_child_by_field_name(func_node, "name", 4);
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
return cbm_node_text(a, name_node, source);
|
||||
}
|
||||
// Arrow functions: check parent variable_declarator
|
||||
if (strcmp(ts_node_type(func_node), "arrow_function") == 0) {
|
||||
TSNode parent = ts_node_parent(func_node);
|
||||
if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "variable_declarator") == 0) {
|
||||
TSNode vname = ts_node_child_by_field_name(parent, "name", 4);
|
||||
if (!ts_node_is_null(vname)) {
|
||||
return cbm_node_text(a, vname, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* cbm_enclosing_func_qn(CBMArena* a, TSNode node, CBMLanguage lang,
|
||||
const char* source, const char* project,
|
||||
const char* rel_path, const char* module_qn) {
|
||||
TSNode func_node = cbm_find_enclosing_func(node, lang);
|
||||
if (ts_node_is_null(func_node)) {
|
||||
return module_qn;
|
||||
}
|
||||
const char* name = func_node_name(a, func_node, source, lang);
|
||||
if (!name || !name[0]) {
|
||||
return module_qn;
|
||||
}
|
||||
|
||||
// Check if the function is inside a class — compute classQN.funcName
|
||||
const CBMLangSpec* spec = cbm_lang_spec(lang);
|
||||
if (spec && spec->class_node_types) {
|
||||
TSNode cur = ts_node_parent(func_node);
|
||||
while (!ts_node_is_null(cur)) {
|
||||
if (cbm_kind_in_set(cur, spec->class_node_types)) {
|
||||
TSNode class_name = ts_node_child_by_field_name(cur, "name", 4);
|
||||
if (!ts_node_is_null(class_name)) {
|
||||
char* cname = cbm_node_text(a, class_name, source);
|
||||
if (cname && cname[0]) {
|
||||
const char* class_qn = cbm_fqn_compute(a, project, rel_path, cname);
|
||||
return cbm_arena_sprintf(a, "%s.%s", class_qn, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
cur = ts_node_parent(cur);
|
||||
}
|
||||
}
|
||||
|
||||
return cbm_fqn_compute(a, project, rel_path, name);
|
||||
}
|
||||
|
||||
// --- Cached enclosing function QN ---
|
||||
|
||||
const char* cbm_enclosing_func_qn_cached(CBMExtractCtx* ctx, TSNode node) {
|
||||
uint32_t pos = ts_node_start_byte(node);
|
||||
|
||||
// Check cache: find a function range that contains this position.
|
||||
// Linear scan is fine for EFC_SIZE=64 (all entries fit in ~1 cache line).
|
||||
for (int i = 0; i < ctx->ef_cache.count; i++) {
|
||||
EFCEntry* e = &ctx->ef_cache.entries[i];
|
||||
if (pos >= e->start_byte && pos < e->end_byte) {
|
||||
return e->qn;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache miss: compute via parent walk
|
||||
const char* qn = cbm_enclosing_func_qn(
|
||||
ctx->arena, node, ctx->language, ctx->source,
|
||||
ctx->project, ctx->rel_path, ctx->module_qn);
|
||||
|
||||
// Cache the result: find the enclosing function's byte range
|
||||
TSNode func_node = cbm_find_enclosing_func(node, ctx->language);
|
||||
if (!ts_node_is_null(func_node) && ctx->ef_cache.count < EFC_SIZE) {
|
||||
EFCEntry* e = &ctx->ef_cache.entries[ctx->ef_cache.count++];
|
||||
e->start_byte = ts_node_start_byte(func_node);
|
||||
e->end_byte = ts_node_end_byte(func_node);
|
||||
e->qn = qn;
|
||||
}
|
||||
|
||||
return qn;
|
||||
}
|
||||
|
||||
// --- Module-level detection ---
|
||||
|
||||
// Module-level parent kind tables
|
||||
static const char* module_parents_go[] = {"source_file",NULL};
|
||||
static const char* module_parents_rust[] = {"source_file","mod_item",NULL};
|
||||
static const char* module_parents_java[] = {"program","class_body",NULL};
|
||||
static const char* module_parents_kotlin[] = {"source_file","class_body",NULL};
|
||||
static const char* module_parents_scala[] = {"compilation_unit","template_body",NULL};
|
||||
static const char* module_parents_csharp[] = {"compilation_unit","class_declaration","namespace_declaration",NULL};
|
||||
static const char* module_parents_php[] = {"program",NULL};
|
||||
static const char* module_parents_ruby[] = {"program","class","module",NULL};
|
||||
static const char* module_parents_c[] = {"translation_unit",NULL};
|
||||
static const char* module_parents_zig[] = {"source_file",NULL};
|
||||
static const char* module_parents_bash[] = {"program",NULL};
|
||||
static const char* module_parents_erlang[] = {"source","source_file",NULL};
|
||||
static const char* module_parents_haskell[] = {"declarations",NULL};
|
||||
static const char* module_parents_ocaml[] = {"compilation_unit",NULL};
|
||||
static const char* module_parents_elixir[] = {"source",NULL};
|
||||
static const char* module_parents_html[] = {"document",NULL};
|
||||
static const char* module_parents_css[] = {"stylesheet",NULL};
|
||||
static const char* module_parents_sql[] = {"source_file","program","statement",NULL};
|
||||
static const char* module_parents_toml[] = {"document",NULL};
|
||||
static const char* module_parents_hcl[] = {"config_file",NULL};
|
||||
|
||||
bool cbm_is_module_level(TSNode node, CBMLanguage lang) {
|
||||
TSNode parent = ts_node_parent(node);
|
||||
if (ts_node_is_null(parent)) return false;
|
||||
const char* pk = ts_node_type(parent);
|
||||
|
||||
// Python: module or expression_statement -> module
|
||||
if (lang == CBM_LANG_PYTHON) {
|
||||
if (strcmp(pk, "module") == 0) return true;
|
||||
if (strcmp(pk, "expression_statement") == 0) {
|
||||
TSNode gp = ts_node_parent(parent);
|
||||
return !ts_node_is_null(gp) && strcmp(ts_node_type(gp), "module") == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// JS/TS: program, or export_statement -> program
|
||||
if (lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX) {
|
||||
if (strcmp(pk, "program") == 0) return true;
|
||||
if (strcmp(pk, "export_statement") == 0) {
|
||||
TSNode gp = ts_node_parent(parent);
|
||||
return !ts_node_is_null(gp) && strcmp(ts_node_type(gp), "program") == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lua: chunk
|
||||
if (lang == CBM_LANG_LUA) {
|
||||
if (strcmp(pk, "chunk") == 0) return true;
|
||||
// assignment_statement -> chunk
|
||||
if (strcmp(pk, "assignment_statement") == 0) {
|
||||
TSNode gp = ts_node_parent(parent);
|
||||
return !ts_node_is_null(gp) && strcmp(ts_node_type(gp), "chunk") == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// YAML: document or stream
|
||||
if (lang == CBM_LANG_YAML) {
|
||||
return strcmp(pk, "document") == 0 || strcmp(pk, "stream") == 0 ||
|
||||
strcmp(pk, "block_mapping") == 0;
|
||||
}
|
||||
|
||||
// Table lookup for the rest
|
||||
const char** parents = NULL;
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO: parents = module_parents_go; break;
|
||||
case CBM_LANG_RUST: parents = module_parents_rust; break;
|
||||
case CBM_LANG_JAVA: parents = module_parents_java; break;
|
||||
case CBM_LANG_KOTLIN: parents = module_parents_kotlin; break;
|
||||
case CBM_LANG_SCALA: parents = module_parents_scala; break;
|
||||
case CBM_LANG_CSHARP: parents = module_parents_csharp; break;
|
||||
case CBM_LANG_PHP: parents = module_parents_php; break;
|
||||
case CBM_LANG_RUBY: parents = module_parents_ruby; break;
|
||||
case CBM_LANG_C:
|
||||
case CBM_LANG_CPP:
|
||||
case CBM_LANG_OBJC: parents = module_parents_c; break;
|
||||
case CBM_LANG_ZIG: parents = module_parents_zig; break;
|
||||
case CBM_LANG_BASH: parents = module_parents_bash; break;
|
||||
case CBM_LANG_ERLANG: parents = module_parents_erlang; break;
|
||||
case CBM_LANG_HASKELL: parents = module_parents_haskell; break;
|
||||
case CBM_LANG_OCAML: parents = module_parents_ocaml; break;
|
||||
case CBM_LANG_ELIXIR: parents = module_parents_elixir; break;
|
||||
case CBM_LANG_HTML: parents = module_parents_html; break;
|
||||
case CBM_LANG_CSS:
|
||||
case CBM_LANG_SCSS: parents = module_parents_css; break;
|
||||
case CBM_LANG_SQL: parents = module_parents_sql; break;
|
||||
case CBM_LANG_TOML: parents = module_parents_toml; break;
|
||||
case CBM_LANG_HCL: parents = module_parents_hcl; break;
|
||||
case CBM_LANG_SWIFT: parents = module_parents_zig; break; // source_file
|
||||
case CBM_LANG_DART: parents = module_parents_php; break; // program
|
||||
case CBM_LANG_PERL: parents = module_parents_zig; break; // source_file
|
||||
case CBM_LANG_GROOVY: parents = module_parents_zig; break; // source_file
|
||||
case CBM_LANG_R: parents = module_parents_php; break; // program
|
||||
default: return false;
|
||||
}
|
||||
if (parents) {
|
||||
for (const char** p = parents; *p; p++) {
|
||||
if (strcmp(pk, *p) == 0) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// --- FQN computation ---
|
||||
// Mirrors Go's fqn.Compute(): project + path_parts_dotted + name
|
||||
|
||||
// Internal helper: find extension start in basename (returns length without ext)
|
||||
static size_t strip_ext_len(const char* s, size_t len) {
|
||||
for (size_t i = len; i > 0; i--) {
|
||||
if (s[i-1] == '.') return i - 1;
|
||||
if (s[i-1] == '/') break;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
char* cbm_fqn_compute(CBMArena* a, const char* project, const char* rel_path, const char* name) {
|
||||
// Build: project.path1.path2.filename.name
|
||||
// where rel_path = "path1/path2/filename.ext"
|
||||
// Strip extension, replace / with ., drop __init__ and index
|
||||
size_t proj_len = strlen(project);
|
||||
size_t path_len = strlen(rel_path);
|
||||
size_t name_len = name ? strlen(name) : 0;
|
||||
|
||||
// Worst case: project + . + path (with / -> .) + . + name + NUL
|
||||
size_t max_len = proj_len + 1 + path_len + 1 + name_len + 1;
|
||||
char* buf = (char*)cbm_arena_alloc(a, max_len);
|
||||
if (!buf) return NULL;
|
||||
|
||||
char* out = buf;
|
||||
memcpy(out, project, proj_len);
|
||||
out += proj_len;
|
||||
|
||||
// Process path: strip extension, split by /, replace with dots
|
||||
// Skip trailing extension
|
||||
size_t plen = strip_ext_len(rel_path, path_len);
|
||||
|
||||
// Split by '/' and append each part as .part
|
||||
const char* start = rel_path;
|
||||
const char* end_ptr = rel_path + plen;
|
||||
while (start < end_ptr) {
|
||||
const char* slash = (const char*)memchr(start, '/', end_ptr - start);
|
||||
const char* part_end = slash ? slash : end_ptr;
|
||||
size_t part_len = (size_t)(part_end - start);
|
||||
|
||||
if (part_len > 0) {
|
||||
// Skip __init__ (Python) and index (JS/TS) if it's the last part
|
||||
bool is_last = (part_end == end_ptr);
|
||||
bool skip = false;
|
||||
if (is_last) {
|
||||
if (part_len == 8 && memcmp(start, "__init__", 8) == 0) skip = true;
|
||||
if (part_len == 5 && memcmp(start, "index", 5) == 0) skip = true;
|
||||
}
|
||||
if (!skip) {
|
||||
*out++ = '.';
|
||||
memcpy(out, start, part_len);
|
||||
out += part_len;
|
||||
}
|
||||
}
|
||||
start = part_end + 1;
|
||||
}
|
||||
|
||||
if (name && name_len > 0) {
|
||||
*out++ = '.';
|
||||
memcpy(out, name, name_len);
|
||||
out += name_len;
|
||||
}
|
||||
*out = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* cbm_fqn_module(CBMArena* a, const char* project, const char* rel_path) {
|
||||
return cbm_fqn_compute(a, project, rel_path, NULL);
|
||||
}
|
||||
|
||||
char* cbm_fqn_folder(CBMArena* a, const char* project, const char* rel_dir) {
|
||||
// project.dir1.dir2
|
||||
size_t proj_len = strlen(project);
|
||||
size_t dir_len = strlen(rel_dir);
|
||||
size_t max_len = proj_len + 1 + dir_len + 1;
|
||||
char* buf = (char*)cbm_arena_alloc(a, max_len);
|
||||
if (!buf) return NULL;
|
||||
|
||||
char* out = buf;
|
||||
memcpy(out, project, proj_len);
|
||||
out += proj_len;
|
||||
|
||||
if (dir_len > 0 && !(dir_len == 1 && rel_dir[0] == '.')) {
|
||||
const char* start = rel_dir;
|
||||
const char* end_ptr = rel_dir + dir_len;
|
||||
while (start < end_ptr) {
|
||||
const char* slash = (const char*)memchr(start, '/', end_ptr - start);
|
||||
const char* part_end = slash ? slash : end_ptr;
|
||||
size_t part_len = (size_t)(part_end - start);
|
||||
if (part_len > 0) {
|
||||
*out++ = '.';
|
||||
memcpy(out, start, part_len);
|
||||
out += part_len;
|
||||
}
|
||||
start = part_end + 1;
|
||||
}
|
||||
}
|
||||
*out = '\0';
|
||||
return buf;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef CBM_HELPERS_H
|
||||
#define CBM_HELPERS_H
|
||||
|
||||
#include "cbm.h"
|
||||
|
||||
// Extract text of a node from source. Returns arena-allocated string.
|
||||
char* cbm_node_text(CBMArena* a, TSNode node, const char* source);
|
||||
|
||||
// Check if a string is a language keyword (should be skipped as callee/usage).
|
||||
bool cbm_is_keyword(const char* name, CBMLanguage lang);
|
||||
|
||||
// Check if a name is exported per language convention.
|
||||
bool cbm_is_exported(const char* name, CBMLanguage lang);
|
||||
|
||||
// Check if a file is a test file based on path and language.
|
||||
bool cbm_is_test_file(const char* rel_path, CBMLanguage lang);
|
||||
|
||||
// Find the innermost enclosing function node by walking parent chain.
|
||||
// Returns a null node if none found.
|
||||
TSNode cbm_find_enclosing_func(TSNode node, CBMLanguage lang);
|
||||
|
||||
// Get the QN of an enclosing function, or module_qn if none.
|
||||
const char* cbm_enclosing_func_qn(CBMArena* a, TSNode node, CBMLanguage lang,
|
||||
const char* source, const char* project,
|
||||
const char* rel_path, const char* module_qn);
|
||||
|
||||
// Cached version: uses ctx->ef_cache to avoid repeated parent-chain walks.
|
||||
const char* cbm_enclosing_func_qn_cached(CBMExtractCtx* ctx, TSNode node);
|
||||
|
||||
// Find a child node by kind string.
|
||||
TSNode cbm_find_child_by_kind(TSNode parent, const char* kind);
|
||||
|
||||
// Check if node kind matches a set of types (NULL-terminated array of strings).
|
||||
bool cbm_kind_in_set(TSNode node, const char** types);
|
||||
|
||||
// Check if node has an ancestor of the given kind, within max_depth levels.
|
||||
bool cbm_has_ancestor_kind(TSNode node, const char* kind, int max_depth);
|
||||
|
||||
// Count nodes of given kinds in subtree (for complexity metric).
|
||||
int cbm_count_branching(TSNode node, const char** branching_types);
|
||||
|
||||
// Is this a module-level node? (not nested inside function/class body)
|
||||
bool cbm_is_module_level(TSNode node, CBMLanguage lang);
|
||||
|
||||
// --- FQN computation ---
|
||||
|
||||
// Compute qualified name: project.rel_path_parts.name
|
||||
char* cbm_fqn_compute(CBMArena* a, const char* project, const char* rel_path, const char* name);
|
||||
|
||||
// Module QN (file without name): project.rel_path_parts
|
||||
char* cbm_fqn_module(CBMArena* a, const char* project, const char* rel_path);
|
||||
|
||||
// Folder QN: project.dir_parts
|
||||
char* cbm_fqn_folder(CBMArena* a, const char* project, const char* rel_dir);
|
||||
|
||||
#endif // CBM_HELPERS_H
|
||||
@@ -0,0 +1,912 @@
|
||||
#include "lang_specs.h"
|
||||
|
||||
// -- Extern declarations for tree-sitter grammar functions --
|
||||
// These symbols are defined in the grammar C code compiled by Go tree-sitter modules.
|
||||
extern const TSLanguage* tree_sitter_go(void);
|
||||
extern const TSLanguage* tree_sitter_python(void);
|
||||
extern const TSLanguage* tree_sitter_javascript(void);
|
||||
extern const TSLanguage* tree_sitter_typescript(void);
|
||||
extern const TSLanguage* tree_sitter_tsx(void);
|
||||
extern const TSLanguage* tree_sitter_rust(void);
|
||||
extern const TSLanguage* tree_sitter_java(void);
|
||||
extern const TSLanguage* tree_sitter_cpp(void);
|
||||
extern const TSLanguage* tree_sitter_c_sharp(void);
|
||||
extern const TSLanguage* tree_sitter_php_only(void);
|
||||
extern const TSLanguage* tree_sitter_lua(void);
|
||||
extern const TSLanguage* tree_sitter_scala(void);
|
||||
extern const TSLanguage* tree_sitter_kotlin(void);
|
||||
extern const TSLanguage* tree_sitter_ruby(void);
|
||||
extern const TSLanguage* tree_sitter_c(void);
|
||||
extern const TSLanguage* tree_sitter_bash(void);
|
||||
extern const TSLanguage* tree_sitter_zig(void);
|
||||
extern const TSLanguage* tree_sitter_elixir(void);
|
||||
extern const TSLanguage* tree_sitter_haskell(void);
|
||||
extern const TSLanguage* tree_sitter_ocaml(void);
|
||||
extern const TSLanguage* tree_sitter_objc(void);
|
||||
extern const TSLanguage* tree_sitter_swift(void);
|
||||
extern const TSLanguage* tree_sitter_dart(void);
|
||||
extern const TSLanguage* tree_sitter_perl(void);
|
||||
extern const TSLanguage* tree_sitter_groovy(void);
|
||||
extern const TSLanguage* tree_sitter_erlang(void);
|
||||
extern const TSLanguage* tree_sitter_r(void);
|
||||
extern const TSLanguage* tree_sitter_html(void);
|
||||
extern const TSLanguage* tree_sitter_css(void);
|
||||
extern const TSLanguage* tree_sitter_scss(void);
|
||||
extern const TSLanguage* tree_sitter_yaml(void);
|
||||
extern const TSLanguage* tree_sitter_toml(void);
|
||||
extern const TSLanguage* tree_sitter_hcl(void);
|
||||
extern const TSLanguage* tree_sitter_sql(void);
|
||||
extern const TSLanguage* tree_sitter_dockerfile(void);
|
||||
// New languages (v0.5 expansion)
|
||||
extern const TSLanguage* tree_sitter_clojure(void);
|
||||
extern const TSLanguage* tree_sitter_fsharp(void);
|
||||
extern const TSLanguage* tree_sitter_julia(void);
|
||||
extern const TSLanguage* tree_sitter_vim(void);
|
||||
extern const TSLanguage* tree_sitter_nix(void);
|
||||
extern const TSLanguage* tree_sitter_commonlisp(void);
|
||||
extern const TSLanguage* tree_sitter_elm(void);
|
||||
extern const TSLanguage* tree_sitter_fortran(void);
|
||||
extern const TSLanguage* tree_sitter_cuda(void);
|
||||
extern const TSLanguage* tree_sitter_COBOL(void);
|
||||
extern const TSLanguage* tree_sitter_verilog(void);
|
||||
extern const TSLanguage* tree_sitter_elisp(void);
|
||||
extern const TSLanguage* tree_sitter_json(void);
|
||||
extern const TSLanguage* tree_sitter_xml(void);
|
||||
extern const TSLanguage* tree_sitter_markdown(void);
|
||||
extern const TSLanguage* tree_sitter_make(void);
|
||||
extern const TSLanguage* tree_sitter_cmake(void);
|
||||
extern const TSLanguage* tree_sitter_proto(void);
|
||||
extern const TSLanguage* tree_sitter_graphql(void);
|
||||
extern const TSLanguage* tree_sitter_vue(void);
|
||||
extern const TSLanguage* tree_sitter_svelte(void);
|
||||
extern const TSLanguage* tree_sitter_meson(void);
|
||||
extern const TSLanguage* tree_sitter_glsl(void);
|
||||
extern const TSLanguage* tree_sitter_ini(void);
|
||||
|
||||
// -- Empty sentinel --
|
||||
static const char* empty_types[] = {NULL};
|
||||
|
||||
// ==================== GO ====================
|
||||
static const char* go_func_types[] = {"function_declaration","method_declaration","method_elem",NULL};
|
||||
static const char* go_class_types[] = {"type_spec","type_alias",NULL};
|
||||
static const char* go_field_types[] = {"field_declaration",NULL};
|
||||
static const char* go_module_types[] = {"source_file",NULL};
|
||||
static const char* go_call_types[] = {"call_expression",NULL};
|
||||
static const char* go_import_types[] = {"import_declaration",NULL};
|
||||
static const char* go_branch_types[] = {"if_statement","for_statement","switch_expression","select_statement","case_clause","default_clause",NULL};
|
||||
static const char* go_var_types[] = {"var_declaration","const_declaration",NULL};
|
||||
static const char* go_assign_types[] = {"assignment_statement","short_var_declaration",NULL};
|
||||
|
||||
// ==================== PYTHON ====================
|
||||
static const char* py_func_types[] = {"function_definition",NULL};
|
||||
static const char* py_class_types[] = {"class_definition",NULL};
|
||||
static const char* py_module_types[] = {"module",NULL};
|
||||
static const char* py_call_types[] = {"call","with_statement",NULL};
|
||||
static const char* py_import_types[] = {"import_statement",NULL};
|
||||
static const char* py_import_from_types[] = {"import_from_statement",NULL};
|
||||
static const char* py_branch_types[] = {"if_statement","for_statement","while_statement","try_statement","except_clause","with_statement","elif_clause",NULL};
|
||||
static const char* py_var_types[] = {"assignment","augmented_assignment",NULL};
|
||||
static const char* py_throw_types[] = {"raise_statement",NULL};
|
||||
static const char* py_decorator_types[] = {"decorator",NULL};
|
||||
|
||||
// ==================== JAVASCRIPT ====================
|
||||
static const char* js_func_types[] = {"function_declaration","generator_function_declaration","function_expression","arrow_function","method_definition",NULL};
|
||||
static const char* js_class_types[] = {"class_declaration","class",NULL};
|
||||
static const char* js_module_types[] = {"program",NULL};
|
||||
static const char* js_call_types[] = {"call_expression",NULL};
|
||||
static const char* js_import_types[] = {"import_statement","lexical_declaration","export_statement",NULL};
|
||||
static const char* js_branch_types[] = {"if_statement","for_statement","for_in_statement","while_statement","switch_statement","case_clause","try_statement","catch_clause",NULL};
|
||||
static const char* js_var_types[] = {"lexical_declaration","variable_declaration",NULL};
|
||||
static const char* js_throw_types[] = {"throw_statement",NULL};
|
||||
|
||||
// ==================== TYPESCRIPT ====================
|
||||
static const char* ts_func_types[] = {"function_declaration","generator_function_declaration","function_expression","arrow_function","method_definition","function_signature",NULL};
|
||||
static const char* ts_class_types[] = {"class_declaration","class","abstract_class_declaration","enum_declaration","interface_declaration","type_alias_declaration","internal_module",NULL};
|
||||
static const char* ts_decorator_types[] = {"decorator",NULL};
|
||||
|
||||
// ==================== RUST ====================
|
||||
static const char* rust_func_types[] = {"function_item","function_signature_item","closure_expression",NULL};
|
||||
static const char* rust_class_types[] = {"struct_item","enum_item","union_item","trait_item","type_item",NULL};
|
||||
static const char* rust_field_types[] = {"field_declaration",NULL};
|
||||
static const char* rust_module_types[] = {"source_file","mod_item",NULL};
|
||||
static const char* rust_call_types[] = {"call_expression","macro_invocation",NULL};
|
||||
static const char* rust_import_types[] = {"use_declaration","extern_crate_declaration",NULL};
|
||||
static const char* rust_import_from_types[] = {"use_declaration",NULL};
|
||||
static const char* rust_branch_types[] = {"if_expression","for_expression","while_expression","loop_expression","match_expression","match_arm",NULL};
|
||||
static const char* rust_var_types[] = {"static_item","const_item",NULL};
|
||||
static const char* rust_assign_types[] = {"assignment_expression","compound_assignment_expr",NULL};
|
||||
static const char* rust_decorator_types[] = {"attribute_item",NULL};
|
||||
|
||||
// ==================== JAVA ====================
|
||||
static const char* java_func_types[] = {"method_declaration","constructor_declaration",NULL};
|
||||
static const char* java_class_types[] = {"class_declaration","interface_declaration","enum_declaration","annotation_type_declaration","record_declaration",NULL};
|
||||
static const char* java_field_types[] = {"field_declaration",NULL};
|
||||
static const char* java_module_types[] = {"program",NULL};
|
||||
static const char* java_call_types[] = {"method_invocation",NULL};
|
||||
static const char* java_import_types[] = {"import_declaration",NULL};
|
||||
static const char* java_branch_types[] = {"if_statement","for_statement","enhanced_for_statement","while_statement","switch_expression","switch_block_statement_group","try_statement","catch_clause",NULL};
|
||||
static const char* java_var_types[] = {"field_declaration","local_variable_declaration",NULL};
|
||||
static const char* java_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* java_throw_types[] = {"throw_statement",NULL};
|
||||
static const char* java_decorator_types[] = {"marker_annotation","annotation",NULL};
|
||||
|
||||
// ==================== C++ ====================
|
||||
static const char* cpp_func_types[] = {"function_definition","declaration","field_declaration","template_declaration","lambda_expression",NULL};
|
||||
static const char* cpp_class_types[] = {"class_specifier","struct_specifier","union_specifier","enum_specifier",NULL};
|
||||
static const char* cpp_field_types[] = {"field_declaration",NULL};
|
||||
static const char* cpp_module_types[] = {"translation_unit","namespace_definition","linkage_specification","declaration",NULL};
|
||||
static const char* cpp_call_types[] = {"call_expression","field_expression","subscript_expression","new_expression","delete_expression","binary_expression","unary_expression","update_expression",NULL};
|
||||
static const char* cpp_import_types[] = {"preproc_include","template_function","declaration",NULL};
|
||||
static const char* cpp_branch_types[] = {"if_statement","for_statement","for_range_loop","while_statement","switch_statement","case_statement","try_statement","catch_clause",NULL};
|
||||
static const char* cpp_var_types[] = {"declaration",NULL};
|
||||
static const char* cpp_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* cpp_throw_types[] = {"throw_statement",NULL};
|
||||
|
||||
// ==================== C# ====================
|
||||
static const char* cs_func_types[] = {"destructor_declaration","local_function_statement","function_pointer_type","constructor_declaration","anonymous_method_expression","lambda_expression","method_declaration",NULL};
|
||||
static const char* cs_class_types[] = {"class_declaration","struct_declaration","enum_declaration","interface_declaration",NULL};
|
||||
static const char* cs_module_types[] = {"compilation_unit",NULL};
|
||||
static const char* cs_call_types[] = {"invocation_expression",NULL};
|
||||
static const char* cs_import_types[] = {"using_directive",NULL};
|
||||
static const char* cs_branch_types[] = {"if_statement","for_statement","foreach_statement","while_statement","switch_statement","case_switch_label","try_statement","catch_clause",NULL};
|
||||
static const char* cs_var_types[] = {"field_declaration","local_declaration_statement",NULL};
|
||||
static const char* cs_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* cs_throw_types[] = {"throw_statement","throw_expression",NULL};
|
||||
static const char* cs_decorator_types[] = {"attribute",NULL};
|
||||
|
||||
// ==================== PHP ====================
|
||||
static const char* php_func_types[] = {"function_static_declaration","anonymous_function","function_definition","arrow_function","method_declaration",NULL};
|
||||
static const char* php_class_types[] = {"trait_declaration","enum_declaration","interface_declaration","class_declaration",NULL};
|
||||
static const char* php_module_types[] = {"program",NULL};
|
||||
static const char* php_call_types[] = {"member_call_expression","scoped_call_expression","function_call_expression","nullsafe_member_call_expression",NULL};
|
||||
static const char* php_branch_types[] = {"if_statement","for_statement","foreach_statement","while_statement","switch_statement","case_statement","try_statement","catch_clause",NULL};
|
||||
static const char* php_var_types[] = {"expression_statement",NULL};
|
||||
static const char* php_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* php_throw_types[] = {"throw_expression",NULL};
|
||||
static const char* php_decorator_types[] = {"attribute_group",NULL};
|
||||
|
||||
// ==================== LUA ====================
|
||||
static const char* lua_func_types[] = {"function_declaration","function_definition",NULL};
|
||||
static const char* lua_module_types[] = {"chunk",NULL};
|
||||
static const char* lua_call_types[] = {"function_call",NULL};
|
||||
static const char* lua_import_types[] = {"function_call",NULL};
|
||||
static const char* lua_branch_types[] = {"if_statement","for_statement","for_in_statement","while_statement","repeat_statement",NULL};
|
||||
static const char* lua_var_types[] = {"variable_declaration",NULL};
|
||||
static const char* lua_assign_types[] = {"assignment_statement",NULL};
|
||||
|
||||
// ==================== SCALA ====================
|
||||
static const char* scala_func_types[] = {"function_definition","function_declaration",NULL};
|
||||
static const char* scala_class_types[] = {"class_definition","object_definition","trait_definition",NULL};
|
||||
static const char* scala_module_types[] = {"compilation_unit",NULL};
|
||||
static const char* scala_call_types[] = {"call_expression","generic_function","field_expression","infix_expression",NULL};
|
||||
static const char* scala_import_types[] = {"import_declaration",NULL};
|
||||
static const char* scala_branch_types[] = {"if_expression","for_expression","while_expression","match_expression","case_clause","try_expression","catch_clause",NULL};
|
||||
static const char* scala_var_types[] = {"val_definition","var_definition","val_declaration","var_declaration",NULL};
|
||||
static const char* scala_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* scala_throw_types[] = {"throw_expression",NULL};
|
||||
|
||||
// ==================== KOTLIN ====================
|
||||
static const char* kotlin_func_types[] = {"function_declaration","secondary_constructor","anonymous_function",NULL};
|
||||
static const char* kotlin_class_types[] = {"class_declaration","object_declaration","companion_object",NULL};
|
||||
static const char* kotlin_module_types[] = {"source_file",NULL};
|
||||
static const char* kotlin_call_types[] = {"call_expression","navigation_expression",NULL};
|
||||
static const char* kotlin_import_types[] = {"import",NULL};
|
||||
static const char* kotlin_branch_types[] = {"if_expression","for_statement","while_statement","when_expression","when_entry","try_expression","catch_block",NULL};
|
||||
static const char* kotlin_var_types[] = {"property_declaration",NULL};
|
||||
static const char* kotlin_assign_types[] = {"assignment","directly_assignable_expression",NULL};
|
||||
static const char* kotlin_throw_types[] = {"throw_expression",NULL};
|
||||
static const char* kotlin_decorator_types[] = {"annotation",NULL};
|
||||
|
||||
// ==================== RUBY ====================
|
||||
static const char* ruby_func_types[] = {"method","singleton_method",NULL};
|
||||
static const char* ruby_class_types[] = {"class","module",NULL};
|
||||
static const char* ruby_module_types[] = {"program",NULL};
|
||||
static const char* ruby_call_types[] = {"call","command_call",NULL};
|
||||
static const char* ruby_import_types[] = {"call",NULL};
|
||||
static const char* ruby_branch_types[] = {"if","unless","while","until","for","case","when","rescue","elsif",NULL};
|
||||
static const char* ruby_var_types[] = {"assignment",NULL};
|
||||
static const char* ruby_assign_types[] = {"assignment","operator_assignment",NULL};
|
||||
|
||||
// ==================== C ====================
|
||||
static const char* c_func_types[] = {"function_definition",NULL};
|
||||
static const char* c_class_types[] = {"struct_specifier","enum_specifier","union_specifier",NULL};
|
||||
static const char* c_field_types[] = {"field_declaration",NULL};
|
||||
static const char* c_module_types[] = {"translation_unit",NULL};
|
||||
static const char* c_call_types[] = {"call_expression",NULL};
|
||||
static const char* c_import_types[] = {"preproc_include",NULL};
|
||||
static const char* c_branch_types[] = {"if_statement","for_statement","while_statement","do_statement","switch_statement","case_statement",NULL};
|
||||
static const char* c_var_types[] = {"declaration",NULL};
|
||||
static const char* c_assign_types[] = {"assignment_expression",NULL};
|
||||
|
||||
// ==================== BASH ====================
|
||||
static const char* bash_func_types[] = {"function_definition",NULL};
|
||||
static const char* bash_module_types[] = {"program",NULL};
|
||||
static const char* bash_call_types[] = {"command",NULL};
|
||||
static const char* bash_import_types[] = {"command",NULL};
|
||||
static const char* bash_branch_types[] = {"if_statement","while_statement","for_statement","case_statement","elif_clause",NULL};
|
||||
static const char* bash_var_types[] = {"variable_assignment",NULL};
|
||||
|
||||
// ==================== ZIG ====================
|
||||
static const char* zig_func_types[] = {"function_declaration","test_declaration",NULL};
|
||||
static const char* zig_class_types[] = {"struct_declaration","enum_declaration","union_declaration",NULL};
|
||||
static const char* zig_field_types[] = {"container_field",NULL};
|
||||
static const char* zig_module_types[] = {"source_file",NULL};
|
||||
static const char* zig_call_types[] = {"call_expression","builtin_function",NULL};
|
||||
static const char* zig_import_types[] = {"builtin_function",NULL};
|
||||
static const char* zig_branch_types[] = {"if_statement","for_statement","while_statement","switch_expression",NULL};
|
||||
static const char* zig_var_types[] = {"variable_declaration",NULL};
|
||||
static const char* zig_assign_types[] = {"assignment_expression",NULL};
|
||||
|
||||
// ==================== ELIXIR ====================
|
||||
static const char* elixir_func_types[] = {"call",NULL};
|
||||
static const char* elixir_module_types[] = {"source",NULL};
|
||||
static const char* elixir_call_types[] = {"call","dot","binary_operator",NULL};
|
||||
static const char* elixir_import_types[] = {"call",NULL};
|
||||
static const char* elixir_branch_types[] = {"call",NULL};
|
||||
static const char* elixir_var_types[] = {"binary_operator",NULL};
|
||||
|
||||
// ==================== HASKELL ====================
|
||||
static const char* haskell_func_types[] = {"function","signature",NULL};
|
||||
static const char* haskell_class_types[] = {"class","data_type","newtype",NULL};
|
||||
static const char* haskell_module_types[] = {"haskell",NULL};
|
||||
static const char* haskell_call_types[] = {"infix","apply",NULL};
|
||||
static const char* haskell_import_types[] = {"import",NULL};
|
||||
static const char* haskell_branch_types[] = {"match","guards","if","case","do","boolean",NULL};
|
||||
static const char* haskell_var_types[] = {"function",NULL};
|
||||
|
||||
// ==================== OCAML ====================
|
||||
static const char* ocaml_func_types[] = {"value_definition",NULL};
|
||||
static const char* ocaml_class_types[] = {"type_definition","class_definition","module_definition",NULL};
|
||||
static const char* ocaml_module_types[] = {"compilation_unit",NULL};
|
||||
static const char* ocaml_call_types[] = {"application_expression","infix_expression",NULL};
|
||||
static const char* ocaml_import_types[] = {"open_module",NULL};
|
||||
static const char* ocaml_branch_types[] = {"match_expression","if_expression","match_case",NULL};
|
||||
static const char* ocaml_var_types[] = {"value_definition",NULL};
|
||||
|
||||
// ==================== OBJECTIVE-C ====================
|
||||
static const char* objc_func_types[] = {"function_definition","method_definition",NULL};
|
||||
static const char* objc_class_types[] = {"class_interface","class_implementation","protocol_declaration",NULL};
|
||||
static const char* objc_field_types[] = {"property_declaration",NULL};
|
||||
static const char* objc_module_types[] = {"translation_unit",NULL};
|
||||
static const char* objc_call_types[] = {"call_expression","message_expression",NULL};
|
||||
static const char* objc_import_types[] = {"preproc_import",NULL};
|
||||
static const char* objc_branch_types[] = {"if_statement","for_statement","while_statement","switch_statement",NULL};
|
||||
static const char* objc_var_types[] = {"declaration",NULL};
|
||||
static const char* objc_assign_types[] = {"assignment_expression",NULL};
|
||||
|
||||
// ==================== SWIFT ====================
|
||||
static const char* swift_func_types[] = {"function_declaration",NULL};
|
||||
static const char* swift_class_types[] = {"class_declaration","protocol_declaration","struct_declaration","enum_declaration",NULL};
|
||||
static const char* swift_field_types[] = {"property_declaration",NULL};
|
||||
static const char* swift_module_types[] = {"source_file",NULL};
|
||||
static const char* swift_call_types[] = {"call_expression",NULL};
|
||||
static const char* swift_import_types[] = {"import_declaration",NULL};
|
||||
static const char* swift_branch_types[] = {"if_statement","guard_statement","for_statement","while_statement","switch_statement",NULL};
|
||||
static const char* swift_var_types[] = {"property_declaration",NULL};
|
||||
static const char* swift_assign_types[] = {"assignment",NULL};
|
||||
static const char* swift_throw_types[] = {"throw_statement",NULL};
|
||||
static const char* swift_decorator_types[] = {"attribute",NULL};
|
||||
|
||||
// ==================== DART ====================
|
||||
static const char* dart_func_types[] = {"function_signature","method_signature",NULL};
|
||||
static const char* dart_class_types[] = {"class_definition","enum_declaration","mixin_declaration",NULL};
|
||||
static const char* dart_field_types[] = {"declaration",NULL};
|
||||
static const char* dart_module_types[] = {"program",NULL};
|
||||
static const char* dart_call_types[] = {"selector",NULL};
|
||||
static const char* dart_import_types[] = {"import_or_export",NULL};
|
||||
static const char* dart_branch_types[] = {"if_statement","for_statement","while_statement","switch_statement",NULL};
|
||||
static const char* dart_var_types[] = {"declaration",NULL};
|
||||
static const char* dart_assign_types[] = {"assignment_expression",NULL};
|
||||
static const char* dart_throw_types[] = {"throw_expression",NULL};
|
||||
static const char* dart_decorator_types[] = {"annotation",NULL};
|
||||
|
||||
// ==================== PERL ====================
|
||||
static const char* perl_func_types[] = {"subroutine_declaration_statement",NULL};
|
||||
static const char* perl_module_types[] = {"source_file",NULL};
|
||||
static const char* perl_call_types[] = {"ambiguous_function_call_expression","function_call_expression","func1op_call_expression",NULL};
|
||||
static const char* perl_import_types[] = {"use_statement","require_statement",NULL};
|
||||
static const char* perl_branch_types[] = {"if_statement","unless_statement","for_statement","foreach_statement","while_statement",NULL};
|
||||
static const char* perl_var_types[] = {"variable_declaration","expression_statement",NULL};
|
||||
static const char* perl_assign_types[] = {"assignment_expression",NULL};
|
||||
|
||||
// ==================== GROOVY ====================
|
||||
static const char* groovy_func_types[] = {"function_definition",NULL};
|
||||
static const char* groovy_class_types[] = {"class_definition",NULL};
|
||||
static const char* groovy_module_types[] = {"source_file",NULL};
|
||||
static const char* groovy_call_types[] = {"function_call","juxt_function_call",NULL};
|
||||
static const char* groovy_import_types[] = {"groovy_import",NULL};
|
||||
static const char* groovy_branch_types[] = {"if_statement","for_statement","while_statement","switch_statement",NULL};
|
||||
static const char* groovy_var_types[] = {"declaration",NULL};
|
||||
static const char* groovy_assign_types[] = {"assignment",NULL};
|
||||
static const char* groovy_throw_types[] = {"throw_statement",NULL};
|
||||
static const char* groovy_decorator_types[] = {"annotation",NULL};
|
||||
|
||||
// ==================== ERLANG ====================
|
||||
static const char* erlang_func_types[] = {"function_clause",NULL};
|
||||
static const char* erlang_module_types[] = {"source_file",NULL};
|
||||
static const char* erlang_call_types[] = {"call",NULL};
|
||||
static const char* erlang_import_types[] = {"module_attribute",NULL};
|
||||
static const char* erlang_branch_types[] = {"if_expression","case_expression","receive_expression",NULL};
|
||||
static const char* erlang_var_types[] = {"pp_define","record_decl",NULL};
|
||||
static const char* erlang_assign_types[] = {"match_expression",NULL};
|
||||
static const char* erlang_throw_types[] = {"call",NULL};
|
||||
|
||||
// ==================== R ====================
|
||||
static const char* r_func_types[] = {"function_definition",NULL};
|
||||
static const char* r_module_types[] = {"program",NULL};
|
||||
static const char* r_call_types[] = {"call",NULL};
|
||||
static const char* r_import_types[] = {"call",NULL};
|
||||
static const char* r_branch_types[] = {"if_statement","for_statement","while_statement",NULL};
|
||||
static const char* r_var_types[] = {"binary_operator",NULL};
|
||||
|
||||
// ==================== HTML ====================
|
||||
static const char* html_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== CSS ====================
|
||||
static const char* css_module_types[] = {"stylesheet",NULL};
|
||||
static const char* css_import_types[] = {"import_statement",NULL};
|
||||
|
||||
// ==================== SCSS ====================
|
||||
static const char* scss_func_types[] = {"mixin_statement","function_statement",NULL};
|
||||
static const char* scss_module_types[] = {"stylesheet",NULL};
|
||||
static const char* scss_call_types[] = {"call_expression",NULL};
|
||||
static const char* scss_import_types[] = {"import_statement","use_statement",NULL};
|
||||
static const char* scss_branch_types[] = {"if_statement",NULL};
|
||||
static const char* scss_var_types[] = {"declaration",NULL};
|
||||
|
||||
// ==================== YAML ====================
|
||||
static const char* yaml_module_types[] = {"stream",NULL};
|
||||
static const char* yaml_var_types[] = {"block_mapping_pair",NULL};
|
||||
|
||||
// ==================== TOML ====================
|
||||
static const char* toml_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== HCL ====================
|
||||
static const char* hcl_class_types[] = {"block",NULL};
|
||||
static const char* hcl_module_types[] = {"config_file",NULL};
|
||||
static const char* hcl_call_types[] = {"function_call",NULL};
|
||||
static const char* hcl_var_types[] = {"attribute",NULL};
|
||||
|
||||
// ==================== SQL ====================
|
||||
static const char* sql_func_types[] = {"create_function",NULL};
|
||||
static const char* sql_field_types[] = {"column_definition",NULL};
|
||||
static const char* sql_module_types[] = {"program",NULL};
|
||||
static const char* sql_call_types[] = {"function_call","invocation",NULL};
|
||||
static const char* sql_branch_types[] = {"if_statement","case_expression",NULL};
|
||||
static const char* sql_var_types[] = {"create_table","create_view",NULL};
|
||||
|
||||
// ==================== DOCKERFILE ====================
|
||||
static const char* dockerfile_module_types[] = {"source_file",NULL};
|
||||
static const char* dockerfile_var_types[] = {"env_instruction","arg_instruction",NULL};
|
||||
|
||||
// ==================== ENV ACCESS ====================
|
||||
static const char* go_env_funcs[] = {"os.Getenv","os.LookupEnv",NULL};
|
||||
static const char* py_env_funcs[] = {"os.getenv","os.environ.get",NULL};
|
||||
static const char* py_env_members[] = {"os.environ",NULL};
|
||||
static const char* js_env_members[] = {"process.env",NULL};
|
||||
static const char* ts_env_members[] = {"process.env",NULL};
|
||||
static const char* rust_env_funcs[] = {"env::var","std::env::var",NULL};
|
||||
static const char* java_env_funcs[] = {"System.getenv","System.getProperty",NULL};
|
||||
static const char* cpp_env_funcs[] = {"getenv","std::getenv",NULL};
|
||||
static const char* cs_env_funcs[] = {"Environment.GetEnvironmentVariable",NULL};
|
||||
static const char* php_env_funcs[] = {"getenv","env",NULL};
|
||||
static const char* lua_env_funcs[] = {"os.getenv",NULL};
|
||||
static const char* scala_env_funcs[] = {"sys.env","System.getenv","System.getProperty",NULL};
|
||||
static const char* kotlin_env_funcs[] = {"System.getenv","System.getProperty",NULL};
|
||||
static const char* ruby_env_members[] = {"ENV",NULL};
|
||||
static const char* c_env_funcs[] = {"getenv",NULL};
|
||||
static const char* zig_env_funcs[] = {"std.os.getenv",NULL};
|
||||
static const char* elixir_env_funcs[] = {"System.get_env",NULL};
|
||||
static const char* haskell_env_funcs[] = {"lookupEnv","getEnv",NULL};
|
||||
static const char* ocaml_env_funcs[] = {"Sys.getenv",NULL};
|
||||
static const char* r_env_funcs[] = {"Sys.getenv",NULL};
|
||||
static const char* perl_env_funcs[] = {"$ENV",NULL};
|
||||
|
||||
// ==================== CLOJURE ====================
|
||||
static const char* clojure_module_types[] = {"source",NULL};
|
||||
static const char* clojure_call_types[] = {"list_lit",NULL};
|
||||
|
||||
// ==================== F# ====================
|
||||
static const char* fsharp_func_types[] = {"function_declaration","value_declaration",NULL};
|
||||
static const char* fsharp_class_types[] = {"type_definition","exception_definition",NULL};
|
||||
static const char* fsharp_module_types[] = {"file",NULL};
|
||||
static const char* fsharp_call_types[] = {"application_expression","dot_expression",NULL};
|
||||
static const char* fsharp_import_types[] = {"import_decl","open_expression",NULL};
|
||||
static const char* fsharp_branch_types[] = {"if_expression","for_expression","while_expression","match_expression","elif_expression",NULL};
|
||||
static const char* fsharp_var_types[] = {"value_declaration",NULL};
|
||||
|
||||
// ==================== JULIA ====================
|
||||
static const char* julia_func_types[] = {"function_definition","short_function_definition",NULL};
|
||||
static const char* julia_class_types[] = {"struct_definition","abstract_definition",NULL};
|
||||
static const char* julia_module_types[] = {"source_file",NULL};
|
||||
static const char* julia_call_types[] = {"call_expression","broadcast_call_expression",NULL};
|
||||
static const char* julia_import_types[] = {"import_statement","using_statement",NULL};
|
||||
static const char* julia_branch_types[] = {"if_statement","for_statement","while_statement","try_statement",NULL};
|
||||
static const char* julia_var_types[] = {"const_statement","assignment",NULL};
|
||||
static const char* julia_assign_types[] = {"assignment","compound_assignment_expression",NULL};
|
||||
static const char* julia_throw_types[] = {"throw_statement",NULL};
|
||||
|
||||
// ==================== VIM SCRIPT ====================
|
||||
static const char* vim_func_types[] = {"function_definition",NULL};
|
||||
static const char* vim_module_types[] = {"script_file",NULL};
|
||||
static const char* vim_call_types[] = {"call_expression",NULL};
|
||||
static const char* vim_branch_types[] = {"if_statement","for_statement","while_statement","try_statement",NULL};
|
||||
static const char* vim_var_types[] = {"let_statement",NULL};
|
||||
|
||||
// ==================== NIX ====================
|
||||
static const char* nix_func_types[] = {"function_expression",NULL};
|
||||
static const char* nix_module_types[] = {"source_expression",NULL};
|
||||
static const char* nix_call_types[] = {"apply_expression",NULL};
|
||||
static const char* nix_branch_types[] = {"if_expression",NULL};
|
||||
static const char* nix_var_types[] = {"binding",NULL};
|
||||
|
||||
// ==================== COMMON LISP ====================
|
||||
static const char* commonlisp_module_types[] = {"source",NULL};
|
||||
static const char* commonlisp_call_types[] = {"list_lit",NULL};
|
||||
|
||||
// ==================== ELM ====================
|
||||
static const char* elm_func_types[] = {"value_declaration","function_declaration",NULL};
|
||||
static const char* elm_class_types[] = {"type_declaration","type_alias_declaration",NULL};
|
||||
static const char* elm_module_types[] = {"file",NULL};
|
||||
static const char* elm_call_types[] = {"function_call",NULL};
|
||||
static const char* elm_import_types[] = {"import",NULL};
|
||||
static const char* elm_branch_types[] = {"case_of_expr","if_else_expr",NULL};
|
||||
|
||||
// ==================== FORTRAN ====================
|
||||
static const char* fortran_func_types[] = {"function","subroutine",NULL};
|
||||
static const char* fortran_class_types[] = {"derived_type_definition",NULL};
|
||||
static const char* fortran_module_types[] = {"translation_unit",NULL};
|
||||
static const char* fortran_call_types[] = {"call_expression","keyword_argument",NULL};
|
||||
static const char* fortran_import_types[] = {"use_statement","include_statement",NULL};
|
||||
static const char* fortran_branch_types[] = {"if_statement","do_loop_statement","where_statement","select_case_statement",NULL};
|
||||
static const char* fortran_var_types[] = {"variable_declaration",NULL};
|
||||
static const char* fortran_assign_types[] = {"assignment_statement",NULL};
|
||||
|
||||
// ==================== CUDA ====================
|
||||
// CUDA extends C++, reuse cpp types (same grammar family)
|
||||
|
||||
// ==================== COBOL ====================
|
||||
static const char* cobol_func_types[] = {"program_definition",NULL};
|
||||
static const char* cobol_module_types[] = {"source_file",NULL};
|
||||
static const char* cobol_call_types[] = {"call_statement",NULL};
|
||||
static const char* cobol_branch_types[] = {"if_statement","evaluate_statement","perform_statement",NULL};
|
||||
static const char* cobol_var_types[] = {"data_description_entry",NULL};
|
||||
|
||||
// ==================== VERILOG ====================
|
||||
static const char* verilog_func_types[] = {"function_declaration","task_declaration",NULL};
|
||||
static const char* verilog_class_types[] = {"module_declaration","class_declaration","interface_declaration",NULL};
|
||||
static const char* verilog_module_types[] = {"source_file",NULL};
|
||||
static const char* verilog_call_types[] = {"system_tf_call","subroutine_call",NULL};
|
||||
static const char* verilog_branch_types[] = {"conditional_statement","case_statement","loop_statement",NULL};
|
||||
static const char* verilog_var_types[] = {"net_declaration","data_declaration",NULL};
|
||||
static const char* verilog_assign_types[] = {"blocking_assignment","nonblocking_assignment",NULL};
|
||||
|
||||
// ==================== EMACS LISP ====================
|
||||
static const char* elisp_func_types[] = {"function_definition","macro_definition",NULL};
|
||||
static const char* elisp_module_types[] = {"source_file",NULL};
|
||||
static const char* elisp_call_types[] = {"list",NULL};
|
||||
|
||||
// ==================== JSON ====================
|
||||
static const char* json_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== XML ====================
|
||||
static const char* xml_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== MARKDOWN ====================
|
||||
static const char* markdown_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== MAKEFILE ====================
|
||||
static const char* makefile_module_types[] = {"makefile",NULL};
|
||||
static const char* makefile_call_types[] = {"function_call",NULL};
|
||||
static const char* makefile_import_types[] = {"include_directive",NULL};
|
||||
static const char* makefile_var_types[] = {"variable_assignment",NULL};
|
||||
|
||||
// ==================== CMAKE ====================
|
||||
static const char* cmake_module_types[] = {"source_file",NULL};
|
||||
static const char* cmake_call_types[] = {"normal_command",NULL};
|
||||
|
||||
// ==================== PROTOBUF ====================
|
||||
static const char* protobuf_class_types[] = {"message","enum",NULL};
|
||||
static const char* protobuf_module_types[] = {"source_file",NULL};
|
||||
static const char* protobuf_field_types[] = {"field","map_field","oneof_field",NULL};
|
||||
static const char* protobuf_import_types[] = {"import",NULL};
|
||||
|
||||
// ==================== GRAPHQL ====================
|
||||
static const char* graphql_class_types[] = {"object_type_definition","input_object_type_definition","enum_type_definition","interface_type_definition","union_type_definition","scalar_type_definition",NULL};
|
||||
static const char* graphql_module_types[] = {"document",NULL};
|
||||
static const char* graphql_field_types[] = {"field_definition","input_value_definition",NULL};
|
||||
|
||||
// ==================== VUE ====================
|
||||
static const char* vue_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== SVELTE ====================
|
||||
static const char* svelte_module_types[] = {"document",NULL};
|
||||
static const char* svelte_branch_types[] = {"if_statement","each_statement","await_statement",NULL};
|
||||
|
||||
// ==================== MESON ====================
|
||||
static const char* meson_func_types[] = {"function_expression",NULL};
|
||||
static const char* meson_module_types[] = {"source_file",NULL};
|
||||
static const char* meson_call_types[] = {"function_expression",NULL};
|
||||
static const char* meson_branch_types[] = {"if_statement","foreach_statement",NULL};
|
||||
static const char* meson_var_types[] = {"assignment_statement",NULL};
|
||||
|
||||
// ==================== GLSL ====================
|
||||
// GLSL extends C, reuse c types (same grammar family)
|
||||
|
||||
// ==================== INI ====================
|
||||
static const char* ini_module_types[] = {"document",NULL};
|
||||
|
||||
// ==================== NEW LANG ENV ACCESS ====================
|
||||
static const char* julia_env_funcs[] = {"ENV",NULL};
|
||||
static const char* nix_env_funcs[] = {"builtins.getEnv",NULL};
|
||||
static const char* fortran_env_funcs[] = {"get_environment_variable",NULL};
|
||||
static const char* fsharp_env_funcs[] = {"Environment.GetEnvironmentVariable",NULL};
|
||||
|
||||
// ==================== SPEC TABLE ====================
|
||||
|
||||
static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
|
||||
// CBM_LANG_GO
|
||||
{CBM_LANG_GO, go_func_types, go_class_types, go_field_types, go_module_types, go_call_types,
|
||||
go_import_types, go_import_types, go_branch_types, go_var_types, go_assign_types,
|
||||
empty_types, NULL, empty_types, go_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_PYTHON
|
||||
{CBM_LANG_PYTHON, py_func_types, py_class_types, empty_types, py_module_types, py_call_types,
|
||||
py_import_types, py_import_from_types, py_branch_types, py_var_types, py_var_types,
|
||||
py_throw_types, NULL, py_decorator_types, py_env_funcs, py_env_members},
|
||||
|
||||
// CBM_LANG_JAVASCRIPT
|
||||
{CBM_LANG_JAVASCRIPT, js_func_types, js_class_types, empty_types, js_module_types, js_call_types,
|
||||
js_import_types, js_import_types, js_branch_types, js_var_types, (const char*[]){"assignment_expression","augmented_assignment_expression",NULL},
|
||||
js_throw_types, NULL, empty_types, NULL, js_env_members},
|
||||
|
||||
// CBM_LANG_TYPESCRIPT
|
||||
{CBM_LANG_TYPESCRIPT, ts_func_types, ts_class_types, empty_types, js_module_types, js_call_types,
|
||||
js_import_types, js_import_types, js_branch_types, js_var_types, (const char*[]){"assignment_expression","augmented_assignment_expression",NULL},
|
||||
js_throw_types, NULL, ts_decorator_types, NULL, ts_env_members},
|
||||
|
||||
// CBM_LANG_TSX
|
||||
{CBM_LANG_TSX, ts_func_types, ts_class_types, empty_types, js_module_types, js_call_types,
|
||||
js_import_types, js_import_types, js_branch_types, js_var_types, (const char*[]){"assignment_expression","augmented_assignment_expression",NULL},
|
||||
js_throw_types, NULL, ts_decorator_types, NULL, ts_env_members},
|
||||
|
||||
// CBM_LANG_RUST
|
||||
{CBM_LANG_RUST, rust_func_types, rust_class_types, rust_field_types, rust_module_types, rust_call_types,
|
||||
rust_import_types, rust_import_from_types, rust_branch_types, rust_var_types, rust_assign_types,
|
||||
empty_types, NULL, rust_decorator_types, rust_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_JAVA
|
||||
{CBM_LANG_JAVA, java_func_types, java_class_types, java_field_types, java_module_types, java_call_types,
|
||||
java_import_types, java_import_types, java_branch_types, java_var_types, java_assign_types,
|
||||
java_throw_types, "throws", java_decorator_types, java_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_CPP
|
||||
{CBM_LANG_CPP, cpp_func_types, cpp_class_types, cpp_field_types, cpp_module_types, cpp_call_types,
|
||||
cpp_import_types, cpp_import_types, cpp_branch_types, cpp_var_types, cpp_assign_types,
|
||||
cpp_throw_types, NULL, empty_types, cpp_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_CSHARP
|
||||
{CBM_LANG_CSHARP, cs_func_types, cs_class_types, empty_types, cs_module_types, cs_call_types,
|
||||
cs_import_types, cs_import_types, cs_branch_types, cs_var_types, cs_assign_types,
|
||||
cs_throw_types, NULL, cs_decorator_types, cs_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_PHP
|
||||
{CBM_LANG_PHP, php_func_types, php_class_types, empty_types, php_module_types, php_call_types,
|
||||
empty_types, empty_types, php_branch_types, php_var_types, php_assign_types,
|
||||
php_throw_types, NULL, php_decorator_types, php_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_LUA
|
||||
{CBM_LANG_LUA, lua_func_types, empty_types, empty_types, lua_module_types, lua_call_types,
|
||||
lua_import_types, empty_types, lua_branch_types, lua_var_types, lua_assign_types,
|
||||
empty_types, NULL, empty_types, lua_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_SCALA
|
||||
{CBM_LANG_SCALA, scala_func_types, scala_class_types, empty_types, scala_module_types, scala_call_types,
|
||||
scala_import_types, scala_import_types, scala_branch_types, scala_var_types, scala_assign_types,
|
||||
scala_throw_types, NULL, empty_types, scala_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_KOTLIN
|
||||
{CBM_LANG_KOTLIN, kotlin_func_types, kotlin_class_types, empty_types, kotlin_module_types, kotlin_call_types,
|
||||
kotlin_import_types, kotlin_import_types, kotlin_branch_types, kotlin_var_types, kotlin_assign_types,
|
||||
kotlin_throw_types, NULL, kotlin_decorator_types, kotlin_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_RUBY
|
||||
{CBM_LANG_RUBY, ruby_func_types, ruby_class_types, empty_types, ruby_module_types, ruby_call_types,
|
||||
ruby_import_types, empty_types, ruby_branch_types, ruby_var_types, ruby_assign_types,
|
||||
empty_types, NULL, empty_types, NULL, ruby_env_members},
|
||||
|
||||
// CBM_LANG_C
|
||||
{CBM_LANG_C, c_func_types, c_class_types, c_field_types, c_module_types, c_call_types,
|
||||
c_import_types, empty_types, c_branch_types, c_var_types, c_assign_types,
|
||||
empty_types, NULL, empty_types, c_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_BASH
|
||||
{CBM_LANG_BASH, bash_func_types, empty_types, empty_types, bash_module_types, bash_call_types,
|
||||
bash_import_types, empty_types, bash_branch_types, bash_var_types, bash_var_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_ZIG
|
||||
{CBM_LANG_ZIG, zig_func_types, zig_class_types, zig_field_types, zig_module_types, zig_call_types,
|
||||
zig_import_types, empty_types, zig_branch_types, zig_var_types, zig_assign_types,
|
||||
empty_types, NULL, empty_types, zig_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_ELIXIR
|
||||
{CBM_LANG_ELIXIR, elixir_func_types, empty_types, empty_types, elixir_module_types, elixir_call_types,
|
||||
elixir_import_types, empty_types, elixir_branch_types, elixir_var_types, elixir_var_types,
|
||||
empty_types, NULL, empty_types, elixir_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_HASKELL
|
||||
{CBM_LANG_HASKELL, haskell_func_types, haskell_class_types, empty_types, haskell_module_types, haskell_call_types,
|
||||
haskell_import_types, empty_types, haskell_branch_types, haskell_var_types, haskell_var_types,
|
||||
empty_types, NULL, empty_types, haskell_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_OCAML
|
||||
{CBM_LANG_OCAML, ocaml_func_types, ocaml_class_types, empty_types, ocaml_module_types, ocaml_call_types,
|
||||
ocaml_import_types, empty_types, ocaml_branch_types, ocaml_var_types, ocaml_var_types,
|
||||
empty_types, NULL, empty_types, ocaml_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_OBJC
|
||||
{CBM_LANG_OBJC, objc_func_types, objc_class_types, objc_field_types, objc_module_types, objc_call_types,
|
||||
objc_import_types, empty_types, objc_branch_types, objc_var_types, objc_assign_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_SWIFT
|
||||
{CBM_LANG_SWIFT, swift_func_types, swift_class_types, swift_field_types, swift_module_types, swift_call_types,
|
||||
swift_import_types, empty_types, swift_branch_types, swift_var_types, swift_assign_types,
|
||||
swift_throw_types, NULL, swift_decorator_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_DART
|
||||
{CBM_LANG_DART, dart_func_types, dart_class_types, dart_field_types, dart_module_types, dart_call_types,
|
||||
dart_import_types, empty_types, dart_branch_types, dart_var_types, dart_assign_types,
|
||||
dart_throw_types, NULL, dart_decorator_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_PERL
|
||||
{CBM_LANG_PERL, perl_func_types, empty_types, empty_types, perl_module_types, perl_call_types,
|
||||
perl_import_types, empty_types, perl_branch_types, perl_var_types, perl_assign_types,
|
||||
empty_types, NULL, empty_types, perl_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_GROOVY
|
||||
{CBM_LANG_GROOVY, groovy_func_types, groovy_class_types, empty_types, groovy_module_types, groovy_call_types,
|
||||
groovy_import_types, empty_types, groovy_branch_types, groovy_var_types, groovy_assign_types,
|
||||
groovy_throw_types, NULL, groovy_decorator_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_ERLANG
|
||||
{CBM_LANG_ERLANG, erlang_func_types, empty_types, empty_types, erlang_module_types, erlang_call_types,
|
||||
erlang_import_types, empty_types, erlang_branch_types, erlang_var_types, erlang_assign_types,
|
||||
erlang_throw_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_R
|
||||
{CBM_LANG_R, r_func_types, empty_types, empty_types, r_module_types, r_call_types,
|
||||
r_import_types, empty_types, r_branch_types, r_var_types, r_var_types,
|
||||
empty_types, NULL, empty_types, r_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_HTML
|
||||
{CBM_LANG_HTML, empty_types, empty_types, empty_types, html_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_CSS
|
||||
{CBM_LANG_CSS, empty_types, empty_types, empty_types, css_module_types, empty_types,
|
||||
css_import_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_SCSS
|
||||
{CBM_LANG_SCSS, scss_func_types, empty_types, empty_types, scss_module_types, scss_call_types,
|
||||
scss_import_types, empty_types, scss_branch_types, scss_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_YAML
|
||||
{CBM_LANG_YAML, empty_types, empty_types, empty_types, yaml_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, yaml_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_TOML
|
||||
{CBM_LANG_TOML, empty_types, empty_types, empty_types, toml_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_HCL
|
||||
{CBM_LANG_HCL, empty_types, hcl_class_types, empty_types, hcl_module_types, hcl_call_types,
|
||||
empty_types, empty_types, empty_types, hcl_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_SQL
|
||||
{CBM_LANG_SQL, sql_func_types, empty_types, sql_field_types, sql_module_types, sql_call_types,
|
||||
empty_types, empty_types, sql_branch_types, sql_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_DOCKERFILE
|
||||
{CBM_LANG_DOCKERFILE, empty_types, empty_types, empty_types, dockerfile_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, dockerfile_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_CLOJURE
|
||||
{CBM_LANG_CLOJURE, empty_types, empty_types, empty_types, clojure_module_types, clojure_call_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_FSHARP
|
||||
{CBM_LANG_FSHARP, fsharp_func_types, fsharp_class_types, empty_types, fsharp_module_types, fsharp_call_types,
|
||||
fsharp_import_types, empty_types, fsharp_branch_types, fsharp_var_types, fsharp_var_types,
|
||||
empty_types, NULL, empty_types, fsharp_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_JULIA
|
||||
{CBM_LANG_JULIA, julia_func_types, julia_class_types, empty_types, julia_module_types, julia_call_types,
|
||||
julia_import_types, empty_types, julia_branch_types, julia_var_types, julia_assign_types,
|
||||
julia_throw_types, NULL, empty_types, julia_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_VIMSCRIPT
|
||||
{CBM_LANG_VIMSCRIPT, vim_func_types, empty_types, empty_types, vim_module_types, vim_call_types,
|
||||
empty_types, empty_types, vim_branch_types, vim_var_types, vim_var_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_NIX
|
||||
{CBM_LANG_NIX, nix_func_types, empty_types, empty_types, nix_module_types, nix_call_types,
|
||||
empty_types, empty_types, nix_branch_types, nix_var_types, nix_var_types,
|
||||
empty_types, NULL, empty_types, nix_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_COMMONLISP
|
||||
{CBM_LANG_COMMONLISP, empty_types, empty_types, empty_types, commonlisp_module_types, commonlisp_call_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_ELM
|
||||
{CBM_LANG_ELM, elm_func_types, elm_class_types, empty_types, elm_module_types, elm_call_types,
|
||||
elm_import_types, empty_types, elm_branch_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_FORTRAN
|
||||
{CBM_LANG_FORTRAN, fortran_func_types, fortran_class_types, empty_types, fortran_module_types, fortran_call_types,
|
||||
fortran_import_types, empty_types, fortran_branch_types, fortran_var_types, fortran_assign_types,
|
||||
empty_types, NULL, empty_types, fortran_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_CUDA (reuses C++ node types)
|
||||
{CBM_LANG_CUDA, cpp_func_types, cpp_class_types, cpp_field_types, cpp_module_types, cpp_call_types,
|
||||
cpp_import_types, cpp_import_types, cpp_branch_types, cpp_var_types, cpp_assign_types,
|
||||
cpp_throw_types, NULL, empty_types, cpp_env_funcs, NULL},
|
||||
|
||||
// CBM_LANG_COBOL
|
||||
{CBM_LANG_COBOL, cobol_func_types, empty_types, empty_types, cobol_module_types, cobol_call_types,
|
||||
empty_types, empty_types, cobol_branch_types, cobol_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_VERILOG
|
||||
{CBM_LANG_VERILOG, verilog_func_types, verilog_class_types, empty_types, verilog_module_types, verilog_call_types,
|
||||
empty_types, empty_types, verilog_branch_types, verilog_var_types, verilog_assign_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_EMACSLISP
|
||||
{CBM_LANG_EMACSLISP, elisp_func_types, empty_types, empty_types, elisp_module_types, elisp_call_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_JSON
|
||||
{CBM_LANG_JSON, empty_types, empty_types, empty_types, json_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_XML
|
||||
{CBM_LANG_XML, empty_types, empty_types, empty_types, xml_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_MARKDOWN
|
||||
{CBM_LANG_MARKDOWN, empty_types, empty_types, empty_types, markdown_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_MAKEFILE
|
||||
{CBM_LANG_MAKEFILE, empty_types, empty_types, empty_types, makefile_module_types, makefile_call_types,
|
||||
makefile_import_types, empty_types, empty_types, makefile_var_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_CMAKE
|
||||
{CBM_LANG_CMAKE, empty_types, empty_types, empty_types, cmake_module_types, cmake_call_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_PROTOBUF
|
||||
{CBM_LANG_PROTOBUF, empty_types, protobuf_class_types, protobuf_field_types, protobuf_module_types, empty_types,
|
||||
protobuf_import_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_GRAPHQL
|
||||
{CBM_LANG_GRAPHQL, empty_types, graphql_class_types, graphql_field_types, graphql_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_VUE
|
||||
{CBM_LANG_VUE, empty_types, empty_types, empty_types, vue_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_SVELTE
|
||||
{CBM_LANG_SVELTE, empty_types, empty_types, empty_types, svelte_module_types, empty_types,
|
||||
empty_types, empty_types, svelte_branch_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_MESON
|
||||
{CBM_LANG_MESON, meson_func_types, empty_types, empty_types, meson_module_types, meson_call_types,
|
||||
empty_types, empty_types, meson_branch_types, meson_var_types, meson_var_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_GLSL (reuses C node types)
|
||||
{CBM_LANG_GLSL, c_func_types, c_class_types, c_field_types, c_module_types, c_call_types,
|
||||
c_import_types, empty_types, c_branch_types, c_var_types, c_assign_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
|
||||
// CBM_LANG_INI
|
||||
{CBM_LANG_INI, empty_types, empty_types, empty_types, ini_module_types, empty_types,
|
||||
empty_types, empty_types, empty_types, empty_types, empty_types,
|
||||
empty_types, NULL, empty_types, NULL, NULL},
|
||||
};
|
||||
|
||||
const CBMLangSpec* cbm_lang_spec(CBMLanguage lang) {
|
||||
if (lang < 0 || lang >= CBM_LANG_COUNT) return NULL;
|
||||
return &lang_specs[lang];
|
||||
}
|
||||
|
||||
const TSLanguage* cbm_ts_language(CBMLanguage lang) {
|
||||
switch (lang) {
|
||||
case CBM_LANG_GO: return tree_sitter_go();
|
||||
case CBM_LANG_PYTHON: return tree_sitter_python();
|
||||
case CBM_LANG_JAVASCRIPT: return tree_sitter_javascript();
|
||||
case CBM_LANG_TYPESCRIPT: return tree_sitter_typescript();
|
||||
case CBM_LANG_TSX: return tree_sitter_tsx();
|
||||
case CBM_LANG_RUST: return tree_sitter_rust();
|
||||
case CBM_LANG_JAVA: return tree_sitter_java();
|
||||
case CBM_LANG_CPP: return tree_sitter_cpp();
|
||||
case CBM_LANG_CSHARP: return tree_sitter_c_sharp();
|
||||
case CBM_LANG_PHP: return tree_sitter_php_only();
|
||||
case CBM_LANG_LUA: return tree_sitter_lua();
|
||||
case CBM_LANG_SCALA: return tree_sitter_scala();
|
||||
case CBM_LANG_KOTLIN: return tree_sitter_kotlin();
|
||||
case CBM_LANG_RUBY: return tree_sitter_ruby();
|
||||
case CBM_LANG_C: return tree_sitter_c();
|
||||
case CBM_LANG_BASH: return tree_sitter_bash();
|
||||
case CBM_LANG_ZIG: return tree_sitter_zig();
|
||||
case CBM_LANG_ELIXIR: return tree_sitter_elixir();
|
||||
case CBM_LANG_HASKELL: return tree_sitter_haskell();
|
||||
case CBM_LANG_OCAML: return tree_sitter_ocaml();
|
||||
case CBM_LANG_OBJC: return tree_sitter_objc();
|
||||
case CBM_LANG_SWIFT: return tree_sitter_swift();
|
||||
case CBM_LANG_DART: return tree_sitter_dart();
|
||||
case CBM_LANG_PERL: return tree_sitter_perl();
|
||||
case CBM_LANG_GROOVY: return tree_sitter_groovy();
|
||||
case CBM_LANG_ERLANG: return tree_sitter_erlang();
|
||||
case CBM_LANG_R: return tree_sitter_r();
|
||||
case CBM_LANG_HTML: return tree_sitter_html();
|
||||
case CBM_LANG_CSS: return tree_sitter_css();
|
||||
case CBM_LANG_SCSS: return tree_sitter_scss();
|
||||
case CBM_LANG_YAML: return tree_sitter_yaml();
|
||||
case CBM_LANG_TOML: return tree_sitter_toml();
|
||||
case CBM_LANG_HCL: return tree_sitter_hcl();
|
||||
case CBM_LANG_SQL: return tree_sitter_sql();
|
||||
case CBM_LANG_DOCKERFILE: return tree_sitter_dockerfile();
|
||||
case CBM_LANG_CLOJURE: return tree_sitter_clojure();
|
||||
case CBM_LANG_FSHARP: return tree_sitter_fsharp();
|
||||
case CBM_LANG_JULIA: return tree_sitter_julia();
|
||||
case CBM_LANG_VIMSCRIPT: return tree_sitter_vim();
|
||||
case CBM_LANG_NIX: return tree_sitter_nix();
|
||||
case CBM_LANG_COMMONLISP: return tree_sitter_commonlisp();
|
||||
case CBM_LANG_ELM: return tree_sitter_elm();
|
||||
case CBM_LANG_FORTRAN: return tree_sitter_fortran();
|
||||
case CBM_LANG_CUDA: return tree_sitter_cuda();
|
||||
case CBM_LANG_COBOL: return tree_sitter_COBOL();
|
||||
case CBM_LANG_VERILOG: return tree_sitter_verilog();
|
||||
case CBM_LANG_EMACSLISP: return tree_sitter_elisp();
|
||||
case CBM_LANG_JSON: return tree_sitter_json();
|
||||
case CBM_LANG_XML: return tree_sitter_xml();
|
||||
case CBM_LANG_MARKDOWN: return tree_sitter_markdown();
|
||||
case CBM_LANG_MAKEFILE: return tree_sitter_make();
|
||||
case CBM_LANG_CMAKE: return tree_sitter_cmake();
|
||||
case CBM_LANG_PROTOBUF: return tree_sitter_proto();
|
||||
case CBM_LANG_GRAPHQL: return tree_sitter_graphql();
|
||||
case CBM_LANG_VUE: return tree_sitter_vue();
|
||||
case CBM_LANG_SVELTE: return tree_sitter_svelte();
|
||||
case CBM_LANG_MESON: return tree_sitter_meson();
|
||||
case CBM_LANG_GLSL: return tree_sitter_glsl();
|
||||
case CBM_LANG_INI: return tree_sitter_ini();
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef CBM_LANG_SPECS_H
|
||||
#define CBM_LANG_SPECS_H
|
||||
|
||||
#include "cbm.h"
|
||||
|
||||
// CBMLangSpec mirrors Go's lang.LanguageSpec with NULL-terminated string arrays.
|
||||
typedef struct {
|
||||
CBMLanguage language;
|
||||
const char** function_node_types;
|
||||
const char** class_node_types;
|
||||
const char** field_node_types;
|
||||
const char** module_node_types;
|
||||
const char** call_node_types;
|
||||
const char** import_node_types;
|
||||
const char** import_from_types;
|
||||
const char** branching_node_types;
|
||||
const char** variable_node_types;
|
||||
const char** assignment_node_types;
|
||||
const char** throw_node_types;
|
||||
const char* throws_clause_field; // NULL if none
|
||||
const char** decorator_node_types;
|
||||
const char** env_access_functions; // NULL-terminated (NULL if none)
|
||||
const char** env_access_member_patterns; // NULL-terminated (NULL if none)
|
||||
} CBMLangSpec;
|
||||
|
||||
// Get the language spec for a given language. Returns NULL for unsupported.
|
||||
const CBMLangSpec* cbm_lang_spec(CBMLanguage lang);
|
||||
|
||||
// Get the TSLanguage* for a given language. Returns NULL for unsupported.
|
||||
// These resolve at link time to grammar symbols from Go tree-sitter modules.
|
||||
const TSLanguage* cbm_ts_language(CBMLanguage lang);
|
||||
|
||||
#endif // CBM_LANG_SPECS_H
|
||||
@@ -0,0 +1,6 @@
|
||||
// Compile vendored tree-sitter runtime as a single compilation unit.
|
||||
// Source: tree-sitter v0.26.0 (DeusData fork)
|
||||
//
|
||||
// lib.c internally #includes all other runtime .c files, so we only
|
||||
// need this one entry point. The runtime headers are at vendored/ts_runtime/src/.
|
||||
#include "vendored/ts_runtime/src/lib.c"
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,54 @@
|
||||
#ifndef TREE_SITTER_ALLOC_H_
|
||||
#define TREE_SITTER_ALLOC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Allow clients to override allocation functions
|
||||
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
||||
|
||||
extern void *(*ts_current_malloc)(size_t size);
|
||||
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
||||
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
||||
extern void (*ts_current_free)(void *ptr);
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc ts_current_malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc ts_current_calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc ts_current_realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free ts_current_free
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free free
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ALLOC_H_
|
||||
@@ -0,0 +1,291 @@
|
||||
#ifndef TREE_SITTER_ARRAY_H_
|
||||
#define TREE_SITTER_ARRAY_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "./alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4101)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
#endif
|
||||
|
||||
#define Array(T) \
|
||||
struct { \
|
||||
T *contents; \
|
||||
uint32_t size; \
|
||||
uint32_t capacity; \
|
||||
}
|
||||
|
||||
/// Initialize an array.
|
||||
#define array_init(self) \
|
||||
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
||||
|
||||
/// Create an empty array.
|
||||
#define array_new() \
|
||||
{ NULL, 0, 0 }
|
||||
|
||||
/// Get a pointer to the element at a given `index` in the array.
|
||||
#define array_get(self, _index) \
|
||||
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
||||
|
||||
/// Get a pointer to the first element in the array.
|
||||
#define array_front(self) array_get(self, 0)
|
||||
|
||||
/// Get a pointer to the last element in the array.
|
||||
#define array_back(self) array_get(self, (self)->size - 1)
|
||||
|
||||
/// Clear the array, setting its size to zero. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_clear(self) ((self)->size = 0)
|
||||
|
||||
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
||||
/// less than the array's current capacity, this function has no effect.
|
||||
#define array_reserve(self, new_capacity) \
|
||||
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
||||
|
||||
/// Free any memory allocated for this array. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_delete(self) _array__delete((Array *)(self))
|
||||
|
||||
/// Push a new `element` onto the end of the array.
|
||||
#define array_push(self, element) \
|
||||
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
||||
(self)->contents[(self)->size++] = (element))
|
||||
|
||||
/// Increase the array's size by `count` elements.
|
||||
/// New elements are zero-initialized.
|
||||
#define array_grow_by(self, count) \
|
||||
do { \
|
||||
if ((count) == 0) break; \
|
||||
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
||||
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
||||
(self)->size += (count); \
|
||||
} while (0)
|
||||
|
||||
/// Append all elements from one array to the end of another.
|
||||
#define array_push_all(self, other) \
|
||||
array_extend((self), (other)->size, (other)->contents)
|
||||
|
||||
/// Append `count` elements to the end of the array, reading their values from the
|
||||
/// `contents` pointer.
|
||||
#define array_extend(self, count, contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), (self)->size, \
|
||||
0, count, contents \
|
||||
)
|
||||
|
||||
/// Remove `old_count` elements from the array starting at the given `index`. At
|
||||
/// the same index, insert `new_count` new elements, reading their values from the
|
||||
/// `new_contents` pointer.
|
||||
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), _index, \
|
||||
old_count, new_count, new_contents \
|
||||
)
|
||||
|
||||
/// Insert one `element` into the array at the given `index`.
|
||||
#define array_insert(self, _index, element) \
|
||||
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
||||
|
||||
/// Remove one element from the array at the given `index`.
|
||||
#define array_erase(self, _index) \
|
||||
_array__erase((Array *)(self), array_elem_size(self), _index)
|
||||
|
||||
/// Pop the last element off the array, returning the element by value.
|
||||
#define array_pop(self) ((self)->contents[--(self)->size])
|
||||
|
||||
/// Assign the contents of one array to another, reallocating if necessary.
|
||||
#define array_assign(self, other) \
|
||||
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
||||
|
||||
/// Swap one array with another
|
||||
#define array_swap(self, other) \
|
||||
_array__swap((Array *)(self), (Array *)(other))
|
||||
|
||||
/// Get the size of the array contents
|
||||
#define array_elem_size(self) (sizeof *(self)->contents)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
///
|
||||
/// If an existing element is found to be equal to `needle`, then the `index`
|
||||
/// out-parameter is set to the existing value's index, and the `exists`
|
||||
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
||||
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
||||
/// is set to false.
|
||||
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using integer comparisons
|
||||
/// of a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_with`.
|
||||
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
#define array_insert_sorted_with(self, compare, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using integer comparisons of
|
||||
/// a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_by`.
|
||||
#define array_insert_sorted_by(self, field, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
// Private
|
||||
|
||||
typedef Array(void) Array;
|
||||
|
||||
/// This is not what you're looking for, see `array_delete`.
|
||||
static inline void _array__delete(Array *self) {
|
||||
if (self->contents) {
|
||||
ts_free(self->contents);
|
||||
self->contents = NULL;
|
||||
self->size = 0;
|
||||
self->capacity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_erase`.
|
||||
static inline void _array__erase(Array *self, size_t element_size,
|
||||
uint32_t index) {
|
||||
assert(index < self->size);
|
||||
char *contents = (char *)self->contents;
|
||||
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
||||
(self->size - index - 1) * element_size);
|
||||
self->size--;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_reserve`.
|
||||
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
||||
if (new_capacity > self->capacity) {
|
||||
if (self->contents) {
|
||||
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
||||
} else {
|
||||
self->contents = ts_malloc(new_capacity * element_size);
|
||||
}
|
||||
self->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_assign`.
|
||||
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
||||
_array__reserve(self, element_size, other->size);
|
||||
self->size = other->size;
|
||||
memcpy(self->contents, other->contents, self->size * element_size);
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_swap`.
|
||||
static inline void _array__swap(Array *self, Array *other) {
|
||||
Array swap = *other;
|
||||
*other = *self;
|
||||
*self = swap;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
||||
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
||||
uint32_t new_size = self->size + count;
|
||||
if (new_size > self->capacity) {
|
||||
uint32_t new_capacity = self->capacity * 2;
|
||||
if (new_capacity < 8) new_capacity = 8;
|
||||
if (new_capacity < new_size) new_capacity = new_size;
|
||||
_array__reserve(self, element_size, new_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_splice`.
|
||||
static inline void _array__splice(Array *self, size_t element_size,
|
||||
uint32_t index, uint32_t old_count,
|
||||
uint32_t new_count, const void *elements) {
|
||||
uint32_t new_size = self->size + new_count - old_count;
|
||||
uint32_t old_end = index + old_count;
|
||||
uint32_t new_end = index + new_count;
|
||||
assert(old_end <= self->size);
|
||||
|
||||
_array__reserve(self, element_size, new_size);
|
||||
|
||||
char *contents = (char *)self->contents;
|
||||
if (self->size > old_end) {
|
||||
memmove(
|
||||
contents + new_end * element_size,
|
||||
contents + old_end * element_size,
|
||||
(self->size - old_end) * element_size
|
||||
);
|
||||
}
|
||||
if (new_count > 0) {
|
||||
if (elements) {
|
||||
memcpy(
|
||||
(contents + index * element_size),
|
||||
elements,
|
||||
new_count * element_size
|
||||
);
|
||||
} else {
|
||||
memset(
|
||||
(contents + index * element_size),
|
||||
0,
|
||||
new_count * element_size
|
||||
);
|
||||
}
|
||||
}
|
||||
self->size += new_count - old_count;
|
||||
}
|
||||
|
||||
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
||||
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
||||
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
||||
do { \
|
||||
*(_index) = start; \
|
||||
*(_exists) = false; \
|
||||
uint32_t size = (self)->size - *(_index); \
|
||||
if (size == 0) break; \
|
||||
int comparison; \
|
||||
while (size > 1) { \
|
||||
uint32_t half_size = size / 2; \
|
||||
uint32_t mid_index = *(_index) + half_size; \
|
||||
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
||||
if (comparison <= 0) *(_index) = mid_index; \
|
||||
size -= half_size; \
|
||||
} \
|
||||
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
||||
if (comparison == 0) *(_exists) = true; \
|
||||
else if (comparison < 0) *(_index) += 1; \
|
||||
} while (0)
|
||||
|
||||
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
||||
/// parameter by reference in order to work with the generic sorting function above.
|
||||
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ARRAY_H_
|
||||
@@ -0,0 +1,286 @@
|
||||
#ifndef TREE_SITTER_PARSER_H_
|
||||
#define TREE_SITTER_PARSER_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ts_builtin_sym_error ((TSSymbol)-1)
|
||||
#define ts_builtin_sym_end 0
|
||||
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
||||
|
||||
#ifndef TREE_SITTER_API_H_
|
||||
typedef uint16_t TSStateId;
|
||||
typedef uint16_t TSSymbol;
|
||||
typedef uint16_t TSFieldId;
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
typedef struct TSLanguageMetadata {
|
||||
uint8_t major_version;
|
||||
uint8_t minor_version;
|
||||
uint8_t patch_version;
|
||||
} TSLanguageMetadata;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
TSFieldId field_id;
|
||||
uint8_t child_index;
|
||||
bool inherited;
|
||||
} TSFieldMapEntry;
|
||||
|
||||
// Used to index the field and supertype maps.
|
||||
typedef struct {
|
||||
uint16_t index;
|
||||
uint16_t length;
|
||||
} TSMapSlice;
|
||||
|
||||
typedef struct {
|
||||
bool visible;
|
||||
bool named;
|
||||
bool supertype;
|
||||
} TSSymbolMetadata;
|
||||
|
||||
typedef struct TSLexer TSLexer;
|
||||
|
||||
struct TSLexer {
|
||||
int32_t lookahead;
|
||||
TSSymbol result_symbol;
|
||||
void (*advance)(TSLexer *, bool);
|
||||
void (*mark_end)(TSLexer *);
|
||||
uint32_t (*get_column)(TSLexer *);
|
||||
bool (*is_at_included_range_start)(const TSLexer *);
|
||||
bool (*eof)(const TSLexer *);
|
||||
void (*log)(const TSLexer *, const char *, ...);
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TSParseActionTypeShift,
|
||||
TSParseActionTypeReduce,
|
||||
TSParseActionTypeAccept,
|
||||
TSParseActionTypeRecover,
|
||||
} TSParseActionType;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t type;
|
||||
TSStateId state;
|
||||
bool extra;
|
||||
bool repetition;
|
||||
} shift;
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t child_count;
|
||||
TSSymbol symbol;
|
||||
int16_t dynamic_precedence;
|
||||
uint16_t production_id;
|
||||
} reduce;
|
||||
uint8_t type;
|
||||
} TSParseAction;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
} TSLexMode;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
uint16_t reserved_word_set_id;
|
||||
} TSLexerMode;
|
||||
|
||||
typedef union {
|
||||
TSParseAction action;
|
||||
struct {
|
||||
uint8_t count;
|
||||
bool reusable;
|
||||
} entry;
|
||||
} TSParseActionEntry;
|
||||
|
||||
typedef struct {
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
} TSCharacterRange;
|
||||
|
||||
struct TSLanguage {
|
||||
uint32_t abi_version;
|
||||
uint32_t symbol_count;
|
||||
uint32_t alias_count;
|
||||
uint32_t token_count;
|
||||
uint32_t external_token_count;
|
||||
uint32_t state_count;
|
||||
uint32_t large_state_count;
|
||||
uint32_t production_id_count;
|
||||
uint32_t field_count;
|
||||
uint16_t max_alias_sequence_length;
|
||||
const uint16_t *parse_table;
|
||||
const uint16_t *small_parse_table;
|
||||
const uint32_t *small_parse_table_map;
|
||||
const TSParseActionEntry *parse_actions;
|
||||
const char * const *symbol_names;
|
||||
const char * const *field_names;
|
||||
const TSMapSlice *field_map_slices;
|
||||
const TSFieldMapEntry *field_map_entries;
|
||||
const TSSymbolMetadata *symbol_metadata;
|
||||
const TSSymbol *public_symbol_map;
|
||||
const uint16_t *alias_map;
|
||||
const TSSymbol *alias_sequences;
|
||||
const TSLexerMode *lex_modes;
|
||||
bool (*lex_fn)(TSLexer *, TSStateId);
|
||||
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
||||
TSSymbol keyword_capture_token;
|
||||
struct {
|
||||
const bool *states;
|
||||
const TSSymbol *symbol_map;
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
||||
unsigned (*serialize)(void *, char *);
|
||||
void (*deserialize)(void *, const char *, unsigned);
|
||||
} external_scanner;
|
||||
const TSStateId *primary_state_ids;
|
||||
const char *name;
|
||||
const TSSymbol *reserved_words;
|
||||
uint16_t max_reserved_word_set_size;
|
||||
uint32_t supertype_count;
|
||||
const TSSymbol *supertype_symbols;
|
||||
const TSMapSlice *supertype_map_slices;
|
||||
const TSSymbol *supertype_map_entries;
|
||||
TSLanguageMetadata metadata;
|
||||
};
|
||||
|
||||
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
|
||||
uint32_t index = 0;
|
||||
uint32_t size = len - index;
|
||||
while (size > 1) {
|
||||
uint32_t half_size = size / 2;
|
||||
uint32_t mid_index = index + half_size;
|
||||
const TSCharacterRange *range = &ranges[mid_index];
|
||||
if (lookahead >= range->start && lookahead <= range->end) {
|
||||
return true;
|
||||
} else if (lookahead > range->end) {
|
||||
index = mid_index;
|
||||
}
|
||||
size -= half_size;
|
||||
}
|
||||
const TSCharacterRange *range = &ranges[index];
|
||||
return (lookahead >= range->start && lookahead <= range->end);
|
||||
}
|
||||
|
||||
/*
|
||||
* Lexer Macros
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define UNUSED __pragma(warning(suppress : 4101))
|
||||
#else
|
||||
#define UNUSED __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#define START_LEXER() \
|
||||
bool result = false; \
|
||||
bool skip = false; \
|
||||
UNUSED \
|
||||
bool eof = false; \
|
||||
int32_t lookahead; \
|
||||
goto start; \
|
||||
next_state: \
|
||||
lexer->advance(lexer, skip); \
|
||||
start: \
|
||||
skip = false; \
|
||||
lookahead = lexer->lookahead;
|
||||
|
||||
#define ADVANCE(state_value) \
|
||||
{ \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ADVANCE_MAP(...) \
|
||||
{ \
|
||||
static const uint16_t map[] = { __VA_ARGS__ }; \
|
||||
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
|
||||
if (map[i] == lookahead) { \
|
||||
state = map[i + 1]; \
|
||||
goto next_state; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SKIP(state_value) \
|
||||
{ \
|
||||
skip = true; \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ACCEPT_TOKEN(symbol_value) \
|
||||
result = true; \
|
||||
lexer->result_symbol = symbol_value; \
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
#define END_STATE() return result;
|
||||
|
||||
/*
|
||||
* Parse Table Macros
|
||||
*/
|
||||
|
||||
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
|
||||
|
||||
#define STATE(id) id
|
||||
|
||||
#define ACTIONS(id) id
|
||||
|
||||
#define SHIFT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value) \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_REPEAT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value), \
|
||||
.repetition = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_EXTRA() \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.extra = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define REDUCE(symbol_name, children, precedence, prod_id) \
|
||||
{{ \
|
||||
.reduce = { \
|
||||
.type = TSParseActionTypeReduce, \
|
||||
.symbol = symbol_name, \
|
||||
.child_count = children, \
|
||||
.dynamic_precedence = precedence, \
|
||||
.production_id = prod_id \
|
||||
}, \
|
||||
}}
|
||||
|
||||
#define RECOVER() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeRecover \
|
||||
}}
|
||||
|
||||
#define ACCEPT_INPUT() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeAccept \
|
||||
}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_PARSER_H_
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,54 @@
|
||||
#ifndef TREE_SITTER_ALLOC_H_
|
||||
#define TREE_SITTER_ALLOC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Allow clients to override allocation functions
|
||||
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
||||
|
||||
extern void *(*ts_current_malloc)(size_t size);
|
||||
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
||||
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
||||
extern void (*ts_current_free)(void *ptr);
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc ts_current_malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc ts_current_calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc ts_current_realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free ts_current_free
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free free
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ALLOC_H_
|
||||
@@ -0,0 +1,291 @@
|
||||
#ifndef TREE_SITTER_ARRAY_H_
|
||||
#define TREE_SITTER_ARRAY_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "./alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4101)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
#endif
|
||||
|
||||
#define Array(T) \
|
||||
struct { \
|
||||
T *contents; \
|
||||
uint32_t size; \
|
||||
uint32_t capacity; \
|
||||
}
|
||||
|
||||
/// Initialize an array.
|
||||
#define array_init(self) \
|
||||
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
||||
|
||||
/// Create an empty array.
|
||||
#define array_new() \
|
||||
{ NULL, 0, 0 }
|
||||
|
||||
/// Get a pointer to the element at a given `index` in the array.
|
||||
#define array_get(self, _index) \
|
||||
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
||||
|
||||
/// Get a pointer to the first element in the array.
|
||||
#define array_front(self) array_get(self, 0)
|
||||
|
||||
/// Get a pointer to the last element in the array.
|
||||
#define array_back(self) array_get(self, (self)->size - 1)
|
||||
|
||||
/// Clear the array, setting its size to zero. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_clear(self) ((self)->size = 0)
|
||||
|
||||
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
||||
/// less than the array's current capacity, this function has no effect.
|
||||
#define array_reserve(self, new_capacity) \
|
||||
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
||||
|
||||
/// Free any memory allocated for this array. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_delete(self) _array__delete((Array *)(self))
|
||||
|
||||
/// Push a new `element` onto the end of the array.
|
||||
#define array_push(self, element) \
|
||||
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
||||
(self)->contents[(self)->size++] = (element))
|
||||
|
||||
/// Increase the array's size by `count` elements.
|
||||
/// New elements are zero-initialized.
|
||||
#define array_grow_by(self, count) \
|
||||
do { \
|
||||
if ((count) == 0) break; \
|
||||
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
||||
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
||||
(self)->size += (count); \
|
||||
} while (0)
|
||||
|
||||
/// Append all elements from one array to the end of another.
|
||||
#define array_push_all(self, other) \
|
||||
array_extend((self), (other)->size, (other)->contents)
|
||||
|
||||
/// Append `count` elements to the end of the array, reading their values from the
|
||||
/// `contents` pointer.
|
||||
#define array_extend(self, count, contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), (self)->size, \
|
||||
0, count, contents \
|
||||
)
|
||||
|
||||
/// Remove `old_count` elements from the array starting at the given `index`. At
|
||||
/// the same index, insert `new_count` new elements, reading their values from the
|
||||
/// `new_contents` pointer.
|
||||
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), _index, \
|
||||
old_count, new_count, new_contents \
|
||||
)
|
||||
|
||||
/// Insert one `element` into the array at the given `index`.
|
||||
#define array_insert(self, _index, element) \
|
||||
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
||||
|
||||
/// Remove one element from the array at the given `index`.
|
||||
#define array_erase(self, _index) \
|
||||
_array__erase((Array *)(self), array_elem_size(self), _index)
|
||||
|
||||
/// Pop the last element off the array, returning the element by value.
|
||||
#define array_pop(self) ((self)->contents[--(self)->size])
|
||||
|
||||
/// Assign the contents of one array to another, reallocating if necessary.
|
||||
#define array_assign(self, other) \
|
||||
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
||||
|
||||
/// Swap one array with another
|
||||
#define array_swap(self, other) \
|
||||
_array__swap((Array *)(self), (Array *)(other))
|
||||
|
||||
/// Get the size of the array contents
|
||||
#define array_elem_size(self) (sizeof *(self)->contents)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
///
|
||||
/// If an existing element is found to be equal to `needle`, then the `index`
|
||||
/// out-parameter is set to the existing value's index, and the `exists`
|
||||
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
||||
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
||||
/// is set to false.
|
||||
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using integer comparisons
|
||||
/// of a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_with`.
|
||||
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
#define array_insert_sorted_with(self, compare, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using integer comparisons of
|
||||
/// a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_by`.
|
||||
#define array_insert_sorted_by(self, field, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
// Private
|
||||
|
||||
typedef Array(void) Array;
|
||||
|
||||
/// This is not what you're looking for, see `array_delete`.
|
||||
static inline void _array__delete(Array *self) {
|
||||
if (self->contents) {
|
||||
ts_free(self->contents);
|
||||
self->contents = NULL;
|
||||
self->size = 0;
|
||||
self->capacity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_erase`.
|
||||
static inline void _array__erase(Array *self, size_t element_size,
|
||||
uint32_t index) {
|
||||
assert(index < self->size);
|
||||
char *contents = (char *)self->contents;
|
||||
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
||||
(self->size - index - 1) * element_size);
|
||||
self->size--;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_reserve`.
|
||||
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
||||
if (new_capacity > self->capacity) {
|
||||
if (self->contents) {
|
||||
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
||||
} else {
|
||||
self->contents = ts_malloc(new_capacity * element_size);
|
||||
}
|
||||
self->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_assign`.
|
||||
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
||||
_array__reserve(self, element_size, other->size);
|
||||
self->size = other->size;
|
||||
memcpy(self->contents, other->contents, self->size * element_size);
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_swap`.
|
||||
static inline void _array__swap(Array *self, Array *other) {
|
||||
Array swap = *other;
|
||||
*other = *self;
|
||||
*self = swap;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
||||
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
||||
uint32_t new_size = self->size + count;
|
||||
if (new_size > self->capacity) {
|
||||
uint32_t new_capacity = self->capacity * 2;
|
||||
if (new_capacity < 8) new_capacity = 8;
|
||||
if (new_capacity < new_size) new_capacity = new_size;
|
||||
_array__reserve(self, element_size, new_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_splice`.
|
||||
static inline void _array__splice(Array *self, size_t element_size,
|
||||
uint32_t index, uint32_t old_count,
|
||||
uint32_t new_count, const void *elements) {
|
||||
uint32_t new_size = self->size + new_count - old_count;
|
||||
uint32_t old_end = index + old_count;
|
||||
uint32_t new_end = index + new_count;
|
||||
assert(old_end <= self->size);
|
||||
|
||||
_array__reserve(self, element_size, new_size);
|
||||
|
||||
char *contents = (char *)self->contents;
|
||||
if (self->size > old_end) {
|
||||
memmove(
|
||||
contents + new_end * element_size,
|
||||
contents + old_end * element_size,
|
||||
(self->size - old_end) * element_size
|
||||
);
|
||||
}
|
||||
if (new_count > 0) {
|
||||
if (elements) {
|
||||
memcpy(
|
||||
(contents + index * element_size),
|
||||
elements,
|
||||
new_count * element_size
|
||||
);
|
||||
} else {
|
||||
memset(
|
||||
(contents + index * element_size),
|
||||
0,
|
||||
new_count * element_size
|
||||
);
|
||||
}
|
||||
}
|
||||
self->size += new_count - old_count;
|
||||
}
|
||||
|
||||
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
||||
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
||||
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
||||
do { \
|
||||
*(_index) = start; \
|
||||
*(_exists) = false; \
|
||||
uint32_t size = (self)->size - *(_index); \
|
||||
if (size == 0) break; \
|
||||
int comparison; \
|
||||
while (size > 1) { \
|
||||
uint32_t half_size = size / 2; \
|
||||
uint32_t mid_index = *(_index) + half_size; \
|
||||
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
||||
if (comparison <= 0) *(_index) = mid_index; \
|
||||
size -= half_size; \
|
||||
} \
|
||||
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
||||
if (comparison == 0) *(_exists) = true; \
|
||||
else if (comparison < 0) *(_index) += 1; \
|
||||
} while (0)
|
||||
|
||||
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
||||
/// parameter by reference in order to work with the generic sorting function above.
|
||||
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ARRAY_H_
|
||||
@@ -0,0 +1,286 @@
|
||||
#ifndef TREE_SITTER_PARSER_H_
|
||||
#define TREE_SITTER_PARSER_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ts_builtin_sym_error ((TSSymbol)-1)
|
||||
#define ts_builtin_sym_end 0
|
||||
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
||||
|
||||
#ifndef TREE_SITTER_API_H_
|
||||
typedef uint16_t TSStateId;
|
||||
typedef uint16_t TSSymbol;
|
||||
typedef uint16_t TSFieldId;
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
typedef struct TSLanguageMetadata {
|
||||
uint8_t major_version;
|
||||
uint8_t minor_version;
|
||||
uint8_t patch_version;
|
||||
} TSLanguageMetadata;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
TSFieldId field_id;
|
||||
uint8_t child_index;
|
||||
bool inherited;
|
||||
} TSFieldMapEntry;
|
||||
|
||||
// Used to index the field and supertype maps.
|
||||
typedef struct {
|
||||
uint16_t index;
|
||||
uint16_t length;
|
||||
} TSMapSlice;
|
||||
|
||||
typedef struct {
|
||||
bool visible;
|
||||
bool named;
|
||||
bool supertype;
|
||||
} TSSymbolMetadata;
|
||||
|
||||
typedef struct TSLexer TSLexer;
|
||||
|
||||
struct TSLexer {
|
||||
int32_t lookahead;
|
||||
TSSymbol result_symbol;
|
||||
void (*advance)(TSLexer *, bool);
|
||||
void (*mark_end)(TSLexer *);
|
||||
uint32_t (*get_column)(TSLexer *);
|
||||
bool (*is_at_included_range_start)(const TSLexer *);
|
||||
bool (*eof)(const TSLexer *);
|
||||
void (*log)(const TSLexer *, const char *, ...);
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TSParseActionTypeShift,
|
||||
TSParseActionTypeReduce,
|
||||
TSParseActionTypeAccept,
|
||||
TSParseActionTypeRecover,
|
||||
} TSParseActionType;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t type;
|
||||
TSStateId state;
|
||||
bool extra;
|
||||
bool repetition;
|
||||
} shift;
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t child_count;
|
||||
TSSymbol symbol;
|
||||
int16_t dynamic_precedence;
|
||||
uint16_t production_id;
|
||||
} reduce;
|
||||
uint8_t type;
|
||||
} TSParseAction;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
} TSLexMode;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
uint16_t reserved_word_set_id;
|
||||
} TSLexerMode;
|
||||
|
||||
typedef union {
|
||||
TSParseAction action;
|
||||
struct {
|
||||
uint8_t count;
|
||||
bool reusable;
|
||||
} entry;
|
||||
} TSParseActionEntry;
|
||||
|
||||
typedef struct {
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
} TSCharacterRange;
|
||||
|
||||
struct TSLanguage {
|
||||
uint32_t abi_version;
|
||||
uint32_t symbol_count;
|
||||
uint32_t alias_count;
|
||||
uint32_t token_count;
|
||||
uint32_t external_token_count;
|
||||
uint32_t state_count;
|
||||
uint32_t large_state_count;
|
||||
uint32_t production_id_count;
|
||||
uint32_t field_count;
|
||||
uint16_t max_alias_sequence_length;
|
||||
const uint16_t *parse_table;
|
||||
const uint16_t *small_parse_table;
|
||||
const uint32_t *small_parse_table_map;
|
||||
const TSParseActionEntry *parse_actions;
|
||||
const char * const *symbol_names;
|
||||
const char * const *field_names;
|
||||
const TSMapSlice *field_map_slices;
|
||||
const TSFieldMapEntry *field_map_entries;
|
||||
const TSSymbolMetadata *symbol_metadata;
|
||||
const TSSymbol *public_symbol_map;
|
||||
const uint16_t *alias_map;
|
||||
const TSSymbol *alias_sequences;
|
||||
const TSLexerMode *lex_modes;
|
||||
bool (*lex_fn)(TSLexer *, TSStateId);
|
||||
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
||||
TSSymbol keyword_capture_token;
|
||||
struct {
|
||||
const bool *states;
|
||||
const TSSymbol *symbol_map;
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
||||
unsigned (*serialize)(void *, char *);
|
||||
void (*deserialize)(void *, const char *, unsigned);
|
||||
} external_scanner;
|
||||
const TSStateId *primary_state_ids;
|
||||
const char *name;
|
||||
const TSSymbol *reserved_words;
|
||||
uint16_t max_reserved_word_set_size;
|
||||
uint32_t supertype_count;
|
||||
const TSSymbol *supertype_symbols;
|
||||
const TSMapSlice *supertype_map_slices;
|
||||
const TSSymbol *supertype_map_entries;
|
||||
TSLanguageMetadata metadata;
|
||||
};
|
||||
|
||||
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
|
||||
uint32_t index = 0;
|
||||
uint32_t size = len - index;
|
||||
while (size > 1) {
|
||||
uint32_t half_size = size / 2;
|
||||
uint32_t mid_index = index + half_size;
|
||||
const TSCharacterRange *range = &ranges[mid_index];
|
||||
if (lookahead >= range->start && lookahead <= range->end) {
|
||||
return true;
|
||||
} else if (lookahead > range->end) {
|
||||
index = mid_index;
|
||||
}
|
||||
size -= half_size;
|
||||
}
|
||||
const TSCharacterRange *range = &ranges[index];
|
||||
return (lookahead >= range->start && lookahead <= range->end);
|
||||
}
|
||||
|
||||
/*
|
||||
* Lexer Macros
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define UNUSED __pragma(warning(suppress : 4101))
|
||||
#else
|
||||
#define UNUSED __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#define START_LEXER() \
|
||||
bool result = false; \
|
||||
bool skip = false; \
|
||||
UNUSED \
|
||||
bool eof = false; \
|
||||
int32_t lookahead; \
|
||||
goto start; \
|
||||
next_state: \
|
||||
lexer->advance(lexer, skip); \
|
||||
start: \
|
||||
skip = false; \
|
||||
lookahead = lexer->lookahead;
|
||||
|
||||
#define ADVANCE(state_value) \
|
||||
{ \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ADVANCE_MAP(...) \
|
||||
{ \
|
||||
static const uint16_t map[] = { __VA_ARGS__ }; \
|
||||
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
|
||||
if (map[i] == lookahead) { \
|
||||
state = map[i + 1]; \
|
||||
goto next_state; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SKIP(state_value) \
|
||||
{ \
|
||||
skip = true; \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ACCEPT_TOKEN(symbol_value) \
|
||||
result = true; \
|
||||
lexer->result_symbol = symbol_value; \
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
#define END_STATE() return result;
|
||||
|
||||
/*
|
||||
* Parse Table Macros
|
||||
*/
|
||||
|
||||
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
|
||||
|
||||
#define STATE(id) id
|
||||
|
||||
#define ACTIONS(id) id
|
||||
|
||||
#define SHIFT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value) \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_REPEAT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value), \
|
||||
.repetition = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_EXTRA() \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.extra = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define REDUCE(symbol_name, children, precedence, prod_id) \
|
||||
{{ \
|
||||
.reduce = { \
|
||||
.type = TSParseActionTypeReduce, \
|
||||
.symbol = symbol_name, \
|
||||
.child_count = children, \
|
||||
.dynamic_precedence = precedence, \
|
||||
.production_id = prod_id \
|
||||
}, \
|
||||
}}
|
||||
|
||||
#define RECOVER() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeRecover \
|
||||
}}
|
||||
|
||||
#define ACCEPT_INPUT() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeAccept \
|
||||
}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_PARSER_H_
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,421 @@
|
||||
#include "tree_sitter/alloc.h"
|
||||
#include "tree_sitter/array.h"
|
||||
#include "tree_sitter/parser.h"
|
||||
|
||||
#include <wctype.h>
|
||||
|
||||
enum TokenType {
|
||||
OPT_SEMI,
|
||||
INTERPOLATION_REGULAR_START,
|
||||
INTERPOLATION_VERBATIM_START,
|
||||
INTERPOLATION_RAW_START,
|
||||
INTERPOLATION_START_QUOTE,
|
||||
INTERPOLATION_END_QUOTE,
|
||||
INTERPOLATION_OPEN_BRACE,
|
||||
INTERPOLATION_CLOSE_BRACE,
|
||||
INTERPOLATION_STRING_CONTENT,
|
||||
RAW_STRING_START,
|
||||
RAW_STRING_END,
|
||||
RAW_STRING_CONTENT,
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
REGULAR = 1 << 0,
|
||||
VERBATIM = 1 << 1,
|
||||
RAW = 1 << 2,
|
||||
} StringType;
|
||||
|
||||
typedef struct {
|
||||
uint8_t dollar_count;
|
||||
uint8_t open_brace_count;
|
||||
uint8_t quote_count;
|
||||
StringType string_type;
|
||||
} Interpolation;
|
||||
|
||||
static inline bool is_regular(Interpolation *interpolation) { return interpolation->string_type & REGULAR; }
|
||||
|
||||
static inline bool is_verbatim(Interpolation *interpolation) { return interpolation->string_type & VERBATIM; }
|
||||
|
||||
static inline bool is_raw(Interpolation *interpolation) { return interpolation->string_type & RAW; }
|
||||
|
||||
typedef struct {
|
||||
uint8_t quote_count;
|
||||
Array(Interpolation) interpolation_stack;
|
||||
} Scanner;
|
||||
|
||||
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
|
||||
|
||||
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
|
||||
|
||||
void *tree_sitter_c_sharp_external_scanner_create() {
|
||||
Scanner *scanner = ts_calloc(1, sizeof(Scanner));
|
||||
array_init(&scanner->interpolation_stack);
|
||||
return scanner;
|
||||
}
|
||||
|
||||
void tree_sitter_c_sharp_external_scanner_destroy(void *payload) {
|
||||
Scanner *scanner = (Scanner *)payload;
|
||||
array_delete(&scanner->interpolation_stack);
|
||||
ts_free(scanner);
|
||||
}
|
||||
|
||||
unsigned tree_sitter_c_sharp_external_scanner_serialize(void *payload, char *buffer) {
|
||||
Scanner *scanner = (Scanner *)payload;
|
||||
|
||||
if (scanner->interpolation_stack.size * 4 + 2 > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned size = 0;
|
||||
|
||||
buffer[size++] = (char)scanner->quote_count;
|
||||
buffer[size++] = (char)scanner->interpolation_stack.size;
|
||||
|
||||
for (unsigned i = 0; i < scanner->interpolation_stack.size; i++) {
|
||||
Interpolation interpolation = scanner->interpolation_stack.contents[i];
|
||||
buffer[size++] = (char)interpolation.dollar_count;
|
||||
buffer[size++] = (char)interpolation.open_brace_count;
|
||||
buffer[size++] = (char)interpolation.quote_count;
|
||||
buffer[size++] = (char)interpolation.string_type;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void tree_sitter_c_sharp_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
|
||||
Scanner *scanner = (Scanner *)payload;
|
||||
|
||||
scanner->quote_count = 0;
|
||||
array_clear(&scanner->interpolation_stack);
|
||||
unsigned size = 0;
|
||||
|
||||
if (length > 0) {
|
||||
scanner->quote_count = (unsigned char)buffer[size++];
|
||||
scanner->interpolation_stack.size = (unsigned char)buffer[size++];
|
||||
array_reserve(&scanner->interpolation_stack, scanner->interpolation_stack.size);
|
||||
|
||||
for (unsigned i = 0; i < scanner->interpolation_stack.size; i++) {
|
||||
Interpolation interpolation = {0};
|
||||
interpolation.dollar_count = buffer[size++];
|
||||
interpolation.open_brace_count = buffer[size++];
|
||||
interpolation.quote_count = buffer[size++];
|
||||
interpolation.string_type = (unsigned char)buffer[size++];
|
||||
scanner->interpolation_stack.contents[i] = interpolation;
|
||||
}
|
||||
}
|
||||
|
||||
assert(size == length);
|
||||
}
|
||||
|
||||
bool tree_sitter_c_sharp_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
||||
Scanner *scanner = (Scanner *)payload;
|
||||
|
||||
uint8_t brace_advanced = 0;
|
||||
uint8_t quote_count = 0;
|
||||
bool did_advance = false;
|
||||
|
||||
// error recovery, gives better trees this way
|
||||
if (valid_symbols[OPT_SEMI] && valid_symbols[INTERPOLATION_REGULAR_START]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valid_symbols[OPT_SEMI]) {
|
||||
lexer->result_symbol = OPT_SEMI;
|
||||
if (lexer->lookahead == ';') {
|
||||
advance(lexer);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (valid_symbols[RAW_STRING_START]) {
|
||||
while (iswspace(lexer->lookahead)) {
|
||||
skip(lexer);
|
||||
}
|
||||
|
||||
if (lexer->lookahead == '"') {
|
||||
while (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
quote_count++;
|
||||
}
|
||||
|
||||
if (quote_count >= 3) {
|
||||
lexer->result_symbol = RAW_STRING_START;
|
||||
scanner->quote_count = quote_count;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (valid_symbols[RAW_STRING_END] && lexer->lookahead == '"') {
|
||||
while (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
quote_count++;
|
||||
}
|
||||
|
||||
if (quote_count == scanner->quote_count) {
|
||||
lexer->result_symbol = RAW_STRING_END;
|
||||
scanner->quote_count = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
did_advance = quote_count > 0;
|
||||
}
|
||||
|
||||
if (valid_symbols[RAW_STRING_CONTENT]) {
|
||||
while (lexer->lookahead) {
|
||||
if (lexer->lookahead == '"') {
|
||||
lexer->mark_end(lexer);
|
||||
quote_count = 0;
|
||||
|
||||
while (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
quote_count++;
|
||||
}
|
||||
|
||||
if (quote_count == scanner->quote_count) {
|
||||
lexer->result_symbol = RAW_STRING_CONTENT;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
advance(lexer);
|
||||
did_advance = true;
|
||||
}
|
||||
lexer->mark_end(lexer);
|
||||
lexer->result_symbol = RAW_STRING_CONTENT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_REGULAR_START] || valid_symbols[INTERPOLATION_VERBATIM_START] ||
|
||||
valid_symbols[INTERPOLATION_RAW_START]) {
|
||||
while (iswspace(lexer->lookahead)) {
|
||||
skip(lexer);
|
||||
}
|
||||
|
||||
uint8_t dollar_advanced = 0;
|
||||
|
||||
bool is_verbatim = false;
|
||||
|
||||
if (lexer->lookahead == '@') {
|
||||
is_verbatim = true;
|
||||
advance(lexer);
|
||||
}
|
||||
|
||||
while (lexer->lookahead == '$' && quote_count == 0) {
|
||||
advance(lexer);
|
||||
dollar_advanced++;
|
||||
}
|
||||
|
||||
if (dollar_advanced > 0 && (lexer->lookahead == '"' || lexer->lookahead == '@')) {
|
||||
lexer->result_symbol = INTERPOLATION_REGULAR_START;
|
||||
Interpolation interpolation = {
|
||||
.dollar_count = dollar_advanced,
|
||||
.open_brace_count = 0,
|
||||
.quote_count = 0,
|
||||
.string_type = 0,
|
||||
};
|
||||
|
||||
if (is_verbatim || lexer->lookahead == '@') {
|
||||
if (lexer->lookahead == '@') {
|
||||
advance(lexer);
|
||||
is_verbatim = true;
|
||||
}
|
||||
lexer->result_symbol = INTERPOLATION_VERBATIM_START;
|
||||
interpolation.string_type = VERBATIM;
|
||||
}
|
||||
|
||||
lexer->mark_end(lexer);
|
||||
advance(lexer);
|
||||
|
||||
if (lexer->lookahead == '"' && !is_verbatim) {
|
||||
advance(lexer);
|
||||
if (lexer->lookahead == '"') {
|
||||
lexer->result_symbol = INTERPOLATION_RAW_START;
|
||||
interpolation.string_type |= RAW;
|
||||
array_push(&scanner->interpolation_stack, interpolation);
|
||||
}
|
||||
// If we find 1 or 3 quotes, we push an interpolation.
|
||||
// If there's only two quotes, that's just an empty string
|
||||
} else {
|
||||
interpolation.string_type |= REGULAR;
|
||||
array_push(&scanner->interpolation_stack, interpolation);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_START_QUOTE] && scanner->interpolation_stack.size > 0) {
|
||||
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
|
||||
|
||||
if (is_verbatim(current_interpolation) || is_regular(current_interpolation)) {
|
||||
if (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
current_interpolation->quote_count++;
|
||||
}
|
||||
} else {
|
||||
while (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
current_interpolation->quote_count++;
|
||||
}
|
||||
}
|
||||
|
||||
lexer->result_symbol = INTERPOLATION_START_QUOTE;
|
||||
return current_interpolation->quote_count > 0;
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_END_QUOTE] && scanner->interpolation_stack.size > 0) {
|
||||
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
|
||||
|
||||
while (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
quote_count++;
|
||||
}
|
||||
|
||||
if (quote_count == current_interpolation->quote_count) {
|
||||
lexer->result_symbol = INTERPOLATION_END_QUOTE;
|
||||
array_pop(&scanner->interpolation_stack);
|
||||
return true;
|
||||
}
|
||||
|
||||
did_advance = quote_count > 0;
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_OPEN_BRACE] && scanner->interpolation_stack.size > 0) {
|
||||
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
|
||||
|
||||
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->dollar_count) {
|
||||
advance(lexer);
|
||||
brace_advanced++;
|
||||
}
|
||||
|
||||
if (brace_advanced > 0 && brace_advanced == current_interpolation->dollar_count &&
|
||||
(brace_advanced == 0 || lexer->lookahead != '{')) {
|
||||
current_interpolation->open_brace_count = brace_advanced;
|
||||
lexer->result_symbol = INTERPOLATION_OPEN_BRACE;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_CLOSE_BRACE] && scanner->interpolation_stack.size > 0) {
|
||||
uint8_t brace_advanced = 0;
|
||||
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
|
||||
|
||||
while (iswspace(lexer->lookahead)) {
|
||||
advance(lexer);
|
||||
}
|
||||
|
||||
while (lexer->lookahead == '}') {
|
||||
advance(lexer);
|
||||
brace_advanced++;
|
||||
|
||||
if (brace_advanced == current_interpolation->open_brace_count) {
|
||||
current_interpolation->open_brace_count = 0;
|
||||
lexer->result_symbol = INTERPOLATION_CLOSE_BRACE;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valid_symbols[INTERPOLATION_STRING_CONTENT] && scanner->interpolation_stack.size > 0) {
|
||||
lexer->result_symbol = INTERPOLATION_STRING_CONTENT;
|
||||
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
|
||||
|
||||
while (lexer->lookahead) {
|
||||
// top-down approach, first see if it's raw
|
||||
if (is_raw(current_interpolation)) {
|
||||
if (lexer->lookahead == '"') {
|
||||
lexer->mark_end(lexer);
|
||||
advance(lexer);
|
||||
if (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
uint8_t quote_advanced = 2;
|
||||
while (lexer->lookahead == '"') {
|
||||
quote_advanced++;
|
||||
advance(lexer);
|
||||
}
|
||||
if (quote_advanced == current_interpolation->quote_count) {
|
||||
return did_advance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer->lookahead == '{') {
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->open_brace_count) {
|
||||
advance(lexer);
|
||||
brace_advanced++;
|
||||
}
|
||||
|
||||
if (brace_advanced == current_interpolation->open_brace_count &&
|
||||
(brace_advanced == 0 || lexer->lookahead != '{')) {
|
||||
return did_advance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// then verbatim, since it could be verbatim + raw, but run the raw branch first
|
||||
else if (is_verbatim(current_interpolation)) {
|
||||
if (lexer->lookahead == '"') {
|
||||
lexer->mark_end(lexer);
|
||||
advance(lexer);
|
||||
if (lexer->lookahead == '"') {
|
||||
advance(lexer);
|
||||
continue;
|
||||
}
|
||||
return did_advance;
|
||||
}
|
||||
|
||||
if (lexer->lookahead == '{') {
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->open_brace_count) {
|
||||
advance(lexer);
|
||||
brace_advanced++;
|
||||
}
|
||||
|
||||
if (brace_advanced == current_interpolation->open_brace_count &&
|
||||
(brace_advanced == 0 || lexer->lookahead != '{')) {
|
||||
return did_advance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// finally regular
|
||||
else if (is_regular(current_interpolation)) {
|
||||
if (lexer->lookahead == '\\' || lexer->lookahead == '\n' || lexer->lookahead == '"') {
|
||||
lexer->mark_end(lexer);
|
||||
return did_advance;
|
||||
}
|
||||
|
||||
if (lexer->lookahead == '{') {
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->open_brace_count) {
|
||||
advance(lexer);
|
||||
brace_advanced++;
|
||||
}
|
||||
|
||||
if (brace_advanced == current_interpolation->open_brace_count &&
|
||||
(brace_advanced == 0 || lexer->lookahead != '{')) { // if we're in a brace we're not allowed to
|
||||
// collect more than the open_brace_count
|
||||
return did_advance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer->lookahead != '{') {
|
||||
brace_advanced = 0;
|
||||
}
|
||||
advance(lexer);
|
||||
did_advance = true;
|
||||
}
|
||||
|
||||
lexer->mark_end(lexer);
|
||||
return did_advance;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef TREE_SITTER_ALLOC_H_
|
||||
#define TREE_SITTER_ALLOC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Allow clients to override allocation functions
|
||||
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
||||
|
||||
extern void *(*ts_current_malloc)(size_t size);
|
||||
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
||||
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
||||
extern void (*ts_current_free)(void *ptr);
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc ts_current_malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc ts_current_calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc ts_current_realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free ts_current_free
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free free
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ALLOC_H_
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user