Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/tree-sitter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The tree-sitter grammar (tooling/tree-sitter-rx): src/ is generated but committed, since Zed compiles src/parser.c
# as it is, so check it is what grammar.js generates; then the corpus, and every tracked .rx file with no ERROR or
# MISSING node.
name: tree-sitter

on:
push:
branches: [main]
paths: ["tooling/tree-sitter-rx/**", "**/*.rx", ".github/workflows/tree-sitter.yml"]
pull_request:
paths: ["tooling/tree-sitter-rx/**", "**/*.rx", ".github/workflows/tree-sitter.yml"]
workflow_dispatch:

jobs:
grammar:
runs-on: ubuntu-latest
defaults:
run:
working-directory: tooling/tree-sitter-rx
steps:
- uses: actions/checkout@v5
- uses: pnpm/action-setup@v4
with:
version: 11.1.1 # as editor.yml
- uses: actions/setup-node@v5
with:
node-version: 22
- run: pnpm install --frozen-lockfile
- name: src/ is what grammar.js generates
run: |
pnpm run generate
git diff --exit-code -- src/
- name: Corpus
run: pnpm test
- name: Every tracked .rx file
run: pnpm run parse:repo
- name: Queries
run: pnpm run query -- ../../examples/*.rx > /dev/null
4 changes: 4 additions & 0 deletions tooling/tree-sitter-rx/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/parser.c linguist-generated
src/grammar.json linguist-generated
src/node-types.json linguist-generated
src/tree_sitter/** linguist-generated
9 changes: 9 additions & 0 deletions tooling/tree-sitter-rx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules/
build/
*.wasm
*.so
*.dylib
*.dll
*.o
log.html
.rx-files
55 changes: 55 additions & 0 deletions tooling/tree-sitter-rx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# tree-sitter-rx

A [tree-sitter](https://tree-sitter.github.io) grammar for `.rx` (and `.d.rx`),
for editors that highlight and navigate with tree-sitter: Zed (`../zed-rx`),
Neovim, Helix. It is not the language: `compiler/src/parse.rs` is, and this
grammar follows it. Where the two disagree, `parse.rs` is right and this is a
bug.

`grammar.js` extends tree-sitter-typescript's TSX grammar
(`grammar(tsx, { … })`, as TypeScript's extends JavaScript's). Expressions,
types and JSX keep TSX's node names, so a TSX query applies to them unchanged.
What `.rx` adds has nodes of its own:

| | nodes |
| --- | --- |
| module | `import_statement` (`side`, `pure`, `fails_clause`), `export_statement` |
| declarations | `source_declaration`, `equation`, `key_clause`, `fn_declaration` (`fn_body`), `fn_signature` (`.d.rx`, `rejects_clause`), `server_function_declaration`, `action_declaration`, `component_declaration`, `effect_declaration`, `error_declaration`, `context_declaration`, `provide_declaration` |
| markup | `view_block` (a reactive block: declarations, then markup), `if_block`, `else_block`, `for_block`, `loading_block`, `errored_block`, `portal_block`, `style_element` (`raw_text`) |
| statements | `write_statement` (`x := e`), `guess_statement`, `refresh_statement`, `affects_statement`, `action_statement` |
| expressions | `pending_expression`, `latest_expression`, `peek_expression`, `pre_expression`, `use_expression`, `throw_expression`, `catch_expression`, `draft_expression`, `fn_expression`, `server_fn_expression`; `await_expression` has `initial` (`-> v`) |

A declaration's name is its `name` field, its right side `value`, a block
`body`; a write's target is `target`. Imperative code (handlers, `action` and
`effect` bodies) is TSX's `statement_block`. TSX's assignment expressions are
gone: a statement-level `x = e` is an `equation`.

`src/scanner.c` is tree-sitter-typescript's external scanner with the changes
`.rx` needs, each marked `rx:`: a newline ends a statement before `<`, `(`, `[`
and a template (`parse.rs` reads none of them across a line) but not before
`catch`; on one line, a write (`a := 1 b := 2`) starts a new statement, as
`parse.rs` reads statements back to back; and a `<style>`'s CSS is raw text up
to `</style>`.

## Regenerating and testing

```sh
pnpm install
pnpm run generate # src/parser.c, src/grammar.json, src/node-types.json
pnpm test # test/corpus
pnpm run parse:repo # every tracked .rx file; none should have an ERROR or MISSING node
pnpm run query -- ../../examples/todos.rx
```

`src/` is generated but committed: Zed compiles `src/parser.c` and
`src/scanner.c` as they are. Regenerate after changing `grammar.js`, and
commit the result with it.

## Queries

`queries/highlights.scm` is tree-sitter-javascript's `highlights.scm` and
`highlights-jsx.scm` and tree-sitter-typescript's `highlights.scm`, then the
`.rx` patterns. Patterns are ordered general first, specific last: a later
pattern wins, as in Zed and Neovim. Helix (and `tree-sitter highlight`) take
the first match, so Helix needs the order reversed. `queries/injections.scm`
parses a `<style>`'s content as CSS.
Loading