### Object Initialization with Reflection Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Example of initializing an object with a reflected namespace. ```cpp constexpr T obj{.r=^^::}; ``` -------------------------------- ### C/C++ Macro Invocation Examples Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/ambiguities.txt Demonstrates top-level macro invocations with varying numbers of arguments. ```c #define DEFINE_SOMETHING(THING_A, "this is a thing a") DEFINE_SOMETHING(THING_A, "this is a thing a"); DEFINE_SOMETHING(THING_B, "this is a thing b", "thanks"); ``` -------------------------------- ### New and Delete Expressions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Provides examples of C++ 'new' and 'delete' expressions, including array allocation and placement new. ```cpp int main() { auto a = new T(); auto b = new U::V{}; auto c = new (&d) T; auto d = new T[5][3](); auto e = new int[5]; d = new(2, f) T; delete a; ::delete[] c; ::new (foo(x)) T(this, x); } ``` -------------------------------- ### C++ Attribute Examples Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Provides examples of C++ attributes applied to various statements like switch, while, if, for, return, and labels. ```cpp void f() { [[a]] switch (b) { [[c]] case 1: {} } [[a]] while (true) {} [[a]] if (true) {} [[a]] for (auto x : y) {} [[a]] for (;;) {} [[a]] return; [[a]] a; [[a]]; [[a]] label: {} [[a]] goto label; } ``` -------------------------------- ### Reference Declaration Example Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt A simple C++ function demonstrating the declaration of a reference. ```cpp int main() { T &x = y(); } ``` -------------------------------- ### User-Defined Literals Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Shows examples of user-defined literals for numbers and strings, including custom suffixes. ```cpp 1.2_km; ``` ```cpp "foo" "bar"_baz; ``` -------------------------------- ### C Switch Statement Example Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/statements.txt Demonstrates the structure of a switch statement with multiple cases and a default case. Includes break statements to exit the switch. ```c void foo(int a) { switch (a) { puts("entered switch!"); case 3: case 5: if (b) { c(); } break; default: c(); break; } } ``` -------------------------------- ### C Labeled Statements and Goto Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/statements.txt Demonstrates the use of labeled statements and the goto keyword for control flow in C. This example shows a recursive pattern using goto. ```c void foo(T *t) { recur: t = t->next(); if (t) goto recur; } ``` -------------------------------- ### Adding Members with Reflection Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Example of using reflection to add a member, likely to a type or struct. ```cpp add_member(^^char const *); ``` -------------------------------- ### Conditional Compilation: Ifdef Else Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt A simple conditional compilation example using #ifdef and #else. ```c #ifdef DEFINE2 #else ``` -------------------------------- ### C++ Noexcept Specifier Examples Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Demonstrates the usage of the noexcept specifier in function declarations, including conditional noexcept. ```cpp void foo() noexcept; ``` ```cpp void foo() noexcept(true); ``` ```cpp template T foo() noexcept(sizeof(T) < 4); ``` -------------------------------- ### Inline Function Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Shows examples of inline function declarations in C++. Includes functions with and without definitions. ```cpp inline int g(); ``` ```cpp inline int h() { } ``` -------------------------------- ### C++ Consteval Block Example Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Demonstrates the use of a consteval block within a C++ class for compile-time definition of an aggregate. ```cpp class A { struct A; consteval { std::meta::define_aggregate(^^S, {}); return; } }; template consteval void tfn2() { consteval { std::meta::define_aggregate(R, {}); } } template struct TCls { static constexpr void sfn() requires ([] { struct S4; consteval { std::meta::define_aggregate(^^S4, {}); } return true; }()) { } }; consteval { TCls::sfn(); } ``` -------------------------------- ### C++ Expansion Statement with Container Iteration Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Example of a C++ expansion statement used for iterating over a pack expansion of containers. ```cpp template for (auto const& c : {Containers...}) {} ``` -------------------------------- ### C++ Throw Specifier Examples Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Illustrates the use of the throw specifier to indicate exceptions a function might throw. ```cpp void foo() throw(); ``` ```cpp void foo() throw(int); ``` ```cpp void foo() throw(std::string, char *); ``` ```cpp void foo() throw(float) { } ``` -------------------------------- ### Generic Selection with Various Types Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Demonstrates the _Generic keyword in C, which allows selecting an expression based on the type of a controlling expression. This example assigns values to variables based on their types. ```c int main(int argc, char **argv) { int a = 10; float b = 3.14; double c = 2.71828; char d = 'A'; a = _Generic(d, int: 5, float: 0, char: 100); b = _Generic(a, void *: 0, int: 4.0, float: 3.14, double: 2.71828, char: 1.0); c = _Generic(b, void *: 0, int: 4.0, float: 3.14, double: 2.71828, char: 1.0); d = _Generic(c, void *: '\0', int: '0', float: '3', double: '2', char: '1'); _Generic(a, int: printf("a is an int\n"), float: printf("a is a float\n"), double: printf("a is a double\n"), char: printf("a is a char\n")); _Generic(b, int: printf("b is an int\n"), float: printf("b is a float\n"), double: printf("b is a double\n"), char: printf("b is a char\n")); _Generic(c, int: printf("c is an int\n"), float: printf("c is a float\n"), double: printf("c is a double\n"), char: printf("c is a char\n")); _Generic(d, int: printf("d is an int\n"), float: printf("d is a float\n"), double: printf("d is a double\n"), char: printf("d is a char\n")); return 0; } ``` -------------------------------- ### C Function Calls Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Shows examples of C function calls, including standard library functions like printf and assert. These are parsed as call_expression nodes. ```c int main() { printf("hi! %d\n", x); __assert_fail("some_error_message", 115, __extension__ __func__); } ``` ```tree-sitter-grammar (translation_unit (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement (expression_statement (call_expression (identifier) (argument_list (string_literal (string_content) (escape_sequence)) (identifier)))) (expression_statement (call_expression (identifier) (argument_list (string_literal (string_content)) (number_literal) (extension_expression (identifier)))))))) ``` -------------------------------- ### C++ Numeric Literals Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Provides examples of various C++ numeric literal formats, including binary, octal, hexadecimal, and floating-point literals with digit separators. ```cpp void f() { 0123'4567; 0b1001'0110; 0B10; 0xabcd'1234; 0XABCD; 1'234; -123; 123l; 123u; 123ULL; 123z; 123UZ; 1.0; -1.0; 1.f; .1f; 1.0l; 1.0f16; 1.0bf16; 1.e1; 1.0e1; 0xa.bp1; 0xap-1; 0x.ap-1; } ``` -------------------------------- ### Ref-qualifiers for Member Functions in C++ Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Illustrates the use of ref-qualifiers (& and &&) for member functions, indicating whether they can be called on lvalue or rvalue objects. Includes examples with and without 'noexcept'. ```cpp class C { void f() &; void f() && noexcept; void f() & {} void f() & noexcept {} }; void C::f() &; void C::f() & noexcept; void C::f() && {} void C::f() & noexcept {} ``` -------------------------------- ### C++ Template Methods Example Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates template methods within a class, including a default template and a template specialization. This is useful for understanding how C++ handles generic programming and specific template instantiations. ```cpp class Person { Person() { this->speak(); } template void speak() {} template <> void speak() {} }; ``` -------------------------------- ### Initializing Arrays with Parameter Pack Expansion Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates initializing an array using a parameter pack expansion, including side effects within the expansion. ```cpp int dummy[sizeof...(Ts)] = { (std::cout << args, 0)... }; ``` -------------------------------- ### Constructor Definitions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/definitions.txt Illustrates various ways to define constructors, including default, with initializers, and try-catch blocks. ```cpp T::T() {} T::T() : f1(0), f2(1, 2) { puts("HI"); } T::T() : Base() {} T::T() try : f1(0) {} catch(...) {} ``` -------------------------------- ### Include Directives Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt Demonstrates different ways to include header files using angle brackets, quotes, and macros. ```c #include "some/path.h" #include #include MACRO #include MACRO(arg1, arg2) ``` -------------------------------- ### Class Inheritance with Parameter Pack Expansion Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates using parameter pack expansion for base class initializations in a class constructor. ```cpp class X : public Mixins { public: X(const Mixins&... mixins) : Mixins(mixins)... { } }; ``` -------------------------------- ### Declaring Containers with Parameter Packs Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates how to declare container types using parameter packs for template arguments, showing different orderings. ```cpp container t1; container t2; ``` -------------------------------- ### C++ Attributes: Class with Multiple Attributes Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Example of a class declaration with multiple attributes, including deprecated. ```cpp class [[gnu::visibility("default")]] [[deprecated]] A {}; ``` -------------------------------- ### Type and Namespace Reflection Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Demonstrates basic reflection for types like 'int' and namespaces like '::'. ```cpp static_assert(std::meta::is_type(^^int())); static_assert(std::meta::is_namespace(^^::)); ``` -------------------------------- ### Expanding Parameter Packs with Casts Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates expanding parameter packs while applying casts to each expanded argument. ```cpp f(const_cast(&args)...); // f(const_cast(&X1), const_cast(&X2), const_cast(&X3)) ``` -------------------------------- ### C++ nullptr Initialization Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates the initialization of a void pointer with nullptr. ```cpp void *x = nullptr; ``` -------------------------------- ### Namespace Definitions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates different ways to define namespaces, including nested and inline namespaces. ```cpp namespace std { int x; } // namespace std namespace A::B { } namespace A::B::inline C { } namespace A::B::inline C::D { } inline namespace A { } ``` ```tree-sitter-grammar (translation_unit (namespace_definition name: (namespace_identifier) body: (declaration_list (declaration type: (primitive_type) declarator: (identifier)))) (comment) (namespace_definition name: (nested_namespace_specifier (namespace_identifier) (namespace_identifier)) body: (declaration_list)) (namespace_definition name: (nested_namespace_specifier (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (namespace_identifier)))) body: (declaration_list)) (namespace_definition name: (nested_namespace_specifier (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (namespace_identifier)))) body: (declaration_list)) (namespace_definition name: (namespace_identifier) body: (declaration_list))) ``` -------------------------------- ### Lambda Capture with Parameter Pack Expansion Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Shows how to capture a parameter pack by reference in a lambda function. ```cpp auto lm = [&, args...] { return g(args...); }; ``` -------------------------------- ### C++ Splice Expression Example: Function Call Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Invokes a function `f` from a reflected namespace `^^A` with arguments `3` and `4`. ```cpp [:^^A::f:](3, 4); ``` -------------------------------- ### C++ Splice Expression Example: Template Instantiation Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a constant integer `d` by instantiating a template `^^TCls` with a specific integer type. ```cpp constexpr int d = template [:^^TCls:]::b; ``` -------------------------------- ### Forwarding Function Arguments with Parameter Pack Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates a common pattern for forwarding variadic function arguments using `std::forward` and parameter pack expansion. ```cpp template void wrap(Args&&... args) { f(forward(args)...); } ``` -------------------------------- ### C++ Splice Expression Example: Template Instantiation with Arithmetic Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Instantiates a template `S` with an argument that is the result of adding `1` to a reflected value `r`. ```cpp S<[:r:] + 1> s3; ``` -------------------------------- ### C++ Variable Initialization with Reflection-like Syntax Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Illustrates C++ variable initialization, including assignments from qualified identifiers and template instantiations, potentially related to reflection. ```cpp int o1 = [:^^S::x:]; auto o2 = typename [:^^TCls:]<([:^^v:])>(); int S::*k = &[:^^S::m:]; int v1 = [:^^TCls<1>:]::s; int v2 = template [:^^TCls:]<2>::s; typename [:^^TCls:]<3>::type v3 = 3; template [:^^TCls:]<3>::type v4 = 4; typename template [:^^TCls:]<3>::type v5 = 5; ``` -------------------------------- ### Using Declarations and Directives Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Illustrates various 'using' declarations for bringing names into scope, including aliasing types and using entire namespaces. ```cpp using a; using ::b; using c::d; using ::e::f::g; using h = i::j; using namespace std; using enum Foo; [[deprecated]] using namespace std; template using a = typename b::c; using foobar [[deprecated]] = int; using foobar [[deprecated]] [[maybe_unused]] = int; ``` ```tree-sitter-grammar (translation_unit (using_declaration (identifier)) (using_declaration (qualified_identifier (identifier))) (using_declaration (qualified_identifier (namespace_identifier) (identifier))) (using_declaration (qualified_identifier (qualified_identifier (namespace_identifier) (qualified_identifier (namespace_identifier) (identifier))))) (alias_declaration (type_identifier) (type_descriptor (qualified_identifier (namespace_identifier) (type_identifier)))) (using_declaration (identifier)) (using_declaration (identifier)) (using_declaration (attribute_declaration (attribute (identifier))) (identifier)) (template_declaration (template_parameter_list (type_parameter_declaration (type_identifier))) (alias_declaration (type_identifier) (type_descriptor (dependent_type (qualified_identifier (template_type (type_identifier) (template_argument_list (type_descriptor (type_identifier)))) (type_identifier)))))) (alias_declaration (type_identifier) (attribute_declaration (attribute (identifier))) (type_descriptor (primitive_type))) (alias_declaration (type_identifier) (attribute_declaration (attribute (identifier))) (attribute_declaration (attribute (identifier))) (type_descriptor (primitive_type)))) ``` -------------------------------- ### C++ Splice Expression Example: Template Alias Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Defines a template alias `e` where the template parameter `V` is directly used as the value. ```cpp template constexpr int e = [:V:]; ``` -------------------------------- ### C++ Splice Expression Example: Template Function Call Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Calls a template function `r` with a template argument `200` to obtain a pointer type `T*`. ```cpp T* p4 = template [:r:]<200>(); ``` -------------------------------- ### Module Declaration with Exports and Imports Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/modules.txt Demonstrates module declaration, export, and conditional import statements using preprocessor directives. ```cpp #ifdef USE_MODULES module; #include export module foo; #endif #if USE_MODULES import std; #else import ; #endif export { #ifdef USE_MODULE class Foo; #endif } ``` -------------------------------- ### Class Declarations with Inheritance Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates different ways to declare classes with inheritance, including access specifiers, qualified base classes, attributes, and virtual inheritance. ```cpp class A : public B {}; class C : C::D, public E {}; class F : [[deprecated]] public G {}; class H : public virtual I {}; class J : virtual protected I {}; class K : virtual I {}; class L : protected I {}; ``` -------------------------------- ### C Declaration with Initialized Char Literal Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Shows a C declaration where a variable is initialized with a character literal. This is a basic example of variable initialization in C. ```c (declaration (primitive_type) (init_declarator (identifier) (char_literal (character)))) ``` -------------------------------- ### Expanding Parameter Packs in Function Arguments Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates expanding parameter packs directly into function arguments, including with pre-increment operators. ```cpp f(&args...); // expands to f(&E1, &E2, &E3) f(n, ++args...); // expands to f(n, ++E1, ++E2, ++E3); f(++args..., n); // expands to f(++E1, ++E2, ++E3, n); ``` -------------------------------- ### C For Loops Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/statements.txt Illustrates various forms of C for loops, including infinite loops, loops with initialization, condition, and increment, and loops with multiple expressions. The AST breakdown is also provided. ```c int main() { for (;;) 1; for (int i = 0; i < 5; next(), i++) { 2; } for (start(); check(); step()) 3; for (i = 0, j = 0, k = 0, l = 0; i < 1, j < 1; i++, j++, k++, l++) 1; } ``` ```c (translation_unit (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement (for_statement (expression_statement (number_literal))) (for_statement (declaration (primitive_type) (init_declarator (identifier) (number_literal))) (binary_expression (identifier) (number_literal)) (comma_expression (call_expression (identifier) (argument_list)) (update_expression (identifier))) (compound_statement (expression_statement (number_literal)))) (for_statement (call_expression (identifier) (argument_list)) (call_expression (identifier) (argument_list)) (call_expression (identifier) (argument_list)) (expression_statement (number_literal))) (for_statement (comma_expression (assignment_expression (identifier) (number_literal)) (comma_expression (assignment_expression (identifier) (number_literal)) (comma_expression (assignment_expression (identifier) (number_literal)) (assignment_expression (identifier) (number_literal))))) (comma_expression (binary_expression (identifier) (number_literal)) (binary_expression (identifier) (number_literal))) (comma_expression (update_expression (identifier)) (comma_expression (update_expression (identifier)) (comma_expression (update_expression (identifier)) (update_expression (identifier))))) (expression_statement (number_literal)))))) ``` -------------------------------- ### C Identifiers Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Shows basic C identifiers, including those starting with underscores or letters followed by alphanumeric characters and underscores. These are parsed as identifier nodes. ```c int main() { _abc; d_EG123; } ``` ```tree-sitter-grammar (translation_unit (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement (expression_statement (identifier)) (expression_statement (identifier))))) ``` -------------------------------- ### Basic #ifdef, #else, #endif Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt Demonstrates the fundamental structure of conditional compilation using #ifdef, #else, and #endif. ```c #ifdef DEFINE3 #else #endif #endif ``` -------------------------------- ### Primitive Type Constructors Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates calling constructors for primitive types, both with and without the 'new' keyword, and within expressions. ```cpp x = int(1); ``` ```cpp x = new int(1); ``` ```cpp x = (int(1) + float(2)); ``` -------------------------------- ### C++ Splice Expression Example: Type Casting with Reflection Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a variable `g` by casting an integer literal `42` to a type obtained through reflection (`^^int`). ```cpp constexpr auto g = typename [:^^int:](42); ``` -------------------------------- ### Structured Binding Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates various forms of structured binding declarations, including simple bindings, references, and within loops. ```c++ auto [a] = B{}; ``` ```c++ int main() { auto &&[b, c] = std::make_tuple(c); const auto [x, y] {1, 2}; for (const auto &[a, b] : c) {} } ``` ```c++ (translation_unit (declaration (structured_binding_declaration (identifier) initializer: (initializer_list))) (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement (expression_statement (assignment_expression left: (structured_binding_declaration (type_qualifier) (identifier_list)) right: (call_expression function: (member_access_expression member: (identifier)) arguments: (argument_list (identifier)))) (expression_statement (assignment_expression left: (structured_binding_declaration (identifier_list)) right: (initializer_list))) (for_statement (range_based_for_statement initializer: (identifier) body: (compound_statement (empty_statement))))))) ``` -------------------------------- ### C++ Splice Expression Example: Integer Constant Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a constant integer `c` using a splice expression to reference a member `a` from a reflected type `^^S`. ```cpp constexpr int c = [:^^S:]::a; ``` -------------------------------- ### Namespace Alias Definitions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Shows how to create aliases for existing namespaces, including aliasing global or nested namespaces. ```cpp namespace A = B; namespace C = ::D; namespace fs = std::filesystem; namespace bfs = ::boost::filesystem; namespace literals = std::chono::literals; ``` ```tree-sitter-grammar (translation_unit (namespace_alias_definition name: (namespace_identifier) (namespace_identifier)) (namespace_alias_definition name: (namespace_identifier) (nested_namespace_specifier (namespace_identifier))) (namespace_alias_definition name: (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (namespace_identifier))) (namespace_alias_definition name: (namespace_identifier) (nested_namespace_specifier (nested_namespace_specifier (namespace_identifier) (namespace_identifier)))) (namespace_alias_definition name: (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (nested_namespace_specifier (namespace_identifier) (namespace_identifier))))) ``` -------------------------------- ### C Struct Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/declarations.txt Demonstrates forward, simple, and bitfield struct declarations in C. ```c struct s1; struct s2 { int x; float y : 5; }; struct s3 { int x : 1, y : 2; }; ``` ```c (translation_unit (struct_specifier name: (type_identifier)) (struct_specifier name: (type_identifier) body: (field_declaration_list (field_declaration type: (primitive_type) declarator: (field_identifier)) (field_declaration type: (primitive_type) declarator: (field_identifier) (bitfield_clause (number_literal))))) (struct_specifier name: (type_identifier) body: (field_declaration_list (field_declaration type: (primitive_type) declarator: (field_identifier) (bitfield_clause (number_literal)) declarator: (field_identifier) (bitfield_clause (number_literal)))))) ``` -------------------------------- ### Function with Boolean Literal Constraints Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/concepts.txt Shows the use of boolean literals `false` and `true` within a requires clause to form a constraint. This example evaluates to `true`. ```cpp template requires false || true // boolean literals T f(T); ``` -------------------------------- ### Reflecting Template Instantiations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Demonstrates reflecting template instantiations like 'fn' and 'std::vector'. ```cpp constexpr std::meta::info a = ^^fn; constexpr std::meta::info b = ^^std::vector; ``` -------------------------------- ### C++ Splice Expression Example: Type Initialization with Reflection Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a variable `h` by initializing a type obtained through reflection (`^^int`) with an initializer list containing `42`. ```cpp constexpr auto h = typename [:^^int:]{42}; ``` -------------------------------- ### General #if, #elif, #else with defined() and include Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt Shows a general conditional compilation block using #if with logical operators, #elif, #else, and #include. ```c #if defined(__GNUC__) && defined(__PIC__) #define inline inline __attribute__((always_inline)) #elif defined(_WIN32) #define something #elif !defined(SOMETHING_ELSE) #define SOMETHING_ELSE #else #include #endif ``` -------------------------------- ### Expanding Parameter Packs in Expressions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Shows how parameter packs can be expanded within expressions, combining function calls and pack expansion. ```cpp f(h(args...) + args...); // expands to f(h(E1,E2,E3) + E1, h(E1,E2,E3) + E2, h(E1,E2,E3) + E3) ``` -------------------------------- ### Function with Multiple C++11 Attributes Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/declarations.txt Applies multiple C++11 attributes to a function declaration, including 'always_inline', 'hot', 'const', and 'nodiscard', to guide compiler optimizations and usage. ```cpp [[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]] int g(void); ``` -------------------------------- ### C++ Splice Expression Example: Template Instantiation with Nested Call Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a variable `j` by instantiating a template `e` with an argument that is a nested call involving a reflected type `^^h`. ```cpp constexpr auto j = e<([:^^h:])>; ``` -------------------------------- ### Calculating Size with Parameter Pack Expansion Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates using `sizeof...(args)` to determine the size of a parameter pack for array declarations. ```cpp const int size = sizeof...(args) + 2; int res[size] = {1,args...,2}; ``` -------------------------------- ### Function Call with Compound Statement Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Illustrates a C function call where the macro TAKES_BLOCK is used with a compound statement. This example shows macro definition and usage within a main function. ```c #define TAKES_BLOCK(x, block) for (i = 0; i < x; i++) block int main(void) { { int x = 0; } TAKES_BLOCK(10, { // Doesn't matter what I put in here }); } ``` ```c (translation_unit (preproc_function_def (identifier) (preproc_params (identifier) (identifier)) (preproc_arg)) (function_definition (primitive_type) (function_declarator (identifier) (parameter_list (parameter_declaration (primitive_type)))) (compound_statement (compound_statement (declaration (primitive_type) (init_declarator (identifier) (number_literal)))) (expression_statement (call_expression (identifier) (argument_list (number_literal) (initializer_list (comment)))))))) ``` -------------------------------- ### Comment with Escaped Newline Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Shows a C-style comment where a newline character is escaped using a backslash. This allows a comment to span multiple lines without starting a new comment block. ```c // one \ two ``` -------------------------------- ### C++ Default Member Initializers Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Shows how to initialize class members with default values directly in their declaration. ```cpp struct A { bool a = 1; vector b = {c, d, e}; F g {h}; }; ``` -------------------------------- ### C++ Splice Expression Example: Template Instantiation with Member Access Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Declares a constant integer `f` by instantiating a template `^^e` with a value obtained from accessing member `a` of a reflected type `^^S`. ```cpp constexpr int f = template [:^^e:]<^^S::a>; ``` -------------------------------- ### Export Declaration with Multiple Items Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/modules.txt Demonstrates exporting a module, a function, and a block of functions within curly braces, as well as a namespace with functions. ```c++ export module A; export char const* hello() { return "hello"; } char const* world() { return "world"; } export { int one() { return 1; } int zero() { return 0; } } export namespace hi { char const* english() { return "Hi!"; } char const* french() { return "Salut!"; } } ``` -------------------------------- ### Variadic Templates in C++ Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates variadic templates for functions and classes, allowing them to accept an arbitrary number of template arguments. ```c++ template class TT { template void func1(Ts ... args) { func3(nullptr); } void func2(Ts &&... args) {} template void func3() {} template void func4() {} }; ``` ```c++ (translation_unit (template_declaration (template_parameter_list (type_parameter_declaration (type_identifier))) (class_specifier (type_identifier) (field_declaration_list (template_declaration (template_parameter_list (variadic_type_parameter_declaration (type_identifier))) (function_definition (primitive_type) (function_declarator (identifier) (parameter_list (variadic_parameter_declaration (type_identifier) (variadic_declarator (identifier))))) (compound_statement (expression_statement (call_expression (identifier) (argument_list (null)))))) (function_definition (primitive_type) (function_declarator (field_identifier) (parameter_list (variadic_parameter_declaration (type_identifier) (reference_declarator (variadic_declarator (identifier)))))) (compound_statement)) (template_declaration (template_parameter_list (variadic_type_parameter_declaration)) (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement))) (template_declaration (template_parameter_list (variadic_type_parameter_declaration (qualified_identifier (namespace_identifier) (type_identifier)) (variadic_declarator))) (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement))))))) ``` -------------------------------- ### Try-Catch Statements Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Illustrates exception handling using try-catch blocks. Covers catching specific exceptions, base class exceptions, and any exception. ```cpp void main() { try { f(); } catch (const std::overflow_error) { // f() throws std::overflow_error (same type rule) } catch (const exception &e) { // f() throws std::logic_error (base class rule) } catch (...) { // f() throws std::string or int or any other unrelated type } } ``` -------------------------------- ### Returning Braced Initializer Lists Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Demonstrates returning a braced initializer list from a function. This is a concise way to return multiple values or an aggregate type. ```cpp T main() { return {0, 5}; } ``` -------------------------------- ### Defining Typedefs with Parameter Packs Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Shows how to use parameter pack expansions within a typedef to create complex type aliases. ```cpp typedef Tuple...> type; ``` -------------------------------- ### C++ Expansion Statement with Range-based for and std::views::iota Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Demonstrates a C++ expansion statement utilizing std::views::iota for generating a range of indices. ```cpp template for (constexpr int I : std::views::iota(0zu, sizeof...(Ts))) {} ``` -------------------------------- ### Function-like Macro Definitions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt Illustrates function-like macro definitions with varying numbers of arguments, including variadic macros. ```c #define ONE() a #define TWO(b) c #define THREE(d, e) f #define FOUR(...) g #define FIVE(h, i, ...) j ``` -------------------------------- ### C Union Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/declarations.txt Illustrates forward and simple union declarations in C. ```c union u1; union s2 { int x; float y; }; ``` ```c (translation_unit (union_specifier name: (type_identifier)) (union_specifier name: (type_identifier) body: (field_declaration_list (field_declaration type: (primitive_type) declarator: (field_identifier)) (field_declaration type: (primitive_type) declarator: (field_identifier))))) ``` -------------------------------- ### C++ Attributes: Class Inheritance with Attributes Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates using attributes on base classes in inheritance. ```cpp class A final : [[deprecated]] public B {}; ``` -------------------------------- ### Explicit Constructor Declaration Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/definitions.txt Demonstrates explicit constructor declarations, including conditional explicit constructors. ```cpp class C { explicit C(int f); explicit(true) C(long f); }; ``` -------------------------------- ### Throw Statements Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/statements.txt Shows how to throw exceptions in C++. Demonstrates throwing variables, expressions, and literal values. ```cpp void main() { throw e; throw x + 1; throw "exception"; } ``` -------------------------------- ### Function Call with Parameter Pack Expansion Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates a function call that utilizes a parameter pack expansion for its arguments, often seen in variadic templates. ```cpp function: (identifier) arguments: (argument_list (parameter_pack_expansion pattern: (call_expression function: (template_function name: (identifier) arguments: (template_argument_list (type_descriptor (type_qualifier) type: (type_identifier) declarator: (abstract_pointer_declarator)))) arguments: (argument_list (pointer_expression argument: (identifier)))))))) ``` -------------------------------- ### Lambda with Capture List and Variadic Capture Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates capturing a specific argument by reference along with a variadic capture. ```cpp auto f = [&arg, &...etc = etc] {}; ``` -------------------------------- ### C++ Class Member Function and Pointer Usage Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates defining a class with a member function and how to use a pointer to that member function in C++. ```cpp class MyClass { public: int memberFunction(int value) { return value * 2; } }; int main() { void (MyClass::*member_function_ptr)(int); MyClass obj; member_function_ptr = &MyClass::memberFunction; (obj.*member_function_ptr)(42); // 84 this->Base::f(); return 0; } ``` -------------------------------- ### C Common Constants Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Demonstrates the use of common C constants like 'true', 'false', and 'NULL'. These are parsed as specific literal nodes (true, false, null) or identifiers. ```c int main() { true; false; NULL; // regression test - identifiers starting w/ these strings should tokenize correctly. true_value; false_value; NULL_value; } ``` ```tree-sitter-grammar (translation_unit (function_definition (primitive_type) (function_declarator (identifier) (parameter_list)) (compound_statement (expression_statement (true)) (expression_statement (false)) (expression_statement (null)) (comment) (expression_statement (identifier)) (expression_statement (identifier)) (expression_statement (identifier))))) ``` -------------------------------- ### Initializer List for Assignment Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates using an initializer list on the right-hand side of an assignment expression. ```cpp void test() { int b = int{1}; b = int{2}; } ``` -------------------------------- ### C Variable Storage Classes Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/declarations.txt Demonstrates C variable declarations with different storage classes like 'extern', 'register', and 'static'. ```c int a; extern int b, c; register int e; static int f; register uint64_t rd_asm("x" "10"); ``` -------------------------------- ### C++ Fold Expressions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Demonstrates the syntax for fold expressions with different operand placements and combinations. ```cpp bool t = (... + IndexOf); bool t2 = (IndexOf + ...); bool t3 = (1 + ... + IndexOf); bool t3 = (IndexOf + ... + 1); ``` -------------------------------- ### C++ Raw String Literals Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Illustrates the use of raw string literals with and without custom delimiters. ```cpp const char *s1 = R"( This is a string. It ends with ')' and a quote. )"; ``` ```cpp const char *s2 = R"FOO( This is a string. It ends with ')FOO' and a quote. )FOO"; ``` ```cpp const char *s3 = uR"FOO( This is a string. It ends with ')FOO' and a quote. )FOO"; ``` ```cpp const char *s4 = UR"FOO( This is a string. It ends with ')FOO' and a quote. )FOO"; ``` ```cpp const char *s5 = u8R"FOO( This is a string. It ends with ')FOO' and a quote. )FOO"; ``` ```cpp const char *s6 = LR"FOO( This is a string. It ends with ')FOO' and a quote. )FOO"; ``` -------------------------------- ### Preprocessor Conditionals in Functions Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/preprocessor.txt Demonstrates the use of #if, #elif, and #else within a function body for conditional logic. ```c int main() { #if d puts("1"); #else puts("2"); #endif #if a return 0; #elif b return 1; #elif c return 2; #else return 3; #endif } ``` -------------------------------- ### C++ Function Declarations vs. Variable Initializations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/ambiguities.txt Demonstrates the distinction between C++ function declarations and variable initializations, particularly when using type identifiers and parentheses. The AST helps parse these correctly. ```cpp // Function declarations T1 a(T2 *b); T3 c(T4 &d, T5 &&e); // Variable declarations with initializers T7 f(g.h); T6 i{j}; ``` ```tree-sitter-grammar (translation_unit (comment) (declaration (type_identifier) (function_declarator (identifier) (parameter_list (parameter_declaration (type_identifier) (pointer_declarator (identifier)))))) (declaration (type_identifier) (function_declarator (identifier) (parameter_list (parameter_declaration (type_identifier) (reference_declarator (identifier))) (parameter_declaration (type_identifier) (reference_declarator (identifier)))))) (comment) (declaration (type_identifier) (init_declarator (identifier) (argument_list (field_expression (identifier) (field_identifier))))) (declaration (type_identifier) (init_declarator (identifier) (initializer_list (identifier))))) ``` -------------------------------- ### Reflection in Function Arguments Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/reflection.txt Shows how to use reflection within function arguments, including type checks and template comparisons. ```cpp consteval void g(std::meta::info r, X xv) { r == (^^int) && true; (^^X) < xv; ^^X < xv; } ``` -------------------------------- ### C++ Constructor and Destructor Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Illustrates different forms of constructor and destructor declarations in C++, including default, parameterized, inline, explicit, deprecated, templated, and those with initializer lists. ```cpp class C { void *data_; public: C(); C(int, float); inline C(); explicit inline C(); [[deprecated]] C(); template C(T t); C() : NS::Base() {} ~C(); }; ``` -------------------------------- ### C++ Attributes: Function Declarations Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Demonstrates various attributes applied to function declarations, including gnu-specific and nodiscard. ```cpp [[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]] inline int g(); ``` ```cpp [[using gnu: always_inline, hot, const, visibility("default")]] [[nodiscard]] inline int g(); ``` -------------------------------- ### C Casts vs. Multiplications Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/ambiguities.txt Demonstrates the difference between C-style casts and parenthesized expressions used for multiplication. ```c int main() { // cast a((B *)c); // parenthesized product d((e * f)); } ``` -------------------------------- ### Basic C Function Declaration and Definition Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/declarations.txt Demonstrates a simple C function definition with a return type, identifier, parameters, and a body. The AST shows the structure of a translation unit containing a function definition. ```c void * do_stuff(int arg1) { return 5; } ``` ```c (translation_unit (function_definition type: (primitive_type) declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) parameters: (parameter_list (parameter_declaration type: (primitive_type) declarator: (identifier))))) body: (compound_statement (return_statement (number_literal))))) ``` -------------------------------- ### Initializer List for push_back Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Use an initializer list to pass a set of values to `push_back` for containers like `std::vector`. ```cpp int main() { pairs.push_back({true, false}); } ``` -------------------------------- ### C++ Function Parameters with Default Values Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/declarations.txt Illustrates defining a function parameter with a default value. ```cpp int foo(bool x = 5) {} ``` -------------------------------- ### C Comments After For Loops with Ambiguities Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/ambiguities.txt Illustrates a C for loop followed by a comment and an expression, showing how comments are handled in ambiguous contexts. ```c int main() { for (a *b = c; d; e) { aff; } // a-comment g; } ``` -------------------------------- ### Constrained Variable Declarations with Concepts Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/concepts.txt Shows how to declare variables using concepts with `auto` and `decltype(auto)`. ```c++ Sortable auto foo = f(); ``` ```c++ Sortable auto bar = g(); ``` ```c++ NS::Concept auto baz = h(); ``` ```c++ Sortable decltype(auto) foo = i(); ``` -------------------------------- ### Class Specification with Member Function and Initializer Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/expressions.txt Defines a class with a base class, a private member function that accepts a variadic parameter, and a field initializer. ```cpp (class_specifier name: (type_identifier) (base_class_clause (access_specifier) (type_identifier)) body: (field_declaration_list (access_specifier) (function_definition declarator: (function_declarator declarator: (identifier) parameters: (parameter_list (variadic_parameter_declaration (type_qualifier) type: (type_identifier) declarator: (reference_declarator (variadic_declarator (identifier)))))) (field_initializer_list (field_initializer (field_identifier) (argument_list (identifier)))) body: (compound_statement)))) ``` -------------------------------- ### C compound literal with designated initializers Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/test/corpus/c/expressions.txt Demonstrates creating compound literals with designated initializers for nested structures and arrays. ```c x = (SomeType) { .f1.f2[f3] = 5, .f4 = {} }; ```