Skip to content

Repository files navigation

tree-sitter-chialisp

A tree-sitter grammar for Chialisp (cl-26) and CLVM — the on-chain languages of the Chia blockchain.

Design: a generic homoiconic s-expression core

Chialisp is a Lisp. Its surface is fully described by the s-expression reader in the clvm_tools_rs compiler frontend (src/compiler/sexp.rs): a list, an atom, a number, a hex byte-string, a quoted string, a comment, and the cons dot. Every formmod, defun[-inline], defconstant/defconst, let/let*, assign[-inline]/assign-lambda, lambda, include, embed-file/ compile-file, namespace/import/export, and every operator/opcode — is an ordinary list whose head atom carries its meaning. The compiler applies a keyword table after reading plain s-expressions; it does not have a distinct grammar per keyword, and neither does this grammar.

So the node-type inventory is small and stable:

Node Meaning
source_file the whole file: a sequence of s-expressions
list ( … ) — proper, or dotted/improper ( … . x )
symbol an atom / identifier (defun, *standard-cl-26*, @, +)
number a decimal integer (5, -5)
hex a hex byte-string (0xdeadbeef)
string a "…" or '…' quoted string (with \ escapes)
comment a ; line comment (in extras)
dot the cons dot in a dotted pair ( a . b )

There are no per-keyword rules. A downstream consumer keys on list + the text of the head symbol — exactly the shape a Scheme-family extractor already understands.

Provenance

Authored clean-room from the authoritative reader — the clvm_tools_rs compiler frontend src/compiler/{sexp.rs, frontend.rs, dialect.rs, preprocessor/mod.rs}. Quexington's tree-sitter-chialisp and Chia Network's LSP were read for understanding only; no code was copied.

Notable correctness points modeled directly from sexp.rs:

  • Commenttoken(seq(';', /[^\n]*/)). The token holds no terminator, so it ends cleanly at LF, at CRLF (the \r rides along; the LF is whitespace), or at EOF. This is the fix for Quexington's ;.*\r\n, which required a CRLF and so failed on every LF-terminated file and at end-of-file.
  • Cons dot vs. namespaced symbol — a lone whitespace-bounded . is the dot token; a . inside a bareword (std.prelude, foo.clib, e.g.) is part of the symbol. This mirrors sexp.rs, where a . arriving with a Bareword inner state is appended to the word, and only a . arriving with an Empty inner state opens a TermList.
  • Stringsboth " and ' terminate, and \ escapes the following character verbatim (QuotedText/QuotedEscaped).
  • Hex and number — hex byte-strings (0x…) and decimals (-?[0-9]+) are distinct from symbols; a bare - is the subtraction operator (a symbol), not a number.

Usage

npm install
npx tree-sitter generate
npx tree-sitter test          # corpus tests
npx tree-sitter parse file.clsp

File types: .clsp, .clib, .clinc.

For tooling that indexes the tree

  • comment is in extras, but note that tree-sitter's C API ts_node_named_child still counts named extras — a comment between a form's head and its name would shift named_child indices. Consumers should skip nodes of type comment when locating the head/name. (In the pinned cl-26 corpus this never occurs, but the guard is one line and worth having.)
  • dot is a named node and appears in named_child for dotted pairs; it never appears at a definition's head position.

License

MIT.

About

Tree-sitter grammar for Chialisp (cl-26) — the Chia smart-coin s-expression language. Clean-room, authored from the clvm_tools_rs compiler frontend.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages