Skip to content

Commit

Permalink
Added comments and removed conversion to list
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 3, 2024
1 parent f76bcd7 commit 88fa594
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tested/dsl/ast_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,13 @@ def extract_comment(code: str) -> str:
"""
comment = ""
tokens = tokenize.generate_tokens(io.StringIO(code).readline)
candidate = list(collections.deque(tokens, maxlen=3))
if len(candidate) and candidate[0].type == tokenize.COMMENT:
comment = candidate[0].string.lstrip("#").strip()
# The "maxlen" is 3 because, the tokenizer will always generate a NEWLINE
# and ENDMARKER token at the end. So a comment comes just before those 2 tokens.
candidates = collections.deque(tokens, maxlen=3)
if len(candidates):
candidate = candidates.popleft()
if candidate.type == tokenize.COMMENT:
comment = candidate.string.lstrip("#").strip()
return comment


Expand Down

0 comments on commit 88fa594

Please sign in to comment.