Skip to content

Parse C++23 and C++26 syntax: if consteval, contracts, pack indexing, and more - #375

Open
arraypress wants to merge 1 commit into
tree-sitter:masterfrom
arraypress:modern-cpp
Open

arraypress wants to merge 1 commit into
tree-sitter:masterfrom
arraypress:modern-cpp

Conversation

@arraypress

Copy link
Copy Markdown

Parses C++23 and C++26 syntax that currently produces ERROR nodes, and fixes two misparses of = default / = delete.

Added

  • if consteval / if !consteval (C++23; overlaps Add support for C++23 feature if consteval #362).
  • Contracts: pre(…) and post(r: …) on function declarators (C++26).
  • Pack indexing Ts...[0] in types and expressions (C++26). ...[ is a single token, so a pack expansion followed by a subscript is never misread.
  • Attributes on structured bindings: auto [a [[maybe_unused]], b] (C++26).
  • = delete("reason") (C++26).
  • Explicit instantiation of a class template: template class Ring<int, 4>;.
  • Attributes before friend (overlaps Friend leading attribute #366).
  • The ->* operator, and pointer-to-member field and typedef declarators (int Widget::* field;, typedef int Widget::* P;).

Fixed

  • = default / = delete on a function outside a class. void f() = delete; produced a MISSING node, and void g() = delete("x"); parsed as a variable initialised by a delete expression.
  • friend auto operator<=>(A const &, A const &) = default; parsed as a declaration whose initialiser was an identifier named default. Its expected tree in declarations.txt now has a function_definition with a default_method_clause, the same as the member form above it.

Tests: the other existing tests pass unchanged. New test/corpus/cpp23-26.txt covers each construct.

Generated with tree-sitter-cli 0.26.13 against the tree-sitter-c version in package.json. That CLI regenerates the current src/parser.c byte for byte. One conflict entry, [_declaration_modifiers, using_declaration], became unnecessary and is removed.

Size: parser.c grows from 25.9 MB to 26.5 MB.

🤖 Generated with Claude Code

… and more

- `if consteval` / `if !consteval` (C++23, as in tree-sitter#362)
- Contracts: `pre(…)`, `post(r: …)` on function declarators (C++26)
- Pack indexing `Ts...[0]` in types and expressions (C++26); `...[` is one token, so a pack
  expansion followed by a subscript is never misread
- Attributes on structured bindings: `auto [a [[maybe_unused]], b]` (C++26)
- `= delete("reason")` (C++26)
- Explicit instantiation of a class template: `template class Ring<int, 4>;`
- Attributes before `friend` (as in tree-sitter#366)
- `= default` / `= delete` on a function outside a class. `void f() = delete;` left a MISSING
  node, and `friend auto operator<=>(…) = default;` parsed as a variable initialised with an
  identifier `default`; the expected tree in declarations.txt is updated for the second
- The `->*` operator, and pointer-to-member field and typedef declarators: `int Widget::* field;`

New corpus: test/corpus/cpp23-26.txt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant