joelspadin / tree-sitter-devicetree

Tree-sitter grammar for Devicetree files
MIT License
28 stars 6 forks source link

Add support for preprocessor conditionals #12

Closed mskelton closed 9 months ago

mskelton commented 11 months ago

Adds support for preprocessor conditionals #ifdef and #ifndef. Borrowed the grammar structure from tree-sitter-c.

One question I have is which preprocessor conditionals are supported by the grammar spec. I don't know where specifically you look for the official spec, I tried looking at the Zephyr docs but didn't find anything super specific. Basically do we need to support #else, #elif, etc.?

joelspadin commented 11 months ago

The Zephyr docs don't specify because it's not actually devicetree. It's just the C preprocessor, so if we add any of them, we should add all of

mskelton commented 11 months ago

@joelspadin I updated the PR to use the full preprocessor conditional grammar from tree-sitter-c along with more tests.

nickcoutsos commented 11 months ago

How are the *_in_node versions used?

mskelton commented 11 months ago

@nickcoutsos Because the list of possible members is different for top level items and nodes, the preprocessor rules need to be separate to account for that difference. The _in_node stuff is aliases so it produces the same resulting types.

https://github.com/joelspadin/tree-sitter-devicetree/pull/12/files#diff-919ac210accac9ecc55a76d10a7590e3d85ca3f0e165b52d30f08faee486d0cbR168-R169

Hope that makes sense.

joelspadin commented 11 months ago

Because the list of possible members is different for top level items and nodes

Maybe I'm misunderstanding what you mean, but the C preprocessor doesn't know or care whether preprocessor commands are at the top level or inside a node. I would expect all the preprocessor commands to work everywhere. (They could even appear inside of a multi-line parameter, but I don't think it's worth the effort to support that.)

nickcoutsos commented 11 months ago

Okay, the distinction I think I'm seeing is that preproc statements appearing at the top level can wrap other _top_level_item elements whereas the in-node variety is expected to contain other elements of _node_members.

Does this need to account for preprocessor statements "interrupting" devicetree grammar? Or is that a question to be answered down the road?

mskelton commented 11 months ago

@joelspadin Any additional thoughts on this PR? @nickcoutsos is correct about the reason for the top level and node level preprocessor nodes due to the possible children for top level and nodes being different.

joelspadin commented 11 months ago

Okay, that does make sense. I think I was originally thinking about this as each #if line being its own isolated thing rather than a scope inside which other things would fit. I'll try to find some time to review this later.

joelspadin commented 9 months ago

Sorry for the long wait on this. This all looks good to me. Thanks!