Skip to content

Commit 403fab8

Browse files
dancinlifeclaude
andcommitted
feat(tree-sitter): add tree-sitter-n6 grammar
Total line model (no external scanner / look-around) — every line is comment/header/edge/body/text/blank, exposing type + edge_op (7-edge alphabet). Verified 0 ERROR on all examples/*.n6. grammar.js + queries/highlights.scm + package.json (src/ generated, gitignored). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0bf9076 commit 403fab8

5 files changed

Lines changed: 76 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ Conservative — unrecognised continuations are hints, never hard errors.
176176
A Claude Code plugin can wire it via `.lsp.json`:
177177
`{ "n6": { "command": "n6-lsp", "extensionToLanguage": {".n6":"n6"} } }`.
178178

179+
### tree-sitter grammar (`tree-sitter-n6/`)
180+
181+
A tree-sitter grammar for editors on the tree-sitter stack (Neovim, Helix,
182+
Zed, Emacs). Total line model — every line resolves to `comment` / `header`
183+
/ `edge` / `body` / `text` / `blank`, exposing `type` and `edge_op` nodes
184+
for `queries/highlights.scm`. Verified: `tree-sitter parse` reaches **0
185+
ERROR** on all `examples/*.n6`. Build: `cd tree-sitter-n6 && tree-sitter generate`.
186+
179187
## Repo layout
180188

181189
```

tree-sitter-n6/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/
2+
node_modules/

tree-sitter-n6/grammar.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* tree-sitter grammar for .n6 (knowledge-atlas grammar, v1).
3+
*
4+
* Total line model, no external scanner, no regex look-around. Every
5+
* line token requires a trailing newline → the top-level repeat always
6+
* progresses (no parser hang). .n6 files are LF-terminated by spec.
7+
* Exposes `type` + `edge_op` for queries/highlights.scm; quoted prose,
8+
* `key = expr` continuations and CJK fall through to `body` / `text`.
9+
*/
10+
module.exports = grammar({
11+
name: 'n6',
12+
13+
extras: $ => [],
14+
15+
rules: {
16+
source_file: $ => repeat($._line),
17+
18+
_line: $ => choice(
19+
$.blank,
20+
$.comment,
21+
$.header,
22+
$.edge,
23+
$.body,
24+
$.text,
25+
),
26+
27+
blank: $ => token(prec(1, /[ \t]*\n/)),
28+
29+
comment: $ => token(prec(5, /[ \t]*#[^\n]*\n/)),
30+
31+
header: $ => seq($.type, $._rest),
32+
33+
type: $ => token(prec(4, /@[A-Z?]/)),
34+
35+
edge: $ => seq($._indent, $.edge_op, $._rest),
36+
37+
body: $ => seq($._indent, $._rest),
38+
39+
_indent: $ => token(prec(3, /[ \t]+/)),
40+
41+
// .n6 v1 edge alphabet (7): depends/derives/application/equivalent/
42+
// converges/verified/breakthrough
43+
edge_op: $ => token(prec(4, choice(
44+
'<-', '->', '=>', '==', '~>', '|>', '!!',
45+
))),
46+
47+
_rest: $ => token(prec(1, /[^\n]*\n/)),
48+
49+
text: $ => token(prec(0, /[^ \t#@\n][^\n]*\n/)),
50+
},
51+
});

tree-sitter-n6/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "tree-sitter-n6",
3+
"version": "0.1.0",
4+
"description": "tree-sitter grammar for the .n6 format",
5+
"repository": "https://github.com/dancinlab/n6",
6+
"license": "CC0-1.0",
7+
"keywords": ["tree-sitter", "parser", "n6", "dancinlab"],
8+
"tree-sitter": [
9+
{ "scope": "source.n6", "file-types": ["n6"], "highlights": ["queries/highlights.scm"] }
10+
]
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; tree-sitter highlight query for .n6
2+
(comment) @comment
3+
(type) @keyword
4+
(edge_op) @operator

0 commit comments

Comments
 (0)