Skip to content

Commit

Permalink
Minor refactoring and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWeber42 committed Jun 17, 2020
1 parent 3e23548 commit fd6994a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
9 changes: 3 additions & 6 deletions dusk/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def loc_from_node(self, node):

class Matcher(ABC):
@abstractmethod
def match(self, ast) -> None:
def match(self, ast, **kwargs) -> None:
raise NotImplementedError


Expand Down Expand Up @@ -132,6 +132,7 @@ def __init__(self, matcher, name: str = None, is_list: bool = False) -> None:
def match(self, node, capturer=None, **kwargs) -> None:
if capturer is not None and self.name is not None:
if not self.is_list:
# TODO: throw if value already exists?
capturer[self.name] = node
else:
capturer.setdefault(self.name, []).append(node)
Expand Down Expand Up @@ -159,11 +160,7 @@ def __init__(self, matcher, active=True):

def match(self, node, **kwargs):
if self.active:
from dusk.util import pprint_matcher

def pprint(node):

print(pprint_matcher(node))
from dusk.util import pprint_matcher as pprint

breakpoint()

Expand Down
11 changes: 7 additions & 4 deletions dusk/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
def pprint_matcher(
def pprint_matcher(node):
print(matcher_to_str(node))


def matcher_to_str(
node, indent_nr: int = 0, indent: str = " ", first_line_prefix=None
) -> str:
# TODO: better name
ind = indent * indent_nr
ind1 = indent * (indent_nr + 1)

Expand All @@ -18,7 +21,7 @@ def pprint_matcher(
out += first_line_prefix + type(node).__name__ + "(\n"

for field in node._fields:
out += pprint_matcher(
out += matcher_to_str(
getattr(node, field),
indent_nr=indent_nr + 1,
indent=indent,
Expand All @@ -32,7 +35,7 @@ def pprint_matcher(
out = ""
out += first_line_prefix + "[\n"
for elem in node:
out += pprint_matcher(elem, indent_nr=indent_nr + 1, indent=indent)
out += matcher_to_str(elem, indent_nr=indent_nr + 1, indent=indent)
out += ind + "]\n"

return out
Expand Down

0 comments on commit fd6994a

Please sign in to comment.