Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
updates for formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishMahendra committed Nov 16, 2023
1 parent a38574e commit c433385
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 314 deletions.
25 changes: 25 additions & 0 deletions jaclang/jac/absyntree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,31 @@ class Semi(Token, CodeBlockStmt):
class CommentToken(Token):
"""CommentToken node type for Jac Ast."""

def __init__(
self,
file_path: str,
name: str,
value: str,
line: int,
col_start: int,
col_end: int,
pos_start: int,
pos_end: int,
kid: Sequence[AstNode],
is_inline: bool = False,
) -> None:
"""Initialize token."""
self.file_path = file_path
self.name = name
self.value = value
self.line_no = line
self.c_start = col_start
self.c_end = col_end
self.pos_start = pos_start
self.pos_end = pos_end
self.is_inline = is_inline
AstNode.__init__(self, kid=kid)


# ----------------
class JacSource(EmptyToken):
Expand Down
48 changes: 28 additions & 20 deletions jaclang/jac/passes/tool/fuse_comments_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,36 @@ def exit_node(self, node: ast.AstNode) -> None:

def after_pass(self) -> None:
"""Insert comment tokens into all_tokens."""
marker = 0
chomp: list[ast.CommentToken] = [*self.comments]
new_tokens: list[ast.Token] = []
if not len(chomp):
return
for i in range(len(self.all_tokens)):
new_tokens.append(self.all_tokens[i])
if i >= len(self.all_tokens) - 1:
break
if marker >= len(self.comments):
new_tokens.extend(self.all_tokens[i + 1 :])
chomp[0].is_inline = chomp[0].is_inline or (
self.all_tokens[i].loc.first_line == chomp[0].loc.first_line
)
# print(chomp[0].is_inline)
if i == len(self.all_tokens) - 1:
if len(chomp):
new_tokens.append(self.all_tokens[i])
new_tokens += chomp
break
elif (
self.all_tokens[i].loc.first_line
<= self.comments[marker].loc.first_line
or self.all_tokens[i].loc.col_start
<= self.comments[marker].loc.col_start
) and (
self.all_tokens[i + 1].loc.last_line
>= self.comments[marker].loc.last_line
or self.all_tokens[i + 1].loc.col_end
>= self.comments[marker].loc.col_end
while (i < len(self.all_tokens) - 1) and (
(
self.all_tokens[i].loc.first_line == chomp[0].loc.first_line
and self.all_tokens[i].loc.col_start > chomp[0].loc.col_start
)
or (self.all_tokens[i].loc.first_line > chomp[0].loc.first_line)
):
new_tokens.append(self.comments[marker])
marker += 1

# print(chomp[0].is_inline)
new_tokens.append(chomp[0])
chomp = chomp[1:]
if not len(chomp):
new_tokens.extend(self.all_tokens[i + 1 :])
break
new_tokens.append(self.all_tokens[i])
if not len(chomp):
break
for i in range(len(new_tokens)):
if isinstance(new_tokens[i], ast.CommentToken):
if i == 0:
Expand All @@ -59,7 +66,8 @@ def after_pass(self) -> None:
new_val = new_tokens[i - 1]
if new_val.parent is not None:
new_kids = new_val.parent.kid
new_kids.insert(new_kids.index(new_val), new_tokens[i])
new_kids.insert(new_kids.index(new_val) + 1, new_tokens[i])
new_val.parent.set_kids(new_kids)
else:
new_val.print()
raise self.ice("Token without parent in AST should be impossible")
Loading

0 comments on commit c433385

Please sign in to comment.