Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
be6d68c
Project Update
Muppetsg2 Apr 4, 2026
7105837
Readme Update
Muppetsg2 Apr 4, 2026
68a6613
compile flags
Muppetsg2 Apr 4, 2026
b6b0802
Update CMakeLists.txt
Muppetsg2 Apr 4, 2026
f0b8172
Update cpp-tree-sitter.hpp
Muppetsg2 Apr 4, 2026
7eb3172
Visitor
Muppetsg2 Apr 5, 2026
de6f8d6
Parser and Language extension
Muppetsg2 Apr 5, 2026
e177709
Query
Muppetsg2 Apr 5, 2026
df5f979
Small bug fixes
Muppetsg2 Apr 5, 2026
62bdfa5
Node Update
Muppetsg2 Apr 6, 2026
e1c6db9
Defines update and code cleanup
Muppetsg2 Apr 6, 2026
17a8ccd
Parser wrapper finished
Muppetsg2 Apr 7, 2026
6c6c727
Tree class improvements
Muppetsg2 Apr 7, 2026
c01bf54
Tree Cursor and Node Update
Muppetsg2 Apr 7, 2026
fb88db0
Query Update
Muppetsg2 Apr 7, 2026
86cde9f
LookaheadIterator
Muppetsg2 Apr 7, 2026
f83fded
Wasm, CMake and Readme
Muppetsg2 Apr 8, 2026
0d46b33
Update copyright year in README
Muppetsg2 Apr 8, 2026
1824db1
Update README.md
Muppetsg2 Apr 8, 2026
8eef2d0
Merge branch 'main' of https://github.com/Muppetsg2/cpp-tree-sitter
Muppetsg2 Apr 8, 2026
41ddd77
Update cpp-tree-sitter.hpp
Muppetsg2 Apr 8, 2026
3395429
Tests and Bug Fixes
Muppetsg2 Apr 9, 2026
d2fc38b
Fix typo in README regarding FreeHelper
Muppetsg2 Apr 9, 2026
40f8979
tests version 1
Muppetsg2 Apr 9, 2026
4674025
Tests and bug fixes in code
Muppetsg2 Apr 10, 2026
056655b
Wasm and shared libs
Muppetsg2 Apr 10, 2026
e34579a
wasm grammar download and safety improvements
Muppetsg2 Apr 13, 2026
c59c9c5
CMake Update and Bug fixes
Muppetsg2 Apr 13, 2026
38a005d
CPPTSAddGrammar naming changed
Muppetsg2 Apr 13, 2026
4478359
New loadLanguage function
Muppetsg2 Apr 13, 2026
7ae59fc
Repairs and Wasmtime local directory
Muppetsg2 Apr 13, 2026
74da5a3
wasm test fixes
Muppetsg2 Apr 13, 2026
1225f8f
Update README.md
Muppetsg2 Apr 13, 2026
47e76bb
Update cpp-tree-sitter.hpp
Muppetsg2 Apr 13, 2026
c7d4569
More tests and bug fixes
Muppetsg2 Apr 13, 2026
1185c4a
Update README.md
Muppetsg2 Apr 13, 2026
5b4654d
Update cpp-tree-sitter version to 0.26.8
Muppetsg2 Apr 13, 2026
43e1955
Update C++ example in README with additional includes
Muppetsg2 Apr 13, 2026
bfe1316
Readme updates
Muppetsg2 Apr 13, 2026
05c6abe
ts::visit callback fixes
Muppetsg2 Apr 14, 2026
12116bb
Update lambda function syntax in README example
Muppetsg2 Apr 14, 2026
850a4be
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 14, 2026
9269cc1
MSVC runtime lib fixes
Muppetsg2 Apr 15, 2026
c3c3fe0
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 15, 2026
eecafdd
Project fixes
Muppetsg2 Apr 15, 2026
9f18340
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 15, 2026
1144b3f
getChildByType
Muppetsg2 Apr 15, 2026
e837bde
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 15, 2026
adaddd3
C++23 support
Muppetsg2 Apr 16, 2026
8c0a417
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 16, 2026
b80b2b0
Fix return type declarations in Children class
Muppetsg2 Apr 17, 2026
ef8ecd1
Merge branch 'main' into improvement/modern-cpp-wrapper
Muppetsg2 Apr 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4

AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AlignEscapedNewlines: Left

InsertBraces: true
BreakAfterReturnType: None
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllArgumentsOnNextLine: false
AlignAfterOpenBracket: Align
PenaltyReturnTypeOnItsOwnLine: 1000
PenaltyBreakAssignment: 500
BreakBeforeBinaryOperators: NonAssignment

AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
IncludeCategories:
- Regex: '^".*'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 4
UseTab: Never
InsertNewlineAtEOF: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
TabWidth: 4

ReflowComments: true
SortUsingDeclarations: true
Standard: c++20
...
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@
*.exe
*.out
*.app

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json

cpm_cache/
out/
\.vs/
[Bb]uild/
15 changes: 15 additions & 0 deletions .runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>

<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<TestSessionTimeout>60000</TestSessionTimeout><!-- Milliseconds -->
</RunConfiguration>

<Catch2Adapter>
<CombinedTimeout>30000</CombinedTimeout><!-- 30s in Milliseconds -->
<FilenameFilter>^cpp-tree-sitter-tests</FilenameFilter>
<TestCaseTimeout>20000</TestCaseTimeout><!-- 20s in Milliseconds -->
</Catch2Adapter>

</RunSettings>
236 changes: 159 additions & 77 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,91 +1,173 @@
cmake_minimum_required(VERSION 3.19)
project(cpp-tree-sitter)
cmake_minimum_required(VERSION 3.30)

set(PACKAGE_NAME cpp-tree-sitter)
set(PACKAGE_VERSION 0.0.3)
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
set(PACKAGE_BUGREPORT "wsumner@sfu.ca")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

add_compile_options(
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra;-Wconversion>"
include(get_cpm)
include(utils)
include(grammar)

project(
cpp-tree-sitter
VERSION 0.26.8
LANGUAGES C CXX
DESCRIPTION "Simple C++ and CMake wrapper around tree-sitter."
)

include(cmake/CPM.cmake)
# OPTIONS
set(CPP_TS_BUILD_TESTS_DEFAULT OFF)
if(PROJECT_IS_TOP_LEVEL)
set(CPP_TS_BUILD_TESTS_DEFAULT ON)
endif()

option(CPP_TS_BUILD_TESTS "Build test for cpp-tree-sitter" ${CPP_TS_BUILD_TESTS_DEFAULT})
option(CPP_TS_FEATURE_WASM "Enable the Wasm feature" OFF)
option(CPP_TS_AMALGAMATED "Build using an amalgamated source" ON)
option(CPP_TS_MSVC_STATIC_RUNTIME "Use static MSVC runtime (/MT or /MTd)" OFF)

if(DEFINED CPP_TS_WASMTIME_PATH AND NOT CACHE{CPP_TS_WASMTIME_PATH})
set(CPP_TS_WASMTIME_PATH "${CPP_TS_WASMTIME_PATH}" CACHE PATH "Path to locally installed wasmtime" FORCE)
endif()

if(NOT CPP_TS_WASMTIME_PATH)
set(CPP_TS_WASMTIME_PATH "" CACHE PATH "Path to locally installed wasmtime")
endif()

# We want to automatically download and provide tree-sitter to users of
# the package, so pull it in and retrofit cmake dependencies on top of it.
CPMAddPackage(
NAME tree-sitter
GIT_REPOSITORY https://github.com/tree-sitter/tree-sitter.git
VERSION 0.22.6
DOWNLOAD_ONLY YES
NAME tree-sitter
GIT_REPOSITORY https://github.com/tree-sitter/tree-sitter.git
VERSION 0.26.8
DOWNLOAD_ONLY YES
)

if (tree-sitter_ADDED)
add_library(tree-sitter)
target_sources(tree-sitter
PRIVATE
"${tree-sitter_SOURCE_DIR}/lib/src/lib.c"
)
target_include_directories(tree-sitter
PRIVATE
$<BUILD_INTERFACE:${tree-sitter_SOURCE_DIR}/lib/src>
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${tree-sitter_SOURCE_DIR}/lib/include>
)
target_compile_options(tree-sitter
PRIVATE
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-conversion>"
)
if(CPP_TS_AMALGAMATED)
set(TREE_SITTER_SOURCE_FILES "${tree-sitter_SOURCE_DIR}/lib/src/lib.c")
else()
file(GLOB TREE_SITTER_SOURCE_FILES CONFIGURE_DEPENDS "${tree-sitter_SOURCE_DIR}/lib/src/*.c")
list(REMOVE_ITEM TREE_SITTER_SOURCE_FILES "${tree-sitter_SOURCE_DIR}/lib/src/lib.c")
endif()

add_library(tree-sitter STATIC ${TREE_SITTER_SOURCE_FILES})
target_include_directories(tree-sitter PRIVATE $<BUILD_INTERFACE:${tree-sitter_SOURCE_DIR}/lib/src> $<BUILD_INTERFACE:${tree-sitter_SOURCE_DIR}/lib/src/wasm>)
target_include_directories(tree-sitter PUBLIC $<INSTALL_INTERFACE:include> $<BUILD_INTERFACE:${tree-sitter_SOURCE_DIR}/lib/include>)
target_compile_options(tree-sitter PRIVATE ${CPP_TS_COMPILE_OPTIONS})

if(MSVC)
target_compile_definitions(tree-sitter PRIVATE NOMINMAX)
set_target_properties(tree-sitter PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<NOT:$<BOOL:${CPP_TS_MSVC_STATIC_RUNTIME}>>:DLL>"
)
endif()
endif()

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
target_link_libraries(${PROJECT_NAME} INTERFACE tree-sitter)
target_compile_options(${PROJECT_NAME} INTERFACE ${CPP_TS_COMPILE_OPTIONS})

function(add_grammar_from_repo NAME REPO VERSION)
CPMAddPackage(
NAME ${NAME}
GIT_REPOSITORY ${REPO}
VERSION ${VERSION}
DOWNLOAD_ONLY YES
)

if ("${${NAME}_ADDED}")
add_library(${NAME})

file(GLOB maybe_scanner "${${NAME}_SOURCE_DIR}/src/scanner.c")
target_sources(${NAME}
PRIVATE
"${${NAME}_SOURCE_DIR}/src/parser.c"
${maybe_scanner}
)
target_include_directories(${NAME}
PRIVATE
# parser.h is stored within the src directory, so we need to include
# src in the search paths
$<BUILD_INTERFACE:${${NAME}_SOURCE_DIR}/src>
PUBLIC
$<INSTALL_INTERFACE:include>
)

target_link_libraries(${NAME}
INTERFACE
tree-sitter
)
target_compile_options(${NAME}
PRIVATE
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-unused-but-set-variable>"
)
endif()
endfunction(add_grammar_from_repo)


add_library(cpp-tree-sitter INTERFACE)
target_include_directories(cpp-tree-sitter
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(cpp-tree-sitter
INTERFACE
tree-sitter
)
if(MSVC)
target_compile_definitions(${PROJECT_NAME} INTERFACE NOMINMAX)
set_target_properties(${PROJECT_NAME} PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<NOT:$<BOOL:${CPP_TS_MSVC_STATIC_RUNTIME}>>:DLL>"
)
endif()

if(CPP_TS_FEATURE_WASM)
set(WASMTIME_INCLUDE_DIR "")
set(WASMTIME_LIB_DIR "")
if(NOT CPP_TS_WASMTIME_PATH)
set(WASM_VERSION 43.0.1)
if(WIN32)
set(WASMTIME_URL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASM_VERSION}/wasmtime-v${WASM_VERSION}-x86_64-windows-c-api.zip")
elseif(APPLE)
set(WASMTIME_URL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASM_VERSION}/wasmtime-v${WASM_VERSION}-x86_64-macos-c-api.tar.xz")
elseif(UNIX)
set(WASMTIME_URL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASM_VERSION}/wasmtime-v${WASM_VERSION}-x86_64-linux-c-api.tar.xz")
endif()

CPMAddPackage(
NAME wasmtime
URL ${WASMTIME_URL}
DOWNLOAD_ONLY YES
)

if(wasmtime_ADDED)
set(WASMTIME_INCLUDE_DIR ${wasmtime_SOURCE_DIR}/include)
set(WASMTIME_LIB_DIR ${wasmtime_SOURCE_DIR}/lib)
endif()
else()
if(EXISTS "${CPP_TS_WASMTIME_PATH}")
set(WASMTIME_INCLUDE_DIR ${CPP_TS_WASMTIME_PATH}/include)
set(WASMTIME_LIB_DIR ${CPP_TS_WASMTIME_PATH}/lib)
endif()
endif()

if(WASMTIME_INCLUDE_DIR AND WASMTIME_LIB_DIR)
set(WASMTIME_LIB_NAME wasmtime)
if(NOT BUILD_SHARED_LIBS)
if(WIN32)
set(WASMTIME_LIB_NAME wasmtime.lib)

# Project
target_compile_definitions(${PROJECT_NAME} INTERFACE WASM_API_EXTERN= WASI_API_EXTERN=)
target_link_libraries(${PROJECT_NAME} INTERFACE ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt)

# Tree-Sitter
target_compile_definitions(tree-sitter PRIVATE WASM_API_EXTERN= WASI_API_EXTERN=)
target_link_libraries(tree-sitter INTERFACE ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt)
elseif(NOT APPLE)
# Project
target_link_libraries(${PROJECT_NAME} INTERFACE pthread dl m)

# Tree-Sitter
target_link_libraries(tree-sitter INTERFACE pthread dl m)
endif()
endif()

unset(WASMTIME_LIBRARY CACHE)
find_library(
WASMTIME_LIBRARY
NAMES ${WASMTIME_LIB_NAME}
PATHS ${WASMTIME_LIB_DIR}
NO_DEFAULT_PATH
)

# Project
target_include_directories(${PROJECT_NAME} INTERFACE ${WASMTIME_INCLUDE_DIR})
target_compile_definitions(${PROJECT_NAME} INTERFACE CPP_TREE_SITTER_FEATURE_WASM)
target_link_libraries(${PROJECT_NAME} INTERFACE ${WASMTIME_LIBRARY})

# Tree-Sitter
target_include_directories(tree-sitter PRIVATE ${WASMTIME_INCLUDE_DIR})
target_compile_definitions(tree-sitter PUBLIC TREE_SITTER_FEATURE_WASM)
target_link_libraries(tree-sitter PUBLIC ${WASMTIME_LIBRARY})
endif()

function(CPPTSCopyWasmtime)
set(options)
set(oneValueArgs TARGET)
set(multiValueArgs)

cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}" "${oneValueArgs}" "${multiValueArgs}"
)

if(NOT arg_TARGET)
message(FATAL_ERROR "cpp_ts_copy_wasmtime requires TARGET")
endif()

if(EXISTS "${WASMTIME_LIB_DIR}")
find_and_copy_lib(
TARGET ${arg_TARGET}
NAMES wasmtime.dll
PATHS ${WASMTIME_LIB_DIR}
)
endif()
endfunction()
endif()

if(CPP_TS_BUILD_TESTS)
add_subdirectory(tests)
endif()
Loading