@willbooster/tree-sitter-cpp
C++ grammar for tree-sitter, forked from tree-sitter/tree-sitter-cpp. We are grateful to its authors and contributors. This is not an official release of that project.
This fork fixes parsing bugs and raises conformance with the ISO C++ standard (working draft).
Usage
The npm package ships tree-sitter-cpp.wasm for
@willbooster/web-tree-sitter, which runs in Node.js, Bun,
browsers, and Cloudflare Workers. Install both:
In Node.js and Bun, load the .wasm file from its path:
import from 'node:url';
import from '@willbooster/web-tree-sitter';
await ;
const parser = ;
const wasmPath = ;
parser.;
const tree = parser.;
In browsers, serve both .wasm files and load them by URL. With Vite:
import from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
import cppUrl from '@willbooster/tree-sitter-cpp/tree-sitter-cpp.wasm?url';
await ;
const parser = ;
parser.;
In Cloudflare Workers, which do not allow compiling Wasm at run time, import both .wasm files as modules:
import from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
import cpp from '@willbooster/tree-sitter-cpp/tree-sitter-cpp.wasm';
await ;
const parser = ;
parser.;
The package also ships the node types in src/node-types.json.
In Rust, depend on the crate:
[]
= "0.27"
= { = "willbooster-tree-sitter-cpp", = "1" }
let mut parser = new;
parser.set_language?;
Development
bun run test runs:
- the corpus in
test/corpus, with the native build and with the Wasm build (the first run downloads the WASI SDK); - an incremental-parsing check (
test/unit/incremental.test.ts):tree-sitter fuzzedits each corpus case at random, reparses it, undoes the edits, and reparses again.TREE_SITTER_SEED,TREE_SITTER_ITERATIONS, andTREE_SITTER_EDITSrun other or more edits; - a check that the real-world C++ files in
examples/, the checked-in ones and those of the cloned repositories, fail to parse exactly as listed inscript/known-failures.txt. The first run clones the repositories. The example repositories are pinned to commits inscript/parse-examples. After a grammar change or a moved pin alters that list,script/parse-examplesrewrites it; review its diff before committing; - a performance check (
test/unit/performance.test.ts) that recovering from an error on each of 10,000 lines takes linear time, since consumers parse files while they are being edited. It loads the Wasm build through @willbooster/web-tree-sitter, whichbun run build/cirebuilds after regenerating the parser; - checks that the Wasm build parses in Chromium (
test/unit/browser/) and in Cloudflare Workers with and without Node.js compatibility (test/unit/workers.test.ts). Runbun run test/ci-setuponce to install Chromium.
CI also runs these tests on Linux arm64 and macOS, where the parser and scanner are compiled natively against each platform's C library, and fuzzes the parser with libFuzzer and sanitizers
(.github/workflows/robustness.yml).