[P1] Add direct SWI-Prolog Tree-sitter C FFI #94
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
nsaspy/prolog-rlm#94
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Parent: #93
Goal
Add a small owned foreign interface that binds SWI-Prolog directly to the Tree-sitter C runtime. No Python, Node, Nim, subprocess, or parser service sits between Prolog and
libtree-sitter.This issue owns mechanical parser access and native resource lifetime, not language-specific semantic extraction.
Required architecture
Keep the C layer deliberately stupid. Tree-sitter mechanics belong here; project facts, language adapters, inference, SPEC, and VERIFY stay in Prolog.
Generic grammar contract
The FFI must operate on generic
TSLanguage *values rather than compile one Prolog predicate per programming language.Support loading a compatible generated grammar through a generic mechanism equivalent to:
A grammar such as Python may export
tree_sitter_python; another grammar exports its own symbol. The generic FFI must not require a C source edit to add each grammar.Dynamic loading must fail structurally for missing libraries, missing symbols, or incompatible Tree-sitter language ABI.
Resource model
Use SWI foreign blobs or an equivalent owned foreign-resource representation for long-lived native objects.
At minimum define safe wrappers/lifetimes for:
TSLanguage/ loaded grammar handle;TSParser;TSTree;Do not expose naked integer addresses as the supported public object representation.
TSNodeis a value referring into a tree. Its wrapper must retain/validate the owning tree lifetime so a node cannot remain apparently usable after its tree is freed.Finalizers/explicit cleanup must be deterministic and idempotent where applicable.
Initial public surface
Provide repository-convention predicates equivalent to:
Exact names/arity should follow repository conventions. Avoid a giant binding surface in the first slice.
Error semantics
Return/throw repository-consistent structured errors for at least:
Do not leak opaque native stderr as the API contract.
Threading / reentrancy
Document ownership and thread-safety assumptions for parser/tree objects under SWI's threaded runtime. Do not share a mutable
TSParseracross concurrent operations unless the wrapper deliberately serializes or proves that use safe.The first implementation may require one parser per logical parse worker/context.
Build integration
Acceptance criteria
Non-goals
Refs #93