Skip to content

Commit e7ddd85

Browse files
Merge pull request #35 from MarcellPerger1/start-ast
fix: Add all classes to nodes.py `__all__`
2 parents 4de2be3 + 63022c8 commit e7ddd85

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

parser/common.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import TypeAlias, Sequence
2+
3+
from .lexer.tokens import Token
4+
from .str_region import StrRegion
5+
from .trees.base_node import AnyNode
6+
7+
RegionUnionFlatT: TypeAlias = Token | AnyNode | StrRegion
8+
RegionUnionArgT: TypeAlias = RegionUnionFlatT | Sequence[RegionUnionFlatT]
9+
10+
11+
def region_union(*args: RegionUnionArgT):
12+
regs = []
13+
for loc in args:
14+
if isinstance(loc, (Token, AnyNode)):
15+
loc = loc.region # Token and AnyNode, both have `.region`
16+
if isinstance(loc, StrRegion):
17+
regs.append(loc)
18+
else:
19+
assert not isinstance(loc, str)
20+
regs.append(region_union(*loc))
21+
return StrRegion.union(*regs)

parser/cst/nodes.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
register_corresponding_token)
88

99
__all__ = [
10-
"NumberNode", "StringNode", "AnyNameLeaf", "IdentNode", "AttrNameNode",
11-
"AutocatNode", "GetattrNode", "GetitemNode", "ParenNode", "CallNode",
12-
"CallArgs", "NopNode", "BlockNode", "ConditionalBlock", "IfBlock",
10+
"ProgramNode", "NumberNode", "StringNode", "AnyNameLeaf", "IdentNode",
11+
"AttrNameNode", "AutocatNode", "GetattrNode", "GetitemNode", "ParenNode",
12+
"CallNode", "CallArgs", "OperatorNode", "UnaryOpNode", "UPlusNode",
13+
"UMinusNode", "NotNode", "BinOpNode", "AddNode", "SubNode", "MulNode",
14+
"DivNode", "ModNode", "PowNode", "ConcatNode", "AndNode", "OrNode",
15+
"ComparisonNode", "EqNode", "NeqNode", "LtNode", "LeNode", "GtNode",
16+
"GeNode", "NopNode", "BlockNode", "ConditionalBlock", "IfBlock",
1317
"ElseIfBlock", "ElseBlock", "NullElseBlock", "WhileBlock", "RepeatBlock",
1418
"DefineNode", "ArgsDeclNode", "ArgDeclNode", "DeclItemNode", "LetNode",
15-
"GlobalNode", "ProgramNode",
19+
"GlobalNode", "AssignOpNode", "AssignNode", "AddEqNode", "SubEqNode",
20+
"MulEqNode", "DivEqNode", "ModEqNode", "PowEqNode", "ConcatEqNode",
21+
"AndEqNode", "OrEqNode",
1622
]
1723

1824

parser/cst/treegen.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

3-
from typing import (TypeVar, cast, TypeAlias, Sequence, overload, Iterable, Callable)
3+
from typing import (TypeVar, cast, Sequence, overload, Iterable, Callable)
44

55
from .nodes import *
66
from .token_matcher import OpM, KwdM, Matcher, PatternT
7+
from ..common import region_union, RegionUnionArgT
78
from ..error import BaseParseError, BaseLocatedError
89
from ..lexer import Tokenizer
910
from ..operators import UNARY_OPS, COMPARISONS, ASSIGN_OPS
@@ -525,27 +526,14 @@ def _parse_or_bool(self, idx: int) -> tuple[AnyNode, int]:
525526
return self._parse_ltr_operator_level(idx, ('||',), self._parse_and_bool)
526527

527528
def err(self, msg: str, loc: RegionUnionArgT):
528-
return LocatedCstError(msg, self.region_union(loc), self.src)
529-
530-
@classmethod
531-
def region_union(cls, *args: RegionUnionArgT):
532-
regs = []
533-
for loc in args:
534-
if isinstance(loc, (Token, AnyNode)):
535-
loc = loc.region # Token and AnyNode, both have `.region`
536-
if isinstance(loc, StrRegion):
537-
regs.append(loc)
538-
else:
539-
assert not isinstance(loc, str)
540-
regs.append(cls.region_union(*loc))
541-
return StrRegion.union(*regs)
529+
return LocatedCstError(msg, region_union(loc), self.src)
542530

543531
@classmethod
544532
def node_from_children(cls, name_or_type: str | type[AnyNamedNode],
545533
children: list[AnyNode],
546534
region: RegionUnionArgT = None,
547535
parent: Node = None, arity: int = None):
548-
region = cls.region_union(region if region is not None else children)
536+
region = region_union(region if region is not None else children)
549537
if isinstance(name_or_type, str):
550538
klass = node_cls_from_name(name_or_type, children, arity)
551539
else:
@@ -555,9 +543,6 @@ def node_from_children(cls, name_or_type: str | type[AnyNamedNode],
555543

556544
CstGen = TreeGen
557545

558-
RegionUnionFlatT: TypeAlias = 'Token | AnyNode | StrRegion'
559-
RegionUnionArgT: TypeAlias = 'RegionUnionFlatT | Sequence[RegionUnionFlatT]'
560-
561546

562547
# operator precedence (most to least binding):
563548
# 1. () [] {} (parens)

0 commit comments

Comments
 (0)