stadelmanma / tree-sitter-fortran

Fortran grammar for tree-sitter
MIT License
30 stars 15 forks source link

Support preprocessor directives #15

Open stadelmanma opened 6 years ago

stadelmanma commented 6 years ago

Given that fortran and C both use the same preprocessor I can probably copy these over directly without too many changes.

stadelmanma commented 5 years ago

One thing to keep in mind for testing is how preprocessor directives in terrible locations are handled such as tweaking the call signature of a subroutine. I belive this is supported in the C grammar but I'm not sure if that method will work for me.

fengjiawang commented 1 year ago

I'm using your excelent plugin. It works perfect except for the highlight of preprocessor. Are you considering just support preprocessor highlights? I think it's easier, maybe.

ZedThree commented 1 year ago

tree-sitter-cpp does something clever where it imports the C grammar: https://github.com/tree-sitter/tree-sitter-cpp/blob/56cec4c2eb5d6af3d2942e69e35db15ae2433740/grammar.js#L1

const C = require("tree-sitter-c/grammar")

I tried to do this and then we could just use their rules directly, but I couldn't find any documentation on how to automatically bring in this dependency. npm install tree-sitter-c gives me some errors, so I couldn't even try it manually.

Probably our best bet is to just copy-paste all the preprocessor rules into the Fortran grammar

stadelmanma commented 1 year ago

I agree copying out the rules would probably be the best place to start, pulling in the whole C grammar might be overkill and could have unintended side-effects.

DavidAnderegg commented 1 year ago

What is going on with this? I would highly value this feature as I am working on a project that is littered with directives.

I am also willing to implement this feature myself. But i would probably need some guidance...

What do you think?

ZedThree commented 1 year ago

It's been awhile since I looked at this, but I think it's is going to be quite tricky. If you look at how the C grammar implements preprocessor directives, they can appear anywhere and have anything as child nodes. For C this isn't much of a problem because there's not many top-level nodes, but with Fortran there's lots of potential kinds of child nodes.

I'd probably start with some of the standalone directives like #define, #line. Actually, it would be especially useful to have the directives that come from preprocessed source: # <line number> <filename>. This would allow running the preprocessor and then parsing the result at least.

stadelmanma commented 1 year ago

@ZedThree yeah it’s a tricky problem since you can do literally anything with preprocessor directives.

DavidAnderegg commented 1 year ago

Alright, thanks for your responses.

I think i will fork it, create a PR and start working on the easy directives like #if etc. first. Then we will see where this is going.