Repository navigation
feat(parsers): add Metal (.metal) language support via the cpp grammar - #351
Conversation
Metal Shading Language is C++14, so MLX-style Python/C++/Metal kernels parse cleanly with the existing tree-sitter-cpp grammar. Add a Language.METAL identity backed by a thin MetalMapping(CppMapping) so .metal files get struct/function-aware chunks instead of the generic UNKNOWN fallback that emits no typed symbols. A dedicated tree-sitter-metal grammar can follow later; ERROR nodes around Metal-only qualifiers (thread/threadgroup, [[kernel]]) are expected and cAST chunks around them. Includes the required per-language coverage entries (LARGE_LANGUAGE_SAMPLES, parser minimal-code, QA content + extension maps) so the "every Language must be covered" guards pass; Metal samples mirror C++. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
3ab6ba0 to
e3c7975
Compare
ofriw
left a comment
There was a problem hiding this comment.
Note: This review was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!
Nice direction overall—the cheap-path reuse of the C++ grammar is reasonable. The blocking issue is that Language.METAL currently only exists at file-detection time, not end-to-end through parser output. In chunkhound/parsers/parser_factory.py, Metal is registered with language_name="cpp", and chunkhound/parsers/universal_parser.py derives both ParseResult.language and emitted Chunk.language from that config string. So a .metal file is detected and stored as metal, but its parsed chunks are emitted and persisted as cpp.
That split identity is the main thing to fix before merge. It leaks into storage and APIs (chunk_language vs file_language), and it also means the current tests miss the most important invariant: they verify routing and symbol extraction, but not that Metal stays Metal after parsing. I’d tighten this by making chunk/result language tagging consistent with the routed language, then adding contract coverage that asserts chunk.language == Language.METAL and ParseResult.language == Language.METAL for .metal inputs.
Attached is an agent optimized description of the changes in this PR - AGENT_REVIEW.md
Reviewer (PR chunkhound#351) flagged a split identity: .metal files were detected as METAL but parsed chunks/results were emitted and persisted as cpp, because the LanguageConfig used language_name="cpp". The grammar comes from the ts_cpp module object, not that string — the string only drives identity (ParseResult.language / Chunk.language) plus diagnostic labels. Dedup only special-cases vue/haskell, and queries come from MetalMapping, so nothing parse-relevant keys on "cpp". Tag it "metal" (mirrors the existing JSX→tsx-grammar precedent); identity now flows METAL through detection → ParseResult → Chunk with no changes to universal_parser. Add contract tests for the two missing invariants: chunk.language and ParseResult.language are METAL for .metal input. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Thanks for the careful review — you were right, the identity was only half wired up. Fixed in d7cec40. Root cause was exactly as you described: the The fix is a one-word change — Added the two contract tests for the invariant the first revision missed:
Both fail against the old |
…CP read-only, and fixes Merge main into matryoshka-fix-6-26. main changes incorporated: - DuckDB compaction with atomic swap (#339) - PowerShell (#350) and Metal (#351) language parsers - MCP --read-only flag (#329) - Research per-step timing (#355) - Various fixes: websearch late-completion guard, quickresearch orphan cleanup, token literal counting, claude-cli auto-updater - openai_compatible in factory's supported_providers() Conflict resolution: chunkhound/services/realtime_indexing_service.py was deleted on main (dead code, no production callers). The branch's ModuleType __class__ hack + its test were removed to match.
Why
Metal Shading Language is a C++14 dialect. MLX's GPU backend is a
Python↔C++↔Metal seam where the
.metalkernels carry the real compute(
reduce,arg_reduce,gemm, …). Today ChunkHound routes.metaltoLanguage.UNKNOWN, so those kernels chunk through the generic text fallback —no struct/function-typed symbols, so semantic and
--regexsymbol search can'tfind a kernel by its struct or reducer name. Metal/MSL users (and anyone
indexing an MLX tree) get struct/function-aware chunks instead.
Approach (cheap path)
MSL is C++14, so the existing
tree-sitter-cppgrammar already parses the realstructure. Keep a distinct
Language.METALidentity (so a dedicatedtree-sitter-metalgrammar can be swapped in later by changing one line) whilereusing all of
CppMapping's node handling through a thin subclass:LanguageConfigis registered withlanguage_name="metal". The cpp grammarcomes from the
ts_cppmodule object, not that string — the string onlydrives the public language identity (
ParseResult.language/Chunk.language)and diagnostic labels. This mirrors the existing JSX→tsx-grammar precedent
(
language_name="jsx"over the tsx grammar). Result:.metalstaysLanguage.METALend-to-end — detection, parse result, emitted chunks, andpersisted chunk language all agree.
Touchpoints (4 + test)
chunkhound/core/types/common.py—Language.METAL;.metal→METALinthe extension map;
METALadded tois_programming_languageandsupports_classes.chunkhound/parsers/mappings/metal.py— new,MetalMapping(CppMapping).chunkhound/parsers/mappings/__init__.py— exportMetalMapping.chunkhound/parsers/parser_factory.py—MetalMappingimport,LANGUAGE_CONFIGS[Language.METAL] = LanguageConfig(ts_cpp, MetalMapping, True, "metal"),and
".metal": Language.METALinEXTENSION_TO_LANGUAGE.tests/test_metal_mapping.py— new contract test through the real parser.Evidence — before → after
1. Routing flips
Before (
origin/main):After (branch):
2. Typed chunks the UNKNOWN fallback can't emit
Parsing the real
mlx/backend/metal/kernels/arg_reduce.metal(182 lines) viacreate_parser(Language.METAL).parse_content(...):tests/test_metal_mapping.pypins this contract against a real MLX snippet —asserts a
ChunkType.CLASSforIndexValPairby name and aChunkType.FUNCTIONforreduce_manyby name. Both are typed symbols theUNKNOWN text fallback cannot produce. The test references
Language.METAL,which does not exist on
main, so it fails there and passes on the branch.3. Language identity stays METAL end-to-end
The first revision routed
.metaltoMETALbut emitted chunks taggedcpp(the config's
language_namefed both grammar selection and identity). Thatsplit is now fixed —
tests/test_metal_mapping.pypins the invariant:test_metal_chunks_stay_tagged_metal— every emitted chunk isLanguage.METALtest_metal_parse_result_tagged_metal—ParseResult.language == Language.METAL4. CLI e2e
5. No regression
Lint: changed/new files clean (
ruff check, noruff formatper version-skewpolicy). The
I001import-sort warning ruff reports oncommon.py/parser_factory.pypre-exists onorigin/mainand is not touched by thischange.
Cheap-path scope (expected)
thread/threadgroupaddress spaces and[[kernel]]attributes aren't C++; the cppgrammar emits ~16 localized ERROR nodes on a real kernel and cAST chunks
around them. These tests assert functions/structs are captured, not zero
errors.
Language.METAL. Identity is derived from the routedlanguage, not the grammar object, so detection /
ParseResult.language/Chunk.language/ persistedchunks.languageall readmetal.tree-sitter-metalgrammar deferred. Swapping it in later is aone-line change to the
LanguageConfigforLanguage.METAL.🤖 Generated with Claude Code