| Title: | 'R' Bindings to the 'C' Grammar for Tree-Sitter |
|---|---|
| Description: | Provides bindings to a 'C' grammar for Tree-sitter, to be used alongside the 'treesitter' package. Tree-sitter builds concrete syntax trees for source files and can efficiently update them or generate code like producing R C API wrappers from C functions, structs and global definitions from header files. |
| Authors: | Sounkou Mahamane Toure [aut, cre], Tree-sitter authors [cph] (Tree-sitter C grammar), Eli Bendersky and Co-authors [cph] (pycparser fake_libc headers) |
| Maintainer: | Sounkou Mahamane Toure <[email protected]> |
| License: | GPL-3 |
| Version: | 0.0.4.2 |
| Built: | 2026-06-07 07:27:30 UTC |
| Source: | https://github.com/sounkou-bioinfo/treesitter.c |
Returns the absolute path to the inst/fake_libc directory in the installed package.
fake_libc_path()fake_libc_path()
Character scalar with the path to fake_libc headers
-dM -E) if
use_cpp = TRUE and a compiler is available; otherwise, a simple scan of #define lines
is used as a fallback.This function will use the configured C compiler to list macro definitions (-dM -E) if
use_cpp = TRUE and a compiler is available; otherwise, a simple scan of #define lines
is used as a fallback.
get_defines_from_file(file, use_cpp = TRUE, cc = r_cc(), ccflags = NULL)get_defines_from_file(file, use_cpp = TRUE, cc = r_cc(), ccflags = NULL)
file |
Path to a header file |
use_cpp |
Logical; use the C preprocessor if available |
cc |
Compiler string; passed to |
ccflags |
Additional flags for the compiler |
Character vector of macro names defined in file
Extract enum members from a parsed header
get_enum_members_from_root(root)get_enum_members_from_root(root)
root |
A tree-sitter root node. |
Data frame with enum member names and values.
Extract enum names from a parsed header
get_enum_nodes(root)get_enum_nodes(root)
root |
A tree-sitter root node. |
Data frame with enum names.
Returns a data frame with capture_name, text, start_line,
and start_col.
get_function_nodes(root, extract_params = FALSE, extract_return = FALSE)get_function_nodes(root, extract_params = FALSE, extract_return = FALSE)
root |
A tree-sitter root node. |
extract_params |
Logical; whether to extract parameter types for found functions. Default FALSE. |
extract_return |
Logical; whether to extract return types for found functions. Default FALSE. |
Data frame with function captures; when extract_params=TRUE a params list-column is present.
Extract global variable names from a parsed tree root
get_globals_from_root(root)get_globals_from_root(root)
root |
A tree-sitter root node. |
Data frame with top-level global names.
Extract global variables with types from a parsed tree root
get_globals_with_types_from_root(root)get_globals_with_types_from_root(root)
root |
A tree-sitter root node. |
Data frame with global names and C types.
Extract members of structs (including nested anonymous struct members)
get_struct_members(root)get_struct_members(root)
root |
A tree-sitter root node. |
Data frame describing struct members, including bitfields.
Extract struct names from a parsed tree root
get_struct_nodes(root)get_struct_nodes(root)
root |
A tree-sitter root node from |
Data frame with struct name captures.
Extract members of unions
get_union_members_from_root(root)get_union_members_from_root(root)
root |
A tree-sitter root node. |
Data frame describing union members, including bitfields.
Extract union names from a parsed header
get_union_nodes(root)get_union_nodes(root)
root |
A tree-sitter root node. |
Data frame with union names.
language() returns a tree_sitter_language object for C for use with the
treesitter package.
language()language()
A tree_sitter_language object.
language()language()
Convert character content of a header file into a tree-sitter root
parse_header_text(text, lang = language())parse_header_text(text, lang = language())
text |
Character scalar with the header content to parse. |
lang |
tree-sitter language object (default: C language from package) |
The tree root node object
if (requireNamespace("treesitter", quietly = TRUE)) { root <- parse_header_text("int foo(int);\n") root }if (requireNamespace("treesitter", quietly = TRUE)) { root <- parse_header_text("int foo(int);\n") root }
functions, structs, struct members, enums, unions, globals, and macros.
parse_headers_collect( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), preprocess = FALSE, cc = r_cc(), ccflags = NULL, include_dirs = NULL, extract_params = FALSE, extract_return = FALSE, ... )parse_headers_collect( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), preprocess = FALSE, cc = r_cc(), ccflags = NULL, include_dirs = NULL, extract_params = FALSE, extract_return = FALSE, ... )
dir |
Directory to search for header files. Defaults to |
recursive |
Whether to search recursively for headers. Default |
pattern |
File name pattern to match header files. Default is |
preprocess |
Run the C preprocessor (using R's configured CC) on header files before parsing. Defaults to |
cc |
The C compiler to use for preprocessing. If |
ccflags |
Extra flags to pass to the compiler when preprocessing. If |
include_dirs |
Additional directories to add to the include path for preprocessing. A character vector of directories. |
extract_params |
Logical; whether to extract parameter types for functions. Default |
extract_return |
Logical; whether to extract return types for functions. Default |
... |
Additional arguments passed to preprocess_header (e.g., extra compiler flags) |
This helper loops over headers found in a directory and returns a list with tidy data.frames. Useful for programmatic analysis of header collections.
A named list of data frames with components: functions, structs, struct_members, enums, unions, globals, defines.
## Not run: if (requireNamespace("treesitter", quietly = TRUE)) { res <- parse_headers_collect(dir = R.home("include"), preprocess = FALSE) head(res$functions) } ## End(Not run)## Not run: if (requireNamespace("treesitter", quietly = TRUE)) { res <- parse_headers_collect(dir = R.home("include"), preprocess = FALSE) head(res$functions) } ## End(Not run)
This utility uses the C language provided by this package and the
treesitter parser to find function declarations and definitions in
C header files. The default dir is R.home("include"), which is
typically where R's headers live.
parse_r_include_headers( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), preprocess = FALSE, cc = r_cc(), ccflags = NULL, include_dirs = NULL, ... )parse_r_include_headers( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), preprocess = FALSE, cc = r_cc(), ccflags = NULL, include_dirs = NULL, ... )
dir |
Directory to search for header files. Defaults to
|
recursive |
Whether to search recursively for headers. Default
|
pattern |
File name pattern to match header files. Default is
|
preprocess |
Run the C preprocessor (using R's configured CC) on
header files before parsing. Defaults to |
cc |
The C compiler to use for preprocessing. If |
ccflags |
Extra flags to pass to the compiler when preprocessing.
If |
include_dirs |
Additional directories to add to the include path for preprocessing. A character vector of directories. |
... |
Arguments passed on to
|
A data frame with columns name, file, line, and kind
(either 'declaration' or 'definition').
if (requireNamespace("treesitter", quietly = TRUE)) { # Parse a small header file from a temp dir tmp <- tempdir() path <- file.path(tmp, "example.h") writeLines(c( "int foo(int a);", "static inline int bar(void) { return 1; }" ), path) parse_r_include_headers(dir = tmp) }if (requireNamespace("treesitter", quietly = TRUE)) { # Parse a small header file from a temp dir tmp <- tempdir() path <- file.path(tmp, "example.h") writeLines(c( "int foo(int a);", "static inline int bar(void) { return 1; }" ), path) parse_r_include_headers(dir = tmp) }
file using the provided compilerThis function runs the configured C compiler with the -E preprocessor
flag and returns the combined preprocessed output as a single string.
preprocess_header(file, cc = r_cc(), ccflags = NULL, ...)preprocess_header(file, cc = r_cc(), ccflags = NULL, ...)
file |
Path to a header file to preprocess. |
cc |
(Character) Compiler command to use. If |
ccflags |
(Character) Additional flags to pass to the compiler. |
... |
Arguments passed on to
|
Character scalar with the preprocessed output of file.
## Not run: # Check for a compiler before running an example that invokes the preprocessor rcc <- treesitter.c::r_cc() if (nzchar(rcc)) { rcc_prog <- strsplit(rcc, "\\s+")[[1]][1] if (nzchar(Sys.which(rcc_prog))) { tmp <- tempfile("hdr3") dir.create(tmp) path <- file.path(tmp, "p.h") writeLines(c("#define TYPE int", "TYPE foo(TYPE x);"), path) out <- preprocess_header(path) grepl("int foo\\(", out) } else { message("Skipping preprocess example: compiler not found on PATH") } } ## End(Not run)## Not run: # Check for a compiler before running an example that invokes the preprocessor rcc <- treesitter.c::r_cc() if (nzchar(rcc)) { rcc_prog <- strsplit(rcc, "\\s+")[[1]][1] if (nzchar(Sys.which(rcc_prog))) { tmp <- tempfile("hdr3") dir.create(tmp) path <- file.path(tmp, "p.h") writeLines(c("#define TYPE int", "TYPE foo(TYPE x);"), path) out <- preprocess_header(path) grepl("int foo\\(", out) } else { message("Skipping preprocess example: compiler not found on PATH") } } ## End(Not run)
dir
This helper calls preprocess_header() for each matching file in
dir and returns a named list with the path as the keys and the
preprocessed text as the values.
preprocess_headers( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), cc = r_cc(), ccflags = NULL, ... )preprocess_headers( dir = R.home("include"), recursive = TRUE, pattern = c("\\.h$", "\\.H$"), cc = r_cc(), ccflags = NULL, ... )
dir |
Directory where header files will be searched. |
recursive |
Logical; whether to search recursively. |
pattern |
File name pattern(s) used to identify header files. |
cc |
Compiler string; passed to |
ccflags |
Compiler flags; passed to |
... |
Arguments passed on to
|
Named list of file => preprocessed text.
This function queries common places to find the C compiler used by R.
It checks Sys.getenv("CC"), then R CMD config CC, and finally cc on PATH.
The returned value may include flags (e.g., 'gcc -std=...').
r_cc()r_cc()
Character scalar with compiler program (or empty string).