-
Notifications
You must be signed in to change notification settings - Fork 28
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
Question: parsing operators and newlines #134
Comments
cc @xffxff |
Note that the rule i proposed would make this code:
set a to Ah, I just remembered that I think I generally permitted newlines inside of vectors and things without a comma (I should write some tests for that...), so this would fit with that. e.g. this is legal dada right now (playground)
and hence:
|
My thinking was that we can just await the whole "trailing |
Given
then
doesn't seem like it would parse, unless Having the grammar be newline-sensitive sure doesn't appeal to me much - I didn't realize Rust did this. (edit: but now that I think about it this is probably the special rule about parsing control structures I always knew rust had but couldn't remember the details of). This problem seems similar to the disambiguation of tuples and function calls in #117, and could be solved the same way, where an opening paren in a function call can't be split onto a new line. |
Cases like this sure do look confusing. The rules could be different inside |
Another seeming solution to the binops case in particular would be to require binops to always be space-delimited, and unary ops not: |
Another newline-sensitive solution that might handle multiple cases in this issue is that for every sequence of expressions both newlines and commas act as separators, with the separators having precedence over continuing to parse the current expression. |
Rust doesn't make the grammar newline sensitive, but it distinguishes uses of things like
Good point, I think that I meant to have |
I'll have to ponder the other suggestions. I also don't love whitespace or newline-sensitive grammars, but I think it's worth trying to not have |
I think we might as well implement the rule you suggest, at least for now. I'd love to get the reference grammar and production grammar in agreement so they can be kept in sync forever. Parol has some ability to turn on and off newline sensitivity based on context, so I think it should be able to handle the rule. Just one more thing to point out: it's been a long time since I read Code Complete but one bit that has stuck with me is the suggestion that splitting binops to a new line before the op reads better than splitting after the op. That is this:
is easier to scan than
and the proposed rule makes that formatting not possible. |
Big +1 to getting ref / actual grammar in sync. I did consider that the rule would mean you can't move operators to the start of the line. I thought it wasn't as popular for some reason, checking rustfmt suggestions it at least does move operators to the beginning (example). One other consideration: requiring that binary operators be separated by whitespace would resolve the |
Another thought that I had: Maybe
Not sure, the parens don't look great. Going to leave this comment for posterity's sake at least though. :) |
How to think about binary operators and newlines? Rust had the same issue to wrestle with and I suspect we want the same general answer. I'm referring to things like this:
The rule I propose:
So that you have to write
b - \n 5
and notb \n - 5
. That'd be a very simple rule.Other rules I can imagine:
if
), when followed by a newline, do not accept binary operators.But I'd rather not have to reason like that, it makes the grammar really complex.
Originally posted by @nikomatsakis in #129 (comment)
The text was updated successfully, but these errors were encountered: