Open
Conversation
Updated copyright year from 2023 to 2026.
- Test are not checked if still are correct
Added includes for string and functional in C++ example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improvement/Modern C++ Wrapper
Motivation / Context
I've spent some time improving the wrapper and adding missing features that were already available in the underlying Tree-sitter C API. This PR aims to make the C++ interface much more comprehensive, safer, and modern, while completely overhauling the CMake build system for better usability.
📌 Important Notes
0.26.8) now strictly tracks the base version of the underlying Tree-sitter C library. This ensures complete clarity for end-users regarding compatibility.CPP_TS_BUILD_TESTSCMake option.Key Changes
1. Architecture & Compatibility (C++ & OS)
TS_HAS_CXX17,TS_HAS_CXX20).ts::StringViewfallback to replacestd::string_viewfor pre-C++17 builds.conceptsfor type verification in the newvisit()function (with an SFINAE /std::enable_iffallback for older standards).fileno/dupvs_fileno/_dup)..hto.hppto better reflect C++ code, along with updated include guards (#pragma onceand#ifndef CPP_TREE_SITTER_HPP).2. Tree-sitter API Expansion
QueryandQueryCursorclasses, alongside helper structures likeQueryCapture,QueryMatch,QueryCursorState, andQueryPredicateStep.Point,InputEdit, andRange.Inputstruct that enables asynchronous or chunked reading of source text along with encoding support.LookaheadIteratorclass for inspecting possible symbols in a given parser state, and expanded navigation methods within theNodeclass.visit()function that allows for easy, depth-first traversal (DFS) of the syntax tree (implemented iteratively for performance).InputEncoding,SymbolType,LogType,Quantifier,QueryPredicateStepType) and support forLanguageMetadata.3. Safety & Memory Management
std::runtime_error,std::length_error,std::invalid_argument,std::logic_error) for issues like query errors, missing languages, or exceeding 4GB buffer limits.std::unique_ptrcoupled with custom deleters (details::FreeHelper), ensuring safe memory management for things like query cursors and iterators without leaks.validate()method toInputEditto ensure edit ranges are mathematically sound before applying them.4. WebAssembly (WASM) Support
WasmStoreclass to initialize and manage the lifecycle of the WASM environment and loaded grammars..wasmfiles.5. CMake Build System Overhaul
/W4,/Zc:__cplusplus) and aCPP_TS_MSVC_STATIC_RUNTIMEoption to toggle between static (/MT) and dynamic (/MD) MSVC runtime linking.grammar.cmake, with helpers inutils.cmakeandget_cpm.cmake.CPPTSAddGrammar: Now searches for grammars in local cache paths (CPP_TS_GRAMMAR_PATH) (resolves Pointing out existing parser's installation path #1)..wasmgrammars on-the-fly directly from GitHub Releases.scanner.cc,scanner.cpp), not just C.wasmtimelibrary for Windows, macOS, and Linux when theCPP_TS_FEATURE_WASMflag is enabled.CPP_TS_AMALGAMATEDoption to compile Tree-sitter from a single unified file (lib.c), significantly speeding up build times.