|
| 1 | +# Agent Instructions |
| 2 | + |
| 3 | +This repository contains a [tree-sitter](https://tree-sitter.github.io/) |
| 4 | +grammar for [NATS server](https://nats.io/) configuration files. |
| 5 | + |
| 6 | +## Project overview |
| 7 | + |
| 8 | +The grammar parses `.conf` files used by `nats-server`. Key source files: |
| 9 | + |
| 10 | +- `grammar.js` — the grammar definition (entry point for tree-sitter) |
| 11 | +- `src/scanner.c` — external scanner for context-sensitive tokens (bare |
| 12 | + strings, comments, identifiers, booleans) |
| 13 | +- `queries/` — highlight, fold, indent, and locals queries (`.scm` files) |
| 14 | +- `test/corpus/` — tree-sitter test corpus (`.txt` files) |
| 15 | +- `examples/` — sample NATS config files used by `tree-sitter parse --stat` |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- **Node.js 22.x** (see `.tool-versions`; Node 24+ is incompatible with |
| 20 | + tree-sitter-cli 0.25.x) |
| 21 | +- **tree-sitter CLI** — install via `cargo install tree-sitter-cli` (not an |
| 22 | + npm dependency) |
| 23 | +- **[Task](https://taskfile.dev/)** (optional but recommended) — task runner; |
| 24 | + see `Taskfile.yaml` |
| 25 | + |
| 26 | +## Common workflows |
| 27 | + |
| 28 | +All commands assume you are in the repository root. |
| 29 | + |
| 30 | +### Setup |
| 31 | + |
| 32 | +```sh |
| 33 | +npm ci |
| 34 | +command -v tree-sitter || cargo install tree-sitter-cli |
| 35 | +``` |
| 36 | + |
| 37 | +### Generate the parser |
| 38 | + |
| 39 | +After editing `grammar.js`, regenerate the C parser: |
| 40 | + |
| 41 | +```sh |
| 42 | +tree-sitter generate # or: task generate |
| 43 | +``` |
| 44 | + |
| 45 | +### Run tests |
| 46 | + |
| 47 | +```sh |
| 48 | +tree-sitter test # or: task test |
| 49 | +``` |
| 50 | + |
| 51 | +This runs the corpus in `test/corpus/`. Every test file contains input |
| 52 | +fragments and expected S-expression parse trees. When adding or changing |
| 53 | +grammar rules, update or add corpus tests to cover them. |
| 54 | + |
| 55 | +### Parse example files |
| 56 | + |
| 57 | +```sh |
| 58 | +tree-sitter parse examples/*.conf --stat # or: task test:parse-examples |
| 59 | +``` |
| 60 | + |
| 61 | +All example files must parse without errors. |
| 62 | + |
| 63 | +### Full CI check (locally) |
| 64 | + |
| 65 | +```sh |
| 66 | +task ci:test |
| 67 | +``` |
| 68 | + |
| 69 | +This runs both `test` and `test:parse-examples`. |
| 70 | + |
| 71 | +## Making changes |
| 72 | + |
| 73 | +### Grammar changes (`grammar.js` / `src/scanner.c`) |
| 74 | + |
| 75 | +1. Edit `grammar.js` (and `src/scanner.c` if the change involves external |
| 76 | + tokens). |
| 77 | +2. Run `tree-sitter generate` to regenerate `src/parser.c`. |
| 78 | +3. Add or update test cases in `test/corpus/`. |
| 79 | +4. Run `tree-sitter test` — all tests must pass. |
| 80 | +5. Run `tree-sitter parse examples/*.conf --stat` — no errors allowed. |
| 81 | +6. If the change adds or renames node types, update the queries in `queries/` |
| 82 | + to match. |
| 83 | + |
| 84 | +### Query changes (`queries/*.scm`) |
| 85 | + |
| 86 | +- `highlights.scm` — syntax highlighting |
| 87 | +- `folds.scm` — code folding regions |
| 88 | +- `indents.scm` — auto-indentation rules |
| 89 | +- `locals.scm` — scope/reference tracking |
| 90 | + |
| 91 | +After editing queries, run `tree-sitter test` to ensure they are still valid |
| 92 | +against the grammar. Highlight queries can be previewed with |
| 93 | +`tree-sitter highlight examples/full.conf`. |
| 94 | + |
| 95 | +### Test corpus (`test/corpus/*.txt`) |
| 96 | + |
| 97 | +Tests use tree-sitter's standard format: |
| 98 | + |
| 99 | +``` |
| 100 | +=== |
| 101 | +Test name |
| 102 | +=== |
| 103 | +input text |
| 104 | +--- |
| 105 | +(expected_sexpression) |
| 106 | +``` |
| 107 | + |
| 108 | +Group related tests in the appropriate file (e.g., `blocks.txt` for block |
| 109 | +syntax, `types.txt` for value types). Each test should be focused and minimal. |
| 110 | + |
| 111 | +### Editor integration docs (`docs/editors/`) |
| 112 | + |
| 113 | +These are per-editor setup guides. When changing grammar node names or query |
| 114 | +file structure, review and update affected editor docs. |
| 115 | + |
| 116 | +## CI |
| 117 | + |
| 118 | +The GitHub Actions workflow (`.github/workflows/ci.yaml`) runs on pushes to |
| 119 | +`main` and on pull requests. It runs two jobs: |
| 120 | + |
| 121 | +- **Test** — `task ci:test` (generate, test, parse examples) |
| 122 | +- **Check GitHub Actions** — lints workflows with |
| 123 | + [zizmor](https://github.com/woodruffw/zizmor) |
| 124 | + |
| 125 | +All CI checks must pass before merging. |
| 126 | + |
| 127 | +## Code style |
| 128 | + |
| 129 | +- `grammar.js` uses the tree-sitter DSL (`grammar()`, `seq()`, `choice()`, |
| 130 | + `repeat()`, etc.). Keep rules concise and well-commented. |
| 131 | +- The external scanner (`src/scanner.c`) is plain C. Follow the existing style |
| 132 | + and keep functions short. |
| 133 | +- Query files (`.scm`) use S-expression syntax. One capture pattern per line. |
| 134 | +- Do not edit `src/parser.c` by hand — it is generated. |
| 135 | +- Do not commit `node_modules/`, build artifacts, or `.wasm` files (see |
| 136 | + `.gitignore`). |
0 commit comments