|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# AST-Level Code Parsing |
| 6 | + |
| 7 | +vectorless-code uses tree-sitter to parse source code into semantic nodes — functions, classes, methods — instead of treating files as flat text. This produces a structured tree that the vectorless engine can navigate with precision. |
| 8 | + |
| 9 | +## Why AST Parsing Matters |
| 10 | + |
| 11 | +Naive code indexing treats each file as a single block of text. When you ask "how does authentication work", the engine has to scan entire files hoping to find relevant snippets. There's no understanding of what a function is, what a class contains, or how methods relate to their parent class. |
| 12 | + |
| 13 | +AST parsing changes this. The engine receives a tree like: |
| 14 | + |
| 15 | +``` |
| 16 | +src/auth.py |
| 17 | +├── class_definition: AuthService |
| 18 | +│ ├── function_definition: __init__ |
| 19 | +│ ├── function_definition: login |
| 20 | +│ └── function_definition: verify_token |
| 21 | +└── function_definition: create_session |
| 22 | +``` |
| 23 | + |
| 24 | +Now the Orchestrator can `cd` into `AuthService`, `ls` to see its methods, and `cat login` to read the authentication logic. This is the same navigation model that works for documents — applied to code with structural precision. |
| 25 | + |
| 26 | +## How It Works |
| 27 | + |
| 28 | +### Per-Language Node Types |
| 29 | + |
| 30 | +Each language defines which AST node types represent semantic units worth indexing: |
| 31 | + |
| 32 | +```python |
| 33 | +SPLITTABLE_NODE_TYPES = { |
| 34 | + "python": { |
| 35 | + "function_definition", |
| 36 | + "class_definition", |
| 37 | + "decorated_definition", |
| 38 | + "async_function_definition", |
| 39 | + }, |
| 40 | + "rust": { |
| 41 | + "function_item", |
| 42 | + "impl_item", |
| 43 | + "struct_item", |
| 44 | + "enum_item", |
| 45 | + "trait_item", |
| 46 | + "mod_item", |
| 47 | + }, |
| 48 | + # ... 12 languages total |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +tree-sitter parses the source into an AST, then vectorless-code walks the tree extracting nodes whose type matches this set. Each extracted node becomes a `CodeNode` with: |
| 53 | + |
| 54 | +- `name` — the symbol name (e.g. `AuthService`, `login`) |
| 55 | +- `node_type` — the AST node type (e.g. `class_definition`) |
| 56 | +- `content` — the full source code of the node |
| 57 | +- `children` — nested definitions (methods inside classes) |
| 58 | + |
| 59 | +### Nested Extraction |
| 60 | + |
| 61 | +When a class is extracted, its methods are extracted as children — not as separate top-level nodes. This preserves the parent-child relationship: |
| 62 | + |
| 63 | +```python |
| 64 | +# Input: Python source |
| 65 | +class AuthService: |
| 66 | + def login(self, username, password): |
| 67 | + token = self._create_token(username) |
| 68 | + return token |
| 69 | + |
| 70 | + def verify_token(self, token): |
| 71 | + return self._decode(token) |
| 72 | + |
| 73 | +# Output: CodeNode tree |
| 74 | +CodeNode( |
| 75 | + name="AuthService", |
| 76 | + node_type="class_definition", |
| 77 | + children=[ |
| 78 | + CodeNode(name="login", node_type="function_definition", ...), |
| 79 | + CodeNode(name="verify_token", node_type="function_definition", ...), |
| 80 | + ], |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +This nesting produces the raw_node tree that vectorless builds into a navigable Document. Level 1 = file, Level 2 = top-level definitions, Level 3 = nested definitions. |
| 85 | + |
| 86 | +### Name Extraction |
| 87 | + |
| 88 | +The parser extracts human-readable names from AST nodes by finding identifier children: |
| 89 | + |
| 90 | +- `function_definition` → looks for `identifier` child → `"login"` |
| 91 | +- `class_definition` → looks for `identifier` child → `"AuthService"` |
| 92 | +- `decorated_definition` → recurses into the decorated node |
| 93 | +- `impl_item` → looks for `type_identifier` → `"impl UserService"` |
| 94 | + |
| 95 | +## Fallback Strategy |
| 96 | + |
| 97 | +When tree-sitter is unavailable (unsupported language, grammar not installed, parse error), vectorless-code falls back to line-based splitting — splitting on blank-line boundaries into blocks. This produces flat `block` nodes without nesting, but still provides functional indexing. |
| 98 | + |
| 99 | +The fallback is transparent. The same `parse_file()` function handles both paths: |
| 100 | + |
| 101 | +```python |
| 102 | +def parse_file(file_path, content, language): |
| 103 | + parser = _get_parser(language) # cached per language |
| 104 | + if parser is None: |
| 105 | + return fallback_split(content, file_path, language) |
| 106 | + |
| 107 | + nodes = ast_extract(parser, content, language) |
| 108 | + if not nodes: |
| 109 | + return fallback_split(content, file_path, language) |
| 110 | + return nodes |
| 111 | +``` |
| 112 | + |
| 113 | +## Performance Considerations |
| 114 | + |
| 115 | +### Parser Caching |
| 116 | + |
| 117 | +tree-sitter `Parser` instances are cached per language. A 10,000-file Python project creates exactly one Python parser, reused for every `.py` file. This avoids repeated memory allocation and grammar loading. |
| 118 | + |
| 119 | +### Single-Pass File Scan |
| 120 | + |
| 121 | +Files are read exactly once. A single pass computes: |
| 122 | + |
| 123 | +1. File hash (SHA-256 for incremental detection) |
| 124 | +2. Stats (line count, byte size, language distribution) |
| 125 | +3. Content for parsing |
| 126 | + |
| 127 | +### Incremental Parsing |
| 128 | + |
| 129 | +On subsequent compiles, only files whose hash changed are re-parsed. Unchanged files reuse cached raw_nodes directly. See [Incremental Compilation](./incremental.mdx). |
| 130 | + |
| 131 | +## Adding a New Language |
| 132 | + |
| 133 | +To add support for a new language: |
| 134 | + |
| 135 | +1. Add the language to `SPLITTABLE_NODE_TYPES` with the relevant AST node types |
| 136 | +2. Add the tree-sitter grammar package to `pyproject.toml` dependencies |
| 137 | +3. Add the package mapping to `_LANG_PACKAGE_MAP` |
| 138 | + |
| 139 | +For example, to add Zig: |
| 140 | + |
| 141 | +```python |
| 142 | +# ast_parser.py |
| 143 | +SPLITTABLE_NODE_TYPES["zig"] = { |
| 144 | + "FunctionDecl", |
| 145 | + "TopLevelDecl", |
| 146 | +} |
| 147 | + |
| 148 | +_LANG_PACKAGE_MAP["zig"] = "tree_sitter_zig" |
| 149 | +``` |
| 150 | + |
| 151 | +```toml |
| 152 | +# pyproject.toml |
| 153 | +"tree-sitter-zig>=0.21", |
| 154 | +``` |
| 155 | + |
| 156 | +No other code changes needed. The parser, cache, fallback, and incremental systems handle it automatically. |
0 commit comments