pico_unminify
Functions to unminify/beautify/format pico8 code from a syntax tree
1"""Functions to unminify/beautify/format pico8 code from a syntax tree""" 2 3from utils import * 4from pico_tokenize import TokenType, Token 5from pico_parse import NodeType, is_function_stmt, is_short_block_stmt 6 7def unminify_code(root, unminify_opts): 8 indent_str = unminify_opts.get("indent", 2) 9 10 output = [] 11 prev_token = Token.none 12 prev_tight = False 13 indent = 0 14 curr_stmt = None 15 stmt_stack = [] 16 17 k_tight_prefix_tokens = ("(", "[", "{", "?", ".", ":", "::") 18 k_tight_suffix_tokens = (")", "]", "}", ",", ";", ".", ":", "::") 19 20 def visit_token(token): 21 nonlocal prev_token, prev_tight 22 value = token.value 23 24 for comment in token.children: 25 comment_value = comment.value 26 if "\n" in comment_value: 27 if prev_tight: 28 output.append("\n") 29 output.append(indent_str * indent) 30 output.append(comment_value) 31 if not comment_value.endswith("\n"): 32 output.append("\n") 33 output.append(indent_str * indent) 34 else: 35 if prev_tight and prev_token.value not in k_tight_prefix_tokens: 36 output.append(" ") 37 output.append(comment_value) 38 prev_tight = False 39 40 if token.value is None: 41 return 42 43 # ignore shorthand parens, to avoid increasing token count as we convert shorthands to longhand 44 gparent = token.parent.parent 45 if gparent and is_short_block_stmt(gparent) and token.parent == gparent.cond and token.value in ("(", ")"): 46 return 47 48 # ignore semicolons inside blocks - our formatting makes them unneeded 49 if token.parent.type == NodeType.block and token.value == ";": 50 return 51 52 # undo ugly token replacements 53 if token.value == "do" and token.parent.type in (NodeType.if_, NodeType.elseif): 54 value = "then" 55 56 if prev_tight and prev_token.value not in k_tight_prefix_tokens and \ 57 token.value not in k_tight_suffix_tokens and \ 58 not (token.value in ("(", "[") and (prev_token.type == TokenType.ident or 59 prev_token.value in ("function", ")", "]", "}"))) and \ 60 not (prev_token.type == TokenType.punct and prev_token.parent.type == NodeType.unary_op): 61 output.append(" ") 62 63 output.append(value) 64 prev_token = token 65 prev_tight = True 66 67 def visit_node(node): 68 nonlocal indent, curr_stmt, prev_tight 69 70 if node.type == NodeType.block: 71 if node.parent: 72 indent += 1 73 # shorthand -> longhand 74 if is_short_block_stmt(node.parent) and node.parent.type != NodeType.else_: 75 output.append(" then" if node.parent.type == NodeType.if_ else " do") 76 77 stmt_stack.append(curr_stmt) 78 curr_stmt = None 79 output.append("\n") 80 prev_tight = False 81 82 elif curr_stmt is None: 83 if is_function_stmt(node): 84 child_i = node.parent.children.index(node) 85 if child_i > 0 and not is_function_stmt(node.parent.children[child_i - 1]): 86 output.append("\n") 87 88 curr_stmt = node 89 output.append(indent_str * indent) 90 prev_tight = False 91 92 def end_visit_node(node): 93 nonlocal indent, curr_stmt, prev_tight 94 95 if node.type == NodeType.block: 96 if node.parent: 97 indent -= 1 98 99 curr_stmt = stmt_stack.pop() 100 output.append(indent_str * indent) 101 prev_tight = False 102 103 # shorthand -> longhand 104 if node.parent and is_short_block_stmt(node.parent) and not (node.parent.type == NodeType.if_ and node.parent.else_): 105 output.append("end") 106 107 elif node is curr_stmt: 108 curr_stmt = None 109 output.append("\n") 110 prev_tight = False 111 112 if is_function_stmt(node): 113 output.append("\n") 114 115 root.traverse_nodes(visit_node, end_visit_node, tokens=visit_token) 116 117 return "".join(output)
def
unminify_code(root, unminify_opts):
8def unminify_code(root, unminify_opts): 9 indent_str = unminify_opts.get("indent", 2) 10 11 output = [] 12 prev_token = Token.none 13 prev_tight = False 14 indent = 0 15 curr_stmt = None 16 stmt_stack = [] 17 18 k_tight_prefix_tokens = ("(", "[", "{", "?", ".", ":", "::") 19 k_tight_suffix_tokens = (")", "]", "}", ",", ";", ".", ":", "::") 20 21 def visit_token(token): 22 nonlocal prev_token, prev_tight 23 value = token.value 24 25 for comment in token.children: 26 comment_value = comment.value 27 if "\n" in comment_value: 28 if prev_tight: 29 output.append("\n") 30 output.append(indent_str * indent) 31 output.append(comment_value) 32 if not comment_value.endswith("\n"): 33 output.append("\n") 34 output.append(indent_str * indent) 35 else: 36 if prev_tight and prev_token.value not in k_tight_prefix_tokens: 37 output.append(" ") 38 output.append(comment_value) 39 prev_tight = False 40 41 if token.value is None: 42 return 43 44 # ignore shorthand parens, to avoid increasing token count as we convert shorthands to longhand 45 gparent = token.parent.parent 46 if gparent and is_short_block_stmt(gparent) and token.parent == gparent.cond and token.value in ("(", ")"): 47 return 48 49 # ignore semicolons inside blocks - our formatting makes them unneeded 50 if token.parent.type == NodeType.block and token.value == ";": 51 return 52 53 # undo ugly token replacements 54 if token.value == "do" and token.parent.type in (NodeType.if_, NodeType.elseif): 55 value = "then" 56 57 if prev_tight and prev_token.value not in k_tight_prefix_tokens and \ 58 token.value not in k_tight_suffix_tokens and \ 59 not (token.value in ("(", "[") and (prev_token.type == TokenType.ident or 60 prev_token.value in ("function", ")", "]", "}"))) and \ 61 not (prev_token.type == TokenType.punct and prev_token.parent.type == NodeType.unary_op): 62 output.append(" ") 63 64 output.append(value) 65 prev_token = token 66 prev_tight = True 67 68 def visit_node(node): 69 nonlocal indent, curr_stmt, prev_tight 70 71 if node.type == NodeType.block: 72 if node.parent: 73 indent += 1 74 # shorthand -> longhand 75 if is_short_block_stmt(node.parent) and node.parent.type != NodeType.else_: 76 output.append(" then" if node.parent.type == NodeType.if_ else " do") 77 78 stmt_stack.append(curr_stmt) 79 curr_stmt = None 80 output.append("\n") 81 prev_tight = False 82 83 elif curr_stmt is None: 84 if is_function_stmt(node): 85 child_i = node.parent.children.index(node) 86 if child_i > 0 and not is_function_stmt(node.parent.children[child_i - 1]): 87 output.append("\n") 88 89 curr_stmt = node 90 output.append(indent_str * indent) 91 prev_tight = False 92 93 def end_visit_node(node): 94 nonlocal indent, curr_stmt, prev_tight 95 96 if node.type == NodeType.block: 97 if node.parent: 98 indent -= 1 99 100 curr_stmt = stmt_stack.pop() 101 output.append(indent_str * indent) 102 prev_tight = False 103 104 # shorthand -> longhand 105 if node.parent and is_short_block_stmt(node.parent) and not (node.parent.type == NodeType.if_ and node.parent.else_): 106 output.append("end") 107 108 elif node is curr_stmt: 109 curr_stmt = None 110 output.append("\n") 111 prev_tight = False 112 113 if is_function_stmt(node): 114 output.append("\n") 115 116 root.traverse_nodes(visit_node, end_visit_node, tokens=visit_token) 117 118 return "".join(output)