Skip to content

Commit

Permalink
Copy parser._Matches to parser_libcst._Matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen152 committed Oct 20, 2022
1 parent e53ce6c commit e3683d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 0 additions & 1 deletion pytype/directors/directors.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def __init__(self, src_tree, errorlog, filename, disable, code):
self._function_ranges = _BlockRanges({})
# Parse the source code for directives.
self._parse_src_tree(src_tree, code)
self._matches = None

@property
def type_comments(self):
Expand Down
23 changes: 23 additions & 0 deletions pytype/directors/parser_libcst.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ def __repr__(self):
"""


class _Matches:
"""Tracks branches of match statements."""

def __init__(self):
self.start_to_end = {}
self.end_to_starts = collections.defaultdict(list)
self.match_cases = {}

def add_match(self, start, end, cases):
self.start_to_end[start] = end
self.end_to_starts[end].append(start)
for case_start, case_end in cases:
for i in range(case_start, case_end + 1):
self.match_cases[i] = start

def __repr__(self):
return f"""
Matches: {sorted(self.start_to_end.items())}
Cases: {self.match_cases}
"""


class _ParseVisitor(libcst.CSTVisitor):
"""Visitor for parsing a source tree.
Expand Down Expand Up @@ -110,6 +132,7 @@ def __init__(self):
self.defs_start = None
self.function_ranges = {}
self.block_returns = _BlockReturns()
self.matches = _Matches()

def _get_containing_groups(self, start_line, end_line=None):
"""Get _StructuredComment groups that fully contain the given line range."""
Expand Down

0 comments on commit e3683d8

Please sign in to comment.