tree-sitter / tree-sitter/tree-sitter-cpp

feature: Alternative handling of trailing backslashes in macro definitions

Open
#312 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
453
Forks
185
PR merge metrics
No merged PRs in 30d

Description

Did you check the tree-sitter docs?
Is your feature request related to a problem? Please describe.

Currently, when defining a macro with a trailing backslash \ at the end of the last line, the parse tree extends the preproc_arg end position to the next line.

For example, given:

#define ADD(x, y) \
    ((x) + (y)) \

The parse tree is:

translation_unit [0, 0] - [2, 0]
  preproc_function_def [0, 0] - [2, 0]
    #define [0, 0] - [0, 7]
    name: identifier [0, 8] - [0, 11]
    parameters: preproc_params [0, 11] - [0, 17]
      ( [0, 11] - [0, 12]
      identifier [0, 12] - [0, 13]
      , [0, 13] - [0, 14]
      identifier [0, 15] - [0, 16]
      ) [0, 16] - [0, 17]
    value: preproc_arg [1, 4] - [2, 0]    // Ends at position [2, 0]
Describe the solution you'd like

I was instead wondering if it would be possible to ensure that, in the parse tree, even with a \ at the end of the last line of the macro, the final position of the preproc_arg node matches the following case:

#define ADD(x, y) \
    ((x) + (y))

Where the resulting parse tree is:

translation_unit [0, 0] - [2, 0]
  preproc_function_def [0, 0] - [2, 0]
    #define [0, 0] - [0, 7]
    name: identifier [0, 8] - [0, 11]
    parameters: preproc_params [0, 11] - [0, 17]
      ( [0, 11] - [0, 12]
      identifier [0, 12] - [0, 13]
      , [0, 13] - [0, 14]
      identifier [0, 15] - [0, 16]
      ) [0, 16] - [0, 17]
    value: preproc_arg [1, 4] - [1, 16]    // Ends at position [1, 16]

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source file or test. Start with the two C++ macro examples and compare their parse-tree endpoints, then read the linked tree-sitter parser documentation and trace how the preproc_arg node is produced. Done means the trailing-backslash case ends at [1, 16], matching the no-trailing-backslash example.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.