src/black/brackets.py
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """Builds on top of nodes.py to track brackets."""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from dataclasses import dataclass, field
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import Dict, Final, Iterable, List, Optional, Sequence, Set, Tuple, Union
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.nodes import (
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BRACKET,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | CLOSING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMPARATORS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LOGIC_OPERATORS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | MATH_OPERATORS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | OPENING_BRACKETS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | UNPACKING_PARENTS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | VARARGS_PARENTS,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | is_vararg,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | syms,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pgen2 import token
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import Leaf, Node
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # types
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LN = Union[Leaf, Node]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Depth = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LeafID = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | NodeType = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Priority = int
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMPREHENSION_PRIORITY: Final = 20
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMMA_PRIORITY: Final = 18
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | TERNARY_PRIORITY: Final = 16
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LOGIC_PRIORITY: Final = 14
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | STRING_PRIORITY: Final = 12
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | COMPARATOR_PRIORITY: Final = 10
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | MATH_PRIORITIES: Final = {
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.VBAR: 9,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.CIRCUMFLEX: 8,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.AMPER: 7,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.LEFTSHIFT: 6,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.RIGHTSHIFT: 6,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.PLUS: 5,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.MINUS: 5,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.STAR: 4,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.SLASH: 4,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.DOUBLESLASH: 4,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.PERCENT: 4,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.AT: 4,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.TILDE: 3,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | token.DOUBLESTAR: 2,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | }
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DOT_PRIORITY: Final = 1
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class BracketMatchError(Exception):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | """Raised when an opening bracket is unable to be matched to a closing bracket."""
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @dataclass
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | class BracketTracker:
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | """Keeps track of brackets on a line."""
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | depth: int = 0
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = field(default_factory=dict)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | delimiters: Dict[LeafID, Priority] = field(default_factory=dict)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | previous: Optional[Leaf] = None
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | _for_loop_depths: List[int] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | _lambda_argument_depths: List[int] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- | invisible: List[Leaf] = field(default_factory=list)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | def mark(self, leaf: Leaf) -> None:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | All leaves receive an int `bracket_depth` field that stores how deep
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | within brackets a given leaf is. 0 means there are no enclosing brackets
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | that started on this line.
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | If a leaf is itself a closing bracket and there is a matching opening
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | bracket earlier, it receives an `opening_bracket` field with which it forms a
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | pair. This is a one-directional link to avoid reference cycles. Closing
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | bracket without opening happens on lines continued from previous
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | breaks, e.g. `) -> "ReturnType":` as part of a funcdef where we place
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | the return type annotation on its own line of the previous closing RPAR.
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | If a leaf is a delimiter (a token on which Black can split the line if
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | needed) and it's on depth 0, its `id()` is stored in the tracker's
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | `delimiters` field.
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | """
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if leaf.type == token.COMMENT:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | return
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if (
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.depth == 0
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | and leaf.type in CLOSING_BRACKETS
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | and (self.depth, leaf.type) not in self.bracket_match
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | ):
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | return
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 |
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.maybe_decrement_after_for_loop_variable(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.maybe_decrement_after_lambda_arguments(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if leaf.type in CLOSING_BRACKETS:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.depth -= 1
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | try:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | except KeyError as e:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | raise BracketMatchError(
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | "Unable to match a closing bracket to the following opening"
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | f" bracket: {leaf}"
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | ) from e
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | leaf.opening_bracket = opening_bracket
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if not leaf.value:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.invisible.append(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | leaf.bracket_depth = self.depth
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if self.depth == 0:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | delim = is_split_before_delimiter(leaf, self.previous)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if delim and self.previous is not None:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.delimiters[id(self.previous)] = delim
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | else:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | delim = is_split_after_delimiter(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if delim:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.delimiters[id(leaf)] = delim
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if leaf.type in OPENING_BRACKETS:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.depth += 1
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | if not leaf.value:
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.invisible.append(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.previous = leaf
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.maybe_increment_lambda_arguments(leaf)
0059 0034 0040 0000 0014 0005 0000 05 14 008 017 014 027 025 041 0190.40 1209.59 0006.35 | self.maybe_increment_for_loop_variable(leaf)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def any_open_for_or_lambda(self) -> bool:
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return True if there is an open for or lambda expression on the line.
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | See maybe_increment_for_loop_variable and maybe_increment_lambda_arguments
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | for details."""
0006 0003 0002 0000 0003 0001 0000 05 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return bool(self._for_loop_depths or self._lambda_argument_depths)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def any_open_brackets(self) -> bool:
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Return True if there is an yet unmatched open bracket on the line."""
0003 0003 0002 0000 0000 0000 0001 05 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return bool(self.bracket_match)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> Priority:
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return the highest priority of a delimiter found on the line.
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | Values are consistent with what `is_split_*_delimiter()` return.
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | Raises ValueError on no delimiters.
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0007 0003 0002 0000 0004 0001 0000 05 03 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return max(v for k, v in self.delimiters.items() if k not in exclude)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | def delimiter_count_with_priority(self, priority: Priority = 0) -> int:
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | """Return the number of delimiters with the given `priority`.
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | If no `priority` is passed, defaults to max priority on the line.
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | """
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | if not self.delimiters:
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | return 0
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 |
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | priority = priority or self.max_delimiter_priority()
0010 0006 0005 0000 0003 0002 0000 05 05 003 004 003 005 007 008 0022.46 0042.11 0001.88 | return sum(1 for p in self.delimiters.values() if p == priority)
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """In a for loop, or comprehension, the variables are often unpacks.
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | To avoid splitting on the comma in this situation, increase the depth of
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | tokens between `for` and `in`.
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | if leaf.type == token.NAME and leaf.value == "for":
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self.depth += 1
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self._for_loop_depths.append(self.depth)
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | return True
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | """See `maybe_increment_for_loop_variable` above for explanation."""
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | if (
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | self._for_loop_depths
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | and self._for_loop_depths[-1] == self.depth
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | and leaf.type == token.NAME
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | and leaf.value == "in"
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | ):
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | self.depth -= 1
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | self._for_loop_depths.pop()
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | return True
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 |
0013 0007 0011 0000 0000 0001 0001 05 05 004 011 006 013 015 019 0074.23 0175.45 0002.36 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """In a lambda expression, there might be more than one argument.
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | To avoid splitting on the comma in this situation, increase the depth of
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | tokens between `lambda` and `:`.
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | """
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | if leaf.type == token.NAME and leaf.value == "lambda":
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self.depth += 1
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | self._lambda_argument_depths.append(self.depth)
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | return True
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 |
0012 0007 0006 0000 0004 0002 0000 05 03 003 008 004 008 011 012 0041.51 0062.27 0001.50 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | """See `maybe_increment_lambda_arguments` above for explanation."""
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | if (
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | self._lambda_argument_depths
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | and self._lambda_argument_depths[-1] == self.depth
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | and leaf.type == token.COLON
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | ):
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | self.depth -= 1
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | self._lambda_argument_depths.pop()
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | return True
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 |
0012 0007 0010 0000 0000 0001 0001 05 04 004 008 005 010 012 015 0053.77 0134.44 0002.50 | return False
---- ---- ---- ---- ---- ---- ---- 05 -- --- --- --- --- --- --- ------- ------- ------- |
0003 0003 0002 0000 0000 0000 0001 05 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def get_open_lsqb(self) -> Optional[Leaf]:
0003 0003 0002 0000 0000 0000 0001 05 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return the most recent opening square bracket (if any)."""
0003 0003 0002 0000 0000 0000 0001 05 01 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return self.bracket_match.get((self.depth - 1, token.RSQB))
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def is_split_after_delimiter(leaf: Leaf) -> Priority:
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Return the priority of the `leaf` delimiter, given a line break after it.
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | The delimiter priorities returned here are from those delimiters that would
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | cause a line break after themselves.
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | Higher numbers are higher priority.
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if leaf.type == token.COMMA:
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return COMMA_PRIORITY
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 |
0012 0005 0004 0000 0005 0003 0000 -- 02 001 002 001 002 003 003 0004.75 0002.38 0000.50 | return 0
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | def is_split_before_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> Priority:
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | """Return the priority of the `leaf` delimiter, given a line break before it.
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | The delimiter priorities returned here are from those delimiters that would
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | cause a line break before themselves.
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | Higher numbers are higher priority.
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | """
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | # * and ** might also be MATH_OPERATORS but in this case they are not.
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | # Don't treat them as a delimiter.
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return 0
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.type == token.DOT
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type not in {syms.import_from, syms.dotted_name}
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and (previous is None or previous.type in CLOSING_BRACKETS)
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return DOT_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.type in MATH_OPERATORS
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type not in {syms.factor, syms.star_expr}
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return MATH_PRIORITIES[leaf.type]
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if leaf.type in COMPARATORS:
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPARATOR_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.type == token.STRING
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous is not None
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous.type == token.STRING
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return STRING_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if leaf.type not in {token.NAME, token.ASYNC}:
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return 0
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.value == "for"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | or leaf.type == token.ASYNC
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | not isinstance(leaf.prev_sibling, Leaf)
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | or leaf.prev_sibling.value != "async"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPREHENSION_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.value == "if"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPREHENSION_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if leaf.value in {"if", "else"} and leaf.parent and leaf.parent.type == syms.test:
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return TERNARY_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if leaf.value == "is":
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPARATOR_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.value == "in"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type in {syms.comp_op, syms.comparison}
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and not (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | previous is not None
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous.type == token.NAME
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous.value == "not"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | )
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPARATOR_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | leaf.value == "not"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and leaf.parent.type == syms.comp_op
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and not (
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | previous is not None
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous.type == token.NAME
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | and previous.value == "is"
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | )
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | ):
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return COMPARATOR_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | if leaf.value in LOGIC_OPERATORS and leaf.parent:
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return LOGIC_PRIORITY
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 |
0094 0030 0072 0002 0005 0015 0002 -- 42 010 066 049 108 076 157 0980.92 8025.75 0008.18 | return 0
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | def max_delimiter_priority_in_atom(node: LN) -> Priority:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | """Return maximum delimiter priority inside `node`.
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 |
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | This is specific to atoms with contents contained in a pair of parentheses.
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | If `node` isn't an atom or there are no enclosing parentheses, returns 0.
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | """
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | if node.type != syms.atom:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | return 0
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 |
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | first = node.children[0]
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | last = node.children[-1]
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | if not (first.type == token.LPAR and last.type == token.RPAR):
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | return 0
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 |
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | bt = BracketTracker()
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | for c in node.children[1:-1]:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | if isinstance(c, Leaf):
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | bt.mark(c)
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | else:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | for leaf in c.leaves():
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | bt.mark(leaf)
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | try:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | return bt.max_delimiter_priority()
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 |
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | except ValueError:
0026 0019 0018 0000 0004 0004 0000 -- 08 005 008 007 011 013 018 0066.61 0228.96 0003.44 | return 0
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | def get_leaves_inside_matching_brackets(leaves: Sequence[Leaf]) -> Set[LeafID]:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | """Return leaves that are inside matching brackets.
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 |
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | The input `leaves` can have non-matching brackets at the head or tail parts.
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | Matching brackets are included.
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | """
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | try:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | # Start with the first opening bracket and ignore closing brackets before.
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | start_index = next(
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | i for i, l in enumerate(leaves) if l.type in OPENING_BRACKETS
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | )
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | except StopIteration:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | return set()
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | bracket_stack = []
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | ids = set()
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | for i in range(start_index, len(leaves)):
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | leaf = leaves[i]
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | if leaf.type in OPENING_BRACKETS:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | bracket_stack.append((BRACKET[leaf.type], i))
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | if leaf.type in CLOSING_BRACKETS:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | if bracket_stack and leaf.type == bracket_stack[-1][0]:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | _, start = bracket_stack.pop()
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | for j in range(start, i + 1):
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | ids.add(id(leaves[j]))
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | else:
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | break
0027 0020 0021 0001 0004 0001 0001 -- 10 005 008 007 013 013 020 0074.01 0300.66 0004.06 | return ids