-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
parser: detect mismatched/misleading indentation #13593
Conversation
5d6e8f4
to
5417cb2
Compare
A major concern with this change is that it would kill any use of Though on the positive side of things, this would stop pesky if (a)
...;
...; issues, which might be well worth it. |
Since I just implemented this tonight, I haven't gotten a realistic picture of how painful this will actually be to use. But one annoyance I foresee is that some editors (including the one I use) are bad at issuing the correct indentation level for the next line when you hit "enter". Which means some annoying friction despite the IDE experience. Looking forward to giving this a shot and seeing what it's like, regardless. |
If running However, if |
In order to avoid friction for developers this should allow as many indentation styles as possible.
function(
x,
y,
z
);
x = a
+ b
+ c;
{
x= y;
// y = z
}
const lambda = struct { fn xyz() void {
code();
}}; |
We should probably keep discourse on the feature itself on the original issue (#35) and keep this PR for implementation/review. But to quickly answer your questions @IntegratedQuantum:
Column is counted as byte offset after a newline or file start, so yes.
No, because there is no way to know tab width.
These are all unaffected. This only affects indentation of statements relative to each other, and your examples are all either single statements or not statements at all.
I think you should look again at the code and test cases, your example is allowed by this change. |
|
||
const parent_block_tok = p.current_block_indent_defining_token; | ||
defer p.current_block_indent_defining_token = parent_block_tok; | ||
const parent_indent = p.tokenColumn(parent_block_tok); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor performance note: This leads to some redundant scanning for the token column, but knowing the token (not just column) is useful for the error note. An alternative is to have Parser
store a tuple of token, column
like var sibling
below.
@andrewrk Let me know if you want me to rebase to get the new CI checks, or if you plan to wait till after 0.10.1 before reviewing this. No rush of course :) |
5417cb2
to
a21726e
Compare
Thanks for working on this! Here are the next steps to getting it merged:
|
Fixes #35
In the spirit of alerting the author to ambiguities or mistakes instead of presumptively fixing it, I added this to the parsing phase. This is similar to how mismatched whitespace around binary operators causes a parse error instead of being reformatted.
Notes
The original issue is light on details, but my interpretation is that potential programmer mistakes arise from sequences of statements, not container decls. So I made this indentation error totally independent per container. Blocks are only considered relative to its container; a sub-container and its contents may be out-dented relative to its parent.
If it is better to have statements in nested containers require monotonic indentation, that is an easy fix, just let me know!
This feature also ignores decls and fields, since that is not likely a source of ambiguity/bugs.
This adds one new field to
Parser
.