Skip to content

Commit

Permalink
Merge pull request #203 from UQ-PAC/format-adt-aslt
Browse files Browse the repository at this point in the history
patch format_adt.py for aslt format.
  • Loading branch information
ailrst authored Jun 4, 2024
2 parents 9292109 + 4492847 commit 104f4aa
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions scripts/format_adt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
notspace_re = re.compile(rb'\S')
head_re = re.compile(rb'[^\s(]+')
num_re = re.compile(rb'[_0-9xa-fA-F]+')
string_re = re.compile(rb'"(?:[^"\\]|\\.)*"')
string_re = re.compile(rb'''"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*\'''')

@dataclasses.dataclass
class Context:
Expand Down Expand Up @@ -79,8 +79,8 @@ def pretty(outfile, data: bytes, spaces: int):
assert m
outfile.write(m[0])
i = m.end(0)
elif c == b',':
outfile.write(b',')
elif c in b',;':
outfile.write(c)
i += 1
if stack[-1].multiline:
outfile.write(b'\n')
Expand All @@ -97,11 +97,14 @@ def pretty(outfile, data: bytes, spaces: int):
outfile.write(c)
i += 1
islist = c == b'[' and ']' != chr(data[i])
multiline = islist or head in (b'Project', b'Def', b'Goto', b'Call', b'Sub', b'Blk', b'Arg')
multiline = islist or head in (b'Project', b'Def', b'Goto', b'Call', b'Sub', b'Blk', b'Arg') or head.startswith(b'Stmt_') or head in (b'Expr_TApply', b'Expr_Slices')
if multiline:
depth += 1
outfile.write(b'\n')
outfile.write(indent * depth)
if islist and stack and stack[-1].multiline:
outfile.write(indent[1:])
else:
outfile.write(b'\n')
outfile.write(indent * depth)

stack.append(Context(i, flip[c], multiline))
elif c in b')]':
Expand All @@ -115,7 +118,7 @@ def pretty(outfile, data: bytes, spaces: int):
depth -= 1
if not stack:
outfile.write(b'\n')
elif c == b'"':
elif c in b'"\'':
string = string_re.match(data, i)
if not string:
raise ValueError(f"unclosed string beginning at byte {i+1}.")
Expand Down

0 comments on commit 104f4aa

Please sign in to comment.