Skip to content

Commit 05a2e87

Browse files
Merge pull request #37 from MarcellPerger1/start-ast
Reduce shared trees code, new `common` directory
2 parents e7ddd85 + 1ef84ae commit 05a2e87

File tree

19 files changed

+47
-34
lines changed

19 files changed

+47
-34
lines changed

fuzz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from parser.lexer.tokenizer import Tokenizer
44
from parser.cst.treegen import TreeGen
5-
from parser.error import BaseParseError
5+
from parser.common.error import BaseParseError
66

77
from pythonfuzz.fuzzer import Fuzzer
88
import pythonfuzz.fuzzer as fuzzer_ns # For patching pythonfuzz

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from util import readfile
55
from parser.cst.treegen import TreeGen
66
from parser.lexer import Tokenizer, print_tokens
7-
from parser.trees.tree_print import tprint
7+
from parser.common.tree_print import tprint
88

99

1010
def make_tree(src: str):

parser/common/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .common import *
2+
from .error import * # <^ might add some stuff so `import *`
3+
from .str_region import StrRegion # <-- won't add stuff to str_region so not `import *`
4+
from .tree_print import *
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
from typing import TypeAlias, Sequence
22

3-
from .lexer.tokens import Token
43
from .str_region import StrRegion
5-
from .trees.base_node import AnyNode
64

7-
RegionUnionFlatT: TypeAlias = Token | AnyNode | StrRegion
5+
__all__ = ['HasRegion', 'RegionUnionArgT', 'region_union']
6+
7+
8+
class HasRegion:
9+
region: StrRegion
10+
11+
12+
RegionUnionFlatT: TypeAlias = HasRegion | StrRegion
813
RegionUnionArgT: TypeAlias = RegionUnionFlatT | Sequence[RegionUnionFlatT]
914

1015

1116
def region_union(*args: RegionUnionArgT):
1217
regs = []
1318
for loc in args:
14-
if isinstance(loc, (Token, AnyNode)):
15-
loc = loc.region # Token and AnyNode, both have `.region`
19+
if getattr(loc, 'region', None) is not None: # Duck-type HasRegion
20+
loc = loc.region
1621
if isinstance(loc, StrRegion):
1722
regs.append(loc)
1823
else:

parser/error.py renamed to parser/common/error.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
from dataclasses import replace as d_replace
77
from typing import Sequence
88

9+
from .common import HasRegion
910
from .str_region import StrRegion
1011

12+
__all__ = ['BaseParseError', 'BaseLocatedError']
13+
1114

1215
# We unconditionally add this polyfill to ensure that
1316
# exceptions are always part of str(exc).
@@ -67,7 +70,7 @@ class BaseParseError(_PolyfillAddNoteMixin, Exception):
6770
pass
6871

6972

70-
class BaseLocatedError(BaseParseError):
73+
class BaseLocatedError(BaseParseError, HasRegion):
7174
__notes__: list[str] # but might not exist
7275

7376
def __init__(self, msg: str, region: StrRegion, src: str):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from dataclasses import dataclass
44

5+
__all__ = ['StrRegion']
6+
57

68
@dataclass
79
class StrRegion:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from io import StringIO
55
from typing import IO, Sequence
66

7-
from .base_node import Leaf, AnyNode, Node
8-
from parser.str_region import StrRegion
7+
from ..cst.base_node import Leaf, AnyNode, Node
8+
from .str_region import StrRegion
99

1010
__all__ = [
1111
'TreePrinter', 'tree_print', 'tree_format', 'tprint', 'tformat'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from dataclasses import dataclass, field
44

5+
from ..common import StrRegion, HasRegion
56
from ..tokens import Token
6-
from ..str_region import StrRegion
77

88

99
@dataclass
10-
class Leaf:
10+
class Leaf(HasRegion):
1111
name: str
1212
region: StrRegion
1313
parent: Node | None = None
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from typing import TYPE_CHECKING, overload, Sequence, cast, Literal
55

66
from .base_node import Leaf, AnyNode, Node
7-
from parser.str_region import StrRegion
7+
from ..common import StrRegion
88

99
if TYPE_CHECKING:
10-
from parser.tokens import Token
10+
from ..tokens import Token
1111

1212

1313
# NOTE: these classes only follow the Liskov substitution principle on

parser/cst/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import cast
44

55
from util import checked_cast
6-
from ..trees.named_node import (NamedLeafCls, NamedNodeCls, NamedSizedNodeCls,
7-
register_corresponding_token)
6+
from .named_node import (NamedLeafCls, NamedNodeCls, NamedSizedNodeCls,
7+
register_corresponding_token)
88

99
__all__ = [
1010
"ProgramNode", "NumberNode", "StringNode", "AnyNameLeaf", "IdentNode",

0 commit comments

Comments
 (0)