|
| 1 | +# Tree-sitter CLI Setup for Local Development |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +Set up tree-sitter CLI for local grammar exploration and testing without affecting the production build. This enables: |
| 6 | +- Testing new language grammars before integration |
| 7 | +- Debugging parser issues with official tree-sitter tools |
| 8 | +- Exploring AST structures interactively |
| 9 | +- Comparing tree-sitter output with our parser implementation |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```bash |
| 14 | +# 1. Install tree-sitter CLI (if not already installed) |
| 15 | +cargo install tree-sitter-cli --locked |
| 16 | + |
| 17 | +# 2. Install a grammar (one-time per language) |
| 18 | +cd contributing/tree-sitter |
| 19 | +./scripts/setup.sh typescript |
| 20 | + |
| 21 | +# 3. Parse a file to see AST |
| 22 | +tree-sitter parse examples/typescript/comprehensive.ts |
| 23 | + |
| 24 | +# 4. Compare with our parser |
| 25 | +.codanna/scripts/compare-nodes.sh typescript |
| 26 | +``` |
| 27 | + |
| 28 | +## Directory Structure |
| 29 | + |
| 30 | +``` |
| 31 | +contributing/tree-sitter/ |
| 32 | +├── README.md # User documentation |
| 33 | +├── grammars/ # Cloned grammar repositories (gitignored) |
| 34 | +│ ├── tree-sitter-typescript/ |
| 35 | +│ ├── tree-sitter-python/ |
| 36 | +│ └── ... |
| 37 | +└── scripts/ |
| 38 | + ├── setup.sh # Install grammars on-demand |
| 39 | + └── explore-ast.sh # Helper to parse files |
| 40 | +
|
| 41 | +.codanna/scripts/ |
| 42 | +└── compare-nodes.sh # Compare tree-sitter with our parser |
| 43 | +``` |
| 44 | + |
| 45 | +## How It Works |
| 46 | + |
| 47 | +1. **Tree-sitter Configuration**: The setup script configures `~/.config/tree-sitter/config.json` to point to our grammars directory |
| 48 | +2. **Grammar Naming**: Grammars must be named `tree-sitter-{language}` for tree-sitter to find them |
| 49 | +3. **On-Demand Installation**: Install only the languages you need with `./scripts/setup.sh <language>` |
| 50 | +4. **Automatic Language Detection**: Tree-sitter determines which grammar to use based on file extension |
| 51 | + |
| 52 | +## Scripts |
| 53 | + |
| 54 | +### setup.sh |
| 55 | + |
| 56 | +Configures tree-sitter and installs grammars on-demand: |
| 57 | + |
| 58 | +```bash |
| 59 | +# Show installed grammars |
| 60 | +./scripts/setup.sh |
| 61 | + |
| 62 | +# Install specific language |
| 63 | +./scripts/setup.sh typescript |
| 64 | +./scripts/setup.sh python |
| 65 | +./scripts/setup.sh rust |
| 66 | +``` |
| 67 | + |
| 68 | +Supported languages: typescript, python, rust, go, php, c, cpp |
| 69 | + |
| 70 | +### explore-ast.sh |
| 71 | + |
| 72 | +Simple wrapper for parsing files: |
| 73 | + |
| 74 | +```bash |
| 75 | +./scripts/explore-ast.sh examples/typescript/comprehensive.ts |
| 76 | +``` |
| 77 | + |
| 78 | +### compare-nodes.sh |
| 79 | + |
| 80 | +Compares tree-sitter AST nodes with our parser implementation: |
| 81 | + |
| 82 | +```bash |
| 83 | +# From project root |
| 84 | +.codanna/scripts/compare-nodes.sh typescript |
| 85 | +``` |
| 86 | + |
| 87 | +This script: |
| 88 | +1. Parses the comprehensive example with tree-sitter |
| 89 | +2. Runs our parser tests (which regenerates audit reports) |
| 90 | +3. Shows differences between the two parsers |
| 91 | + |
| 92 | +## Usage Examples |
| 93 | + |
| 94 | +### Exploring AST Structure |
| 95 | + |
| 96 | +```bash |
| 97 | +# Parse any file with tree-sitter |
| 98 | +tree-sitter parse examples/typescript/comprehensive.ts |
| 99 | + |
| 100 | +# See specific node types |
| 101 | +tree-sitter parse examples/python/main.py | grep "class_definition" |
| 102 | +``` |
| 103 | + |
| 104 | +### Debugging Parser Differences |
| 105 | + |
| 106 | +```bash |
| 107 | +# Compare node recognition |
| 108 | +.codanna/scripts/compare-nodes.sh typescript |
| 109 | + |
| 110 | +# Output shows: |
| 111 | +# - Nodes tree-sitter finds that we don't handle |
| 112 | +# - Nodes we report that tree-sitter doesn't find |
| 113 | +``` |
| 114 | + |
| 115 | +### Testing New Languages |
| 116 | + |
| 117 | +```bash |
| 118 | +# Install a new grammar |
| 119 | +./contributing/tree-sitter/scripts/setup.sh go |
| 120 | + |
| 121 | +# Test parsing |
| 122 | +tree-sitter parse examples/go/main.go |
| 123 | +``` |
| 124 | + |
| 125 | +## Benefits |
| 126 | + |
| 127 | +1. **Official Reference**: Compare our parser against the official tree-sitter implementation |
| 128 | +2. **Node Discovery**: Find exact node names for parser implementation |
| 129 | +3. **Grammar Updates**: Test new grammar versions before updating dependencies |
| 130 | +4. **Debugging**: Identify parsing differences and missing node handlers |
| 131 | +5. **Report Generation**: Running compare-nodes.sh updates audit reports with current timestamps |
| 132 | + |
| 133 | +## Notes |
| 134 | + |
| 135 | +- Grammars are cloned with `--depth 1` for speed |
| 136 | +- The grammars directory is gitignored (contains large files) |
| 137 | +- Each developer's tree-sitter config points to their local grammar directory |
| 138 | +- No environment variables or .env files needed - uses tree-sitter's native configuration |
| 139 | +- Running compare-nodes.sh regenerates the audit reports as a side effect |
| 140 | + |
| 141 | +## Integration with Development Workflow |
| 142 | + |
| 143 | +1. When implementing a new language parser: |
| 144 | + - Install the grammar: `./scripts/setup.sh <language>` |
| 145 | + - Parse examples to understand AST structure |
| 146 | + - Use compare-nodes.sh to verify coverage |
| 147 | + |
| 148 | +2. When debugging parsing issues: |
| 149 | + - Parse the problematic file with tree-sitter |
| 150 | + - Compare output with our parser |
| 151 | + - Identify missing or incorrectly handled nodes |
| 152 | + |
| 153 | +3. When updating parser coverage: |
| 154 | + - Run compare-nodes.sh to see gaps |
| 155 | + - Implement missing node handlers |
| 156 | + - Verify with updated audit reports |
0 commit comments