-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add to allow use comments in other scopes that is not from a fu…
…nction
- Loading branch information
Showing
5 changed files
with
54 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
from norminette.rules import Rule | ||
|
||
allowed_on_comment = ["COMMENT", "MULT_COMMENT", "SPACE", "TAB"] | ||
|
||
|
||
class CheckComment(Rule): | ||
def __init__(self): | ||
super().__init__() | ||
self.depends_on = [] | ||
|
||
def run(self, context): | ||
""" | ||
Comments are only allowed in GlobalScope. | ||
Comments are forbidden inside functions and in the middle of instructions. | ||
""" | ||
i = context.skip_ws(0) | ||
has_comment = False | ||
while ( | ||
context.peek_token(i) is not None | ||
and context.check_token(i, "NEWLINE") is False | ||
): | ||
if context.check_token(i, allowed_on_comment) is False: | ||
if has_comment is True: | ||
context.new_error("COMMENT_ON_INSTR", context.peek_token(i)) | ||
return True, i | ||
elif context.check_token(i, ["COMMENT", "MULT_COMMENT"]) is True: | ||
if ( | ||
context.scope.name != "GlobalScope" | ||
or context.history[-1] == "IsFuncDeclaration" | ||
): | ||
context.new_error("WRONG_SCOPE_COMMENT", context.peek_token(i)) | ||
has_comment = True | ||
|
||
tokens = [] | ||
while context.peek_token(i) and not context.check_token(i, "NEWLINE"): | ||
token = context.peek_token(i) | ||
tokens.append(token) | ||
i += 1 | ||
i = context.skip_ws(0) | ||
return False, 0 | ||
|
||
for index, token in enumerate(tokens): | ||
if token.type in ("COMMENT", "MULT_COMMENT"): | ||
if self.is_inside_a_function(context): | ||
context.new_error("WRONG_SCOPE_COMMENT", token) | ||
if index == 0 or self.is_last_token(token, tokens[index+1:]): | ||
continue | ||
context.new_error("COMMENT_ON_INSTR", token) | ||
|
||
def is_inside_a_function(self, context): | ||
if context.history[-2:] == ["IsFuncDeclaration", "IsBlockStart"]: | ||
return True | ||
if context.scope.__class__.__name__.lower() == "function": | ||
return True | ||
# Sometimes the context scope is a `ControlStructure` scope instead of | ||
# `Function` scope, so, to outsmart this bug, we need check manually | ||
# the `context.history`. | ||
last = None | ||
for index, record in enumerate(reversed(context.history)): | ||
if record == "IsFuncDeclaration" and last == "IsBlockStart": | ||
# Since the limited history API, we can't say if we're in a | ||
# nested function to reach the first enclosing function, so, | ||
# we'll consider that the user just declared a normal function | ||
# in global scope. | ||
stack = 1 | ||
index -= 1 # Jumps to next record after `IsBlockStart` | ||
while index > 0 and stack > 0: | ||
record = context.history[-index] | ||
index -= 1 | ||
if record not in ("IsBlockStart", "IsBlockEnd"): | ||
continue | ||
stack = stack + (1, -1)[record == "IsBlockEnd"] | ||
return bool(stack) | ||
last = record | ||
return False | ||
|
||
def is_last_token(self, token, foward): | ||
expected = ("SPACE", "TAB") | ||
if token.type == "MULT_COMMENT": | ||
expected += ("COMMENT", "MULT_COMMENT") | ||
return all(it.type in ("SPACE", "TAB", "COMMENT", "MULT_COMMENT") for it in foward) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters