tree-sitter / tree-sitter/tree-sitter-cpp
bug: Parse errors with preprocessor conditionals
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 453
- Forks
- 185
- PR merge metrics
- No merged PRs in 30d
Description
Did you check existing issues?
- I have read all the tree-sitter docs if it relates to using the parser
- I have searched the existing issues of tree-sitter-cpp
Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)
tree-sitter 0.25.3 (2a835ee029dca1c325e6f1c01dbce40396f6123e)
Describe the bug
When parsing code with preprocessor conditionals, the parse tree reports errors in valid code. For example, given the following snippet:
#include <iostream>
#define ONE
int main()
{
int x =
#ifdef ONE
1
#else
2
#endif
;
std::cout << x;
return 0;
}
the following errors are reported:
(ERROR [5, 2] - [5, 9])
(ERROR [7, 2] - [7, 3])
(ERROR [9, 2] - [9, 3])
These errors seem to correspond to the int x = assignment and the conditional values 1 and 2, even though the code is syntactically valid.
Steps To Reproduce/Bad Parse Tree
- Write the code above in a C++ file.
- Run
tree-sitter parseon the file. - Observe the obtained parse tree:
(translation_unit [0, 0] - [17, 0]
(preproc_include [0, 0] - [1, 0]
path: (system_lib_string [0, 9] - [0, 19]))
(preproc_def [1, 0] - [2, 0]
name: (identifier [1, 8] - [1, 11]))
(function_definition [3, 0] - [16, 1]
type: (primitive_type [3, 0] - [3, 3])
declarator: (function_declarator [3, 4] - [3, 10]
declarator: (identifier [3, 4] - [3, 8])
parameters: (parameter_list [3, 8] - [3, 10]))
body: (compound_statement [4, 0] - [16, 1]
(ERROR [5, 2] - [5, 9]
type: (primitive_type [5, 2] - [5, 5])
(identifier [5, 6] - [5, 7]))
(preproc_ifdef [6, 0] - [10, 6]
name: (identifier [6, 7] - [6, 10])
(ERROR [7, 2] - [7, 3]
(number_literal [7, 2] - [7, 3]))
alternative: (preproc_else [8, 0] - [8, 5])
(ERROR [9, 2] - [9, 3]
(number_literal [9, 2] - [9, 3])))
(expression_statement [11, 2] - [11, 3])
(expression_statement [13, 2] - [13, 17]
(binary_expression [13, 2] - [13, 16]
left: (qualified_identifier [13, 2] - [13, 11]
scope: (namespace_identifier [13, 2] - [13, 5])
name: (identifier [13, 7] - [13, 11]))
right: (identifier [13, 15] - [13, 16])))
(return_statement [15, 2] - [15, 11]
(number_literal [15, 9] - [15, 10])))))
Expected Behavior/Parse Tree
The parser should correctly handle this code without generating errors.
Repro
#include <iostream>
#define ONE
int main()
{
int x =
#ifdef ONE
1
#else
2
#endif
;
std::cout << x;
return 0;
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running tree-sitter parse on the supplied C++ snippet and compare the reported tree with the expected behavior. Trace how the tree-sitter-cpp grammar handles preprocessor conditionals inside an expression or declaration. Done means the example parses without ERROR nodes while preserving the conditional structure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100