├── .editorconfig ├── .gitattributes ├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ ├── config.yml │ └── feature_request.yml ├── dependabot.yml └── workflows │ ├── ci.yml │ ├── fuzz.yml │ ├── lint.yml │ └── publish.yml ├── .gitignore ├── CMakeLists.txt ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── Makefile ├── Package.swift ├── README.md ├── binding.gyp ├── bindings ├── c │ ├── tree-sitter-cpp.h │ └── tree-sitter-cpp.pc.in ├── go │ ├── binding.go │ └── binding_test.go ├── node │ ├── binding.cc │ ├── binding_test.js │ ├── index.d.ts │ └── index.js ├── python │ ├── tests │ │ └── test_binding.py │ └── tree_sitter_cpp │ │ ├── __init__.py │ │ ├── __init__.pyi │ │ ├── binding.c │ │ └── py.typed ├── rust │ ├── build.rs │ └── lib.rs └── swift │ ├── TreeSitterCPP │ └── cpp.h │ └── TreeSitterCPPTests │ └── TreeSitterCPPTests.swift ├── eslint.config.mjs ├── examples ├── marker-index.h └── rule.cc ├── go.mod ├── go.sum ├── grammar.js ├── package-lock.json ├── package.json ├── pyproject.toml ├── queries ├── highlights.scm ├── injections.scm └── tags.scm ├── setup.py ├── src ├── grammar.json ├── node-types.json ├── parser.c ├── scanner.c └── tree_sitter │ ├── alloc.h │ ├── array.h │ └── parser.h ├── test ├── corpus │ ├── ambiguities.txt │ ├── c │ │ ├── ambiguities.txt │ │ ├── crlf.txt │ │ ├── declarations.txt │ │ ├── expressions.txt │ │ ├── microsoft.txt │ │ ├── preprocessor.txt │ │ ├── statements.txt │ │ └── types.txt │ ├── concepts.txt │ ├── declarations.txt │ ├── definitions.txt │ ├── expressions.txt │ ├── microsoft.txt │ ├── modules.txt │ ├── statements.txt │ └── types.txt └── highlight │ ├── keywords.cpp │ └── names.cpp └── tree-sitter.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | 6 | [*.{json,toml,yml,gyp}] 7 | indent_style = space 8 | indent_size = 2 9 | 10 | [*.js] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.scm] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [*.{c,cc,h}] 19 | indent_style = space 20 | indent_size = 4 21 | 22 | [*.rs] 23 | indent_style = space 24 | indent_size = 4 25 | 26 | [*.{py,pyi}] 27 | indent_style = space 28 | indent_size = 4 29 | 30 | [*.swift] 31 | indent_style = space 32 | indent_size = 4 33 | 34 | [*.go] 35 | indent_style = tab 36 | indent_size = 8 37 | 38 | [Makefile] 39 | indent_style = tab 40 | indent_size = 8 41 | 42 | [parser.c] 43 | indent_size = 2 44 | 45 | [{alloc,array,parser}.h] 46 | indent_size = 2 47 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | 3 | # Generated source files 4 | src/*.json linguist-generated 5 | src/parser.c linguist-generated 6 | src/tree_sitter/* linguist-generated 7 | 8 | # C bindings 9 | bindings/c/* linguist-generated 10 | CMakeLists.txt linguist-generated 11 | Makefile linguist-generated 12 | 13 | # Rust bindings 14 | bindings/rust/* linguist-generated 15 | Cargo.toml linguist-generated 16 | Cargo.lock linguist-generated 17 | 18 | # Node.js bindings 19 | bindings/node/* linguist-generated 20 | binding.gyp linguist-generated 21 | package.json linguist-generated 22 | package-lock.json linguist-generated 23 | 24 | # Python bindings 25 | bindings/python/** linguist-generated 26 | setup.py linguist-generated 27 | pyproject.toml linguist-generated 28 | 29 | # Go bindings 30 | bindings/go/* linguist-generated 31 | go.mod linguist-generated 32 | go.sum linguist-generated 33 | 34 | # Swift bindings 35 | bindings/swift/** linguist-generated 36 | Package.swift linguist-generated 37 | Package.resolved linguist-generated 38 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: tree-sitter 4 | patreon: # Replace with a single Patreon username 5 | open_collective: tree-sitter # Replace with a single Open Collective username 6 | ko_fi: amaanq 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | polar: # Replace with a single Polar username 13 | buy_me_a_coffee: # Replace with a single Buy Me a Coffee username 14 | thanks_dev: # Replace with a single thanks.dev username 15 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: File a bug or issue 3 | title: "bug: " 4 | labels: [bug] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | **Before** reporting an issue, make sure to search [existing issues](https://github.com/tree-sitter/tree-sitter-cpp/issues). Usage questions such as ***"How do I...?"*** either belong in [Discussions](https://github.com/tree-sitter/tree-sitter/discussions) upstream or in our [Discord server](https://discord.gg/w7nTvsVJhm) and will be closed. 10 | If your issue is related to a bug in your editor-experience because your editor *leverages* tree-sitter and this parser, then it is likely your issue does *NOT* belong here and belongs in the relevant editor's repository. 11 | - type: checkboxes 12 | attributes: 13 | label: Did you check existing issues? 14 | description: Make sure you've checked all of the below before submitting an issue 15 | options: 16 | - label: I have read all the [tree-sitter docs](https://tree-sitter.github.io/tree-sitter/using-parsers) if it relates to using the parser 17 | required: false 18 | - label: I have searched the existing issues of tree-sitter-cpp 19 | required: true 20 | - type: input 21 | attributes: 22 | label: "Tree-Sitter CLI Version, if relevant (output of `tree-sitter --version`)" 23 | placeholder: "tree-sitter 0.20.8 (6bbb50bef8249e6460e7d69e42cc8146622fa4fd)" 24 | validations: 25 | required: false 26 | - type: textarea 27 | attributes: 28 | label: Describe the bug 29 | description: A clear and concise description of what the bug is. Please include any related errors you see such as parsing errors or tree-sitter cli errors. 30 | validations: 31 | required: true 32 | - type: textarea 33 | attributes: 34 | label: Steps To Reproduce/Bad Parse Tree 35 | description: Steps to reproduce the behavior. If you have a bad parse tree, please include it here. You can get this by running `tree-sitter parse ` and copying the output. 36 | placeholder: | 37 | 1. 38 | 2. 39 | 3. 40 | validations: 41 | required: true 42 | - type: textarea 43 | attributes: 44 | label: Expected Behavior/Parse Tree 45 | description: A concise description of what you expected to happen, or in the case of a bad parse tree, the expected parse tree. 46 | validations: 47 | required: true 48 | - type: textarea 49 | attributes: 50 | label: Repro 51 | description: Minimal code to reproduce this issue. Ideally this should be reproducible with the C library or the tree-sitter cli, do not suggest an editor or external tool. 52 | value: | 53 | // Example code that causes the issue 54 | void foo() { 55 | // Code that fails to parse, or causes an error 56 | } 57 | render: cpp 58 | validations: 59 | required: false 60 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Feature Request 2 | description: Suggest a new feature 3 | title: "feature: " 4 | labels: [enhancement] 5 | body: 6 | - type: checkboxes 7 | attributes: 8 | label: Did you check the tree-sitter docs? 9 | description: Make sure you read all the docs before submitting a feature request 10 | options: 11 | - label: I have read all the [tree-sitter docs](https://tree-sitter.github.io/tree-sitter/using-parsers) if it relates to using the parser 12 | required: false 13 | - type: textarea 14 | validations: 15 | required: true 16 | attributes: 17 | label: Is your feature request related to a problem? Please describe. 18 | description: A clear and concise description of what the problem is. Ex. I think the grammar models this rule incorrectly and can be improved, or the scanner can be improved by doing [...], or C++ has officially added a new feature that should be added to the grammar. 19 | - type: textarea 20 | validations: 21 | required: true 22 | attributes: 23 | label: Describe the solution you'd like 24 | description: A clear and concise description of what you want to happen. 25 | - type: textarea 26 | validations: 27 | required: true 28 | attributes: 29 | label: Describe alternatives you've considered 30 | description: A clear and concise description of any alternative solutions or features you've considered. 31 | - type: textarea 32 | validations: 33 | required: false 34 | attributes: 35 | label: Additional context 36 | description: Add any other context or screenshots about the feature request here. If your feature request is related to a new C++ feature, please include a link to the relevant **official** C++ documentation. 37 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | commit-message: 8 | prefix: "ci" 9 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | paths: 7 | - grammar.js 8 | - src/** 9 | - test/** 10 | - bindings/** 11 | - binding.gyp 12 | pull_request: 13 | paths: 14 | - grammar.js 15 | - src/** 16 | - test/** 17 | - bindings/** 18 | - binding.gyp 19 | 20 | concurrency: 21 | group: ${{github.workflow}}-${{github.ref}} 22 | cancel-in-progress: true 23 | 24 | jobs: 25 | test: 26 | name: Test parser 27 | runs-on: ${{matrix.os}} 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | os: [ubuntu-latest, windows-latest, macos-14] 32 | steps: 33 | - name: Checkout repository 34 | uses: actions/checkout@v4 35 | - name: Checkout tree-sitter-c 36 | uses: actions/checkout@v4 37 | with: 38 | repository: tree-sitter/tree-sitter-c 39 | path: node_modules/tree-sitter-c 40 | sparse-checkout: queries/ 41 | ref: v0.23.1 42 | - name: Set up tree-sitter 43 | uses: tree-sitter/setup-action/cli@v2 44 | - name: Run tests 45 | uses: tree-sitter/parser-test-action@v2 46 | with: 47 | test-rust: true 48 | test-node: true 49 | test-python: true 50 | test-go: true 51 | test-swift: true 52 | - name: Parse examples 53 | uses: tree-sitter/parse-action@v4 54 | with: 55 | files: examples/* 56 | -------------------------------------------------------------------------------- /.github/workflows/fuzz.yml: -------------------------------------------------------------------------------- 1 | name: Fuzz Parser 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | paths: 7 | - src/scanner.c 8 | pull_request: 9 | paths: 10 | - src/scanner.c 11 | 12 | jobs: 13 | fuzz: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout repository 17 | uses: actions/checkout@v4 18 | - name: Run fuzzer 19 | uses: tree-sitter/fuzz-action@v4 20 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | paths: 7 | - grammar.js 8 | pull_request: 9 | paths: 10 | - grammar.js 11 | 12 | jobs: 13 | lint: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout repository 17 | uses: actions/checkout@v4 18 | - name: Set up Node.js 19 | uses: actions/setup-node@v4 20 | with: 21 | cache: npm 22 | node-version: ${{vars.NODE_VERSION}} 23 | - name: Install modules 24 | run: npm ci --legacy-peer-deps 25 | - name: Run ESLint 26 | run: npm run lint 27 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish packages 2 | 3 | on: 4 | push: 5 | tags: ["*"] 6 | 7 | permissions: 8 | contents: write 9 | id-token: write 10 | attestations: write 11 | 12 | jobs: 13 | github: 14 | uses: tree-sitter/workflows/.github/workflows/release.yml@main 15 | with: 16 | generate: true 17 | attestations: true 18 | npm: 19 | uses: tree-sitter/workflows/.github/workflows/package-npm.yml@main 20 | secrets: 21 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} 22 | with: 23 | generate: true 24 | crates: 25 | uses: tree-sitter/workflows/.github/workflows/package-crates.yml@main 26 | secrets: 27 | CARGO_REGISTRY_TOKEN: ${{secrets.CARGO_REGISTRY_TOKEN}} 28 | with: 29 | generate: true 30 | pypi: 31 | uses: tree-sitter/workflows/.github/workflows/package-pypi.yml@main 32 | secrets: 33 | PYPI_API_TOKEN: ${{secrets.PYPI_API_TOKEN}} 34 | with: 35 | generate: true 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Rust artifacts 2 | target/ 3 | 4 | # Node artifacts 5 | build/ 6 | prebuilds/ 7 | node_modules/ 8 | 9 | # Swift artifacts 10 | .build/ 11 | 12 | # Go artifacts 13 | _obj/ 14 | 15 | # Python artifacts 16 | .venv/ 17 | dist/ 18 | *.egg-info 19 | *.whl 20 | 21 | # C artifacts 22 | *.a 23 | *.so 24 | *.so.* 25 | *.dylib 26 | *.dll 27 | *.pc 28 | 29 | # Example dirs 30 | /examples/*/ 31 | 32 | # Grammar volatiles 33 | *.wasm 34 | *.obj 35 | *.o 36 | 37 | # Archives 38 | *.tar.gz 39 | *.tgz 40 | *.zip 41 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | 3 | project(tree-sitter-cpp 4 | VERSION "0.23.4" 5 | DESCRIPTION "C++ grammar for tree-sitter" 6 | HOMEPAGE_URL "https://github.com/tree-sitter/tree-sitter-cpp" 7 | LANGUAGES C) 8 | 9 | option(BUILD_SHARED_LIBS "Build using shared libraries" ON) 10 | option(TREE_SITTER_REUSE_ALLOCATOR "Reuse the library allocator" OFF) 11 | 12 | set(TREE_SITTER_ABI_VERSION 14 CACHE STRING "Tree-sitter ABI version") 13 | if(NOT ${TREE_SITTER_ABI_VERSION} MATCHES "^[0-9]+$") 14 | unset(TREE_SITTER_ABI_VERSION CACHE) 15 | message(FATAL_ERROR "TREE_SITTER_ABI_VERSION must be an integer") 16 | endif() 17 | 18 | find_program(TREE_SITTER_CLI tree-sitter DOC "Tree-sitter CLI") 19 | 20 | add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c" 21 | DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json" 22 | COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json 23 | --abi=${TREE_SITTER_ABI_VERSION} 24 | WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" 25 | COMMENT "Generating parser.c") 26 | 27 | add_library(tree-sitter-cpp src/parser.c) 28 | if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c) 29 | target_sources(tree-sitter-cpp PRIVATE src/scanner.c) 30 | endif() 31 | target_include_directories(tree-sitter-cpp PRIVATE src) 32 | 33 | target_compile_definitions(tree-sitter-cpp PRIVATE 34 | $<$:TREE_SITTER_REUSE_ALLOCATOR> 35 | $<$:TREE_SITTER_DEBUG>) 36 | 37 | set_target_properties(tree-sitter-cpp 38 | PROPERTIES 39 | C_STANDARD 11 40 | POSITION_INDEPENDENT_CODE ON 41 | SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}" 42 | DEFINE_SYMBOL "") 43 | 44 | configure_file(bindings/c/tree-sitter-cpp.pc.in 45 | "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-cpp.pc" @ONLY) 46 | 47 | include(GNUInstallDirs) 48 | 49 | install(FILES bindings/c/tree-sitter-cpp.h 50 | DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tree_sitter") 51 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-cpp.pc" 52 | DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig") 53 | install(TARGETS tree-sitter-cpp 54 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") 55 | 56 | add_custom_target(ts-test "${TREE_SITTER_CLI}" test 57 | WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" 58 | COMMENT "tree-sitter test") 59 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "1.1.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "cc" 16 | version = "1.1.37" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "40545c26d092346d8a8dab71ee48e7685a7a9cba76e634790c215b41a4a7b4cf" 19 | dependencies = [ 20 | "shlex", 21 | ] 22 | 23 | [[package]] 24 | name = "memchr" 25 | version = "2.7.4" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 28 | 29 | [[package]] 30 | name = "regex" 31 | version = "1.11.1" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 34 | dependencies = [ 35 | "aho-corasick", 36 | "memchr", 37 | "regex-automata", 38 | "regex-syntax", 39 | ] 40 | 41 | [[package]] 42 | name = "regex-automata" 43 | version = "0.4.8" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" 46 | dependencies = [ 47 | "aho-corasick", 48 | "memchr", 49 | "regex-syntax", 50 | ] 51 | 52 | [[package]] 53 | name = "regex-syntax" 54 | version = "0.8.5" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 57 | 58 | [[package]] 59 | name = "shlex" 60 | version = "1.3.0" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 63 | 64 | [[package]] 65 | name = "streaming-iterator" 66 | version = "0.1.9" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" 69 | 70 | [[package]] 71 | name = "tree-sitter" 72 | version = "0.24.4" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | checksum = "b67baf55e7e1b6806063b1e51041069c90afff16afcbbccd278d899f9d84bca4" 75 | dependencies = [ 76 | "cc", 77 | "regex", 78 | "regex-syntax", 79 | "streaming-iterator", 80 | "tree-sitter-language", 81 | ] 82 | 83 | [[package]] 84 | name = "tree-sitter-cpp" 85 | version = "0.23.4" 86 | dependencies = [ 87 | "cc", 88 | "tree-sitter", 89 | "tree-sitter-language", 90 | ] 91 | 92 | [[package]] 93 | name = "tree-sitter-language" 94 | version = "0.1.2" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "e8ddffe35a0e5eeeadf13ff7350af564c6e73993a24db62caee1822b185c2600" 97 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "tree-sitter-cpp" 3 | description = "C++ grammar for tree-sitter" 4 | version = "0.23.4" 5 | authors = [ 6 | "Max Brunsfeld ", 7 | "Amaan Qureshi ", 8 | ] 9 | license = "MIT" 10 | readme = "README.md" 11 | keywords = ["incremental", "parsing", "tree-sitter", "cpp"] 12 | categories = ["parsing", "text-editors"] 13 | repository = "https://github.com/tree-sitter/tree-sitter-cpp" 14 | edition = "2021" 15 | autoexamples = false 16 | 17 | build = "bindings/rust/build.rs" 18 | include = [ 19 | "LICENSE", 20 | "bindings/rust/*", 21 | "./grammar.js", 22 | "queries/*", 23 | "src/*", 24 | "tree-sitter.json", 25 | ] 26 | 27 | [lib] 28 | path = "bindings/rust/lib.rs" 29 | 30 | [dependencies] 31 | tree-sitter-language = "0.1" 32 | 33 | [build-dependencies] 34 | cc = "1.1" 35 | 36 | [dev-dependencies] 37 | tree-sitter = "0.24" 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Max Brunsfeld 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(OS),Windows_NT) 2 | $(error Windows is not supported) 3 | endif 4 | 5 | LANGUAGE_NAME := tree-sitter-cpp 6 | HOMEPAGE_URL := https://github.com/tree-sitter/tree-sitter-cpp 7 | VERSION := 0.23.4 8 | 9 | # repository 10 | SRC_DIR := src 11 | 12 | TS ?= tree-sitter 13 | 14 | # install directory layout 15 | PREFIX ?= /usr/local 16 | INCLUDEDIR ?= $(PREFIX)/include 17 | LIBDIR ?= $(PREFIX)/lib 18 | PCLIBDIR ?= $(LIBDIR)/pkgconfig 19 | 20 | # source/object files 21 | PARSER := $(SRC_DIR)/parser.c 22 | EXTRAS := $(filter-out $(PARSER),$(wildcard $(SRC_DIR)/*.c)) 23 | OBJS := $(patsubst %.c,%.o,$(PARSER) $(EXTRAS)) 24 | 25 | # flags 26 | ARFLAGS ?= rcs 27 | override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC 28 | 29 | # ABI versioning 30 | SONAME_MAJOR = $(shell sed -n 's/\#define LANGUAGE_VERSION //p' $(PARSER)) 31 | SONAME_MINOR = $(word 1,$(subst ., ,$(VERSION))) 32 | 33 | # OS-specific bits 34 | ifeq ($(shell uname),Darwin) 35 | SOEXT = dylib 36 | SOEXTVER_MAJOR = $(SONAME_MAJOR).$(SOEXT) 37 | SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).$(SOEXT) 38 | LINKSHARED = -dynamiclib -Wl,-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks 39 | else 40 | SOEXT = so 41 | SOEXTVER_MAJOR = $(SOEXT).$(SONAME_MAJOR) 42 | SOEXTVER = $(SOEXT).$(SONAME_MAJOR).$(SONAME_MINOR) 43 | LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER) 44 | endif 45 | ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),) 46 | PCLIBDIR := $(PREFIX)/libdata/pkgconfig 47 | endif 48 | 49 | all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc 50 | 51 | lib$(LANGUAGE_NAME).a: $(OBJS) 52 | $(AR) $(ARFLAGS) $@ $^ 53 | 54 | lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS) 55 | $(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@ 56 | ifneq ($(STRIP),) 57 | $(STRIP) $@ 58 | endif 59 | 60 | $(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in 61 | sed -e 's|@PROJECT_VERSION@|$(VERSION)|' \ 62 | -e 's|@CMAKE_INSTALL_LIBDIR@|$(LIBDIR:$(PREFIX)/%=%)|' \ 63 | -e 's|@CMAKE_INSTALL_INCLUDEDIR@|$(INCLUDEDIR:$(PREFIX)/%=%)|' \ 64 | -e 's|@PROJECT_DESCRIPTION@|$(DESCRIPTION)|' \ 65 | -e 's|@PROJECT_HOMEPAGE_URL@|$(HOMEPAGE_URL)|' \ 66 | -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|' $< > $@ 67 | 68 | $(PARSER): $(SRC_DIR)/grammar.json 69 | $(TS) generate $^ 70 | 71 | install: all 72 | install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)' 73 | install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h 74 | install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc 75 | install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a 76 | install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) 77 | ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) 78 | ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) 79 | 80 | uninstall: 81 | $(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \ 82 | '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) \ 83 | '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) \ 84 | '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \ 85 | '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \ 86 | '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc 87 | 88 | clean: 89 | $(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) 90 | 91 | test: 92 | $(TS) test 93 | 94 | .PHONY: all install uninstall clean test 95 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "TreeSitterCPP", 6 | products: [ 7 | .library(name: "TreeSitterCPP", targets: ["TreeSitterCPP"]), 8 | ], 9 | dependencies: [ 10 | .package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"), 11 | ], 12 | targets: [ 13 | .target( 14 | name: "TreeSitterCPP", 15 | dependencies: [], 16 | path: ".", 17 | sources: [ 18 | "src/parser.c", 19 | "src/scanner.c", 20 | ], 21 | resources: [ 22 | .copy("queries") 23 | ], 24 | publicHeadersPath: "bindings/swift", 25 | cSettings: [.headerSearchPath("src")] 26 | ), 27 | .testTarget( 28 | name: "TreeSitterCPPTests", 29 | dependencies: [ 30 | "SwiftTreeSitter", 31 | "TreeSitterCPP", 32 | ], 33 | path: "bindings/swift/TreeSitterCPPTests" 34 | ) 35 | ], 36 | cLanguageStandard: .c11 37 | ) 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tree-sitter-cpp 2 | 3 | [![CI][ci]](https://github.com/tree-sitter/tree-sitter-cpp/actions/workflows/ci.yml) 4 | [![discord][discord]](https://discord.gg/w7nTvsVJhm) 5 | [![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org) 6 | [![crates][crates]](https://crates.io/crates/tree-sitter-cpp) 7 | [![npm][npm]](https://www.npmjs.com/package/tree-sitter-cpp) 8 | [![pypi][pypi]](https://pypi.org/project/tree-sitter-cpp) 9 | 10 | C++ grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter). 11 | 12 | ## References 13 | 14 | - [Hyperlinked C++ BNF Grammar](http://www.nongnu.org/hcb/) 15 | - [EBNF Syntax: C++](http://www.externsoft.ch/download/cpp-iso.html) 16 | 17 | [ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/tree-sitter-cpp/ci.yml?logo=github&label=CI 18 | [discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord 19 | [matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix 20 | [npm]: https://img.shields.io/npm/v/tree-sitter-cpp?logo=npm 21 | [crates]: https://img.shields.io/crates/v/tree-sitter-cpp?logo=rust 22 | [pypi]: https://img.shields.io/pypi/v/tree-sitter-cpp?logo=pypi&logoColor=ffd242 23 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "tree_sitter_cpp_binding", 5 | "dependencies": [ 6 | " 2 | 3 | typedef struct TSLanguage TSLanguage; 4 | 5 | extern "C" TSLanguage *tree_sitter_cpp(); 6 | 7 | // "tree-sitter", "language" hashed with BLAKE2 8 | const napi_type_tag LANGUAGE_TYPE_TAG = { 9 | 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16 10 | }; 11 | 12 | Napi::Object Init(Napi::Env env, Napi::Object exports) { 13 | exports["name"] = Napi::String::New(env, "cpp"); 14 | auto language = Napi::External::New(env, tree_sitter_cpp()); 15 | language.TypeTag(&LANGUAGE_TYPE_TAG); 16 | exports["language"] = language; 17 | return exports; 18 | } 19 | 20 | NODE_API_MODULE(tree_sitter_cpp_binding, Init) 21 | -------------------------------------------------------------------------------- /bindings/node/binding_test.js: -------------------------------------------------------------------------------- 1 | const assert = require("node:assert"); 2 | const { test } = require("node:test"); 3 | 4 | const Parser = require("tree-sitter"); 5 | 6 | test("can load grammar", () => { 7 | const parser = new Parser(); 8 | assert.doesNotThrow(() => parser.setLanguage(require("."))); 9 | }); 10 | -------------------------------------------------------------------------------- /bindings/node/index.d.ts: -------------------------------------------------------------------------------- 1 | type BaseNode = { 2 | type: string; 3 | named: boolean; 4 | }; 5 | 6 | type ChildNode = { 7 | multiple: boolean; 8 | required: boolean; 9 | types: BaseNode[]; 10 | }; 11 | 12 | type NodeInfo = 13 | | (BaseNode & { 14 | subtypes: BaseNode[]; 15 | }) 16 | | (BaseNode & { 17 | fields: { [name: string]: ChildNode }; 18 | children: ChildNode[]; 19 | }); 20 | 21 | type Language = { 22 | name: string; 23 | language: unknown; 24 | nodeTypeInfo: NodeInfo[]; 25 | }; 26 | 27 | declare const language: Language; 28 | export = language; 29 | -------------------------------------------------------------------------------- /bindings/node/index.js: -------------------------------------------------------------------------------- 1 | const root = require("path").join(__dirname, "..", ".."); 2 | 3 | module.exports = 4 | typeof process.versions.bun === "string" 5 | // Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time 6 | ? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-cpp.node`) 7 | : require("node-gyp-build")(root); 8 | 9 | try { 10 | module.exports.nodeTypeInfo = require("../../src/node-types.json"); 11 | } catch (_) {} 12 | -------------------------------------------------------------------------------- /bindings/python/tests/test_binding.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | 3 | import tree_sitter, tree_sitter_cpp 4 | 5 | 6 | class TestLanguage(TestCase): 7 | def test_can_load_grammar(self): 8 | try: 9 | tree_sitter.Language(tree_sitter_cpp.language()) 10 | except Exception: 11 | self.fail("Error loading C++ grammar") 12 | -------------------------------------------------------------------------------- /bindings/python/tree_sitter_cpp/__init__.py: -------------------------------------------------------------------------------- 1 | """C++ grammar for tree-sitter""" 2 | 3 | from importlib.resources import files as _files 4 | 5 | from ._binding import language 6 | 7 | 8 | def _get_query(name, file): 9 | query = _files(f"{__package__}.queries") / file 10 | globals()[name] = query.read_text() 11 | return globals()[name] 12 | 13 | 14 | def __getattr__(name): 15 | if name == "HIGHLIGHTS_QUERY": 16 | return _get_query("HIGHLIGHTS_QUERY", "highlights.scm") 17 | if name == "INJECTIONS_QUERY": 18 | return _get_query("INJECTIONS_QUERY", "injections.scm") 19 | if name == "TAGS_QUERY": 20 | return _get_query("TAGS_QUERY", "tags.scm") 21 | 22 | raise AttributeError(f"module {__name__!r} has no attribute {name!r}") 23 | 24 | 25 | __all__ = [ 26 | "language", 27 | "HIGHLIGHTS_QUERY", 28 | "INJECTIONS_QUERY", 29 | "TAGS_QUERY", 30 | ] 31 | 32 | 33 | def __dir__(): 34 | return sorted(__all__ + [ 35 | "__all__", "__builtins__", "__cached__", "__doc__", "__file__", 36 | "__loader__", "__name__", "__package__", "__path__", "__spec__", 37 | ]) 38 | -------------------------------------------------------------------------------- /bindings/python/tree_sitter_cpp/__init__.pyi: -------------------------------------------------------------------------------- 1 | from typing import Final 2 | 3 | HIGHLIGHTS_QUERY: Final[str] 4 | INJECTIONS_QUERY: Final[str] 5 | TAGS_QUERY: Final[str] 6 | 7 | def language() -> object: ... 8 | -------------------------------------------------------------------------------- /bindings/python/tree_sitter_cpp/binding.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct TSLanguage TSLanguage; 4 | 5 | TSLanguage *tree_sitter_cpp(void); 6 | 7 | static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) { 8 | return PyCapsule_New(tree_sitter_cpp(), "tree_sitter.Language", NULL); 9 | } 10 | 11 | static PyMethodDef methods[] = { 12 | {"language", _binding_language, METH_NOARGS, 13 | "Get the tree-sitter language for this grammar."}, 14 | {NULL, NULL, 0, NULL} 15 | }; 16 | 17 | static struct PyModuleDef module = { 18 | .m_base = PyModuleDef_HEAD_INIT, 19 | .m_name = "_binding", 20 | .m_doc = NULL, 21 | .m_size = -1, 22 | .m_methods = methods 23 | }; 24 | 25 | PyMODINIT_FUNC PyInit__binding(void) { 26 | return PyModule_Create(&module); 27 | } 28 | -------------------------------------------------------------------------------- /bindings/python/tree_sitter_cpp/py.typed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tree-sitter/tree-sitter-cpp/56455f4245baf4ea4e0881c5169de69d7edd5ae7/bindings/python/tree_sitter_cpp/py.typed -------------------------------------------------------------------------------- /bindings/rust/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let src_dir = std::path::Path::new("src"); 3 | 4 | let mut c_config = cc::Build::new(); 5 | c_config.std("c11").include(src_dir); 6 | 7 | #[cfg(target_env = "msvc")] 8 | c_config.flag("-utf-8"); 9 | 10 | let parser_path = src_dir.join("parser.c"); 11 | c_config.file(&parser_path); 12 | println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); 13 | 14 | let scanner_path = src_dir.join("scanner.c"); 15 | c_config.file(&scanner_path); 16 | println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); 17 | 18 | c_config.compile("tree-sitter-cpp"); 19 | } 20 | -------------------------------------------------------------------------------- /bindings/rust/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides C++ language support for the [tree-sitter][] parsing library. 2 | //! 3 | //! Typically, you will use the [LANGUAGE][] constant to add this language to a 4 | //! tree-sitter [Parser][], and then use the parser to parse some code: 5 | //! 6 | //! ``` 7 | //! use tree_sitter::Parser; 8 | //! 9 | //! let code = r#" 10 | //! int double(int x) { 11 | //! return x * 2; 12 | //! } 13 | //! "#; 14 | //! let mut parser = tree_sitter::Parser::new(); 15 | //! let language = tree_sitter_cpp::LANGUAGE; 16 | //! parser 17 | //! .set_language(&language.into()) 18 | //! .expect("Error loading C++ parser"); 19 | //! let tree = parser.parse(code, None).unwrap(); 20 | //! assert!(!tree.root_node().has_error()); 21 | //! ``` 22 | //! 23 | //! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html 24 | //! [tree-sitter]: https://tree-sitter.github.io/ 25 | 26 | use tree_sitter_language::LanguageFn; 27 | 28 | extern "C" { 29 | fn tree_sitter_cpp() -> *const (); 30 | } 31 | 32 | /// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar. 33 | /// 34 | /// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html 35 | pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_cpp) }; 36 | 37 | /// The content of the [`node-types.json`][] file for this grammar. 38 | /// 39 | /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types 40 | pub const NODE_TYPES: &str = include_str!("../../src/node-types.json"); 41 | 42 | /// The syntax highlighting query for this language. 43 | pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm"); 44 | 45 | /// The symbol tagging query for this language. 46 | pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm"); 47 | 48 | #[cfg(test)] 49 | mod tests { 50 | #[test] 51 | fn test_can_load_grammar() { 52 | let mut parser = tree_sitter::Parser::new(); 53 | parser 54 | .set_language(&super::LANGUAGE.into()) 55 | .expect("Error loading C++ parser"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /bindings/swift/TreeSitterCPP/cpp.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_SITTER_CPP_H_ 2 | #define TREE_SITTER_CPP_H_ 3 | 4 | typedef struct TSLanguage TSLanguage; 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | const TSLanguage *tree_sitter_cpp(void); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif // TREE_SITTER_CPP_H_ 17 | -------------------------------------------------------------------------------- /bindings/swift/TreeSitterCPPTests/TreeSitterCPPTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import SwiftTreeSitter 3 | import TreeSitterCPP 4 | 5 | final class TreeSitterCPPTests: XCTestCase { 6 | func testCanLoadGrammar() throws { 7 | let parser = Parser() 8 | let language = Language(language: tree_sitter_cpp()) 9 | XCTAssertNoThrow(try parser.setLanguage(language), 10 | "Error loading C++ grammar") 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /eslint.config.mjs: -------------------------------------------------------------------------------- 1 | import treesitter from 'eslint-config-treesitter'; 2 | 3 | export default [ 4 | ...treesitter, 5 | ]; 6 | -------------------------------------------------------------------------------- /examples/marker-index.h: -------------------------------------------------------------------------------- 1 | #ifndef MARKER_INDEX_H_ 2 | #define MARKER_INDEX_H_ 3 | 4 | #include 5 | #include 6 | #include "flat_set.h" 7 | #include "point.h" 8 | #include "range.h" 9 | 10 | class MarkerIndex { 11 | public: 12 | using MarkerId = unsigned; 13 | using MarkerIdSet = flat_set; 14 | 15 | struct SpliceResult { 16 | flat_set touch; 17 | flat_set inside; 18 | flat_set overlap; 19 | flat_set surround; 20 | }; 21 | 22 | struct Boundary { 23 | Point position; 24 | flat_set starting; 25 | flat_set ending; 26 | }; 27 | 28 | struct BoundaryQueryResult { 29 | std::vector containing_start; 30 | std::vector boundaries; 31 | }; 32 | 33 | MarkerIndex(unsigned seed = 0u); 34 | ~MarkerIndex(); 35 | int generate_random_number(); 36 | void insert(MarkerId id, Point start, Point end); 37 | void set_exclusive(MarkerId id, bool exclusive); 38 | void remove(MarkerId id); 39 | bool has(MarkerId id); 40 | SpliceResult splice(Point start, Point old_extent, Point new_extent); 41 | Point get_start(MarkerId id) const; 42 | Point get_end(MarkerId id) const; 43 | Range get_range(MarkerId id) const; 44 | 45 | int compare(MarkerId id1, MarkerId id2) const; 46 | flat_set find_intersecting(Point start, Point end); 47 | flat_set find_containing(Point start, Point end); 48 | flat_set find_contained_in(Point start, Point end); 49 | flat_set find_starting_in(Point start, Point end); 50 | flat_set find_starting_at(Point position); 51 | flat_set find_ending_in(Point start, Point end); 52 | flat_set find_ending_at(Point position); 53 | BoundaryQueryResult find_boundaries_after(Point start, size_t max_count); 54 | 55 | std::unordered_map dump(); 56 | 57 | private: 58 | friend class Iterator; 59 | 60 | struct Node { 61 | Node *parent; 62 | Node *left; 63 | Node *right; 64 | Point left_extent; 65 | flat_set left_marker_ids; 66 | flat_set right_marker_ids; 67 | flat_set start_marker_ids; 68 | flat_set end_marker_ids; 69 | int priority; 70 | 71 | Node(Node *parent, Point left_extent); 72 | bool is_marker_endpoint(); 73 | }; 74 | 75 | class Iterator { 76 | public: 77 | Iterator(MarkerIndex *marker_index); 78 | void reset(); 79 | Node* insert_marker_start(const MarkerId &id, const Point &start_position, const Point &end_position); 80 | Node* insert_marker_end(const MarkerId &id, const Point &start_position, const Point &end_position); 81 | Node* insert_splice_boundary(const Point &position, bool is_insertion_end); 82 | void find_intersecting(const Point &start, const Point &end, flat_set *result); 83 | void find_contained_in(const Point &start, const Point &end, flat_set *result); 84 | void find_starting_in(const Point &start, const Point &end, flat_set *result); 85 | void find_ending_in(const Point &start, const Point &end, flat_set *result); 86 | void find_boundaries_after(Point start, size_t max_count, BoundaryQueryResult *result); 87 | std::unordered_map dump(); 88 | 89 | private: 90 | void ascend(); 91 | void descend_left(); 92 | void descend_right(); 93 | void move_to_successor(); 94 | void seek_to_first_node_greater_than_or_equal_to(const Point &position); 95 | void mark_right(const MarkerId &id, const Point &start_position, const Point &end_position); 96 | void mark_left(const MarkerId &id, const Point &start_position, const Point &end_position); 97 | Node* insert_left_child(const Point &position); 98 | Node* insert_right_child(const Point &position); 99 | void check_intersection(const Point &start, const Point &end, flat_set *results); 100 | void cache_node_position() const; 101 | 102 | MarkerIndex *marker_index; 103 | Node *current_node; 104 | Point current_node_position; 105 | Point left_ancestor_position; 106 | Point right_ancestor_position; 107 | std::vector left_ancestor_position_stack; 108 | std::vector right_ancestor_position_stack; 109 | }; 110 | 111 | Point get_node_position(const Node *node) const; 112 | void delete_node(Node *node); 113 | void delete_subtree(Node *node); 114 | void bubble_node_up(Node *node); 115 | void bubble_node_down(Node *node); 116 | void rotate_node_left(Node *pivot); 117 | void rotate_node_right(Node *pivot); 118 | void get_starting_and_ending_markers_within_subtree(const Node *node, flat_set *starting, flat_set *ending); 119 | void populate_splice_invalidation_sets(SpliceResult *invalidated, const Node *start_node, const Node *end_node, const flat_set &starting_inside_splice, const flat_set &ending_inside_splice); 120 | 121 | std::default_random_engine random_engine; 122 | std::uniform_int_distribution random_distribution; 123 | Node *root; 124 | std::unordered_map start_nodes_by_id; 125 | std::unordered_map end_nodes_by_id; 126 | Iterator iterator; 127 | flat_set exclusive_marker_ids; 128 | mutable std::unordered_map node_position_cache; 129 | }; 130 | 131 | #endif // MARKER_INDEX_H_ 132 | -------------------------------------------------------------------------------- /examples/rule.cc: -------------------------------------------------------------------------------- 1 | #include "compiler/rule.h" 2 | #include "compiler/util/hash_combine.h" 3 | 4 | namespace tree_sitter { 5 | namespace rules { 6 | 7 | using std::move; 8 | using std::vector; 9 | using util::hash_combine; 10 | 11 | Rule::Rule(const Rule &other) : blank_(Blank{}), type(BlankType) { 12 | *this = other; 13 | } 14 | 15 | Rule::Rule(Rule &&other) noexcept : blank_(Blank{}), type(BlankType) { 16 | *this = move(other); 17 | } 18 | 19 | static void destroy_value(Rule *rule) { 20 | switch (rule->type) { 21 | case Rule::BlankType: return rule->blank_.~Blank(); 22 | case Rule::CharacterSetType: return rule->character_set_.~CharacterSet(); 23 | case Rule::StringType: return rule->string_ .~String(); 24 | case Rule::PatternType: return rule->pattern_ .~Pattern(); 25 | case Rule::NamedSymbolType: return rule->named_symbol_.~NamedSymbol(); 26 | case Rule::SymbolType: return rule->symbol_ .~Symbol(); 27 | case Rule::ChoiceType: return rule->choice_ .~Choice(); 28 | case Rule::MetadataType: return rule->metadata_ .~Metadata(); 29 | case Rule::RepeatType: return rule->repeat_ .~Repeat(); 30 | case Rule::SeqType: return rule->seq_ .~Seq(); 31 | } 32 | } 33 | 34 | Rule &Rule::operator=(const Rule &other) { 35 | destroy_value(this); 36 | type = other.type; 37 | switch (type) { 38 | case BlankType: 39 | new (&blank_) Blank(other.blank_); 40 | break; 41 | case CharacterSetType: 42 | new (&character_set_) CharacterSet(other.character_set_); 43 | break; 44 | case StringType: 45 | new (&string_) String(other.string_); 46 | break; 47 | case PatternType: 48 | new (&pattern_) Pattern(other.pattern_); 49 | break; 50 | case NamedSymbolType: 51 | new (&named_symbol_) NamedSymbol(other.named_symbol_); 52 | break; 53 | case SymbolType: 54 | new (&symbol_) Symbol(other.symbol_); 55 | break; 56 | case ChoiceType: 57 | new (&choice_) Choice(other.choice_); 58 | break; 59 | case MetadataType: 60 | new (&metadata_) Metadata(other.metadata_); 61 | break; 62 | case RepeatType: 63 | new (&repeat_) Repeat(other.repeat_); 64 | break; 65 | case SeqType: 66 | new (&seq_) Seq(other.seq_); 67 | break; 68 | } 69 | return *this; 70 | } 71 | 72 | Rule &Rule::operator=(Rule &&other) noexcept { 73 | destroy_value(this); 74 | type = other.type; 75 | switch (type) { 76 | case BlankType: 77 | new (&blank_) Blank(move(other.blank_)); 78 | break; 79 | case CharacterSetType: 80 | new (&character_set_) CharacterSet(move(other.character_set_)); 81 | break; 82 | case StringType: 83 | new (&string_) String(move(other.string_)); 84 | break; 85 | case PatternType: 86 | new (&pattern_) Pattern(move(other.pattern_)); 87 | break; 88 | case NamedSymbolType: 89 | new (&named_symbol_) NamedSymbol(move(other.named_symbol_)); 90 | break; 91 | case SymbolType: 92 | new (&symbol_) Symbol(move(other.symbol_)); 93 | break; 94 | case ChoiceType: 95 | new (&choice_) Choice(move(other.choice_)); 96 | break; 97 | case MetadataType: 98 | new (&metadata_) Metadata(move(other.metadata_)); 99 | break; 100 | case RepeatType: 101 | new (&repeat_) Repeat(move(other.repeat_)); 102 | break; 103 | case SeqType: 104 | new (&seq_) Seq(move(other.seq_)); 105 | break; 106 | } 107 | other.type = BlankType; 108 | other.blank_ = Blank{}; 109 | return *this; 110 | } 111 | 112 | Rule::~Rule() noexcept { 113 | destroy_value(this); 114 | } 115 | 116 | bool Rule::operator==(const Rule &other) const { 117 | if (type != other.type) return false; 118 | switch (type) { 119 | case Rule::CharacterSetType: return character_set_ == other.character_set_; 120 | case Rule::StringType: return string_ == other.string_; 121 | case Rule::PatternType: return pattern_ == other.pattern_; 122 | case Rule::NamedSymbolType: return named_symbol_ == other.named_symbol_; 123 | case Rule::SymbolType: return symbol_ == other.symbol_; 124 | case Rule::ChoiceType: return choice_ == other.choice_; 125 | case Rule::MetadataType: return metadata_ == other.metadata_; 126 | case Rule::RepeatType: return repeat_ == other.repeat_; 127 | case Rule::SeqType: return seq_ == other.seq_; 128 | default: return blank_ == other.blank_; 129 | } 130 | } 131 | 132 | template <> 133 | bool Rule::is() const { return type == BlankType; } 134 | 135 | template <> 136 | bool Rule::is() const { return type == SymbolType; } 137 | 138 | template <> 139 | bool Rule::is() const { return type == RepeatType; } 140 | 141 | template <> 142 | const Symbol & Rule::get_unchecked() const { return symbol_; } 143 | 144 | static inline void add_choice_element(std::vector *elements, const Rule &new_rule) { 145 | new_rule.match( 146 | [elements](Choice choice) { 147 | for (auto &element : choice.elements) { 148 | add_choice_element(elements, element); 149 | } 150 | }, 151 | 152 | [elements](auto rule) { 153 | for (auto &element : *elements) { 154 | if (element == rule) return; 155 | } 156 | elements->push_back(rule); 157 | } 158 | ); 159 | } 160 | 161 | Rule Rule::choice(const vector &rules) { 162 | vector elements; 163 | for (auto &element : rules) { 164 | add_choice_element(&elements, element); 165 | } 166 | return (elements.size() == 1) ? elements.front() : Choice{elements}; 167 | } 168 | 169 | Rule Rule::repeat(const Rule &rule) { 170 | return rule.is() ? rule : Repeat{rule}; 171 | } 172 | 173 | Rule Rule::seq(const vector &rules) { 174 | Rule result; 175 | for (const auto &rule : rules) { 176 | rule.match( 177 | [](Blank) {}, 178 | [&](Metadata metadata) { 179 | if (!metadata.rule->is()) { 180 | result = Seq{result, rule}; 181 | } 182 | }, 183 | [&](auto) { 184 | if (result.is()) { 185 | result = rule; 186 | } else { 187 | result = Seq{result, rule}; 188 | } 189 | } 190 | ); 191 | } 192 | return result; 193 | } 194 | 195 | } // namespace rules 196 | } // namespace tree_sitter 197 | 198 | namespace std { 199 | 200 | size_t hash::operator()(const Symbol &symbol) const { 201 | auto result = hash()(symbol.index); 202 | hash_combine(&result, hash()(symbol.type)); 203 | return result; 204 | } 205 | 206 | size_t hash::operator()(const NamedSymbol &symbol) const { 207 | return hash()(symbol.value); 208 | } 209 | 210 | size_t hash::operator()(const Pattern &symbol) const { 211 | return hash()(symbol.value); 212 | } 213 | 214 | size_t hash::operator()(const String &symbol) const { 215 | return hash()(symbol.value); 216 | } 217 | 218 | size_t hash::operator()(const CharacterSet &character_set) const { 219 | size_t result = 0; 220 | hash_combine(&result, character_set.includes_all); 221 | hash_combine(&result, character_set.included_chars.size()); 222 | for (uint32_t c : character_set.included_chars) { 223 | hash_combine(&result, c); 224 | } 225 | hash_combine(&result, character_set.excluded_chars.size()); 226 | for (uint32_t c : character_set.excluded_chars) { 227 | hash_combine(&result, c); 228 | } 229 | return result; 230 | } 231 | 232 | size_t hash::operator()(const Blank &blank) const { 233 | return 0; 234 | } 235 | 236 | size_t hash::operator()(const Choice &choice) const { 237 | size_t result = 0; 238 | for (const auto &element : choice.elements) { 239 | symmetric_hash_combine(&result, element); 240 | } 241 | return result; 242 | } 243 | 244 | size_t hash::operator()(const Repeat &repeat) const { 245 | size_t result = 0; 246 | hash_combine(&result, *repeat.rule); 247 | return result; 248 | } 249 | 250 | size_t hash::operator()(const Seq &seq) const { 251 | size_t result = 0; 252 | hash_combine(&result, *seq.left); 253 | hash_combine(&result, *seq.right); 254 | return result; 255 | } 256 | 257 | size_t hash::operator()(const Metadata &metadata) const { 258 | size_t result = 0; 259 | hash_combine(&result, *metadata.rule); 260 | hash_combine(&result, metadata.params.precedence); 261 | hash_combine(&result, metadata.params.associativity); 262 | hash_combine(&result, metadata.params.has_precedence); 263 | hash_combine(&result, metadata.params.has_associativity); 264 | hash_combine(&result, metadata.params.is_token); 265 | hash_combine(&result, metadata.params.is_string); 266 | hash_combine(&result, metadata.params.is_active); 267 | hash_combine(&result, metadata.params.is_main_token); 268 | return result; 269 | } 270 | 271 | size_t hash::operator()(const Rule &rule) const { 272 | size_t result = hash()(rule.type); 273 | switch (rule.type) { 274 | case Rule::CharacterSetType: return result ^ hash()(rule.character_set_); 275 | case Rule::StringType: return result ^ hash()(rule.string_); 276 | case Rule::PatternType: return result ^ hash()(rule.pattern_); 277 | case Rule::NamedSymbolType: return result ^ hash()(rule.named_symbol_); 278 | case Rule::SymbolType: return result ^ hash()(rule.symbol_); 279 | case Rule::ChoiceType: return result ^ hash()(rule.choice_); 280 | case Rule::MetadataType: return result ^ hash()(rule.metadata_); 281 | case Rule::RepeatType: return result ^ hash()(rule.repeat_); 282 | case Rule::SeqType: return result ^ hash()(rule.seq_); 283 | default: return result ^ hash()(rule.blank_); 284 | } 285 | } 286 | 287 | } // namespace std -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tree-sitter/tree-sitter-cpp 2 | 3 | go 1.22 4 | 5 | require github.com/tree-sitter/go-tree-sitter v0.24.0 6 | 7 | require github.com/mattn/go-pointer v0.0.1 // indirect 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0= 4 | github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 8 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 9 | github.com/tree-sitter/go-tree-sitter v0.24.0 h1:kRZb6aBNfcI/u0Qh8XEt3zjNVnmxTisDBN+kXK0xRYQ= 10 | github.com/tree-sitter/go-tree-sitter v0.24.0/go.mod h1:x681iFVoLMEwOSIHA1chaLkXlroXEN7WY+VHGFaoDbk= 11 | github.com/tree-sitter/tree-sitter-c v0.21.5-0.20240818205408-927da1f210eb h1:A8425heRM8mylnv4H58FPUiH+aYivyitre0PzxrfmWs= 12 | github.com/tree-sitter/tree-sitter-c v0.21.5-0.20240818205408-927da1f210eb/go.mod h1:dOF6gtQiF9UwNh995T5OphYmtIypkjsp3ap7r9AN/iA= 13 | github.com/tree-sitter/tree-sitter-embedded-template v0.21.1-0.20240819044651-ffbf64942c33 h1:TwqSV3qLp3tKSqirGLRHnjFk9Tc2oy57LIl+FQ4GjI4= 14 | github.com/tree-sitter/tree-sitter-embedded-template v0.21.1-0.20240819044651-ffbf64942c33/go.mod h1:CvCKCt3v04Ufos1zZnNCelBDeCGRpPucaN8QczoUsN4= 15 | github.com/tree-sitter/tree-sitter-go v0.21.3-0.20240818010209-8c0f0e7a6012 h1:Xvxck3tE5FW7F7bTS97iNM2ADMyCMJztVqn5HYKdJGo= 16 | github.com/tree-sitter/tree-sitter-go v0.21.3-0.20240818010209-8c0f0e7a6012/go.mod h1:T40D0O1cPvUU/+AmiXVXy1cncYQT6wem4Z0g4SfAYvY= 17 | github.com/tree-sitter/tree-sitter-html v0.20.5-0.20240818004741-d11201a263d0 h1:c46K6uh5Dz00zJeU9BfjXdb8I+E4RkUdfnWJpQADXFo= 18 | github.com/tree-sitter/tree-sitter-html v0.20.5-0.20240818004741-d11201a263d0/go.mod h1:hcNt/kOJHcIcuMvouE7LJcYdeFUFbVpBJ6d4wmOA+tU= 19 | github.com/tree-sitter/tree-sitter-java v0.21.1-0.20240824015150-576d8097e495 h1:jrt4qbJVEFs4H93/ITxygHc6u0TGqAkkate7TQ4wFSA= 20 | github.com/tree-sitter/tree-sitter-java v0.21.1-0.20240824015150-576d8097e495/go.mod h1:oyaR7fLnRV0hT9z6qwE9GkaeTom/hTDwK3H2idcOJFc= 21 | github.com/tree-sitter/tree-sitter-javascript v0.21.5-0.20240818005344-15887341e5b5 h1:om4X9AVg3asL8gxNJDcz4e/Wp+VpQj1PY3uJXKr6EOg= 22 | github.com/tree-sitter/tree-sitter-javascript v0.21.5-0.20240818005344-15887341e5b5/go.mod h1:nNqgPoV/h9uYWk6kYEFdEAhNVOacpfpRW5SFmdaP4tU= 23 | github.com/tree-sitter/tree-sitter-json v0.21.1-0.20240818005659-bdd69eb8c8a5 h1:pfV3G3k7NCKqKk8THBmyuh2zA33lgYHS3GVrzRR8ry4= 24 | github.com/tree-sitter/tree-sitter-json v0.21.1-0.20240818005659-bdd69eb8c8a5/go.mod h1:GbMKRjLfk0H+PI7nLi1Sx5lHf5wCpLz9al8tQYSxpEk= 25 | github.com/tree-sitter/tree-sitter-php v0.22.9-0.20240819002312-a552625b56c1 h1:ZXZMDwE+IhUtGug4Brv6NjJWUU3rfkZBKpemf6RY8/g= 26 | github.com/tree-sitter/tree-sitter-php v0.22.9-0.20240819002312-a552625b56c1/go.mod h1:UKCLuYnJ312Mei+3cyTmGOHzn0YAnaPRECgJmHtzrqs= 27 | github.com/tree-sitter/tree-sitter-python v0.21.1-0.20240818005537-55a9b8a4fbfb h1:EXEM82lFM7JjJb6qiKZXkpIDaCcbV2obNn82ghwj9lw= 28 | github.com/tree-sitter/tree-sitter-python v0.21.1-0.20240818005537-55a9b8a4fbfb/go.mod h1:lXCF1nGG5Dr4J3BTS0ObN4xJCCICiSu/b+Xe/VqMV7g= 29 | github.com/tree-sitter/tree-sitter-ruby v0.21.1-0.20240818211811-7dbc1e2d0e2d h1:fcYCvoXdcP1uRQYXqJHRy6Hec+uKScQdKVtMwK9JeCI= 30 | github.com/tree-sitter/tree-sitter-ruby v0.21.1-0.20240818211811-7dbc1e2d0e2d/go.mod h1:T1nShQ4v5AJtozZ8YyAS4uzUtDAJj/iv4YfwXSbUHzg= 31 | github.com/tree-sitter/tree-sitter-rust v0.21.3-0.20240818005432-2b43eafe6447 h1:o9alBu1J/WjrcTKEthYtXmdkDc5OVXD+PqlvnEZ0Lzc= 32 | github.com/tree-sitter/tree-sitter-rust v0.21.3-0.20240818005432-2b43eafe6447/go.mod h1:1Oh95COkkTn6Ezp0vcMbvfhRP5gLeqqljR0BYnBzWvc= 33 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 34 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tree-sitter-cpp", 3 | "version": "0.23.4", 4 | "description": "C++ grammar for tree-sitter", 5 | "repository": "https://github.com/tree-sitter/tree-sitter-cpp", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Max Brunsfeld", 9 | "email": "maxbrunsfeld@gmail.com" 10 | }, 11 | "maintainers": [ 12 | { 13 | "name": "Amaan Qureshi", 14 | "email": "amaanq12@gmail.com" 15 | } 16 | ], 17 | "main": "bindings/node", 18 | "types": "bindings/node", 19 | "keywords": [ 20 | "incremental", 21 | "parsing", 22 | "tree-sitter", 23 | "c++" 24 | ], 25 | "files": [ 26 | "grammar.js", 27 | "tree-sitter.json", 28 | "binding.gyp", 29 | "prebuilds/**", 30 | "bindings/node/*", 31 | "queries/*", 32 | "src/**", 33 | "*.wasm" 34 | ], 35 | "dependencies": { 36 | "node-addon-api": "^8.2.1", 37 | "node-gyp-build": "^4.8.2", 38 | "tree-sitter-c": "^0.23.1" 39 | }, 40 | "devDependencies": { 41 | "eslint": "^9.12.0", 42 | "eslint-config-treesitter": "^1.0.2", 43 | "prebuildify": "^6.0.1", 44 | "tree-sitter-cli": "^0.24.3" 45 | }, 46 | "peerDependencies": { 47 | "tree-sitter": "^0.21.1" 48 | }, 49 | "peerDependenciesMeta": { 50 | "tree-sitter": { 51 | "optional": true 52 | } 53 | }, 54 | "scripts": { 55 | "install": "node-gyp-build", 56 | "lint": "eslint grammar.js", 57 | "prestart": "tree-sitter build --wasm", 58 | "start": "tree-sitter playground", 59 | "test": "node --test bindings/node/*_test.js" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=42", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "tree-sitter-cpp" 7 | description = "C++ grammar for tree-sitter" 8 | version = "0.23.4" 9 | keywords = ["incremental", "parsing", "tree-sitter", "cpp"] 10 | classifiers = [ 11 | "Intended Audience :: Developers", 12 | "License :: OSI Approved :: MIT License", 13 | "Topic :: Software Development :: Compilers", 14 | "Topic :: Text Processing :: Linguistic", 15 | "Typing :: Typed", 16 | ] 17 | authors = [ 18 | { name = "Max Brunsfeld", email = "maxbrunsfeld@gmail.com" }, 19 | { name = "Amaan Qureshi", email = "amaanq12@gmail.com" }, 20 | ] 21 | requires-python = ">=3.9" 22 | license.text = "MIT" 23 | readme = "README.md" 24 | 25 | [project.urls] 26 | Homepage = "https://github.com/tree-sitter/tree-sitter-cpp" 27 | 28 | [project.optional-dependencies] 29 | core = ["tree-sitter~=0.22"] 30 | 31 | [tool.cibuildwheel] 32 | build = "cp39-*" 33 | build-frontend = "build" 34 | -------------------------------------------------------------------------------- /queries/highlights.scm: -------------------------------------------------------------------------------- 1 | ; Functions 2 | 3 | (call_expression 4 | function: (qualified_identifier 5 | name: (identifier) @function)) 6 | 7 | (template_function 8 | name: (identifier) @function) 9 | 10 | (template_method 11 | name: (field_identifier) @function) 12 | 13 | (template_function 14 | name: (identifier) @function) 15 | 16 | (function_declarator 17 | declarator: (qualified_identifier 18 | name: (identifier) @function)) 19 | 20 | (function_declarator 21 | declarator: (field_identifier) @function) 22 | 23 | ; Types 24 | 25 | ((namespace_identifier) @type 26 | (#match? @type "^[A-Z]")) 27 | 28 | (auto) @type 29 | 30 | ; Constants 31 | 32 | (this) @variable.builtin 33 | (null "nullptr" @constant) 34 | 35 | ; Modules 36 | (module_name 37 | (identifier) @module) 38 | 39 | ; Keywords 40 | 41 | [ 42 | "catch" 43 | "class" 44 | "co_await" 45 | "co_return" 46 | "co_yield" 47 | "constexpr" 48 | "constinit" 49 | "consteval" 50 | "delete" 51 | "explicit" 52 | "final" 53 | "friend" 54 | "mutable" 55 | "namespace" 56 | "noexcept" 57 | "new" 58 | "override" 59 | "private" 60 | "protected" 61 | "public" 62 | "template" 63 | "throw" 64 | "try" 65 | "typename" 66 | "using" 67 | "concept" 68 | "requires" 69 | "virtual" 70 | "import" 71 | "export" 72 | "module" 73 | ] @keyword 74 | 75 | ; Strings 76 | 77 | (raw_string_literal) @string 78 | -------------------------------------------------------------------------------- /queries/injections.scm: -------------------------------------------------------------------------------- 1 | (raw_string_literal 2 | delimiter: (raw_string_delimiter) @injection.language 3 | (raw_string_content) @injection.content) 4 | -------------------------------------------------------------------------------- /queries/tags.scm: -------------------------------------------------------------------------------- 1 | (struct_specifier name: (type_identifier) @name body:(_)) @definition.class 2 | 3 | (declaration type: (union_specifier name: (type_identifier) @name)) @definition.class 4 | 5 | (function_declarator declarator: (identifier) @name) @definition.function 6 | 7 | (function_declarator declarator: (field_identifier) @name) @definition.function 8 | 9 | (function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @local.scope name: (identifier) @name)) @definition.method 10 | 11 | (type_definition declarator: (type_identifier) @name) @definition.type 12 | 13 | (enum_specifier name: (type_identifier) @name) @definition.type 14 | 15 | (class_specifier name: (type_identifier) @name) @definition.class 16 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from os.path import isdir, join 2 | from platform import system 3 | 4 | from setuptools import Extension, find_packages, setup 5 | from setuptools.command.build import build 6 | from wheel.bdist_wheel import bdist_wheel 7 | 8 | 9 | class Build(build): 10 | def run(self): 11 | if isdir("queries"): 12 | dest = join(self.build_lib, "tree_sitter_cpp", "queries") 13 | self.copy_tree("queries", dest) 14 | super().run() 15 | 16 | 17 | class BdistWheel(bdist_wheel): 18 | def get_tag(self): 19 | python, abi, platform = super().get_tag() 20 | if python.startswith("cp"): 21 | python, abi = "cp39", "abi3" 22 | return python, abi, platform 23 | 24 | 25 | setup( 26 | packages=find_packages("bindings/python"), 27 | package_dir={"": "bindings/python"}, 28 | package_data={ 29 | "tree_sitter_cpp": ["*.pyi", "py.typed"], 30 | "tree_sitter_cpp.queries": ["*.scm"], 31 | }, 32 | ext_package="tree_sitter_cpp", 33 | ext_modules=[ 34 | Extension( 35 | name="_binding", 36 | sources=[ 37 | "bindings/python/tree_sitter_cpp/binding.c", 38 | "src/parser.c", 39 | "src/scanner.c", 40 | ], 41 | extra_compile_args=[ 42 | "-std=c11", 43 | "-fvisibility=hidden", 44 | ] if system() != "Windows" else [ 45 | "/std:c11", 46 | "/utf-8", 47 | ], 48 | define_macros=[ 49 | ("Py_LIMITED_API", "0x03090000"), 50 | ("PY_SSIZE_T_CLEAN", None), 51 | ("TREE_SITTER_HIDE_SYMBOLS", None), 52 | ], 53 | include_dirs=["src"], 54 | py_limited_api=True, 55 | ) 56 | ], 57 | cmdclass={ 58 | "build": Build, 59 | "bdist_wheel": BdistWheel 60 | }, 61 | zip_safe=False 62 | ) 63 | -------------------------------------------------------------------------------- /src/scanner.c: -------------------------------------------------------------------------------- 1 | #include "tree_sitter/alloc.h" 2 | #include "tree_sitter/parser.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | enum TokenType { RAW_STRING_DELIMITER, RAW_STRING_CONTENT }; 9 | 10 | /// The spec limits delimiters to 16 chars 11 | #define MAX_DELIMITER_LENGTH 16 12 | 13 | typedef struct { 14 | uint8_t delimiter_length; 15 | wchar_t delimiter[MAX_DELIMITER_LENGTH]; 16 | } Scanner; 17 | 18 | static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); } 19 | 20 | static inline void reset(Scanner *scanner) { 21 | scanner->delimiter_length = 0; 22 | memset(scanner->delimiter, 0, sizeof scanner->delimiter); 23 | } 24 | 25 | /// Scan the raw string delimiter in R"delimiter(content)delimiter" 26 | static bool scan_raw_string_delimiter(Scanner *scanner, TSLexer *lexer) { 27 | if (scanner->delimiter_length > 0) { 28 | // Closing delimiter: must exactly match the opening delimiter. 29 | // We already checked this when scanning content, but this is how we 30 | // know when to stop. We can't stop at ", because R"""hello""" is valid. 31 | for (int i = 0; i < scanner->delimiter_length; ++i) { 32 | if (lexer->lookahead != scanner->delimiter[i]) { 33 | return false; 34 | } 35 | advance(lexer); 36 | } 37 | reset(scanner); 38 | return true; 39 | } 40 | 41 | // Opening delimiter: record the d-char-sequence up to (. 42 | // d-char is any basic character except parens, backslashes, and spaces. 43 | for (;;) { 44 | if (scanner->delimiter_length >= MAX_DELIMITER_LENGTH || lexer->eof(lexer) || lexer->lookahead == '\\' || 45 | iswspace(lexer->lookahead)) { 46 | return false; 47 | } 48 | if (lexer->lookahead == '(') { 49 | // Rather than create a token for an empty delimiter, we fail and 50 | // let the grammar fall back to a delimiter-less rule. 51 | return scanner->delimiter_length > 0; 52 | } 53 | scanner->delimiter[scanner->delimiter_length++] = lexer->lookahead; 54 | advance(lexer); 55 | } 56 | } 57 | 58 | /// Scan the raw string content in R"delimiter(content)delimiter" 59 | static bool scan_raw_string_content(Scanner *scanner, TSLexer *lexer) { 60 | // The progress made through the delimiter since the last ')'. 61 | // The delimiter may not contain ')' so a single counter suffices. 62 | for (int delimiter_index = -1;;) { 63 | // If we hit EOF, consider the content to terminate there. 64 | // This forms an incomplete raw_string_literal, and models the code 65 | // well. 66 | if (lexer->eof(lexer)) { 67 | lexer->mark_end(lexer); 68 | return true; 69 | } 70 | 71 | if (delimiter_index >= 0) { 72 | if (delimiter_index == scanner->delimiter_length) { 73 | if (lexer->lookahead == '"') { 74 | return true; 75 | } 76 | delimiter_index = -1; 77 | } else { 78 | if (lexer->lookahead == scanner->delimiter[delimiter_index]) { 79 | delimiter_index += 1; 80 | } else { 81 | delimiter_index = -1; 82 | } 83 | } 84 | } 85 | 86 | if (delimiter_index == -1 && lexer->lookahead == ')') { 87 | // The content doesn't include the )delimiter" part. 88 | // We must still scan through it, but exclude it from the token. 89 | lexer->mark_end(lexer); 90 | delimiter_index = 0; 91 | } 92 | 93 | advance(lexer); 94 | } 95 | } 96 | 97 | void *tree_sitter_cpp_external_scanner_create() { 98 | Scanner *scanner = (Scanner *)ts_calloc(1, sizeof(Scanner)); 99 | memset(scanner, 0, sizeof(Scanner)); 100 | return scanner; 101 | } 102 | 103 | bool tree_sitter_cpp_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) { 104 | Scanner *scanner = (Scanner *)payload; 105 | 106 | if (valid_symbols[RAW_STRING_DELIMITER] && valid_symbols[RAW_STRING_CONTENT]) { 107 | // we're in error recovery 108 | return false; 109 | } 110 | 111 | // No skipping leading whitespace: raw-string grammar is space-sensitive. 112 | if (valid_symbols[RAW_STRING_DELIMITER]) { 113 | lexer->result_symbol = RAW_STRING_DELIMITER; 114 | return scan_raw_string_delimiter(scanner, lexer); 115 | } 116 | 117 | if (valid_symbols[RAW_STRING_CONTENT]) { 118 | lexer->result_symbol = RAW_STRING_CONTENT; 119 | return scan_raw_string_content(scanner, lexer); 120 | } 121 | 122 | return false; 123 | } 124 | 125 | unsigned tree_sitter_cpp_external_scanner_serialize(void *payload, char *buffer) { 126 | static_assert(MAX_DELIMITER_LENGTH * sizeof(wchar_t) < TREE_SITTER_SERIALIZATION_BUFFER_SIZE, 127 | "Serialized delimiter is too long!"); 128 | 129 | Scanner *scanner = (Scanner *)payload; 130 | size_t size = scanner->delimiter_length * sizeof(wchar_t); 131 | memcpy(buffer, scanner->delimiter, size); 132 | return (unsigned)size; 133 | } 134 | 135 | void tree_sitter_cpp_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) { 136 | assert(length % sizeof(wchar_t) == 0 && "Can't decode serialized delimiter!"); 137 | 138 | Scanner *scanner = (Scanner *)payload; 139 | scanner->delimiter_length = length / sizeof(wchar_t); 140 | if (length > 0) { 141 | memcpy(&scanner->delimiter[0], buffer, length); 142 | } 143 | } 144 | 145 | void tree_sitter_cpp_external_scanner_destroy(void *payload) { 146 | Scanner *scanner = (Scanner *)payload; 147 | ts_free(scanner); 148 | } 149 | -------------------------------------------------------------------------------- /src/tree_sitter/alloc.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_SITTER_ALLOC_H_ 2 | #define TREE_SITTER_ALLOC_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Allow clients to override allocation functions 13 | #ifdef TREE_SITTER_REUSE_ALLOCATOR 14 | 15 | extern void *(*ts_current_malloc)(size_t size); 16 | extern void *(*ts_current_calloc)(size_t count, size_t size); 17 | extern void *(*ts_current_realloc)(void *ptr, size_t size); 18 | extern void (*ts_current_free)(void *ptr); 19 | 20 | #ifndef ts_malloc 21 | #define ts_malloc ts_current_malloc 22 | #endif 23 | #ifndef ts_calloc 24 | #define ts_calloc ts_current_calloc 25 | #endif 26 | #ifndef ts_realloc 27 | #define ts_realloc ts_current_realloc 28 | #endif 29 | #ifndef ts_free 30 | #define ts_free ts_current_free 31 | #endif 32 | 33 | #else 34 | 35 | #ifndef ts_malloc 36 | #define ts_malloc malloc 37 | #endif 38 | #ifndef ts_calloc 39 | #define ts_calloc calloc 40 | #endif 41 | #ifndef ts_realloc 42 | #define ts_realloc realloc 43 | #endif 44 | #ifndef ts_free 45 | #define ts_free free 46 | #endif 47 | 48 | #endif 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif // TREE_SITTER_ALLOC_H_ 55 | -------------------------------------------------------------------------------- /src/tree_sitter/array.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_SITTER_ARRAY_H_ 2 | #define TREE_SITTER_ARRAY_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "./alloc.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #ifdef _MSC_VER 17 | #pragma warning(disable : 4101) 18 | #elif defined(__GNUC__) || defined(__clang__) 19 | #pragma GCC diagnostic push 20 | #pragma GCC diagnostic ignored "-Wunused-variable" 21 | #endif 22 | 23 | #define Array(T) \ 24 | struct { \ 25 | T *contents; \ 26 | uint32_t size; \ 27 | uint32_t capacity; \ 28 | } 29 | 30 | /// Initialize an array. 31 | #define array_init(self) \ 32 | ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) 33 | 34 | /// Create an empty array. 35 | #define array_new() \ 36 | { NULL, 0, 0 } 37 | 38 | /// Get a pointer to the element at a given `index` in the array. 39 | #define array_get(self, _index) \ 40 | (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) 41 | 42 | /// Get a pointer to the first element in the array. 43 | #define array_front(self) array_get(self, 0) 44 | 45 | /// Get a pointer to the last element in the array. 46 | #define array_back(self) array_get(self, (self)->size - 1) 47 | 48 | /// Clear the array, setting its size to zero. Note that this does not free any 49 | /// memory allocated for the array's contents. 50 | #define array_clear(self) ((self)->size = 0) 51 | 52 | /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is 53 | /// less than the array's current capacity, this function has no effect. 54 | #define array_reserve(self, new_capacity) \ 55 | _array__reserve((Array *)(self), array_elem_size(self), new_capacity) 56 | 57 | /// Free any memory allocated for this array. Note that this does not free any 58 | /// memory allocated for the array's contents. 59 | #define array_delete(self) _array__delete((Array *)(self)) 60 | 61 | /// Push a new `element` onto the end of the array. 62 | #define array_push(self, element) \ 63 | (_array__grow((Array *)(self), 1, array_elem_size(self)), \ 64 | (self)->contents[(self)->size++] = (element)) 65 | 66 | /// Increase the array's size by `count` elements. 67 | /// New elements are zero-initialized. 68 | #define array_grow_by(self, count) \ 69 | do { \ 70 | if ((count) == 0) break; \ 71 | _array__grow((Array *)(self), count, array_elem_size(self)); \ 72 | memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ 73 | (self)->size += (count); \ 74 | } while (0) 75 | 76 | /// Append all elements from one array to the end of another. 77 | #define array_push_all(self, other) \ 78 | array_extend((self), (other)->size, (other)->contents) 79 | 80 | /// Append `count` elements to the end of the array, reading their values from the 81 | /// `contents` pointer. 82 | #define array_extend(self, count, contents) \ 83 | _array__splice( \ 84 | (Array *)(self), array_elem_size(self), (self)->size, \ 85 | 0, count, contents \ 86 | ) 87 | 88 | /// Remove `old_count` elements from the array starting at the given `index`. At 89 | /// the same index, insert `new_count` new elements, reading their values from the 90 | /// `new_contents` pointer. 91 | #define array_splice(self, _index, old_count, new_count, new_contents) \ 92 | _array__splice( \ 93 | (Array *)(self), array_elem_size(self), _index, \ 94 | old_count, new_count, new_contents \ 95 | ) 96 | 97 | /// Insert one `element` into the array at the given `index`. 98 | #define array_insert(self, _index, element) \ 99 | _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element)) 100 | 101 | /// Remove one element from the array at the given `index`. 102 | #define array_erase(self, _index) \ 103 | _array__erase((Array *)(self), array_elem_size(self), _index) 104 | 105 | /// Pop the last element off the array, returning the element by value. 106 | #define array_pop(self) ((self)->contents[--(self)->size]) 107 | 108 | /// Assign the contents of one array to another, reallocating if necessary. 109 | #define array_assign(self, other) \ 110 | _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self)) 111 | 112 | /// Swap one array with another 113 | #define array_swap(self, other) \ 114 | _array__swap((Array *)(self), (Array *)(other)) 115 | 116 | /// Get the size of the array contents 117 | #define array_elem_size(self) (sizeof *(self)->contents) 118 | 119 | /// Search a sorted array for a given `needle` value, using the given `compare` 120 | /// callback to determine the order. 121 | /// 122 | /// If an existing element is found to be equal to `needle`, then the `index` 123 | /// out-parameter is set to the existing value's index, and the `exists` 124 | /// out-parameter is set to true. Otherwise, `index` is set to an index where 125 | /// `needle` should be inserted in order to preserve the sorting, and `exists` 126 | /// is set to false. 127 | #define array_search_sorted_with(self, compare, needle, _index, _exists) \ 128 | _array__search_sorted(self, 0, compare, , needle, _index, _exists) 129 | 130 | /// Search a sorted array for a given `needle` value, using integer comparisons 131 | /// of a given struct field (specified with a leading dot) to determine the order. 132 | /// 133 | /// See also `array_search_sorted_with`. 134 | #define array_search_sorted_by(self, field, needle, _index, _exists) \ 135 | _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) 136 | 137 | /// Insert a given `value` into a sorted array, using the given `compare` 138 | /// callback to determine the order. 139 | #define array_insert_sorted_with(self, compare, value) \ 140 | do { \ 141 | unsigned _index, _exists; \ 142 | array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ 143 | if (!_exists) array_insert(self, _index, value); \ 144 | } while (0) 145 | 146 | /// Insert a given `value` into a sorted array, using integer comparisons of 147 | /// a given struct field (specified with a leading dot) to determine the order. 148 | /// 149 | /// See also `array_search_sorted_by`. 150 | #define array_insert_sorted_by(self, field, value) \ 151 | do { \ 152 | unsigned _index, _exists; \ 153 | array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ 154 | if (!_exists) array_insert(self, _index, value); \ 155 | } while (0) 156 | 157 | // Private 158 | 159 | typedef Array(void) Array; 160 | 161 | /// This is not what you're looking for, see `array_delete`. 162 | static inline void _array__delete(Array *self) { 163 | if (self->contents) { 164 | ts_free(self->contents); 165 | self->contents = NULL; 166 | self->size = 0; 167 | self->capacity = 0; 168 | } 169 | } 170 | 171 | /// This is not what you're looking for, see `array_erase`. 172 | static inline void _array__erase(Array *self, size_t element_size, 173 | uint32_t index) { 174 | assert(index < self->size); 175 | char *contents = (char *)self->contents; 176 | memmove(contents + index * element_size, contents + (index + 1) * element_size, 177 | (self->size - index - 1) * element_size); 178 | self->size--; 179 | } 180 | 181 | /// This is not what you're looking for, see `array_reserve`. 182 | static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) { 183 | if (new_capacity > self->capacity) { 184 | if (self->contents) { 185 | self->contents = ts_realloc(self->contents, new_capacity * element_size); 186 | } else { 187 | self->contents = ts_malloc(new_capacity * element_size); 188 | } 189 | self->capacity = new_capacity; 190 | } 191 | } 192 | 193 | /// This is not what you're looking for, see `array_assign`. 194 | static inline void _array__assign(Array *self, const Array *other, size_t element_size) { 195 | _array__reserve(self, element_size, other->size); 196 | self->size = other->size; 197 | memcpy(self->contents, other->contents, self->size * element_size); 198 | } 199 | 200 | /// This is not what you're looking for, see `array_swap`. 201 | static inline void _array__swap(Array *self, Array *other) { 202 | Array swap = *other; 203 | *other = *self; 204 | *self = swap; 205 | } 206 | 207 | /// This is not what you're looking for, see `array_push` or `array_grow_by`. 208 | static inline void _array__grow(Array *self, uint32_t count, size_t element_size) { 209 | uint32_t new_size = self->size + count; 210 | if (new_size > self->capacity) { 211 | uint32_t new_capacity = self->capacity * 2; 212 | if (new_capacity < 8) new_capacity = 8; 213 | if (new_capacity < new_size) new_capacity = new_size; 214 | _array__reserve(self, element_size, new_capacity); 215 | } 216 | } 217 | 218 | /// This is not what you're looking for, see `array_splice`. 219 | static inline void _array__splice(Array *self, size_t element_size, 220 | uint32_t index, uint32_t old_count, 221 | uint32_t new_count, const void *elements) { 222 | uint32_t new_size = self->size + new_count - old_count; 223 | uint32_t old_end = index + old_count; 224 | uint32_t new_end = index + new_count; 225 | assert(old_end <= self->size); 226 | 227 | _array__reserve(self, element_size, new_size); 228 | 229 | char *contents = (char *)self->contents; 230 | if (self->size > old_end) { 231 | memmove( 232 | contents + new_end * element_size, 233 | contents + old_end * element_size, 234 | (self->size - old_end) * element_size 235 | ); 236 | } 237 | if (new_count > 0) { 238 | if (elements) { 239 | memcpy( 240 | (contents + index * element_size), 241 | elements, 242 | new_count * element_size 243 | ); 244 | } else { 245 | memset( 246 | (contents + index * element_size), 247 | 0, 248 | new_count * element_size 249 | ); 250 | } 251 | } 252 | self->size += new_count - old_count; 253 | } 254 | 255 | /// A binary search routine, based on Rust's `std::slice::binary_search_by`. 256 | /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. 257 | #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ 258 | do { \ 259 | *(_index) = start; \ 260 | *(_exists) = false; \ 261 | uint32_t size = (self)->size - *(_index); \ 262 | if (size == 0) break; \ 263 | int comparison; \ 264 | while (size > 1) { \ 265 | uint32_t half_size = size / 2; \ 266 | uint32_t mid_index = *(_index) + half_size; \ 267 | comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ 268 | if (comparison <= 0) *(_index) = mid_index; \ 269 | size -= half_size; \ 270 | } \ 271 | comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ 272 | if (comparison == 0) *(_exists) = true; \ 273 | else if (comparison < 0) *(_index) += 1; \ 274 | } while (0) 275 | 276 | /// Helper macro for the `_sorted_by` routines below. This takes the left (existing) 277 | /// parameter by reference in order to work with the generic sorting function above. 278 | #define _compare_int(a, b) ((int)*(a) - (int)(b)) 279 | 280 | #ifdef _MSC_VER 281 | #pragma warning(default : 4101) 282 | #elif defined(__GNUC__) || defined(__clang__) 283 | #pragma GCC diagnostic pop 284 | #endif 285 | 286 | #ifdef __cplusplus 287 | } 288 | #endif 289 | 290 | #endif // TREE_SITTER_ARRAY_H_ 291 | -------------------------------------------------------------------------------- /src/tree_sitter/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_SITTER_PARSER_H_ 2 | #define TREE_SITTER_PARSER_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define ts_builtin_sym_error ((TSSymbol)-1) 13 | #define ts_builtin_sym_end 0 14 | #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 15 | 16 | #ifndef TREE_SITTER_API_H_ 17 | typedef uint16_t TSStateId; 18 | typedef uint16_t TSSymbol; 19 | typedef uint16_t TSFieldId; 20 | typedef struct TSLanguage TSLanguage; 21 | #endif 22 | 23 | typedef struct { 24 | TSFieldId field_id; 25 | uint8_t child_index; 26 | bool inherited; 27 | } TSFieldMapEntry; 28 | 29 | typedef struct { 30 | uint16_t index; 31 | uint16_t length; 32 | } TSFieldMapSlice; 33 | 34 | typedef struct { 35 | bool visible; 36 | bool named; 37 | bool supertype; 38 | } TSSymbolMetadata; 39 | 40 | typedef struct TSLexer TSLexer; 41 | 42 | struct TSLexer { 43 | int32_t lookahead; 44 | TSSymbol result_symbol; 45 | void (*advance)(TSLexer *, bool); 46 | void (*mark_end)(TSLexer *); 47 | uint32_t (*get_column)(TSLexer *); 48 | bool (*is_at_included_range_start)(const TSLexer *); 49 | bool (*eof)(const TSLexer *); 50 | void (*log)(const TSLexer *, const char *, ...); 51 | }; 52 | 53 | typedef enum { 54 | TSParseActionTypeShift, 55 | TSParseActionTypeReduce, 56 | TSParseActionTypeAccept, 57 | TSParseActionTypeRecover, 58 | } TSParseActionType; 59 | 60 | typedef union { 61 | struct { 62 | uint8_t type; 63 | TSStateId state; 64 | bool extra; 65 | bool repetition; 66 | } shift; 67 | struct { 68 | uint8_t type; 69 | uint8_t child_count; 70 | TSSymbol symbol; 71 | int16_t dynamic_precedence; 72 | uint16_t production_id; 73 | } reduce; 74 | uint8_t type; 75 | } TSParseAction; 76 | 77 | typedef struct { 78 | uint16_t lex_state; 79 | uint16_t external_lex_state; 80 | } TSLexMode; 81 | 82 | typedef union { 83 | TSParseAction action; 84 | struct { 85 | uint8_t count; 86 | bool reusable; 87 | } entry; 88 | } TSParseActionEntry; 89 | 90 | typedef struct { 91 | int32_t start; 92 | int32_t end; 93 | } TSCharacterRange; 94 | 95 | struct TSLanguage { 96 | uint32_t version; 97 | uint32_t symbol_count; 98 | uint32_t alias_count; 99 | uint32_t token_count; 100 | uint32_t external_token_count; 101 | uint32_t state_count; 102 | uint32_t large_state_count; 103 | uint32_t production_id_count; 104 | uint32_t field_count; 105 | uint16_t max_alias_sequence_length; 106 | const uint16_t *parse_table; 107 | const uint16_t *small_parse_table; 108 | const uint32_t *small_parse_table_map; 109 | const TSParseActionEntry *parse_actions; 110 | const char * const *symbol_names; 111 | const char * const *field_names; 112 | const TSFieldMapSlice *field_map_slices; 113 | const TSFieldMapEntry *field_map_entries; 114 | const TSSymbolMetadata *symbol_metadata; 115 | const TSSymbol *public_symbol_map; 116 | const uint16_t *alias_map; 117 | const TSSymbol *alias_sequences; 118 | const TSLexMode *lex_modes; 119 | bool (*lex_fn)(TSLexer *, TSStateId); 120 | bool (*keyword_lex_fn)(TSLexer *, TSStateId); 121 | TSSymbol keyword_capture_token; 122 | struct { 123 | const bool *states; 124 | const TSSymbol *symbol_map; 125 | void *(*create)(void); 126 | void (*destroy)(void *); 127 | bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); 128 | unsigned (*serialize)(void *, char *); 129 | void (*deserialize)(void *, const char *, unsigned); 130 | } external_scanner; 131 | const TSStateId *primary_state_ids; 132 | }; 133 | 134 | static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) { 135 | uint32_t index = 0; 136 | uint32_t size = len - index; 137 | while (size > 1) { 138 | uint32_t half_size = size / 2; 139 | uint32_t mid_index = index + half_size; 140 | TSCharacterRange *range = &ranges[mid_index]; 141 | if (lookahead >= range->start && lookahead <= range->end) { 142 | return true; 143 | } else if (lookahead > range->end) { 144 | index = mid_index; 145 | } 146 | size -= half_size; 147 | } 148 | TSCharacterRange *range = &ranges[index]; 149 | return (lookahead >= range->start && lookahead <= range->end); 150 | } 151 | 152 | /* 153 | * Lexer Macros 154 | */ 155 | 156 | #ifdef _MSC_VER 157 | #define UNUSED __pragma(warning(suppress : 4101)) 158 | #else 159 | #define UNUSED __attribute__((unused)) 160 | #endif 161 | 162 | #define START_LEXER() \ 163 | bool result = false; \ 164 | bool skip = false; \ 165 | UNUSED \ 166 | bool eof = false; \ 167 | int32_t lookahead; \ 168 | goto start; \ 169 | next_state: \ 170 | lexer->advance(lexer, skip); \ 171 | start: \ 172 | skip = false; \ 173 | lookahead = lexer->lookahead; 174 | 175 | #define ADVANCE(state_value) \ 176 | { \ 177 | state = state_value; \ 178 | goto next_state; \ 179 | } 180 | 181 | #define ADVANCE_MAP(...) \ 182 | { \ 183 | static const uint16_t map[] = { __VA_ARGS__ }; \ 184 | for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \ 185 | if (map[i] == lookahead) { \ 186 | state = map[i + 1]; \ 187 | goto next_state; \ 188 | } \ 189 | } \ 190 | } 191 | 192 | #define SKIP(state_value) \ 193 | { \ 194 | skip = true; \ 195 | state = state_value; \ 196 | goto next_state; \ 197 | } 198 | 199 | #define ACCEPT_TOKEN(symbol_value) \ 200 | result = true; \ 201 | lexer->result_symbol = symbol_value; \ 202 | lexer->mark_end(lexer); 203 | 204 | #define END_STATE() return result; 205 | 206 | /* 207 | * Parse Table Macros 208 | */ 209 | 210 | #define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) 211 | 212 | #define STATE(id) id 213 | 214 | #define ACTIONS(id) id 215 | 216 | #define SHIFT(state_value) \ 217 | {{ \ 218 | .shift = { \ 219 | .type = TSParseActionTypeShift, \ 220 | .state = (state_value) \ 221 | } \ 222 | }} 223 | 224 | #define SHIFT_REPEAT(state_value) \ 225 | {{ \ 226 | .shift = { \ 227 | .type = TSParseActionTypeShift, \ 228 | .state = (state_value), \ 229 | .repetition = true \ 230 | } \ 231 | }} 232 | 233 | #define SHIFT_EXTRA() \ 234 | {{ \ 235 | .shift = { \ 236 | .type = TSParseActionTypeShift, \ 237 | .extra = true \ 238 | } \ 239 | }} 240 | 241 | #define REDUCE(symbol_name, children, precedence, prod_id) \ 242 | {{ \ 243 | .reduce = { \ 244 | .type = TSParseActionTypeReduce, \ 245 | .symbol = symbol_name, \ 246 | .child_count = children, \ 247 | .dynamic_precedence = precedence, \ 248 | .production_id = prod_id \ 249 | }, \ 250 | }} 251 | 252 | #define RECOVER() \ 253 | {{ \ 254 | .type = TSParseActionTypeRecover \ 255 | }} 256 | 257 | #define ACCEPT_INPUT() \ 258 | {{ \ 259 | .type = TSParseActionTypeAccept \ 260 | }} 261 | 262 | #ifdef __cplusplus 263 | } 264 | #endif 265 | 266 | #endif // TREE_SITTER_PARSER_H_ 267 | -------------------------------------------------------------------------------- /test/corpus/ambiguities.txt: -------------------------------------------------------------------------------- 1 | ================================================ 2 | template functions vs relational expressions 3 | ================================================ 4 | 5 | T1 a = b < c > d; 6 | T2 e = f(g); 7 | int a = std::get<0>(t); 8 | if (x.y < z) break; // Not a template missing a '>' 9 | if (x.y < x->y && x.y > 50) break; 10 | if (x.foo < 0 || bar >= 1) break; 11 | if (x.Base::foo < 0 || bar >= 1) { 12 | bool i = x.Base::foo < 0 || 1 > (5); // No way to tell 13 | } 14 | 15 | --- 16 | 17 | (translation_unit 18 | (declaration 19 | (type_identifier) 20 | (init_declarator 21 | (identifier) 22 | (binary_expression 23 | (binary_expression (identifier) (identifier)) 24 | (identifier)))) 25 | (declaration 26 | (type_identifier) 27 | (init_declarator 28 | (identifier) 29 | (call_expression 30 | (template_function (identifier) (template_argument_list 31 | (type_descriptor (type_identifier)))) 32 | (argument_list (identifier))))) 33 | (declaration 34 | (primitive_type) 35 | (init_declarator 36 | (identifier) 37 | (call_expression 38 | (qualified_identifier 39 | (namespace_identifier) 40 | (template_function 41 | (identifier) 42 | (template_argument_list (number_literal)))) 43 | (argument_list (identifier))))) 44 | (if_statement 45 | (condition_clause 46 | (binary_expression 47 | (field_expression 48 | (identifier) 49 | (field_identifier)) 50 | (identifier))) 51 | (break_statement)) 52 | (comment) 53 | (if_statement 54 | (condition_clause 55 | (binary_expression 56 | (binary_expression 57 | (field_expression 58 | (identifier) 59 | (field_identifier)) 60 | (field_expression 61 | (identifier) 62 | (field_identifier))) 63 | (binary_expression 64 | (field_expression 65 | (identifier) 66 | (field_identifier)) 67 | (number_literal)))) 68 | (break_statement)) 69 | (if_statement 70 | (condition_clause 71 | (binary_expression 72 | (binary_expression 73 | (field_expression 74 | (identifier) 75 | (field_identifier)) 76 | (number_literal)) 77 | (binary_expression 78 | (identifier) 79 | (number_literal)))) 80 | (break_statement)) 81 | (if_statement 82 | (condition_clause 83 | (binary_expression 84 | (binary_expression 85 | (field_expression 86 | (identifier) 87 | (qualified_identifier 88 | (namespace_identifier) 89 | (field_identifier))) 90 | (number_literal)) 91 | (binary_expression 92 | (identifier) 93 | (number_literal)))) 94 | (compound_statement 95 | (declaration 96 | (primitive_type) 97 | (init_declarator 98 | (identifier) 99 | (binary_expression 100 | (binary_expression 101 | (field_expression 102 | (identifier) 103 | (qualified_identifier 104 | (namespace_identifier) 105 | (field_identifier))) 106 | (number_literal)) 107 | (binary_expression 108 | (number_literal) 109 | (parenthesized_expression 110 | (number_literal)))))) 111 | (comment)))) 112 | 113 | ================================================= 114 | function declarations vs variable initializations 115 | ================================================= 116 | 117 | // Function declarations 118 | T1 a(T2 *b); 119 | T3 c(T4 &d, T5 &&e); 120 | 121 | // Variable declarations with initializers 122 | T7 f(g.h); 123 | T6 i{j}; 124 | 125 | --- 126 | 127 | (translation_unit 128 | (comment) 129 | (declaration 130 | (type_identifier) 131 | (function_declarator 132 | (identifier) 133 | (parameter_list (parameter_declaration (type_identifier) (pointer_declarator (identifier)))))) 134 | (declaration 135 | (type_identifier) 136 | (function_declarator 137 | (identifier) 138 | (parameter_list 139 | (parameter_declaration (type_identifier) (reference_declarator (identifier))) 140 | (parameter_declaration (type_identifier) (reference_declarator (identifier)))))) 141 | 142 | (comment) 143 | (declaration 144 | (type_identifier) 145 | (init_declarator 146 | (identifier) 147 | (argument_list (field_expression (identifier) (field_identifier))))) 148 | (declaration 149 | (type_identifier) 150 | (init_declarator 151 | (identifier) 152 | (initializer_list (identifier))))) 153 | 154 | ================================================ 155 | template classes vs relational expressions 156 | ================================================ 157 | 158 | int main() { 159 | T1 v1; 160 | T1 v2 = v3; 161 | } 162 | 163 | --- 164 | 165 | (translation_unit (function_definition 166 | (primitive_type) 167 | (function_declarator (identifier) (parameter_list)) 168 | (compound_statement 169 | (declaration 170 | (template_type (type_identifier) 171 | (template_argument_list (type_descriptor (type_identifier)))) 172 | (identifier)) 173 | (declaration 174 | (template_type (type_identifier) 175 | (template_argument_list (type_descriptor (type_identifier)))) 176 | (init_declarator (identifier) (identifier)))))) 177 | -------------------------------------------------------------------------------- /test/corpus/c/ambiguities.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | pointer declarations vs expressions 3 | ================================================================================ 4 | 5 | TSLanguage *(*lang_parser)(void); 6 | 7 | char (*ptr_to_array)[]; 8 | 9 | int main() { 10 | // declare a function pointer 11 | T1 * b(T2 a); 12 | 13 | // evaluate expressions 14 | c * d(5); 15 | e(f * g); 16 | } 17 | 18 | -------------------------------------------------------------------------------- 19 | 20 | (translation_unit 21 | (declaration 22 | (type_identifier) 23 | (pointer_declarator 24 | (function_declarator 25 | (parenthesized_declarator 26 | (pointer_declarator 27 | (identifier))) 28 | (parameter_list 29 | (parameter_declaration 30 | (primitive_type)))))) 31 | (expression_statement 32 | (subscript_expression 33 | (call_expression 34 | (primitive_type) 35 | (argument_list 36 | (pointer_expression 37 | (identifier)))) 38 | (subscript_argument_list))) 39 | (function_definition 40 | (primitive_type) 41 | (function_declarator 42 | (identifier) 43 | (parameter_list)) 44 | (compound_statement 45 | (comment) 46 | (declaration 47 | (type_identifier) 48 | (pointer_declarator 49 | (function_declarator 50 | (identifier) 51 | (parameter_list 52 | (parameter_declaration 53 | (type_identifier) 54 | (identifier)))))) 55 | (comment) 56 | (expression_statement 57 | (binary_expression 58 | (identifier) 59 | (call_expression 60 | (identifier) 61 | (argument_list 62 | (number_literal))))) 63 | (expression_statement 64 | (call_expression 65 | (identifier) 66 | (argument_list 67 | (binary_expression 68 | (identifier) 69 | (identifier)))))))) 70 | 71 | ================================================================================ 72 | casts vs multiplications 73 | ================================================================================ 74 | 75 | /* 76 | * ambiguities 77 | */ 78 | 79 | int main() { 80 | // cast 81 | a((B *)c); 82 | 83 | // parenthesized product 84 | d((e * f)); 85 | } 86 | 87 | -------------------------------------------------------------------------------- 88 | 89 | (translation_unit 90 | (comment) 91 | (function_definition 92 | (primitive_type) 93 | (function_declarator 94 | (identifier) 95 | (parameter_list)) 96 | (compound_statement 97 | (comment) 98 | (expression_statement 99 | (call_expression 100 | (identifier) 101 | (argument_list 102 | (cast_expression 103 | (type_descriptor 104 | (type_identifier) 105 | (abstract_pointer_declarator)) 106 | (identifier))))) 107 | (comment) 108 | (expression_statement 109 | (call_expression 110 | (identifier) 111 | (argument_list 112 | (parenthesized_expression 113 | (binary_expression 114 | (identifier) 115 | (identifier))))))))) 116 | 117 | ================================================================================ 118 | function calls vs parenthesized declarators vs macro types 119 | ================================================================================ 120 | 121 | int main() { 122 | /* 123 | * Could be either: 124 | * - function call 125 | * - declaration w/ parenthesized declarator 126 | * - declaration w/ macro type, no declarator 127 | */ 128 | ABC(d); 129 | 130 | /* 131 | * Normal declaration 132 | */ 133 | efg hij; 134 | } 135 | 136 | -------------------------------------------------------------------------------- 137 | 138 | (translation_unit 139 | (function_definition 140 | (primitive_type) 141 | (function_declarator 142 | (identifier) 143 | (parameter_list)) 144 | (compound_statement 145 | (comment) 146 | (expression_statement 147 | (call_expression 148 | (identifier) 149 | (argument_list 150 | (identifier)))) 151 | (comment) 152 | (declaration 153 | (type_identifier) 154 | (identifier))))) 155 | 156 | ================================================================================ 157 | Call expressions vs empty declarations w/ macros as types 158 | ================================================================================ 159 | 160 | int main() { 161 | int a = 1; 162 | b(a); 163 | } 164 | 165 | -------------------------------------------------------------------------------- 166 | 167 | (translation_unit 168 | (function_definition 169 | (primitive_type) 170 | (function_declarator 171 | (identifier) 172 | (parameter_list)) 173 | (compound_statement 174 | (declaration 175 | (primitive_type) 176 | (init_declarator 177 | (identifier) 178 | (number_literal))) 179 | (expression_statement 180 | (call_expression 181 | (identifier) 182 | (argument_list 183 | (identifier))))))) 184 | 185 | ================================================================================ 186 | Comments after for loops with ambiguities 187 | ================================================================================ 188 | 189 | int main() { 190 | for (a *b = c; d; e) { 191 | aff; 192 | } 193 | 194 | // a-comment 195 | 196 | g; 197 | } 198 | 199 | -------------------------------------------------------------------------------- 200 | 201 | (translation_unit 202 | (function_definition 203 | (primitive_type) 204 | (function_declarator 205 | (identifier) 206 | (parameter_list)) 207 | (compound_statement 208 | (for_statement 209 | (declaration 210 | (type_identifier) 211 | (init_declarator 212 | (pointer_declarator 213 | (identifier)) 214 | (identifier))) 215 | (identifier) 216 | (identifier) 217 | (compound_statement 218 | (expression_statement 219 | (identifier)))) 220 | (comment) 221 | (expression_statement 222 | (identifier))))) 223 | 224 | ================================================================================ 225 | Top-level macro invocations 226 | ================================================================================ 227 | 228 | DEFINE_SOMETHING(THING_A, "this is a thing a"); 229 | DEFINE_SOMETHING(THING_B, "this is a thing b", "thanks"); 230 | 231 | -------------------------------------------------------------------------------- 232 | 233 | (translation_unit 234 | (expression_statement 235 | (call_expression 236 | (identifier) 237 | (argument_list 238 | (identifier) 239 | (string_literal 240 | (string_content))))) 241 | (expression_statement 242 | (call_expression 243 | (identifier) 244 | (argument_list 245 | (identifier) 246 | (string_literal 247 | (string_content)) 248 | (string_literal 249 | (string_content)))))) 250 | -------------------------------------------------------------------------------- /test/corpus/c/crlf.txt: -------------------------------------------------------------------------------- 1 | ============================================ 2 | Line comments with escaped CRLF line endings 3 | ============================================ 4 | 5 | // hello \ 6 | this is still a comment 7 | this_is_not a_comment; 8 | 9 | --- 10 | 11 | (translation_unit 12 | (comment) 13 | (declaration (type_identifier) (identifier))) 14 | -------------------------------------------------------------------------------- /test/corpus/c/microsoft.txt: -------------------------------------------------------------------------------- 1 | ================================ 2 | declaration specs 3 | ================================ 4 | 5 | struct __declspec(dllexport) s2 6 | { 7 | }; 8 | 9 | union __declspec(noinline) u2 { 10 | }; 11 | 12 | --- 13 | 14 | (translation_unit 15 | (struct_specifier 16 | (ms_declspec_modifier 17 | (identifier)) 18 | name: (type_identifier) 19 | body: (field_declaration_list)) 20 | (union_specifier 21 | (ms_declspec_modifier 22 | (identifier)) 23 | name: (type_identifier) 24 | body: (field_declaration_list))) 25 | 26 | ================================ 27 | pointers 28 | ================================ 29 | 30 | struct s2 31 | { 32 | int * __restrict x; 33 | int * __sptr psp; 34 | int * __uptr pup; 35 | int * __unaligned pup; 36 | }; 37 | 38 | void sum2(int n, int * __restrict a, int * __restrict b, 39 | int * c, int * d) { 40 | int i; 41 | for (i = 0; i < n; i++) { 42 | a[i] = b[i] + c[i]; 43 | c[i] = b[i] + d[i]; 44 | } 45 | } 46 | 47 | void MyFunction(char * __uptr myValue); 48 | 49 | --- 50 | 51 | (translation_unit 52 | (struct_specifier 53 | name: (type_identifier) 54 | body: (field_declaration_list 55 | (field_declaration 56 | type: (primitive_type) 57 | declarator: (pointer_declarator 58 | (ms_pointer_modifier 59 | (ms_restrict_modifier)) 60 | declarator: (field_identifier))) 61 | (field_declaration 62 | type: (primitive_type) 63 | declarator: (pointer_declarator 64 | (ms_pointer_modifier 65 | (ms_signed_ptr_modifier)) 66 | declarator: (field_identifier))) 67 | (field_declaration 68 | type: (primitive_type) 69 | declarator: (pointer_declarator 70 | (ms_pointer_modifier 71 | (ms_unsigned_ptr_modifier)) 72 | declarator: (field_identifier))) 73 | (field_declaration 74 | type: (primitive_type) 75 | declarator: (pointer_declarator 76 | (ms_pointer_modifier 77 | (ms_unaligned_ptr_modifier)) 78 | declarator: (field_identifier))))) 79 | (function_definition 80 | type: (primitive_type) 81 | declarator: (function_declarator 82 | declarator: (identifier) 83 | parameters: (parameter_list 84 | (parameter_declaration 85 | type: (primitive_type) 86 | declarator: (identifier)) 87 | (parameter_declaration 88 | type: (primitive_type) 89 | declarator: (pointer_declarator 90 | (ms_pointer_modifier 91 | (ms_restrict_modifier)) 92 | declarator: (identifier))) 93 | (parameter_declaration 94 | type: (primitive_type) 95 | declarator: (pointer_declarator 96 | (ms_pointer_modifier 97 | (ms_restrict_modifier)) 98 | declarator: (identifier))) 99 | (parameter_declaration 100 | type: (primitive_type) 101 | declarator: (pointer_declarator 102 | declarator: (identifier))) 103 | (parameter_declaration 104 | type: (primitive_type) 105 | declarator: (pointer_declarator 106 | declarator: (identifier))))) 107 | body: (compound_statement 108 | (declaration 109 | type: (primitive_type) 110 | declarator: (identifier)) 111 | (for_statement 112 | initializer: (assignment_expression 113 | left: (identifier) 114 | right: (number_literal)) 115 | condition: (binary_expression 116 | left: (identifier) 117 | right: (identifier)) 118 | update: (update_expression 119 | argument: (identifier)) 120 | body: (compound_statement 121 | (expression_statement 122 | (assignment_expression 123 | left: (subscript_expression 124 | argument: (identifier) 125 | indices: (subscript_argument_list 126 | (identifier))) 127 | right: (binary_expression 128 | left: (subscript_expression 129 | argument: (identifier) 130 | indices: (subscript_argument_list 131 | (identifier))) 132 | right: (subscript_expression 133 | argument: (identifier) 134 | indices: (subscript_argument_list 135 | (identifier)))))) 136 | (expression_statement 137 | (assignment_expression 138 | left: (subscript_expression 139 | argument: (identifier) 140 | indices: (subscript_argument_list 141 | (identifier))) 142 | right: (binary_expression 143 | left: (subscript_expression 144 | argument: (identifier) 145 | indices: (subscript_argument_list 146 | (identifier))) 147 | right: (subscript_expression 148 | argument: (identifier) 149 | indices: (subscript_argument_list 150 | (identifier)))))))))) 151 | (declaration 152 | type: (primitive_type) 153 | declarator: (function_declarator 154 | declarator: (identifier) 155 | parameters: (parameter_list 156 | (parameter_declaration 157 | type: (primitive_type) 158 | declarator: (pointer_declarator 159 | (ms_pointer_modifier 160 | (ms_unsigned_ptr_modifier)) 161 | declarator: (identifier))))))) 162 | 163 | ================================ 164 | call modifiers 165 | ================================ 166 | 167 | __cdecl void mymethod(){ 168 | return; 169 | } 170 | 171 | __fastcall void mymethod(){ 172 | return; 173 | } 174 | 175 | --- 176 | 177 | (translation_unit 178 | (function_definition 179 | (ms_call_modifier) 180 | type: (primitive_type) 181 | declarator: (function_declarator 182 | declarator: (identifier) 183 | parameters: (parameter_list)) 184 | body: (compound_statement 185 | (return_statement))) 186 | (function_definition 187 | (ms_call_modifier) 188 | type: (primitive_type) 189 | declarator: (function_declarator 190 | declarator: (identifier) 191 | parameters: (parameter_list)) 192 | body: (compound_statement 193 | (return_statement)))) 194 | -------------------------------------------------------------------------------- /test/corpus/c/preprocessor.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | Include directives 3 | ================================================================================ 4 | 5 | #include "some/path.h" 6 | #include 7 | #include MACRO 8 | #include MACRO(arg1, arg2) 9 | 10 | -------------------------------------------------------------------------------- 11 | 12 | (translation_unit 13 | (preproc_include 14 | path: (string_literal 15 | (string_content))) 16 | (preproc_include 17 | path: (system_lib_string)) 18 | (preproc_include 19 | path: (identifier)) 20 | (preproc_include 21 | path: (call_expression 22 | function: (identifier) 23 | arguments: (argument_list 24 | (identifier) 25 | (identifier))))) 26 | 27 | ================================================================================ 28 | Object-like macro definitions 29 | ================================================================================ 30 | 31 | #define ONE 32 | #define TWO int a = b; 33 | #define THREE \ 34 | c == d ? \ 35 | e : \ 36 | f 37 | #define FOUR (mno * pq) 38 | #define FIVE(a,b) x \ 39 | + y 40 | #define SIX(a, \ 41 | b) x \ 42 | + y 43 | #define SEVEN 7/* seven has an 44 | * annoying comment */ 45 | #define EIGHT(x) do { \ 46 | x = x + 1; \ 47 | x = x / 2; \ 48 | } while (x > 0); 49 | 50 | -------------------------------------------------------------------------------- 51 | 52 | (translation_unit 53 | (preproc_def 54 | name: (identifier)) 55 | (preproc_def 56 | name: (identifier) 57 | value: (preproc_arg)) 58 | (preproc_def 59 | name: (identifier) 60 | value: (preproc_arg)) 61 | (preproc_def 62 | name: (identifier) 63 | value: (preproc_arg)) 64 | (preproc_function_def 65 | name: (identifier) 66 | parameters: (preproc_params 67 | (identifier) 68 | (identifier)) 69 | value: (preproc_arg)) 70 | (preproc_function_def 71 | name: (identifier) 72 | parameters: (preproc_params 73 | (identifier) 74 | (identifier)) 75 | value: (preproc_arg)) 76 | (preproc_def 77 | name: (identifier) 78 | value: (preproc_arg) 79 | (comment)) 80 | (preproc_function_def 81 | name: (identifier) 82 | parameters: (preproc_params 83 | (identifier)) 84 | value: (preproc_arg))) 85 | 86 | ================================================================================ 87 | Function-like macro definitions 88 | ================================================================================ 89 | 90 | #define ONE() a 91 | #define TWO(b) c 92 | #define THREE(d, e) f 93 | #define FOUR(...) g 94 | #define FIVE(h, i, ...) j 95 | 96 | -------------------------------------------------------------------------------- 97 | 98 | (translation_unit 99 | (preproc_function_def 100 | name: (identifier) 101 | parameters: (preproc_params) 102 | value: (preproc_arg)) 103 | (preproc_function_def 104 | name: (identifier) 105 | parameters: (preproc_params 106 | (identifier)) 107 | value: (preproc_arg)) 108 | (preproc_function_def 109 | name: (identifier) 110 | parameters: (preproc_params 111 | (identifier) 112 | (identifier)) 113 | value: (preproc_arg)) 114 | (preproc_function_def 115 | name: (identifier) 116 | parameters: (preproc_params) 117 | value: (preproc_arg)) 118 | (preproc_function_def 119 | name: (identifier) 120 | parameters: (preproc_params 121 | (identifier) 122 | (identifier)) 123 | value: (preproc_arg))) 124 | 125 | ================================================================================ 126 | Ifdefs 127 | ================================================================================ 128 | 129 | #ifndef DEFINE1 130 | int j; 131 | #endif 132 | 133 | #ifdef DEFINE2 134 | ssize_t b; 135 | #define c 32 136 | #elif defined DEFINE3 137 | #else 138 | int b; 139 | #define c 16 140 | #endif 141 | 142 | #ifdef DEFINE2 143 | #else 144 | # ifdef DEFINE3 145 | # else 146 | # endif 147 | #endif 148 | 149 | -------------------------------------------------------------------------------- 150 | 151 | (translation_unit 152 | (preproc_ifdef 153 | name: (identifier) 154 | (declaration 155 | type: (primitive_type) 156 | declarator: (identifier))) 157 | (preproc_ifdef 158 | name: (identifier) 159 | (declaration 160 | type: (primitive_type) 161 | declarator: (identifier)) 162 | (preproc_def 163 | name: (identifier) 164 | value: (preproc_arg)) 165 | alternative: (preproc_elif 166 | condition: (preproc_defined 167 | (identifier)) 168 | alternative: (preproc_else 169 | (declaration 170 | type: (primitive_type) 171 | declarator: (identifier)) 172 | (preproc_def 173 | name: (identifier) 174 | value: (preproc_arg))))) 175 | (preproc_ifdef 176 | name: (identifier) 177 | alternative: (preproc_else 178 | (preproc_ifdef 179 | name: (identifier) 180 | alternative: (preproc_else))))) 181 | 182 | ================================================================================ 183 | Elifdefs 184 | ================================================================================ 185 | 186 | #ifndef DEFINE1 187 | int j; 188 | #elifndef DEFINE2 189 | int k; 190 | #endif 191 | 192 | #ifdef DEFINE2 193 | ssize_t b; 194 | #elifdef DEFINE3 195 | ssize_t c; 196 | #else 197 | int b; 198 | #endif 199 | 200 | -------------------------------------------------------------------------------- 201 | 202 | (translation_unit 203 | (preproc_ifdef 204 | (identifier) 205 | (declaration 206 | (primitive_type) 207 | (identifier)) 208 | (preproc_elifdef 209 | (identifier) 210 | (declaration 211 | (primitive_type) 212 | (identifier)))) 213 | (preproc_ifdef 214 | (identifier) 215 | (declaration 216 | (primitive_type) 217 | (identifier)) 218 | (preproc_elifdef 219 | (identifier) 220 | (declaration 221 | (primitive_type) 222 | (identifier)) 223 | (preproc_else 224 | (declaration 225 | (primitive_type) 226 | (identifier)))))) 227 | 228 | ================================================================================ 229 | General if blocks 230 | ================================================================================ 231 | 232 | #if defined(__GNUC__) && defined(__PIC__) 233 | #define inline inline __attribute__((always_inline)) 234 | #elif defined(_WIN32) 235 | #define something 236 | #elif !defined(SOMETHING_ELSE) 237 | #define SOMETHING_ELSE 238 | #else 239 | #include 240 | #endif 241 | 242 | -------------------------------------------------------------------------------- 243 | 244 | (translation_unit 245 | (preproc_if 246 | condition: (binary_expression 247 | left: (preproc_defined 248 | (identifier)) 249 | right: (preproc_defined 250 | (identifier))) 251 | (preproc_def 252 | name: (identifier) 253 | value: (preproc_arg)) 254 | alternative: (preproc_elif 255 | condition: (preproc_defined 256 | (identifier)) 257 | (preproc_def 258 | name: (identifier)) 259 | alternative: (preproc_elif 260 | condition: (unary_expression 261 | argument: (preproc_defined 262 | (identifier))) 263 | (preproc_def 264 | name: (identifier)) 265 | alternative: (preproc_else 266 | (preproc_include 267 | path: (system_lib_string))))))) 268 | 269 | ================================================================================ 270 | Preprocessor conditionals in functions 271 | ================================================================================ 272 | 273 | int main() { 274 | #if d 275 | puts("1"); 276 | #else 277 | puts("2"); 278 | #endif 279 | 280 | #if a 281 | return 0; 282 | #elif b 283 | return 1; 284 | #elif c 285 | return 2; 286 | #else 287 | return 3; 288 | #endif 289 | } 290 | 291 | -------------------------------------------------------------------------------- 292 | 293 | (translation_unit 294 | (function_definition 295 | (primitive_type) 296 | (function_declarator 297 | (identifier) 298 | (parameter_list)) 299 | (compound_statement 300 | (preproc_if 301 | (identifier) 302 | (expression_statement 303 | (call_expression 304 | (identifier) 305 | (argument_list 306 | (string_literal 307 | (string_content))))) 308 | (preproc_else 309 | (expression_statement 310 | (call_expression 311 | (identifier) 312 | (argument_list 313 | (string_literal 314 | (string_content))))))) 315 | (preproc_if 316 | (identifier) 317 | (return_statement 318 | (number_literal)) 319 | (preproc_elif 320 | (identifier) 321 | (return_statement 322 | (number_literal)) 323 | (preproc_elif 324 | (identifier) 325 | (return_statement 326 | (number_literal)) 327 | (preproc_else 328 | (return_statement 329 | (number_literal))))))))) 330 | 331 | ================================================================================ 332 | Preprocessor conditionals in struct/union bodies 333 | ================================================================================ 334 | 335 | struct S { 336 | #ifdef _WIN32 337 | LONG f2; 338 | #else 339 | uint32_t f2; 340 | #endif 341 | }; 342 | 343 | -------------------------------------------------------------------------------- 344 | 345 | (translation_unit 346 | (struct_specifier 347 | (type_identifier) 348 | (field_declaration_list 349 | (preproc_ifdef 350 | (identifier) 351 | (field_declaration 352 | (type_identifier) 353 | (field_identifier)) 354 | (preproc_else 355 | (field_declaration 356 | (primitive_type) 357 | (field_identifier))))))) 358 | 359 | ================================================================================ 360 | Unknown preprocessor directives 361 | ================================================================================ 362 | 363 | #pragma mark - UIViewController 364 | 365 | -------------------------------------------------------------------------------- 366 | 367 | (translation_unit 368 | (preproc_call 369 | directive: (preproc_directive) 370 | argument: (preproc_arg))) 371 | 372 | ================================================================================ 373 | Preprocessor expressions 374 | ================================================================================ 375 | 376 | #if A(B || C) && \ 377 | !D(F) 378 | 379 | uint32_t a; 380 | 381 | #endif 382 | 383 | -------------------------------------------------------------------------------- 384 | 385 | (translation_unit 386 | (preproc_if 387 | (binary_expression 388 | (call_expression 389 | (identifier) 390 | (argument_list 391 | (binary_expression 392 | (identifier) 393 | (identifier)))) 394 | (unary_expression 395 | (call_expression 396 | (identifier) 397 | (argument_list 398 | (identifier))))) 399 | (declaration 400 | (primitive_type) 401 | (identifier)))) 402 | -------------------------------------------------------------------------------- /test/corpus/c/statements.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | If statements 3 | ================================================================================ 4 | 5 | int main() { 6 | if (a) 7 | 1; 8 | 9 | if (!a) { 10 | 2; 11 | } else { 12 | 3; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- 17 | 18 | (translation_unit 19 | (function_definition 20 | (primitive_type) 21 | (function_declarator 22 | (identifier) 23 | (parameter_list)) 24 | (compound_statement 25 | (if_statement 26 | (condition_clause 27 | (identifier)) 28 | (expression_statement 29 | (number_literal))) 30 | (if_statement 31 | (condition_clause 32 | (unary_expression 33 | (identifier))) 34 | (compound_statement 35 | (expression_statement 36 | (number_literal))) 37 | (else_clause 38 | (compound_statement 39 | (expression_statement 40 | (number_literal)))))))) 41 | 42 | ================================================================================ 43 | For loops 44 | ================================================================================ 45 | 46 | int main() { 47 | for (;;) 48 | 1; 49 | 50 | for (int i = 0; i < 5; next(), i++) { 51 | 2; 52 | } 53 | 54 | for (start(); check(); step()) 55 | 3; 56 | 57 | for (i = 0, j = 0, k = 0, l = 0; i < 1, j < 1; i++, j++, k++, l++) 58 | 1; 59 | } 60 | 61 | -------------------------------------------------------------------------------- 62 | 63 | (translation_unit 64 | (function_definition 65 | (primitive_type) 66 | (function_declarator 67 | (identifier) 68 | (parameter_list)) 69 | (compound_statement 70 | (for_statement 71 | (expression_statement 72 | (number_literal))) 73 | (for_statement 74 | (declaration 75 | (primitive_type) 76 | (init_declarator 77 | (identifier) 78 | (number_literal))) 79 | (binary_expression 80 | (identifier) 81 | (number_literal)) 82 | (comma_expression 83 | (call_expression 84 | (identifier) 85 | (argument_list)) 86 | (update_expression 87 | (identifier))) 88 | (compound_statement 89 | (expression_statement 90 | (number_literal)))) 91 | (for_statement 92 | (call_expression 93 | (identifier) 94 | (argument_list)) 95 | (call_expression 96 | (identifier) 97 | (argument_list)) 98 | (call_expression 99 | (identifier) 100 | (argument_list)) 101 | (expression_statement 102 | (number_literal))) 103 | (for_statement 104 | (comma_expression 105 | (assignment_expression 106 | (identifier) 107 | (number_literal)) 108 | (comma_expression 109 | (assignment_expression 110 | (identifier) 111 | (number_literal)) 112 | (comma_expression 113 | (assignment_expression 114 | (identifier) 115 | (number_literal)) 116 | (assignment_expression 117 | (identifier) 118 | (number_literal))))) 119 | (comma_expression 120 | (binary_expression 121 | (identifier) 122 | (number_literal)) 123 | (binary_expression 124 | (identifier) 125 | (number_literal))) 126 | (comma_expression 127 | (update_expression 128 | (identifier)) 129 | (comma_expression 130 | (update_expression 131 | (identifier)) 132 | (comma_expression 133 | (update_expression 134 | (identifier)) 135 | (update_expression 136 | (identifier))))) 137 | (expression_statement 138 | (number_literal)))))) 139 | 140 | ================================================================================ 141 | While loops 142 | ================================================================================ 143 | 144 | int main() { 145 | while (x) 146 | printf("hi"); 147 | } 148 | 149 | -------------------------------------------------------------------------------- 150 | 151 | (translation_unit 152 | (function_definition 153 | (primitive_type) 154 | (function_declarator 155 | (identifier) 156 | (parameter_list)) 157 | (compound_statement 158 | (while_statement 159 | (condition_clause 160 | (identifier)) 161 | (expression_statement 162 | (call_expression 163 | (identifier) 164 | (argument_list 165 | (string_literal 166 | (string_content))))))))) 167 | 168 | ================================================================================ 169 | Labeled statements 170 | ================================================================================ 171 | 172 | void foo(T *t) { 173 | recur: 174 | t = t->next(); 175 | if (t) goto recur; 176 | } 177 | 178 | -------------------------------------------------------------------------------- 179 | 180 | (translation_unit 181 | (function_definition 182 | (primitive_type) 183 | (function_declarator 184 | (identifier) 185 | (parameter_list 186 | (parameter_declaration 187 | (type_identifier) 188 | (pointer_declarator 189 | (identifier))))) 190 | (compound_statement 191 | (labeled_statement 192 | (statement_identifier) 193 | (expression_statement 194 | (assignment_expression 195 | (identifier) 196 | (call_expression 197 | (field_expression 198 | (identifier) 199 | (field_identifier)) 200 | (argument_list))))) 201 | (if_statement 202 | (condition_clause 203 | (identifier)) 204 | (goto_statement 205 | (statement_identifier)))))) 206 | 207 | ================================================================================ 208 | Switch statements 209 | ================================================================================ 210 | 211 | void foo(int a) { 212 | switch (a) { 213 | puts("entered switch!"); 214 | 215 | case 3: 216 | case 5: 217 | if (b) { 218 | c(); 219 | } 220 | break; 221 | 222 | default: 223 | c(); 224 | break; 225 | } 226 | } 227 | 228 | -------------------------------------------------------------------------------- 229 | 230 | (translation_unit 231 | (function_definition 232 | (primitive_type) 233 | (function_declarator 234 | (identifier) 235 | (parameter_list 236 | (parameter_declaration 237 | (primitive_type) 238 | (identifier)))) 239 | (compound_statement 240 | (switch_statement 241 | (condition_clause 242 | (identifier)) 243 | (compound_statement 244 | (expression_statement 245 | (call_expression 246 | (identifier) 247 | (argument_list 248 | (string_literal 249 | (string_content))))) 250 | (case_statement 251 | (number_literal)) 252 | (case_statement 253 | (number_literal) 254 | (if_statement 255 | (condition_clause 256 | (identifier)) 257 | (compound_statement 258 | (expression_statement 259 | (call_expression 260 | (identifier) 261 | (argument_list))))) 262 | (break_statement)) 263 | (case_statement 264 | (expression_statement 265 | (call_expression 266 | (identifier) 267 | (argument_list))) 268 | (break_statement))))))) 269 | 270 | ================================================================================ 271 | Case statements separate from switch statements 272 | ================================================================================ 273 | 274 | int main() { 275 | switch (count % 8) { 276 | case 0: 277 | do { 278 | *to = *from++; 279 | case 2: *to = *from++; 280 | case 1: *to = *from++; 281 | } while (--n > 0); 282 | } 283 | } 284 | 285 | -------------------------------------------------------------------------------- 286 | 287 | (translation_unit 288 | (function_definition 289 | (primitive_type) 290 | (function_declarator 291 | (identifier) 292 | (parameter_list)) 293 | (compound_statement 294 | (switch_statement 295 | (condition_clause 296 | (binary_expression 297 | (identifier) 298 | (number_literal))) 299 | (compound_statement 300 | (case_statement 301 | (number_literal) 302 | (do_statement 303 | (compound_statement 304 | (expression_statement 305 | (assignment_expression 306 | (pointer_expression 307 | (identifier)) 308 | (pointer_expression 309 | (update_expression 310 | (identifier))))) 311 | (case_statement 312 | (number_literal) 313 | (expression_statement 314 | (assignment_expression 315 | (pointer_expression 316 | (identifier)) 317 | (pointer_expression 318 | (update_expression 319 | (identifier)))))) 320 | (case_statement 321 | (number_literal) 322 | (expression_statement 323 | (assignment_expression 324 | (pointer_expression 325 | (identifier)) 326 | (pointer_expression 327 | (update_expression 328 | (identifier))))))) 329 | (parenthesized_expression 330 | (binary_expression 331 | (update_expression 332 | (identifier)) 333 | (number_literal)))))))))) 334 | 335 | ================================================================================ 336 | Return statements 337 | ================================================================================ 338 | 339 | void foo() { 340 | return; 341 | return a; 342 | return a, b; 343 | } 344 | 345 | -------------------------------------------------------------------------------- 346 | 347 | (translation_unit 348 | (function_definition 349 | (primitive_type) 350 | (function_declarator 351 | (identifier) 352 | (parameter_list)) 353 | (compound_statement 354 | (return_statement) 355 | (return_statement 356 | (identifier)) 357 | (return_statement 358 | (comma_expression 359 | (identifier) 360 | (identifier)))))) 361 | 362 | ================================================================================ 363 | Comments with asterisks 364 | ================================================================================ 365 | 366 | /************************* 367 | * odd number of asterisks 368 | *************************/ 369 | int a; 370 | 371 | /************************** 372 | * even number of asterisks 373 | **************************/ 374 | int b; 375 | 376 | -------------------------------------------------------------------------------- 377 | 378 | (translation_unit 379 | (comment) 380 | (declaration 381 | (primitive_type) 382 | (identifier)) 383 | (comment) 384 | (declaration 385 | (primitive_type) 386 | (identifier))) 387 | 388 | ================================================================================ 389 | Comment with multiple backslashes 390 | ================================================================================ 391 | 392 | int a = 3; // Hello \\ 393 | World 394 | 395 | -------------------------------------------------------------------------------- 396 | 397 | (translation_unit 398 | (declaration 399 | (primitive_type) 400 | (init_declarator 401 | (identifier) 402 | (number_literal))) 403 | (comment)) 404 | 405 | ================================================================================ 406 | Attributes 407 | ================================================================================ 408 | 409 | void f() { 410 | [[a]] switch (b) { 411 | [[c]] case 1: {} 412 | case 2: 413 | [[fallthrough]]; 414 | default: 415 | } 416 | [[a]] while (true) {} 417 | [[a]] if (true) {} 418 | [[a]] for (;;) {} 419 | [[a]] return; 420 | [[a]] a; 421 | [[a]]; 422 | [[a]] label: {} 423 | [[a]] goto label; 424 | 425 | // these are c++ specific, but their bind locations should be c-compatible 426 | if (true) [[likely]] {} else [[unlikely]] {} 427 | do [[likely]] {} while (true); 428 | } 429 | 430 | -------------------------------------------------------------------------------- 431 | 432 | (translation_unit 433 | (function_definition 434 | (primitive_type) 435 | (function_declarator 436 | (identifier) 437 | (parameter_list)) 438 | (compound_statement 439 | (attributed_statement 440 | (attribute_declaration 441 | (attribute 442 | (identifier))) 443 | (switch_statement 444 | (condition_clause 445 | (identifier)) 446 | (compound_statement 447 | (attributed_statement 448 | (attribute_declaration 449 | (attribute 450 | (identifier))) 451 | (case_statement 452 | (number_literal) 453 | (compound_statement))) 454 | (case_statement 455 | (number_literal) 456 | (attributed_statement 457 | (attribute_declaration 458 | (attribute 459 | (identifier))) 460 | (expression_statement))) 461 | (case_statement)))) 462 | (attributed_statement 463 | (attribute_declaration 464 | (attribute 465 | (identifier))) 466 | (while_statement 467 | (condition_clause 468 | (true)) 469 | (compound_statement))) 470 | (attributed_statement 471 | (attribute_declaration 472 | (attribute 473 | (identifier))) 474 | (if_statement 475 | (condition_clause 476 | (true)) 477 | (compound_statement))) 478 | (attributed_statement 479 | (attribute_declaration 480 | (attribute 481 | (identifier))) 482 | (for_statement 483 | (compound_statement))) 484 | (attributed_statement 485 | (attribute_declaration 486 | (attribute 487 | (identifier))) 488 | (return_statement)) 489 | (attributed_statement 490 | (attribute_declaration 491 | (attribute 492 | (identifier))) 493 | (expression_statement 494 | (identifier))) 495 | (attributed_statement 496 | (attribute_declaration 497 | (attribute 498 | (identifier))) 499 | (expression_statement)) 500 | (attributed_statement 501 | (attribute_declaration 502 | (attribute 503 | (identifier))) 504 | (labeled_statement 505 | (statement_identifier) 506 | (compound_statement))) 507 | (attributed_statement 508 | (attribute_declaration 509 | (attribute 510 | (identifier))) 511 | (goto_statement 512 | (statement_identifier))) 513 | (comment) 514 | (if_statement 515 | (condition_clause 516 | (true)) 517 | (attributed_statement 518 | (attribute_declaration 519 | (attribute 520 | (identifier))) 521 | (compound_statement)) 522 | (else_clause 523 | (attributed_statement 524 | (attribute_declaration 525 | (attribute 526 | (identifier))) 527 | (compound_statement)))) 528 | (do_statement 529 | (attributed_statement 530 | (attribute_declaration 531 | (attribute 532 | (identifier))) 533 | (compound_statement)) 534 | (parenthesized_expression 535 | (true)))))) 536 | -------------------------------------------------------------------------------- /test/corpus/c/types.txt: -------------------------------------------------------------------------------- 1 | ======================================== 2 | Primitive types 3 | ======================================== 4 | 5 | int a; 6 | uint8_t a; 7 | uint16_t a; 8 | uint32_t a; 9 | uint64_t a; 10 | uintptr_t a; 11 | 12 | int8_t a; 13 | int16_t a; 14 | int32_t a; 15 | int64_t a; 16 | intptr_t a; 17 | 18 | char16_t a; 19 | char32_t a; 20 | 21 | size_t a; 22 | ssize_t a; 23 | 24 | --- 25 | 26 | (translation_unit 27 | (declaration (primitive_type) (identifier)) 28 | (declaration (primitive_type) (identifier)) 29 | (declaration (primitive_type) (identifier)) 30 | (declaration (primitive_type) (identifier)) 31 | (declaration (primitive_type) (identifier)) 32 | (declaration (primitive_type) (identifier)) 33 | (declaration (primitive_type) (identifier)) 34 | (declaration (primitive_type) (identifier)) 35 | (declaration (primitive_type) (identifier)) 36 | (declaration (primitive_type) (identifier)) 37 | (declaration (primitive_type) (identifier)) 38 | (declaration (primitive_type) (identifier)) 39 | (declaration (primitive_type) (identifier)) 40 | (declaration (primitive_type) (identifier)) 41 | (declaration (primitive_type) (identifier))) 42 | 43 | ======================================== 44 | Type modifiers 45 | ======================================== 46 | 47 | void f(unsigned); 48 | void f(unsigned int); 49 | void f(signed long int); 50 | void f(unsigned v1); 51 | void f(unsigned long v2); 52 | 53 | --- 54 | 55 | (translation_unit 56 | (declaration 57 | (primitive_type) 58 | (function_declarator 59 | (identifier) 60 | (parameter_list (parameter_declaration (sized_type_specifier))))) 61 | (declaration 62 | (primitive_type) 63 | (function_declarator 64 | (identifier) 65 | (parameter_list (parameter_declaration (sized_type_specifier (primitive_type)))))) 66 | (declaration 67 | (primitive_type) 68 | (function_declarator 69 | (identifier) 70 | (parameter_list (parameter_declaration (sized_type_specifier (primitive_type)))))) 71 | (declaration 72 | (primitive_type) 73 | (function_declarator 74 | (identifier) 75 | (parameter_list (parameter_declaration (sized_type_specifier) (identifier))))) 76 | (declaration 77 | (primitive_type) 78 | (function_declarator 79 | (identifier) 80 | (parameter_list (parameter_declaration (sized_type_specifier) (identifier)))))) 81 | -------------------------------------------------------------------------------- /test/corpus/concepts.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | Concept definition 3 | ================================================================================ 4 | 5 | template 6 | concept Derived = std::is_base_of::value; 7 | -------------------------------------------------------------------------------- 8 | 9 | (translation_unit 10 | (template_declaration 11 | (template_parameter_list 12 | (type_parameter_declaration 13 | (type_identifier)) 14 | (type_parameter_declaration 15 | (type_identifier))) 16 | (concept_definition 17 | (identifier) 18 | (qualified_identifier 19 | (namespace_identifier) 20 | (qualified_identifier 21 | (template_type 22 | (type_identifier) 23 | (template_argument_list 24 | (type_descriptor 25 | (type_identifier)) 26 | (type_descriptor 27 | (type_identifier)))) 28 | (identifier)))))) 29 | 30 | ================================================================================ 31 | Concept definition with requires expression 32 | ================================================================================ 33 | 34 | template 35 | concept Hashable = requires(T a) { 36 | { std::hash{}(a) } -> std::convertible_to; 37 | }; 38 | -------------------------------------------------------------------------------- 39 | 40 | (translation_unit 41 | (template_declaration 42 | (template_parameter_list 43 | (type_parameter_declaration 44 | (type_identifier))) 45 | (concept_definition 46 | (identifier) 47 | (requires_expression 48 | (parameter_list 49 | (parameter_declaration 50 | (type_identifier) 51 | (identifier))) 52 | (requirement_seq 53 | (compound_requirement 54 | (call_expression 55 | (compound_literal_expression 56 | (qualified_identifier 57 | (namespace_identifier) 58 | (template_type 59 | (type_identifier) 60 | (template_argument_list 61 | (type_descriptor 62 | (type_identifier))))) 63 | (initializer_list)) 64 | (argument_list 65 | (identifier))) 66 | (trailing_return_type 67 | (type_descriptor 68 | (qualified_identifier 69 | (namespace_identifier) 70 | (template_type 71 | (type_identifier) 72 | (template_argument_list 73 | (type_descriptor 74 | (qualified_identifier 75 | (namespace_identifier) 76 | (type_identifier)))))))))))))) 77 | 78 | ================================================================================ 79 | Requires clauses and expressions 80 | ================================================================================ 81 | 82 | template 83 | void f(T&&) requires Eq; // can appear as the last element of a function declarator 84 | 85 | template requires Addable // or right after a template parameter list 86 | T add(T a, T b) { return a + b; } 87 | 88 | template 89 | concept Addable = requires (T x) { x + x; }; // requires-expression 90 | 91 | template 92 | requires requires (T x) { x + x; } // ad-hoc constraint, note keyword used twice 93 | T add(T a, T b) { return a + b; } 94 | 95 | template 96 | requires (!std::is_same_v) // parenthesized expressions are allowed 97 | void f(T); 98 | 99 | template requires Addable && Subtractable // conjunctions 100 | T f(T); 101 | 102 | template requires Addable and Subtractable // conjunctions 103 | T f(T); 104 | 105 | template requires Addable || Subtractable // disjunctions 106 | T f(T); 107 | 108 | template requires Addable or Subtractable // conjunctions 109 | T f(T); 110 | 111 | template requires false || true // boolean literals 112 | T f(T); 113 | 114 | template requires (... && Addable) // fold expressions 115 | T f(T); 116 | 117 | -------------------------------------------------------------------------------- 118 | 119 | (translation_unit 120 | (template_declaration 121 | (template_parameter_list 122 | (type_parameter_declaration 123 | (type_identifier))) 124 | (declaration 125 | (primitive_type) 126 | (function_declarator 127 | (identifier) 128 | (parameter_list 129 | (parameter_declaration 130 | (type_identifier) 131 | (abstract_reference_declarator))) 132 | (requires_clause 133 | (template_type 134 | (type_identifier) 135 | (template_argument_list 136 | (type_descriptor 137 | (type_identifier)))))))) 138 | (comment) 139 | (template_declaration 140 | (template_parameter_list 141 | (type_parameter_declaration 142 | (type_identifier))) 143 | (requires_clause 144 | (template_type 145 | (type_identifier) 146 | (template_argument_list 147 | (type_descriptor 148 | (type_identifier))))) 149 | (comment) 150 | (function_definition 151 | (type_identifier) 152 | (function_declarator 153 | (identifier) 154 | (parameter_list 155 | (parameter_declaration 156 | (type_identifier) 157 | (identifier)) 158 | (parameter_declaration 159 | (type_identifier) 160 | (identifier)))) 161 | (compound_statement 162 | (return_statement 163 | (binary_expression 164 | (identifier) 165 | (identifier)))))) 166 | (template_declaration 167 | (template_parameter_list 168 | (type_parameter_declaration 169 | (type_identifier))) 170 | (concept_definition 171 | (identifier) 172 | (requires_expression 173 | (parameter_list 174 | (parameter_declaration 175 | (type_identifier) 176 | (identifier))) 177 | (requirement_seq 178 | (simple_requirement 179 | (binary_expression 180 | (identifier) 181 | (identifier))))))) 182 | (comment) 183 | (template_declaration 184 | (template_parameter_list 185 | (type_parameter_declaration 186 | (type_identifier))) 187 | (requires_clause 188 | (requires_expression 189 | (parameter_list 190 | (parameter_declaration 191 | (type_identifier) 192 | (identifier))) 193 | (requirement_seq 194 | (simple_requirement 195 | (binary_expression 196 | (identifier) 197 | (identifier)))))) 198 | (comment) 199 | (function_definition 200 | (type_identifier) 201 | (function_declarator 202 | (identifier) 203 | (parameter_list 204 | (parameter_declaration 205 | (type_identifier) 206 | (identifier)) 207 | (parameter_declaration 208 | (type_identifier) 209 | (identifier)))) 210 | (compound_statement 211 | (return_statement 212 | (binary_expression 213 | (identifier) 214 | (identifier)))))) 215 | (template_declaration 216 | (template_parameter_list 217 | (type_parameter_declaration 218 | (type_identifier))) 219 | (requires_clause 220 | (unary_expression 221 | (qualified_identifier 222 | (namespace_identifier) 223 | (template_function 224 | (identifier) 225 | (template_argument_list 226 | (type_descriptor 227 | (type_identifier)) 228 | (type_descriptor 229 | (primitive_type))))))) 230 | (comment) 231 | (declaration 232 | (primitive_type) 233 | (function_declarator 234 | (identifier) 235 | (parameter_list 236 | (parameter_declaration 237 | (type_identifier)))))) 238 | (template_declaration 239 | (template_parameter_list 240 | (type_parameter_declaration 241 | (type_identifier))) 242 | (requires_clause 243 | (constraint_conjunction 244 | (template_type 245 | (type_identifier) 246 | (template_argument_list 247 | (type_descriptor 248 | (type_identifier)))) 249 | (template_type 250 | (type_identifier) 251 | (template_argument_list 252 | (type_descriptor 253 | (type_identifier)))))) 254 | (comment) 255 | (declaration 256 | (type_identifier) 257 | (function_declarator 258 | (identifier) 259 | (parameter_list 260 | (parameter_declaration 261 | (type_identifier)))))) 262 | (template_declaration 263 | (template_parameter_list 264 | (type_parameter_declaration 265 | (type_identifier))) 266 | (requires_clause 267 | (constraint_conjunction 268 | (template_type 269 | (type_identifier) 270 | (template_argument_list 271 | (type_descriptor 272 | (type_identifier)))) 273 | (template_type 274 | (type_identifier) 275 | (template_argument_list 276 | (type_descriptor 277 | (type_identifier)))))) 278 | (comment) 279 | (declaration 280 | (type_identifier) 281 | (function_declarator 282 | (identifier) 283 | (parameter_list 284 | (parameter_declaration 285 | (type_identifier)))))) 286 | (template_declaration 287 | (template_parameter_list 288 | (type_parameter_declaration 289 | (type_identifier))) 290 | (requires_clause 291 | (constraint_disjunction 292 | (template_type 293 | (type_identifier) 294 | (template_argument_list 295 | (type_descriptor 296 | (type_identifier)))) 297 | (template_type 298 | (type_identifier) 299 | (template_argument_list 300 | (type_descriptor 301 | (type_identifier)))))) 302 | (comment) 303 | (declaration 304 | (type_identifier) 305 | (function_declarator 306 | (identifier) 307 | (parameter_list 308 | (parameter_declaration 309 | (type_identifier)))))) 310 | (template_declaration 311 | (template_parameter_list 312 | (type_parameter_declaration 313 | (type_identifier))) 314 | (requires_clause 315 | (constraint_disjunction 316 | (template_type 317 | (type_identifier) 318 | (template_argument_list 319 | (type_descriptor 320 | (type_identifier)))) 321 | (template_type 322 | (type_identifier) 323 | (template_argument_list 324 | (type_descriptor 325 | (type_identifier)))))) 326 | (comment) 327 | (declaration 328 | (type_identifier) 329 | (function_declarator 330 | (identifier) 331 | (parameter_list 332 | (parameter_declaration 333 | (type_identifier)))))) 334 | (template_declaration 335 | (template_parameter_list 336 | (type_parameter_declaration 337 | (type_identifier))) 338 | (requires_clause 339 | (constraint_disjunction 340 | (false) 341 | (true))) 342 | (comment) 343 | (declaration 344 | (type_identifier) 345 | (function_declarator 346 | (identifier) 347 | (parameter_list 348 | (parameter_declaration 349 | (type_identifier)))))) 350 | (template_declaration 351 | (template_parameter_list 352 | (variadic_type_parameter_declaration 353 | (type_identifier))) 354 | (requires_clause 355 | (fold_expression 356 | (template_function 357 | (identifier) 358 | (template_argument_list 359 | (type_descriptor 360 | (type_identifier)))))) 361 | (comment) 362 | (declaration 363 | (type_identifier) 364 | (function_declarator 365 | (identifier) 366 | (parameter_list 367 | (parameter_declaration 368 | (type_identifier))))))) 369 | 370 | ================================================================================ 371 | Compound requirements 372 | ================================================================================ 373 | 374 | template concept C2 = 375 | requires(T x) { 376 | {*x} -> std::convertible_to; // the expression *x must be valid 377 | // AND the type T::inner must be valid 378 | // AND the result of *x must be convertible to T::inner 379 | {x + 1} -> std::same_as; // the expression x + 1 must be valid 380 | // AND std::same_as must be satisfied 381 | // i.e., (x + 1) must be a prvalue of type int 382 | {x * 1} -> std::convertible_to; // the expression x * 1 must be valid 383 | // AND its result must be convertible to T 384 | }; 385 | 386 | -------------------------------------------------------------------------------- 387 | 388 | (translation_unit 389 | (template_declaration 390 | (template_parameter_list 391 | (type_parameter_declaration 392 | (type_identifier))) 393 | (concept_definition 394 | (identifier) 395 | (requires_expression 396 | (parameter_list 397 | (parameter_declaration 398 | (type_identifier) 399 | (identifier))) 400 | (requirement_seq 401 | (compound_requirement 402 | (pointer_expression 403 | (identifier)) 404 | (trailing_return_type 405 | (type_descriptor 406 | (qualified_identifier 407 | (namespace_identifier) 408 | (template_type 409 | (type_identifier) 410 | (template_argument_list 411 | (type_descriptor 412 | (dependent_type 413 | (qualified_identifier 414 | (namespace_identifier) 415 | (type_identifier)))))))))) 416 | (comment) 417 | (comment) 418 | (comment) 419 | (compound_requirement 420 | (binary_expression 421 | (identifier) 422 | (number_literal)) 423 | (trailing_return_type 424 | (type_descriptor 425 | (qualified_identifier 426 | (namespace_identifier) 427 | (template_type 428 | (type_identifier) 429 | (template_argument_list 430 | (type_descriptor 431 | (primitive_type)))))))) 432 | (comment) 433 | (comment) 434 | (comment) 435 | (compound_requirement 436 | (binary_expression 437 | (identifier) 438 | (number_literal)) 439 | (trailing_return_type 440 | (type_descriptor 441 | (qualified_identifier 442 | (namespace_identifier) 443 | (template_type 444 | (type_identifier) 445 | (template_argument_list 446 | (type_descriptor 447 | (type_identifier)))))))) 448 | (comment) 449 | (comment)))))) 450 | 451 | ================================================================================ 452 | Nested requirements 453 | ================================================================================ 454 | 455 | template 456 | concept Semiregular = DefaultConstructible && 457 | CopyConstructible && Destructible && CopyAssignable && 458 | requires(T a, size_t n) { 459 | requires Same; // nested: "Same<...> evaluates to true" 460 | { a.~T() } noexcept; // compound: "a.~T()" is a valid expression that doesn't throw 461 | requires Same; // nested: "Same<...> evaluates to true" 462 | requires Same; // nested 463 | { delete new T }; // compound 464 | { delete new T[n] }; // compound 465 | }; 466 | -------------------------------------------------------------------------------- 467 | 468 | (translation_unit 469 | (template_declaration 470 | (template_parameter_list 471 | (type_parameter_declaration 472 | (type_identifier))) 473 | (concept_definition 474 | (identifier) 475 | (binary_expression 476 | (binary_expression 477 | (binary_expression 478 | (binary_expression 479 | (template_function 480 | (identifier) 481 | (template_argument_list 482 | (type_descriptor 483 | (type_identifier)))) 484 | (template_function 485 | (identifier) 486 | (template_argument_list 487 | (type_descriptor 488 | (type_identifier))))) 489 | (template_function 490 | (identifier) 491 | (template_argument_list 492 | (type_descriptor 493 | (type_identifier))))) 494 | (template_function 495 | (identifier) 496 | (template_argument_list 497 | (type_descriptor 498 | (type_identifier))))) 499 | (requires_expression 500 | (parameter_list 501 | (parameter_declaration 502 | (type_identifier) 503 | (identifier)) 504 | (parameter_declaration 505 | (primitive_type) 506 | (identifier))) 507 | (requirement_seq 508 | (simple_requirement 509 | (requires_clause 510 | (template_type 511 | (type_identifier) 512 | (template_argument_list 513 | (type_descriptor 514 | (type_identifier) 515 | (abstract_pointer_declarator)) 516 | (type_descriptor 517 | (decltype 518 | (pointer_expression 519 | (identifier)))))))) 520 | (comment) 521 | (compound_requirement 522 | (call_expression 523 | (field_expression 524 | (identifier) 525 | (destructor_name 526 | (identifier))) 527 | (argument_list))) 528 | (comment) 529 | (simple_requirement 530 | (requires_clause 531 | (template_type 532 | (type_identifier) 533 | (template_argument_list 534 | (type_descriptor 535 | (type_identifier) 536 | (abstract_pointer_declarator)) 537 | (type_descriptor 538 | (decltype 539 | (new_expression 540 | (type_identifier)))))))) 541 | (comment) 542 | (simple_requirement 543 | (requires_clause 544 | (template_type 545 | (type_identifier) 546 | (template_argument_list 547 | (type_descriptor 548 | (type_identifier) 549 | (abstract_pointer_declarator)) 550 | (type_descriptor 551 | (decltype 552 | (new_expression 553 | (type_identifier) 554 | (new_declarator 555 | (identifier))))))))) 556 | (comment) 557 | (compound_requirement 558 | (delete_expression 559 | (new_expression 560 | (type_identifier)))) 561 | (comment) 562 | (compound_requirement 563 | (delete_expression 564 | (new_expression 565 | (type_identifier) 566 | (new_declarator 567 | (identifier))))) 568 | (comment))))))) 569 | 570 | ================================================================================ 571 | Constraints 572 | ================================================================================ 573 | 574 | template 575 | void f(const T&); // constrained function template declaration 576 | 577 | void f(const EqualityComparable auto&); // constrained function template declaration 578 | 579 | Sortable auto foo = f(); 580 | Sortable auto bar = g(); 581 | NS::Concept auto baz = h(); 582 | 583 | Sortable decltype(auto) foo = i(); 584 | 585 | --- 586 | 587 | (translation_unit 588 | (template_declaration 589 | (template_parameter_list 590 | (parameter_declaration 591 | (type_identifier) 592 | (identifier))) 593 | (declaration 594 | (primitive_type) 595 | (function_declarator 596 | (identifier) 597 | (parameter_list 598 | (parameter_declaration 599 | (type_qualifier) 600 | (type_identifier) 601 | (abstract_reference_declarator)))))) 602 | (comment) 603 | (declaration 604 | (primitive_type) 605 | (function_declarator 606 | (identifier) 607 | (parameter_list 608 | (parameter_declaration 609 | (type_qualifier) 610 | (placeholder_type_specifier 611 | (type_identifier) 612 | (auto)) 613 | (abstract_reference_declarator))))) 614 | (comment) 615 | (declaration 616 | (placeholder_type_specifier 617 | (type_identifier) 618 | (auto)) 619 | (init_declarator 620 | (identifier) 621 | (call_expression 622 | (identifier) 623 | (argument_list)))) 624 | (declaration 625 | (placeholder_type_specifier 626 | (template_type 627 | (type_identifier) 628 | (template_argument_list 629 | (type_descriptor 630 | (type_identifier)))) 631 | (auto)) 632 | (init_declarator 633 | (identifier) 634 | (call_expression 635 | (identifier) 636 | (argument_list)))) 637 | (declaration 638 | (placeholder_type_specifier 639 | (qualified_identifier 640 | (namespace_identifier) 641 | (template_type 642 | (type_identifier) 643 | (template_argument_list 644 | (type_descriptor 645 | (type_identifier))))) 646 | (auto)) 647 | (init_declarator 648 | (identifier) 649 | (call_expression 650 | (identifier) 651 | (argument_list)))) 652 | (declaration 653 | (placeholder_type_specifier 654 | (type_identifier) 655 | (decltype 656 | (auto))) 657 | (init_declarator 658 | (identifier) 659 | (call_expression 660 | (identifier) 661 | (argument_list))))) 662 | -------------------------------------------------------------------------------- /test/corpus/definitions.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | Scoped function definitions 3 | ================================================================================ 4 | 5 | int T::foo() { return 1; } 6 | int T::foo() const { return 0; } 7 | 8 | -------------------------------------------------------------------------------- 9 | 10 | (translation_unit 11 | (function_definition 12 | (primitive_type) 13 | (function_declarator 14 | (qualified_identifier 15 | (namespace_identifier) 16 | (identifier)) 17 | (parameter_list)) 18 | (compound_statement 19 | (return_statement 20 | (number_literal)))) 21 | (function_definition 22 | (primitive_type) 23 | (function_declarator 24 | (qualified_identifier 25 | (namespace_identifier) 26 | (identifier)) 27 | (parameter_list) 28 | (type_qualifier)) 29 | (compound_statement 30 | (return_statement 31 | (number_literal))))) 32 | 33 | ================================================================================ 34 | Constructor definitions 35 | ================================================================================ 36 | 37 | T::T() {} 38 | 39 | T::T() : f1(0), f2(1, 2) { 40 | puts("HI"); 41 | } 42 | 43 | T::T() : Base() {} 44 | 45 | T::T() try : f1(0) {} catch(...) {} 46 | 47 | -------------------------------------------------------------------------------- 48 | 49 | (translation_unit 50 | (function_definition 51 | (function_declarator 52 | (qualified_identifier 53 | (namespace_identifier) 54 | (identifier)) 55 | (parameter_list)) 56 | (compound_statement)) 57 | (function_definition 58 | (function_declarator 59 | (qualified_identifier 60 | (namespace_identifier) 61 | (identifier)) 62 | (parameter_list)) 63 | (field_initializer_list 64 | (field_initializer 65 | (field_identifier) 66 | (argument_list 67 | (number_literal))) 68 | (field_initializer 69 | (field_identifier) 70 | (argument_list 71 | (number_literal) 72 | (number_literal)))) 73 | (compound_statement 74 | (expression_statement 75 | (call_expression 76 | (identifier) 77 | (argument_list 78 | (string_literal 79 | (string_content))))))) 80 | (function_definition 81 | (function_declarator 82 | (qualified_identifier 83 | (namespace_identifier) 84 | (identifier)) 85 | (parameter_list)) 86 | (field_initializer_list 87 | (field_initializer 88 | (template_method 89 | (field_identifier) 90 | (template_argument_list 91 | (type_descriptor 92 | (type_identifier)))) 93 | (argument_list))) 94 | (compound_statement)) 95 | (function_definition 96 | (function_declarator 97 | (qualified_identifier 98 | (namespace_identifier) 99 | (identifier)) 100 | (parameter_list)) 101 | (try_statement 102 | (field_initializer_list 103 | (field_initializer 104 | (field_identifier) 105 | (argument_list 106 | (number_literal)))) 107 | (compound_statement) 108 | (catch_clause 109 | (parameter_list) 110 | (compound_statement))))) 111 | 112 | ================================================================================ 113 | Explicit constructor definitions 114 | ================================================================================ 115 | 116 | class C { 117 | explicit C(int f) : f_(f) {} 118 | 119 | private: 120 | int f_; 121 | }; 122 | 123 | -------------------------------------------------------------------------------- 124 | 125 | (translation_unit 126 | (class_specifier 127 | (type_identifier) 128 | (field_declaration_list 129 | (function_definition 130 | (explicit_function_specifier) 131 | (function_declarator 132 | (identifier) 133 | (parameter_list 134 | (parameter_declaration 135 | (primitive_type) 136 | (identifier)))) 137 | (field_initializer_list 138 | (field_initializer 139 | (field_identifier) 140 | (argument_list 141 | (identifier)))) 142 | (compound_statement)) 143 | (access_specifier) 144 | (field_declaration 145 | (primitive_type) 146 | (field_identifier))))) 147 | 148 | ================================================================================ 149 | Explicit constructor declaration 150 | ================================================================================ 151 | 152 | class C { 153 | explicit C(int f); 154 | explicit(true) C(long f); 155 | }; 156 | 157 | -------------------------------------------------------------------------------- 158 | 159 | (translation_unit 160 | (class_specifier 161 | (type_identifier) 162 | (field_declaration_list 163 | (declaration 164 | (explicit_function_specifier) 165 | (function_declarator 166 | (identifier) 167 | (parameter_list 168 | (parameter_declaration 169 | (primitive_type) 170 | (identifier))))) 171 | (declaration 172 | (explicit_function_specifier 173 | (true)) 174 | (function_declarator 175 | (identifier) 176 | (parameter_list 177 | (parameter_declaration 178 | (sized_type_specifier) 179 | (identifier)))))))) 180 | 181 | ================================================================================ 182 | Default, delete, and pure virtual methods 183 | ================================================================================ 184 | 185 | class A : public B { 186 | A() = default; 187 | A(A &&) = delete; 188 | void f() = delete; 189 | A& operator=(const A&) = default; 190 | A& operator=(A&&) = delete; 191 | ~A() = 0; 192 | }; 193 | 194 | -------------------------------------------------------------------------------- 195 | 196 | (translation_unit 197 | (class_specifier 198 | (type_identifier) 199 | (base_class_clause 200 | (access_specifier) 201 | (type_identifier)) 202 | (field_declaration_list 203 | (function_definition 204 | (function_declarator 205 | (identifier) 206 | (parameter_list)) 207 | (default_method_clause)) 208 | (function_definition 209 | (function_declarator 210 | (identifier) 211 | (parameter_list 212 | (parameter_declaration 213 | (type_identifier) 214 | (abstract_reference_declarator)))) 215 | (delete_method_clause)) 216 | (function_definition 217 | (primitive_type) 218 | (function_declarator 219 | (field_identifier) 220 | (parameter_list)) 221 | (delete_method_clause)) 222 | (function_definition 223 | (type_identifier) 224 | (reference_declarator 225 | (function_declarator 226 | (operator_name) 227 | (parameter_list 228 | (parameter_declaration 229 | (type_qualifier) 230 | (type_identifier) 231 | (abstract_reference_declarator))))) 232 | (default_method_clause)) 233 | (function_definition 234 | (type_identifier) 235 | (reference_declarator 236 | (function_declarator 237 | (operator_name) 238 | (parameter_list 239 | (parameter_declaration 240 | (type_identifier) 241 | (abstract_reference_declarator))))) 242 | (delete_method_clause)) 243 | (function_definition 244 | (function_declarator 245 | (destructor_name 246 | (identifier)) 247 | (parameter_list)) 248 | (pure_virtual_clause))))) 249 | 250 | ================================================================================ 251 | Destructor definitions 252 | ================================================================================ 253 | 254 | ~T() {} 255 | T::~T() {} 256 | 257 | -------------------------------------------------------------------------------- 258 | 259 | (translation_unit 260 | (function_definition 261 | (function_declarator 262 | (destructor_name 263 | (identifier)) 264 | (parameter_list)) 265 | (compound_statement)) 266 | (function_definition 267 | (function_declarator 268 | (qualified_identifier 269 | (namespace_identifier) 270 | (destructor_name 271 | (identifier))) 272 | (parameter_list)) 273 | (compound_statement))) 274 | 275 | ================================================================================ 276 | Function-try-block definitions 277 | ================================================================================ 278 | 279 | void foo() try {} catch(...) {} 280 | 281 | -------------------------------------------------------------------------------- 282 | 283 | (translation_unit 284 | (function_definition 285 | (primitive_type) 286 | (function_declarator 287 | (identifier) 288 | (parameter_list)) 289 | (try_statement 290 | (compound_statement) 291 | (catch_clause 292 | (parameter_list) 293 | (compound_statement))))) 294 | 295 | ================================================================================ 296 | Conversion operator definitions 297 | ================================================================================ 298 | 299 | T::operator int() try { throw 1; } catch (...) { return 2; } 300 | 301 | -------------------------------------------------------------------------------- 302 | 303 | (translation_unit 304 | (function_definition 305 | (qualified_identifier 306 | (namespace_identifier) 307 | (operator_cast 308 | (primitive_type) 309 | (abstract_function_declarator 310 | (parameter_list)))) 311 | (try_statement 312 | (compound_statement 313 | (throw_statement 314 | (number_literal))) 315 | (catch_clause 316 | (parameter_list) 317 | (compound_statement 318 | (return_statement 319 | (number_literal))))))) 320 | -------------------------------------------------------------------------------- /test/corpus/microsoft.txt: -------------------------------------------------------------------------------- 1 | ================================ 2 | declaration specs 3 | ================================ 4 | 5 | struct __declspec(dllexport) s2 6 | { 7 | }; 8 | 9 | union __declspec(noinline) u2 { 10 | }; 11 | 12 | class __declspec(uuid) u2 { 13 | }; 14 | 15 | --- 16 | 17 | (translation_unit 18 | (struct_specifier 19 | (ms_declspec_modifier 20 | (identifier)) 21 | name: (type_identifier) 22 | body: (field_declaration_list)) 23 | (union_specifier 24 | (ms_declspec_modifier 25 | (identifier)) 26 | name: (type_identifier) 27 | body: (field_declaration_list)) 28 | (class_specifier 29 | (ms_declspec_modifier 30 | (identifier)) 31 | name: (type_identifier) 32 | body: (field_declaration_list))) 33 | -------------------------------------------------------------------------------- /test/corpus/modules.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | Module Definition 3 | ================================================================================ 4 | 5 | module mod; 6 | -------------------------------------------------------------------------------- 7 | (translation_unit 8 | (module_declaration 9 | (module_name 10 | (identifier)))) 11 | 12 | ================================================================================ 13 | Module Definition with Export 14 | ================================================================================ 15 | 16 | export module mod; 17 | -------------------------------------------------------------------------------- 18 | (translation_unit 19 | (module_declaration 20 | (module_name 21 | (identifier)))) 22 | 23 | ================================================================================ 24 | Module Definition with Export and Partition 25 | ================================================================================ 26 | 27 | export module mod:mod; 28 | -------------------------------------------------------------------------------- 29 | 30 | (translation_unit 31 | (module_declaration 32 | (module_name 33 | (identifier)) 34 | (module_partition 35 | (module_name 36 | (identifier))))) 37 | 38 | ================================================================================ 39 | Module Definition with Export, Partition and Attribute 40 | ================================================================================ 41 | 42 | export module mod:mod [[attribute]]; 43 | -------------------------------------------------------------------------------- 44 | 45 | (translation_unit 46 | (module_declaration 47 | name: (module_name 48 | (identifier)) 49 | partition: (module_partition 50 | (module_name 51 | (identifier))) 52 | (attribute_declaration 53 | (attribute 54 | name: (identifier))))) 55 | 56 | ================================================================================ 57 | import Declaration 58 | ================================================================================ 59 | 60 | import mod; 61 | -------------------------------------------------------------------------------- 62 | 63 | (translation_unit 64 | (import_declaration 65 | name: (module_name 66 | (identifier)))) 67 | 68 | ================================================================================ 69 | import Declaration with export 70 | ================================================================================ 71 | 72 | export import mod; 73 | -------------------------------------------------------------------------------- 74 | 75 | (translation_unit 76 | (import_declaration 77 | name: (module_name 78 | (identifier)))) 79 | 80 | ================================================================================ 81 | import Declaration partition with export 82 | ================================================================================ 83 | 84 | export import :mod; 85 | -------------------------------------------------------------------------------- 86 | 87 | (translation_unit 88 | (import_declaration 89 | partition: (module_partition 90 | (module_name 91 | (identifier))))) 92 | 93 | ================================================================================ 94 | import Declaration headerunit with export 95 | ================================================================================ 96 | 97 | export import ; 98 | -------------------------------------------------------------------------------- 99 | 100 | (translation_unit 101 | (import_declaration 102 | header: (system_lib_string))) 103 | 104 | ================================================================================ 105 | global module fragment 106 | ================================================================================ 107 | 108 | module; 109 | -------------------------------------------------------------------------------- 110 | 111 | (translation_unit 112 | (global_module_fragment_declaration)) 113 | 114 | ================================================================================ 115 | private module fragment 116 | ================================================================================ 117 | 118 | module :private; 119 | -------------------------------------------------------------------------------- 120 | 121 | (translation_unit 122 | (private_module_fragment_declaration)) 123 | 124 | 125 | ================================================================================ 126 | export declaration 127 | ================================================================================ 128 | 129 | export module A; 130 | 131 | export char const* hello() { return "hello"; } 132 | 133 | char const* world() { return "world"; } 134 | 135 | export { 136 | int one() { return 1; } 137 | int zero() { return 0; } 138 | } 139 | 140 | export namespace hi { 141 | char const* english() { return "Hi!"; } 142 | char const* french() { return "Salut!"; } 143 | } 144 | -------------------------------------------------------------------------------- 145 | 146 | (translation_unit 147 | (module_declaration 148 | name: (module_name 149 | (identifier))) 150 | (export_declaration 151 | (function_definition 152 | type: (primitive_type) 153 | (type_qualifier) 154 | declarator: (pointer_declarator 155 | declarator: (function_declarator 156 | declarator: (identifier) 157 | parameters: (parameter_list))) 158 | body: (compound_statement 159 | (return_statement 160 | (string_literal 161 | (string_content)))))) 162 | (function_definition 163 | type: (primitive_type) 164 | (type_qualifier) 165 | declarator: (pointer_declarator 166 | declarator: (function_declarator 167 | declarator: (identifier) 168 | parameters: (parameter_list))) 169 | body: (compound_statement 170 | (return_statement 171 | (string_literal 172 | (string_content))))) 173 | (export_declaration 174 | (function_definition 175 | type: (primitive_type) 176 | declarator: (function_declarator 177 | declarator: (identifier) 178 | parameters: (parameter_list)) 179 | body: (compound_statement 180 | (return_statement 181 | (number_literal)))) 182 | (function_definition 183 | type: (primitive_type) 184 | declarator: (function_declarator 185 | declarator: (identifier) 186 | parameters: (parameter_list)) 187 | body: (compound_statement 188 | (return_statement 189 | (number_literal))))) 190 | (export_declaration 191 | (namespace_definition 192 | name: (namespace_identifier) 193 | body: (declaration_list 194 | (function_definition 195 | type: (primitive_type) 196 | (type_qualifier) 197 | declarator: (pointer_declarator 198 | declarator: (function_declarator 199 | declarator: (identifier) 200 | parameters: (parameter_list))) 201 | body: (compound_statement 202 | (return_statement 203 | (string_literal 204 | (string_content))))) 205 | (function_definition 206 | type: (primitive_type) 207 | (type_qualifier) 208 | declarator: (pointer_declarator 209 | declarator: (function_declarator 210 | declarator: (identifier) 211 | parameters: (parameter_list))) 212 | body: (compound_statement 213 | (return_statement 214 | (string_literal 215 | (string_content))))))))) 216 | -------------------------------------------------------------------------------- /test/corpus/statements.txt: -------------------------------------------------------------------------------- 1 | ================================================================================ 2 | Returning braced initializer lists 3 | ================================================================================ 4 | 5 | T main() { 6 | return {0, 5}; 7 | } 8 | 9 | -------------------------------------------------------------------------------- 10 | 11 | (translation_unit 12 | (function_definition 13 | (type_identifier) 14 | (function_declarator 15 | (identifier) 16 | (parameter_list)) 17 | (compound_statement 18 | (return_statement 19 | (initializer_list 20 | (number_literal) 21 | (number_literal)))))) 22 | 23 | ================================================================================ 24 | Range-based for loops 25 | ================================================================================ 26 | 27 | T main() { 28 | for (Value &value : values) { 29 | cout << value; 30 | } 31 | 32 | for (const auto &value : values) { 33 | cout << value; 34 | } 35 | 36 | for (const auto &value : {1, 2, 3}) { 37 | cout << value; 38 | } 39 | 40 | for (auto n = v.size(); auto i : v) { 41 | cout << --n + i << ' '; 42 | } 43 | 44 | for (using elem_t = T::value_type; elem_t i : v) { 45 | cout << --n + i << ' '; 46 | } 47 | 48 | for (int x; int v : {1}) {} 49 | } 50 | 51 | -------------------------------------------------------------------------------- 52 | 53 | (translation_unit 54 | (function_definition 55 | type: (type_identifier) 56 | declarator: (function_declarator 57 | declarator: (identifier) 58 | parameters: (parameter_list)) 59 | body: (compound_statement 60 | (for_range_loop 61 | type: (type_identifier) 62 | declarator: (reference_declarator 63 | (identifier)) 64 | right: (identifier) 65 | body: (compound_statement 66 | (expression_statement 67 | (binary_expression 68 | left: (identifier) 69 | right: (identifier))))) 70 | (for_range_loop 71 | (type_qualifier) 72 | type: (placeholder_type_specifier 73 | (auto)) 74 | declarator: (reference_declarator 75 | (identifier)) 76 | right: (identifier) 77 | body: (compound_statement 78 | (expression_statement 79 | (binary_expression 80 | left: (identifier) 81 | right: (identifier))))) 82 | (for_range_loop 83 | (type_qualifier) 84 | type: (placeholder_type_specifier 85 | (auto)) 86 | declarator: (reference_declarator 87 | (identifier)) 88 | right: (initializer_list 89 | (number_literal) 90 | (number_literal) 91 | (number_literal)) 92 | body: (compound_statement 93 | (expression_statement 94 | (binary_expression 95 | left: (identifier) 96 | right: (identifier))))) 97 | (for_range_loop 98 | initializer: (init_statement 99 | (declaration 100 | type: (placeholder_type_specifier 101 | (auto)) 102 | declarator: (init_declarator 103 | declarator: (identifier) 104 | value: (call_expression 105 | function: (field_expression 106 | argument: (identifier) 107 | field: (field_identifier)) 108 | arguments: (argument_list))))) 109 | type: (placeholder_type_specifier 110 | (auto)) 111 | declarator: (identifier) 112 | right: (identifier) 113 | body: (compound_statement 114 | (expression_statement 115 | (binary_expression 116 | left: (binary_expression 117 | left: (identifier) 118 | right: (binary_expression 119 | left: (update_expression 120 | argument: (identifier)) 121 | right: (identifier))) 122 | right: (char_literal 123 | (character)))))) 124 | (for_range_loop 125 | initializer: (init_statement 126 | (alias_declaration 127 | name: (type_identifier) 128 | type: (type_descriptor 129 | type: (qualified_identifier 130 | scope: (namespace_identifier) 131 | name: (type_identifier))))) 132 | type: (type_identifier) 133 | declarator: (identifier) 134 | right: (identifier) 135 | body: (compound_statement 136 | (expression_statement 137 | (binary_expression 138 | left: (binary_expression 139 | left: (identifier) 140 | right: (binary_expression 141 | left: (update_expression 142 | argument: (identifier)) 143 | right: (identifier))) 144 | right: (char_literal 145 | (character)))))) 146 | (for_range_loop 147 | initializer: (init_statement 148 | (declaration 149 | type: (primitive_type) 150 | declarator: (identifier))) 151 | type: (primitive_type) 152 | declarator: (identifier) 153 | right: (initializer_list 154 | (number_literal)) 155 | body: (compound_statement))))) 156 | 157 | ================================================================================ 158 | Constexpr if statements 159 | ================================================================================ 160 | 161 | T f() { 162 | if constexpr (std::is_pointer_v) 163 | return *t; 164 | else 165 | return t; 166 | } 167 | 168 | -------------------------------------------------------------------------------- 169 | 170 | (translation_unit 171 | (function_definition 172 | type: (type_identifier) 173 | declarator: (function_declarator 174 | declarator: (identifier) 175 | parameters: (parameter_list)) 176 | body: (compound_statement 177 | (if_statement 178 | condition: (condition_clause 179 | value: (qualified_identifier 180 | scope: (namespace_identifier) 181 | name: (template_function 182 | name: (identifier) 183 | arguments: (template_argument_list 184 | (type_descriptor 185 | type: (type_identifier)))))) 186 | consequence: (return_statement 187 | (pointer_expression 188 | argument: (identifier))) 189 | alternative: (else_clause 190 | (return_statement 191 | (identifier))))))) 192 | 193 | ================================================================================ 194 | If statements with declarations 195 | ================================================================================ 196 | 197 | void f() { 198 | if (const int x = foo()) { } 199 | if (const int x { foo() }) { } 200 | if (const int x = foo(); x != 0) { } 201 | } 202 | 203 | -------------------------------------------------------------------------------- 204 | 205 | (translation_unit 206 | (function_definition 207 | type: (primitive_type) 208 | declarator: (function_declarator 209 | declarator: (identifier) 210 | parameters: (parameter_list)) 211 | body: (compound_statement 212 | (if_statement 213 | condition: (condition_clause 214 | value: (declaration 215 | (type_qualifier) 216 | type: (primitive_type) 217 | declarator: (identifier) 218 | value: (call_expression 219 | function: (identifier) 220 | arguments: (argument_list)))) 221 | consequence: (compound_statement)) 222 | (if_statement 223 | condition: (condition_clause 224 | value: (declaration 225 | (type_qualifier) 226 | type: (primitive_type) 227 | declarator: (identifier) 228 | value: (initializer_list 229 | (call_expression 230 | function: (identifier) 231 | arguments: (argument_list))))) 232 | consequence: (compound_statement)) 233 | (if_statement 234 | condition: (condition_clause 235 | initializer: (init_statement 236 | (declaration 237 | (type_qualifier) 238 | type: (primitive_type) 239 | declarator: (init_declarator 240 | declarator: (identifier) 241 | value: (call_expression 242 | function: (identifier) 243 | arguments: (argument_list))))) 244 | value: (binary_expression 245 | left: (identifier) 246 | right: (number_literal))) 247 | consequence: (compound_statement))))) 248 | 249 | ================================================================================ 250 | Try/catch statements 251 | ================================================================================ 252 | 253 | void main() { 254 | try { 255 | f(); 256 | } catch (const std::overflow_error) { 257 | // f() throws std::overflow_error (same type rule) 258 | } catch (const exception &e) { 259 | // f() throws std::logic_error (base class rule) 260 | } catch (...) { 261 | // f() throws std::string or int or any other unrelated type 262 | } 263 | } 264 | 265 | -------------------------------------------------------------------------------- 266 | 267 | (translation_unit 268 | (function_definition 269 | (primitive_type) 270 | (function_declarator 271 | (identifier) 272 | (parameter_list)) 273 | (compound_statement 274 | (try_statement 275 | (compound_statement 276 | (expression_statement 277 | (call_expression 278 | (identifier) 279 | (argument_list)))) 280 | (catch_clause 281 | (parameter_list 282 | (parameter_declaration 283 | (type_qualifier) 284 | (qualified_identifier 285 | (namespace_identifier) 286 | (type_identifier)))) 287 | (compound_statement 288 | (comment))) 289 | (catch_clause 290 | (parameter_list 291 | (parameter_declaration 292 | (type_qualifier) 293 | (type_identifier) 294 | (reference_declarator 295 | (identifier)))) 296 | (compound_statement 297 | (comment))) 298 | (catch_clause 299 | (parameter_list) 300 | (compound_statement 301 | (comment))))))) 302 | 303 | ================================================================================ 304 | Throw statements 305 | ================================================================================ 306 | 307 | void main() { 308 | throw e; 309 | throw x + 1; 310 | throw "exception"; 311 | } 312 | 313 | -------------------------------------------------------------------------------- 314 | 315 | (translation_unit 316 | (function_definition 317 | (primitive_type) 318 | (function_declarator 319 | (identifier) 320 | (parameter_list)) 321 | (compound_statement 322 | (throw_statement 323 | (identifier)) 324 | (throw_statement 325 | (binary_expression 326 | (identifier) 327 | (number_literal))) 328 | (throw_statement 329 | (string_literal 330 | (string_content)))))) 331 | 332 | ================================================================================ 333 | Noexcept specifier 334 | ================================================================================ 335 | 336 | void foo() noexcept; 337 | void foo() noexcept(true); 338 | template T foo() noexcept(sizeof(T) < 4); 339 | 340 | -------------------------------------------------------------------------------- 341 | 342 | (translation_unit 343 | (declaration 344 | (primitive_type) 345 | (function_declarator 346 | (identifier) 347 | (parameter_list) 348 | (noexcept))) 349 | (declaration 350 | (primitive_type) 351 | (function_declarator 352 | (identifier) 353 | (parameter_list) 354 | (noexcept 355 | (true)))) 356 | (template_declaration 357 | (template_parameter_list 358 | (type_parameter_declaration 359 | (type_identifier))) 360 | (declaration 361 | (type_identifier) 362 | (function_declarator 363 | (identifier) 364 | (parameter_list) 365 | (noexcept 366 | (binary_expression 367 | (sizeof_expression 368 | (parenthesized_expression 369 | (identifier))) 370 | (number_literal))))))) 371 | 372 | ================================================================================ 373 | Throw specifier 374 | ================================================================================ 375 | 376 | void foo() throw(); 377 | void foo() throw(int); 378 | void foo() throw(std::string, char *); 379 | void foo() throw(float) { } 380 | 381 | -------------------------------------------------------------------------------- 382 | 383 | (translation_unit 384 | (declaration 385 | (primitive_type) 386 | (function_declarator 387 | (identifier) 388 | (parameter_list) 389 | (throw_specifier))) 390 | (declaration 391 | (primitive_type) 392 | (function_declarator 393 | (identifier) 394 | (parameter_list) 395 | (throw_specifier 396 | (type_descriptor 397 | (primitive_type))))) 398 | (declaration 399 | (primitive_type) 400 | (function_declarator 401 | (identifier) 402 | (parameter_list) 403 | (throw_specifier 404 | (type_descriptor 405 | (qualified_identifier 406 | (namespace_identifier) 407 | (type_identifier))) 408 | (type_descriptor 409 | (primitive_type) 410 | (abstract_pointer_declarator))))) 411 | (function_definition 412 | (primitive_type) 413 | (function_declarator 414 | (identifier) 415 | (parameter_list) 416 | (throw_specifier 417 | (type_descriptor 418 | (primitive_type)))) 419 | (compound_statement))) 420 | 421 | ================================================================================ 422 | Assignment 423 | ================================================================================ 424 | 425 | a::b::c = 1; 426 | 427 | -------------------------------------------------------------------------------- 428 | 429 | (translation_unit 430 | (expression_statement 431 | (assignment_expression 432 | (qualified_identifier 433 | (namespace_identifier) 434 | (qualified_identifier 435 | (namespace_identifier) 436 | (identifier))) 437 | (number_literal)))) 438 | 439 | ================================================================================ 440 | Attributes 441 | ================================================================================ 442 | 443 | void f() { 444 | [[a]] switch (b) { 445 | [[c]] case 1: {} 446 | } 447 | [[a]] while (true) {} 448 | [[a]] if (true) {} 449 | [[a]] for (auto x : y) {} 450 | [[a]] for (;;) {} 451 | [[a]] return; 452 | [[a]] a; 453 | [[a]]; 454 | [[a]] label: {} 455 | [[a]] goto label; 456 | } 457 | 458 | -------------------------------------------------------------------------------- 459 | 460 | (translation_unit 461 | (function_definition 462 | (primitive_type) 463 | (function_declarator 464 | (identifier) 465 | (parameter_list)) 466 | (compound_statement 467 | (attributed_statement 468 | (attribute_declaration 469 | (attribute 470 | (identifier))) 471 | (switch_statement 472 | (condition_clause 473 | (identifier)) 474 | (compound_statement 475 | (attributed_statement 476 | (attribute_declaration 477 | (attribute 478 | (identifier))) 479 | (case_statement 480 | (number_literal) 481 | (compound_statement)))))) 482 | (attributed_statement 483 | (attribute_declaration 484 | (attribute 485 | (identifier))) 486 | (while_statement 487 | (condition_clause 488 | (true)) 489 | (compound_statement))) 490 | (attributed_statement 491 | (attribute_declaration 492 | (attribute 493 | (identifier))) 494 | (if_statement 495 | (condition_clause 496 | (true)) 497 | (compound_statement))) 498 | (attributed_statement 499 | (attribute_declaration 500 | (attribute 501 | (identifier))) 502 | (for_range_loop 503 | (placeholder_type_specifier 504 | (auto)) 505 | (identifier) 506 | (identifier) 507 | (compound_statement))) 508 | (attributed_statement 509 | (attribute_declaration 510 | (attribute 511 | (identifier))) 512 | (for_statement 513 | (compound_statement))) 514 | (attributed_statement 515 | (attribute_declaration 516 | (attribute 517 | (identifier))) 518 | (return_statement)) 519 | (attributed_statement 520 | (attribute_declaration 521 | (attribute 522 | (identifier))) 523 | (expression_statement 524 | (identifier))) 525 | (attributed_statement 526 | (attribute_declaration 527 | (attribute 528 | (identifier))) 529 | (expression_statement)) 530 | (attributed_statement 531 | (attribute_declaration 532 | (attribute 533 | (identifier))) 534 | (labeled_statement 535 | (statement_identifier) 536 | (compound_statement))) 537 | (attributed_statement 538 | (attribute_declaration 539 | (attribute 540 | (identifier))) 541 | (goto_statement 542 | (statement_identifier)))))) 543 | 544 | ================================================================================ 545 | Coroutines 546 | ================================================================================ 547 | 548 | co_return 1; 549 | co_return; 550 | co_yield 1; 551 | 552 | -------------------------------------------------------------------------------- 553 | 554 | (translation_unit 555 | (co_return_statement 556 | (number_literal)) 557 | (co_return_statement) 558 | (co_yield_statement 559 | (number_literal))) 560 | 561 | ================================================================================ 562 | Switch statements 563 | ================================================================================ 564 | 565 | void foo(int a) { 566 | switch (a) { 567 | case 1: 568 | for (auto i : vec) {} 569 | case 2: 570 | try { 571 | // do something 572 | } catch(...) {} 573 | throw 1; 574 | case 3: 575 | co_return; 576 | default: 577 | co_yield a; 578 | } 579 | } 580 | 581 | -------------------------------------------------------------------------------- 582 | 583 | (translation_unit 584 | (function_definition 585 | (primitive_type) 586 | (function_declarator 587 | (identifier) 588 | (parameter_list 589 | (parameter_declaration 590 | (primitive_type) 591 | (identifier)))) 592 | (compound_statement 593 | (switch_statement 594 | (condition_clause 595 | (identifier)) 596 | (compound_statement 597 | (case_statement 598 | (number_literal) 599 | (for_range_loop 600 | (placeholder_type_specifier 601 | (auto)) 602 | (identifier) 603 | (identifier) 604 | (compound_statement))) 605 | (case_statement 606 | (number_literal) 607 | (try_statement 608 | (compound_statement 609 | (comment)) 610 | (catch_clause 611 | (parameter_list) 612 | (compound_statement))) 613 | (throw_statement 614 | (number_literal))) 615 | (case_statement 616 | (number_literal) 617 | (co_return_statement)) 618 | (case_statement 619 | (co_yield_statement 620 | (identifier)))))))) 621 | -------------------------------------------------------------------------------- /test/corpus/types.txt: -------------------------------------------------------------------------------- 1 | ========================================== 2 | The auto type 3 | ========================================== 4 | 5 | void foo() { 6 | auto x = 1; 7 | } 8 | 9 | --- 10 | 11 | (translation_unit 12 | (function_definition 13 | (primitive_type) 14 | (function_declarator (identifier) (parameter_list)) 15 | (compound_statement 16 | (declaration (placeholder_type_specifier (auto)) (init_declarator (identifier) (number_literal)))))) 17 | 18 | ========================================== 19 | Namespaced types 20 | ========================================== 21 | 22 | std::string my_string; 23 | std::vector::size_typ my_string; 24 | 25 | --- 26 | 27 | (translation_unit 28 | (declaration 29 | (qualified_identifier (namespace_identifier) (type_identifier)) 30 | (identifier)) 31 | (declaration 32 | (qualified_identifier 33 | (namespace_identifier) 34 | (qualified_identifier 35 | (template_type 36 | (type_identifier) 37 | (template_argument_list (type_descriptor (primitive_type)))) 38 | (type_identifier))) 39 | (identifier))) 40 | 41 | ========================================== 42 | Dependent type names 43 | ========================================== 44 | 45 | template 46 | struct X : B 47 | { 48 | typename T::A* pa; 49 | }; 50 | 51 | --- 52 | 53 | (translation_unit 54 | (template_declaration 55 | (template_parameter_list (type_parameter_declaration (type_identifier))) 56 | (struct_specifier 57 | (type_identifier) 58 | (base_class_clause 59 | (template_type (type_identifier) (template_argument_list (type_descriptor (type_identifier))))) 60 | (field_declaration_list 61 | (field_declaration 62 | (dependent_type (qualified_identifier (namespace_identifier) (type_identifier))) 63 | (pointer_declarator (field_identifier))))))) 64 | 65 | ========================================== 66 | Template types with empty argument lists 67 | ========================================== 68 | 69 | use_future_t<> use_future; 70 | 71 | --- 72 | 73 | (translation_unit 74 | (declaration (template_type (type_identifier) (template_argument_list)) (identifier))) 75 | 76 | ================================ 77 | Function types as template arguments 78 | ================================ 79 | 80 | typedef std::function MyFunc; 81 | typedef std::function b; 82 | 83 | --- 84 | 85 | (translation_unit 86 | (type_definition 87 | (qualified_identifier 88 | (namespace_identifier) 89 | (template_type 90 | (type_identifier) 91 | (template_argument_list 92 | (type_descriptor 93 | (type_identifier) 94 | (abstract_function_declarator (parameter_list 95 | (parameter_declaration (primitive_type)))))))) 96 | (type_identifier)) 97 | (type_definition 98 | (qualified_identifier 99 | (namespace_identifier) 100 | (template_type 101 | (type_identifier) 102 | (template_argument_list 103 | (type_descriptor 104 | (primitive_type) 105 | (abstract_function_declarator (parameter_list 106 | (parameter_declaration (primitive_type)))))))) 107 | (type_identifier))) 108 | 109 | ==================================================== 110 | Decltype 111 | ==================================================== 112 | 113 | decltype(A) x; 114 | decltype(B) foo(void x, decltype(C) y); 115 | template auto add(T t, U u) -> decltype(t + u); 116 | array arr; 117 | 118 | --- 119 | 120 | (translation_unit 121 | (declaration 122 | (decltype (identifier)) 123 | (identifier)) 124 | (declaration 125 | (decltype (identifier)) 126 | (function_declarator (identifier) 127 | (parameter_list 128 | (parameter_declaration (primitive_type) (identifier)) 129 | (parameter_declaration (decltype (identifier)) (identifier))))) 130 | (template_declaration 131 | (template_parameter_list 132 | (type_parameter_declaration (type_identifier)) (type_parameter_declaration (type_identifier))) 133 | (declaration 134 | (placeholder_type_specifier (auto)) 135 | (function_declarator 136 | (identifier) 137 | (parameter_list 138 | (parameter_declaration (type_identifier) (identifier)) 139 | (parameter_declaration (type_identifier) (identifier))) 140 | (trailing_return_type 141 | (type_descriptor 142 | (decltype (binary_expression (identifier) (identifier)))))))) 143 | (declaration 144 | (template_type 145 | (type_identifier) 146 | (template_argument_list 147 | (type_descriptor 148 | (qualified_identifier 149 | (decltype (identifier)) 150 | (type_identifier))) 151 | (number_literal))) 152 | (identifier))) 153 | 154 | ==================================================== 155 | Trailing return type 156 | ==================================================== 157 | 158 | auto a::foo() const -> const A& {} 159 | auto b::foo() const -> A const& {} 160 | 161 | --- 162 | 163 | (translation_unit 164 | (function_definition 165 | (placeholder_type_specifier (auto)) 166 | (function_declarator 167 | (qualified_identifier (namespace_identifier) (identifier)) 168 | (parameter_list) 169 | (type_qualifier) 170 | (trailing_return_type 171 | (type_descriptor 172 | (type_qualifier) 173 | (template_type (type_identifier) (template_argument_list (type_descriptor (type_identifier)))) 174 | (abstract_reference_declarator)))) 175 | (compound_statement)) 176 | (function_definition 177 | (placeholder_type_specifier (auto)) 178 | (function_declarator 179 | (qualified_identifier (namespace_identifier) (identifier)) 180 | (parameter_list) 181 | (type_qualifier) 182 | (trailing_return_type 183 | (type_descriptor 184 | (template_type (type_identifier) (template_argument_list (type_descriptor (type_identifier)))) 185 | (type_qualifier) 186 | (abstract_reference_declarator)))) 187 | (compound_statement)) 188 | ) 189 | -------------------------------------------------------------------------------- /test/highlight/keywords.cpp: -------------------------------------------------------------------------------- 1 | export module keywords; 2 | // <- keyword 3 | // ^ keyword 4 | 5 | import std; 6 | // ^ keyword 7 | 8 | using namespace std; 9 | // ^ keyword 10 | 11 | namespace foo {} 12 | // ^ keyword 13 | 14 | template 15 | // ^ keyword 16 | // ^ keyword 17 | 18 | class A { 19 | // <- keyword 20 | 21 | public: 22 | // <- keyword 23 | private: 24 | // <- keyword 25 | protected: 26 | // <- keyword 27 | virtual ~A() = 0; 28 | // <- keyword 29 | }; 30 | 31 | int main() { 32 | throw new Error(); 33 | // ^ keyword 34 | // ^ keyword 35 | 36 | try { 37 | // <- keyword 38 | } catch (e) { 39 | // <- keyword 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/highlight/names.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | a(); 3 | // <- function 4 | 5 | a::b(); 6 | // ^ function 7 | 8 | a::b(); 9 | // ^ function 10 | 11 | this->b(); 12 | // ^ function 13 | 14 | auto x = y; 15 | // <- type 16 | 17 | vector a; 18 | // <- type 19 | 20 | std::vector a; 21 | // ^ type 22 | } 23 | 24 | class C : D{ 25 | A(); 26 | // <- function 27 | 28 | void efg() { 29 | // ^ function 30 | } 31 | } 32 | 33 | void A::b() { 34 | // ^ function 35 | } 36 | -------------------------------------------------------------------------------- /tree-sitter.json: -------------------------------------------------------------------------------- 1 | { 2 | "grammars": [ 3 | { 4 | "name": "cpp", 5 | "camelcase": "CPP", 6 | "scope": "source.cpp", 7 | "path": ".", 8 | "file-types": [ 9 | "cc", 10 | "cpp", 11 | "cxx", 12 | "hpp", 13 | "hxx", 14 | "h" 15 | ], 16 | "highlights": [ 17 | "node_modules/tree-sitter-c/queries/highlights.scm", 18 | "queries/highlights.scm" 19 | ], 20 | "injections": "queries/injections.scm", 21 | "tags": "queries/tags.scm", 22 | "injection-regex": "^(cc|cpp)$" 23 | } 24 | ], 25 | "metadata": { 26 | "version": "0.23.4", 27 | "license": "MIT", 28 | "description": "C++ grammar for tree-sitter", 29 | "authors": [ 30 | { 31 | "name": "Max Brunsfeld", 32 | "email": "maxbrunsfeld@gmail.com" 33 | }, 34 | { 35 | "name": "Amaan Qureshi", 36 | "email": "amaanq12@gmail.com" 37 | }, 38 | { 39 | "name": "John Drouhard", 40 | "email": "john@drouhard.dev" 41 | } 42 | ], 43 | "links": { 44 | "repository": "https://github.com/tree-sitter/tree-sitter-cpp" 45 | } 46 | }, 47 | "bindings": { 48 | "c": true, 49 | "go": true, 50 | "node": true, 51 | "python": true, 52 | "rust": true, 53 | "swift": true 54 | } 55 | } 56 | --------------------------------------------------------------------------------