-
Notifications
You must be signed in to change notification settings - Fork 141
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
If..Else Inside A While Scope #419
Comments
Hi @yumamur! You should check the following link. This is not a bug, an intended behavior. If you use an if statement in a while statement, it is obvious that the while statement contains more than two lines. Lines 219 to 220 in af5bf14
|
Hi @kiokuless. Thanks for the addition, I reported the error unaware of this fact.
|
Yes, your opinion is better.
By the way, I guess it looks like a lot of work to implement this with the current norminette. Because the current norminette implementation is looking at the token sequence by lexical analysis, and doesn't do syntactic analysis. So it seems to be difficult to detect this... |
I believe we can solve this problem just by viewing the history, if one statement (flow control) is inside another one, we show the error. But, history is extremely limited to do this, we just store strings in history instead of Example: class CheckStatements(Rule):
# New design
def __init__(self, context, scope):
self.context = context
self.scope = scope
def run(self, context): # Context to compatibility
if not self.is_statement(context):
return
if last := context.history.last:
if last.scope != self.scope:
context.new_error("MORE_THAN_1_LINE", context.peek_token(0))
def is_statement(self, context):
... |
I came up with a simpler implementation. It is to check the first token of the statement part of if/if-else/while statements.
Since the control structures allowed in norminette are only if/if-else/while, this approach looks good to me. Is there anything that could be left out? |
@kiokuless, I believe that the problem cannot be solved with lexical analysis (checking if the first token after the conditional of an With your solution, how do you plan to allow the first void main(void)
{
if (1)
return 1;
if (1)
if (2)
return 2;
} |
When an if statement exists in a while scope, which is not delimited by curly brackets, Norminette expects the else of the if to be ordinated with the while.
Erroneous code
The text was updated successfully, but these errors were encountered: