-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to output comments in the parser #239
Comments
Update: what I wanted already exists, namely |
Hey. So the lexer includes comments (this is indeed what I use to pretty-print the code later). The parser, ignores them. See https://github.com/florianschanda/miss_hit/blob/master/miss_hit_core/m_parser.py#L168 And I think all parsers I know of do that; and you already found the one special utility that checks for docstrings. What you want is the comments in the parse tree - but this is not obvious how to do. Bear in mind a comment can appear at any point, including in the middle of statements. Consider for example:
What should the parse tree look like here? Right now we have:
But there is no real place to attach a comment. Should it be on the +? On the assign? For tracability of course it would be good to keep them somehow, but likely I will recover them with some post-processing similar to what I do with docstrings. As an exercise, consider writing a BNF for a language where comments are part of the BNF grammar. It would be possible, but you quickly realise that the only comments you can support sensibly are the ones between statements. |
I am going to close this ticket as I think the functionality to extract comments is there; but if you have a concrete proposal we can open it again. |
One more bit of detail on code generation:
|
OK, know what I will re-open this (but do not expect anything anytime soon). There is still massive issues here. Some bits are easy, consider:
Parse tree here could be:
Inline comments could be attached as a special attribute, most likely the top-level statement. For example:
Gives us something like:
But where it gets fun is when it becomes ambiguous:
I bet you can guess where this is going :) One statement? Or a statement + a comment node? We could come up with some rules, like if there is no spacing, it gets merged, but if there is, it is made separate. Since we're never going to do anything with these comments that semantically makes a difference, it doesn't matter too much. But it's just a massive can of worms. Maybe you can share a bit what you had in mind, or if the docstring thing does fix all your problems? |
This is indeed a tough question, so let me dump here my thoughts about this.
Yep, that's a relatively easy one, exactly what I had in mind.
Basically what I had in mind too.
Hmm, this is the hard one. I thought it would be nice if you have a list of comments connected to the top-most statement of these lines. So, in this case, it is attached to the Simple_Assignment_Statement (or something). Since an expression might cover multiple lines, and thus also multiple comments, it should be a list. Other example:
Here the comment should be attached to the "=" from Full example:
Here the should should be something like this (I don't know the exact names, but you get the point):
Note that docstrings might be catched twice in that case (if you run the post-process function), but it's up to the user to properly handle that I think. |
It would be very nice if there was a command-line option to also include the comments in the parser, rather than skipping them entirely. This is useful for code generation later (then you might prefer to keep the comments as well).
The text was updated successfully, but these errors were encountered: