Skip to content

Commit 06d658c

Browse files
committed
Implemented parse() and unparse()
1 parent b125a8b commit 06d658c

19 files changed

+914
-188
lines changed

main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pynescript
77

8-
from pynescript.ast.parser.helpers import parse
8+
from pynescript.ast import parse, dump
99

1010

1111
def test_random_file():
@@ -18,10 +18,10 @@ def test_random_file():
1818
script_index = random.randrange(builtin_script_filepaths_count)
1919
script_filepath = builtin_script_filepaths[script_index]
2020

21-
res = parse(script_filepath, parse_all=True)
21+
tree = parse(script_filepath, debug=True)
2222

23-
print(res)
2423
print(script_filepath)
24+
print(dump(tree, indent=2))
2525

2626

2727
if __name__ == "__main__":

pynescript/ast/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from .types import *
2+
from .parser import *
3+
from .helpers import *

pynescript/ast/helpers.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1+
from __future__ import annotations
2+
13
from typing import Union, Optional
24

35
from pynescript.ast.types import AST
46

57

6-
def _dump_value_impl(value, indent: int = 0, depth: int = 0):
7-
indent_step = " " * indent
8+
def iter_fields(node: AST):
9+
return node.iter_fields()
10+
11+
12+
def _dump_value_impl(value, indent: str = "", depth: int = 0):
813
if isinstance(value, AST):
914
return _dump_impl(value, indent=indent, depth=depth)
1015
elif isinstance(value, list) and len(value) > 0:
1116
lines = []
1217
lines.append("[")
1318
for subvalue in value:
1419
lines.append(
15-
f"{indent_step * (depth + 1)}{_dump_value_impl(subvalue, indent=indent, depth=depth + 1)},"
20+
f"{indent * (depth + 1)}{_dump_value_impl(subvalue, indent=indent, depth=depth + 1)},"
1621
)
17-
lines.append(f"{indent_step * depth}]")
22+
lines.append(f"{indent * depth}]")
1823
return "\n".join(lines)
1924
else:
2025
return f"{value!r}"
2126

2227

23-
def _dump_impl(node: AST, indent: int = 0, depth: int = 0):
28+
def _dump_impl(node: AST, indent: str = "", depth: int = 0):
2429
class_name = node.__class__.__name__
25-
class_params_except = [
26-
"loc",
27-
"end_loc",
28-
"lineno",
29-
"col_offset",
30-
"end_lineno",
31-
"end_col_offset",
32-
]
33-
class_params = {
34-
name: value
35-
for name, value in node.__dict__.items()
36-
if name not in class_params_except
37-
}
38-
if indent > 0 and len(class_params) > 0:
39-
indent_step = " " * indent
30+
class_params = dict(iter_fields(node))
31+
if indent and len(class_params) > 0:
4032
lines = []
4133
lines.append(f"{class_name}(")
4234
for name, value in class_params.items():
4335
lines.append(
44-
f"{indent_step * (depth + 1)}{name}={_dump_value_impl(value, indent=indent, depth=depth + 1)},"
36+
f"{indent * (depth + 1)}{name}={_dump_value_impl(value, indent=indent, depth=depth + 1)},"
4537
)
46-
lines.append(f"{indent_step * depth})")
38+
lines.append(f"{indent * depth})")
4739
return "\n".join(lines)
4840
else:
4941
class_params = [f"{name}={value!r}" for name, value in class_params.items()]
5042
class_params = ", ".join(class_params)
5143
return f"{class_name}({class_params})"
5244

5345

54-
def dump(node: AST, indent: Optional[int] = None) -> str:
46+
def dump(node: AST, indent: Optional[Union[int, str]] = None) -> str:
5547
if indent is None:
56-
indent = 2
57-
48+
indent = 0
49+
if isinstance(indent, int):
50+
indent = " " * indent
5851
return _dump_impl(node, indent=indent)
5952

6053

54+
def unparse(node: AST) -> str:
55+
from pynescript.ast.node_visitors import Unparser
56+
57+
unparser = Unparser()
58+
return unparser.visit(node)
59+
60+
6161
def literal_eval(node_or_string: Union[str, AST]):
6262
raise NotImplementedError()

0 commit comments

Comments
 (0)