Skip to content

Commit 6c13401

Browse files
committed
Merge branch 'main' into fix-and-scanner
2 parents 746a967 + 130cd8e commit 6c13401

65 files changed

Lines changed: 296163 additions & 1821 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
6+
[*.{json,toml,yml,gyp}]
7+
indent_style = space
8+
indent_size = 2
9+
10+
[*.js]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.scm]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[*.{c,cc,h}]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.rs]
23+
indent_style = space
24+
indent_size = 4
25+
26+
[*.{py,pyi}]
27+
indent_style = space
28+
indent_size = 4
29+
30+
[*.swift]
31+
indent_style = space
32+
indent_size = 4
33+
34+
[*.go]
35+
indent_style = tab
36+
indent_size = 8
37+
38+
[Makefile]
39+
indent_style = tab
40+
indent_size = 8
41+
42+
[parser.c]
43+
indent_size = 2
44+
45+
[{alloc,array,parser}.h]
46+
indent_size = 2

.gitattributes

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
* text=auto eol=lf
2+
3+
# Generated source files
4+
src/*.json linguist-generated
5+
src/parser.c linguist-generated
6+
src/tree_sitter/* linguist-generated
7+
8+
# C bindings
9+
bindings/c/** linguist-generated
10+
CMakeLists.txt linguist-generated
11+
Makefile linguist-generated
12+
13+
# Rust bindings
14+
bindings/rust/* linguist-generated
15+
Cargo.toml linguist-generated
16+
Cargo.lock linguist-generated
17+
18+
# Node.js bindings
19+
bindings/node/* linguist-generated
20+
binding.gyp linguist-generated
21+
package.json linguist-generated
22+
package-lock.json linguist-generated
23+
24+
# Python bindings
25+
bindings/python/** linguist-generated
26+
setup.py linguist-generated
27+
pyproject.toml linguist-generated
28+
29+
# Go bindings
30+
bindings/go/* linguist-generated
31+
go.mod linguist-generated
32+
go.sum linguist-generated
33+
34+
# Swift bindings
35+
bindings/swift/** linguist-generated
36+
Package.swift linguist-generated
37+
Package.resolved linguist-generated
38+
39+
# Zig bindings
40+
build.zig linguist-generated
41+
build.zig.zon linguist-generated

.github/workflows/build-test.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [main]
5+
paths:
6+
- grammar.js
7+
- src/**
8+
- test/**
9+
- bindings/**
10+
- binding.gyp
11+
- package.json
12+
- package-lock.json
13+
- .nvmrc
14+
- .github/workflows/**
15+
pull_request:
16+
paths:
17+
- grammar.js
18+
- src/**
19+
- test/**
20+
- bindings/**
21+
- binding.gyp
22+
- package.json
23+
- package-lock.json
24+
- .nvmrc
25+
- .github/workflows/**
26+
27+
concurrency:
28+
group: ${{github.workflow}}-${{github.ref}}
29+
cancel-in-progress: true
30+
31+
jobs:
32+
test:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
38+
- name: Set up Node.js
39+
uses: actions/setup-node@v4
40+
with:
41+
node-version-file: '.nvmrc'
42+
43+
- name: Set up tree-sitter
44+
uses: tree-sitter/setup-action/cli@v2
45+
with:
46+
tree-sitter-ref: v0.26.3
47+
48+
- name: Run tests
49+
uses: tree-sitter/parser-test-action@v2
50+
with:
51+
test-rust: true
52+
test-node: false
53+
test-python: true
54+
test-go: true
55+
test-swift: false

.github/workflows/gh-pages.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: pages
15+
cancel-in-progress: false
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version-file: .nvmrc
28+
29+
- name: Install dependencies
30+
run: npm ci
31+
32+
- name: Build WASM
33+
run: npx tree-sitter build --wasm
34+
35+
- name: Export playground
36+
run: npx tree-sitter playground --export ./public
37+
38+
- name: Set LANGUAGE_BASE_URL for GitHub Pages
39+
run: |
40+
sed -i 's|LANGUAGE_BASE_URL = ""|LANGUAGE_BASE_URL = "/tree-sitter-rescript"|' ./public/index.html
41+
42+
- name: Setup Pages
43+
uses: actions/configure-pages@v5
44+
45+
- name: Upload artifact
46+
uses: actions/upload-pages-artifact@v3
47+
with:
48+
path: ./public
49+
50+
deploy:
51+
environment:
52+
name: github-pages
53+
url: ${{ steps.deployment.outputs.page_url }}
54+
runs-on: ubuntu-latest
55+
needs: build
56+
steps:
57+
- name: Deploy to GitHub Pages
58+
id: deployment
59+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
1-
bindings/
1+
# Rust artifacts
2+
target/
3+
4+
# Node artifacts
5+
build/
6+
prebuilds/
27
node_modules/
3-
src/*
4-
!src/scanner.c
8+
9+
# Swift artifacts
10+
.build/
11+
12+
# Go artifacts
13+
_obj/
14+
15+
# Python artifacts
16+
.venv/
17+
dist/
18+
*.egg-info
19+
*.whl
20+
21+
# C artifacts
22+
*.a
23+
*.so
24+
*.so.*
25+
*.dylib
26+
*.dll
27+
*.pc
28+
*.exp
29+
*.lib
30+
31+
# Zig artifacts
32+
.zig-cache/
33+
zig-cache/
34+
zig-out/
35+
36+
# Example dirs
37+
/examples/*/
38+
39+
# Grammar volatiles
40+
*.wasm
41+
*.obj
42+
*.o
43+
44+
# Archives
45+
*.tar.gz
46+
*.tgz
47+
*.zip
48+
49+
# Others
550
log.html
6-
binding.gyp
751
test_wild/
8-
build/
52+
53+
# Playground
54+
public/

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v24.11.1

AGENTS.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# AGENTS.md
2+
3+
This file provides guidance to AI/LLM when working with code in this repository.
4+
5+
## Build and Test Commands
6+
7+
```bash
8+
# Generate parser from grammar (required after grammar.js changes)
9+
npx tree-sitter generate
10+
11+
# Run all corpus tests
12+
npx tree-sitter test
13+
14+
# Run tests for a specific grammar rule (filter by name)
15+
npx tree-sitter test --include "switch"
16+
17+
# Parse a file to see syntax tree (useful for debugging)
18+
npm run parse path/to/file.res
19+
20+
# Compare with ReScript compiler's AST (useful for debugging grammar issues)
21+
npx bsc -dparsetree -only-parse -ignore-parse-errors path/to/file.res
22+
23+
# Launch interactive playground (builds WASM first)
24+
npm start
25+
26+
# Install npm dependencies
27+
npm install
28+
```
29+
30+
## Architecture
31+
32+
### Grammar Definition
33+
34+
**grammar.js** - The main grammar definition using Tree-sitter's JavaScript DSL. Key sections:
35+
- `externals` - Tokens handled by the custom scanner (newlines, comments, template strings, decorators, parentheses)
36+
- `precedences` - Operator precedence rules for expressions and module paths
37+
- `conflicts` - Shift/reduce conflict resolutions for ambiguous constructs
38+
- `rules` - All grammar rules (~80+ covering statements, expressions, types, modules, JSX)
39+
40+
### External Scanner
41+
42+
**src/scanner.c** - Custom C scanner for context-sensitive tokens that the grammar alone cannot handle:
43+
- Significant vs insignificant newlines (statement-ending vs formatting)
44+
- Nested multiline comments (`/* /* */ */`)
45+
- Template string interpolation (`\`hello ${name}\``)
46+
- Parenthesis nesting tracking (affects newline significance)
47+
- `list{` and `dict{` constructor detection
48+
- Decorator parsing (`@decorator` vs `@decorator(args)`)
49+
50+
The scanner maintains state (`ScannerState`) tracking parenthesis nesting depth, whether inside quotes/backticks, and EOF reporting.
51+
52+
### Generated Files (do not edit manually)
53+
54+
- `src/parser.c` - Generated LR parser from grammar.json
55+
- `src/grammar.json` - Intermediate grammar representation
56+
- `src/node-types.json` - AST node type definitions
57+
58+
### Query Files
59+
60+
**queries/** - TreeSitter query files in S-expression syntax:
61+
- `highlights.scm` - Syntax highlighting rules mapping AST nodes to semantic scopes
62+
- `injections.scm` - Language injection boundaries
63+
- `locals.scm` - Variable scope definitions
64+
- `textobjects.scm` - Editor text object definitions
65+
66+
### Test Corpus
67+
68+
**test/corpus/*.txt** - Test cases in Tree-sitter's corpus format. Each test has a name, ReScript code, and expected parse tree. Files cover: comments, decorators, expressions, functions, JSX, let bindings, literals, modules, type declarations.
69+
70+
## Development Workflow
71+
72+
1. Modify `grammar.js` to add/fix grammar rules
73+
2. Run `tree-sitter generate` to regenerate the parser
74+
3. Add test cases in `test/corpus/*.txt` with expected parse trees
75+
4. Run `tree-sitter test` to verify
76+
5. For scanner changes, edit `src/scanner.c` and rebuild
77+
78+
## Language Bindings
79+
80+
Bindings are provided for Node.js, Python, Rust, Go, Swift, and C in the `bindings/` directory. CI tests Rust, Python, and Go bindings automatically.

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md

0 commit comments

Comments
 (0)