All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 String transformers that can split and merge strings.
4 from abc import ABC, abstractmethod
5 from collections import defaultdict
6 from dataclasses import dataclass
23 from black.rusty import Result, Ok, Err
25 from black.mode import Feature
26 from black.nodes import syms, replace_child, parent_type
27 from black.nodes import is_empty_par, is_empty_lpar, is_empty_rpar
28 from black.nodes import CLOSING_BRACKETS, STANDALONE_COMMENT
29 from black.lines import Line, append_leaves
30 from black.brackets import BracketMatchError
31 from black.comments import contains_pragma_comment
32 from black.strings import has_triple_quotes, get_string_prefix, assert_is_leaf_string
33 from black.strings import normalize_string_quotes
35 from blib2to3.pytree import Leaf, Node
36 from blib2to3.pgen2 import token
39 class CannotTransform(Exception):
40 """Base class for errors raised by Transformers."""
45 LN = Union[Leaf, Node]
46 Transformer = Callable[[Line, Collection[Feature]], Iterator[Line]]
51 TResult = Result[T, CannotTransform] # (T)ransform Result
52 TMatchResult = TResult[Index]
55 def TErr(err_msg: str) -> Err[CannotTransform]:
58 Convenience function used when working with the TResult type.
60 cant_transform = CannotTransform(err_msg)
61 return Err(cant_transform)
64 @dataclass # type: ignore
65 class StringTransformer(ABC):
67 An implementation of the Transformer protocol that relies on its
68 subclasses overriding the template methods `do_match(...)` and
71 This Transformer works exclusively on strings (for example, by merging
74 The following sections can be found among the docstrings of each concrete
75 StringTransformer subclass.
78 Which requirements must be met of the given Line for this
79 StringTransformer to be applied?
82 If the given Line meets all of the above requirements, which string
83 transformations can you expect to be applied to it by this
87 What contractual agreements does this StringTransformer have with other
88 StringTransfomers? Such collaborations should be eliminated/minimized
93 normalize_strings: bool
94 __name__ = "StringTransformer"
97 def do_match(self, line: Line) -> TMatchResult:
100 * Ok(string_idx) such that `line.leaves[string_idx]` is our target
101 string, if a match was able to be made.
103 * Err(CannotTransform), if a match was not able to be made.
107 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
110 * Ok(new_line) where new_line is the new transformed line.
112 * Err(CannotTransform) if the transformation failed for some reason. The
113 `do_match(...)` template method should usually be used to reject
114 the form of the given Line, but in some cases it is difficult to
115 know whether or not a Line meets the StringTransformer's
116 requirements until the transformation is already midway.
119 This method should NOT mutate @line directly, but it MAY mutate the
120 Line's underlying Node structure. (WARNING: If the underlying Node
121 structure IS altered, then this method should NOT be allowed to
122 yield an CannotTransform after that point.)
125 def __call__(self, line: Line, _features: Collection[Feature]) -> Iterator[Line]:
127 StringTransformer instances have a call signature that mirrors that of
128 the Transformer type.
131 CannotTransform(...) if the concrete StringTransformer class is unable
134 # Optimization to avoid calling `self.do_match(...)` when the line does
135 # not contain any string.
136 if not any(leaf.type == token.STRING for leaf in line.leaves):
137 raise CannotTransform("There are no strings in this line.")
139 match_result = self.do_match(line)
141 if isinstance(match_result, Err):
142 cant_transform = match_result.err()
143 raise CannotTransform(
144 f"The string transformer {self.__class__.__name__} does not recognize"
145 " this line as one that it can transform."
146 ) from cant_transform
148 string_idx = match_result.ok()
150 for line_result in self.do_transform(line, string_idx):
151 if isinstance(line_result, Err):
152 cant_transform = line_result.err()
153 raise CannotTransform(
154 "StringTransformer failed while attempting to transform string."
155 ) from cant_transform
156 line = line_result.ok()
162 """A custom (i.e. manual) string split.
164 A single CustomSplit instance represents a single substring.
167 Consider the following string:
174 This string will correspond to the following three CustomSplit instances:
176 CustomSplit(False, 16)
177 CustomSplit(False, 17)
178 CustomSplit(True, 16)
186 class CustomSplitMapMixin:
188 This mixin class is used to map merged strings to a sequence of
189 CustomSplits, which will then be used to re-split the strings iff none of
190 the resultant substrings go over the configured max line length.
193 _Key = Tuple[StringID, str]
194 _CUSTOM_SPLIT_MAP: Dict[_Key, Tuple[CustomSplit, ...]] = defaultdict(tuple)
197 def _get_key(string: str) -> "CustomSplitMapMixin._Key":
200 A unique identifier that is used internally to map @string to a
201 group of custom splits.
203 return (id(string), string)
205 def add_custom_splits(
206 self, string: str, custom_splits: Iterable[CustomSplit]
208 """Custom Split Map Setter Method
211 Adds a mapping from @string to the custom splits @custom_splits.
213 key = self._get_key(string)
214 self._CUSTOM_SPLIT_MAP[key] = tuple(custom_splits)
216 def pop_custom_splits(self, string: str) -> List[CustomSplit]:
217 """Custom Split Map Getter Method
220 * A list of the custom splits that are mapped to @string, if any
226 Deletes the mapping between @string and its associated custom
227 splits (which are returned to the caller).
229 key = self._get_key(string)
231 custom_splits = self._CUSTOM_SPLIT_MAP[key]
232 del self._CUSTOM_SPLIT_MAP[key]
234 return list(custom_splits)
236 def has_custom_splits(self, string: str) -> bool:
239 True iff @string is associated with a set of custom splits.
241 key = self._get_key(string)
242 return key in self._CUSTOM_SPLIT_MAP
245 class StringMerger(CustomSplitMapMixin, StringTransformer):
246 """StringTransformer that merges strings together.
249 (A) The line contains adjacent strings such that ALL of the validation checks
250 listed in StringMerger.__validate_msg(...)'s docstring pass.
252 (B) The line contains a string which uses line continuation backslashes.
255 Depending on which of the two requirements above where met, either:
257 (A) The string group associated with the target string is merged.
259 (B) All line-continuation backslashes are removed from the target string.
262 StringMerger provides custom split information to StringSplitter.
265 def do_match(self, line: Line) -> TMatchResult:
268 is_valid_index = is_valid_index_factory(LL)
270 for (i, leaf) in enumerate(LL):
272 leaf.type == token.STRING
273 and is_valid_index(i + 1)
274 and LL[i + 1].type == token.STRING
278 if leaf.type == token.STRING and "\\\n" in leaf.value:
281 return TErr("This line has no strings that need merging.")
283 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
285 rblc_result = self._remove_backslash_line_continuation_chars(
288 if isinstance(rblc_result, Ok):
289 new_line = rblc_result.ok()
291 msg_result = self._merge_string_group(new_line, string_idx)
292 if isinstance(msg_result, Ok):
293 new_line = msg_result.ok()
295 if isinstance(rblc_result, Err) and isinstance(msg_result, Err):
296 msg_cant_transform = msg_result.err()
297 rblc_cant_transform = rblc_result.err()
298 cant_transform = CannotTransform(
299 "StringMerger failed to merge any strings in this line."
302 # Chain the errors together using `__cause__`.
303 msg_cant_transform.__cause__ = rblc_cant_transform
304 cant_transform.__cause__ = msg_cant_transform
306 yield Err(cant_transform)
311 def _remove_backslash_line_continuation_chars(
312 line: Line, string_idx: int
315 Merge strings that were split across multiple lines using
316 line-continuation backslashes.
319 Ok(new_line), if @line contains backslash line-continuation
322 Err(CannotTransform), otherwise.
326 string_leaf = LL[string_idx]
328 string_leaf.type == token.STRING
329 and "\\\n" in string_leaf.value
330 and not has_triple_quotes(string_leaf.value)
333 f"String leaf {string_leaf} does not contain any backslash line"
334 " continuation characters."
337 new_line = line.clone()
338 new_line.comments = line.comments.copy()
339 append_leaves(new_line, line, LL)
341 new_string_leaf = new_line.leaves[string_idx]
342 new_string_leaf.value = new_string_leaf.value.replace("\\\n", "")
346 def _merge_string_group(self, line: Line, string_idx: int) -> TResult[Line]:
348 Merges string group (i.e. set of adjacent strings) where the first
349 string in the group is `line.leaves[string_idx]`.
352 Ok(new_line), if ALL of the validation checks found in
353 __validate_msg(...) pass.
355 Err(CannotTransform), otherwise.
359 is_valid_index = is_valid_index_factory(LL)
361 vresult = self._validate_msg(line, string_idx)
362 if isinstance(vresult, Err):
365 # If the string group is wrapped inside an Atom node, we must make sure
366 # to later replace that Atom with our new (merged) string leaf.
367 atom_node = LL[string_idx].parent
369 # We will place BREAK_MARK in between every two substrings that we
370 # merge. We will then later go through our final result and use the
371 # various instances of BREAK_MARK we find to add the right values to
372 # the custom split map.
373 BREAK_MARK = "@@@@@ BLACK BREAKPOINT MARKER @@@@@"
375 QUOTE = LL[string_idx].value[-1]
377 def make_naked(string: str, string_prefix: str) -> str:
378 """Strip @string (i.e. make it a "naked" string)
381 * assert_is_leaf_string(@string)
384 A string that is identical to @string except that
385 @string_prefix has been stripped, the surrounding QUOTE
386 characters have been removed, and any remaining QUOTE
387 characters have been escaped.
389 assert_is_leaf_string(string)
391 RE_EVEN_BACKSLASHES = r"(?:(?<!\\)(?:\\\\)*)"
392 naked_string = string[len(string_prefix) + 1 : -1]
393 naked_string = re.sub(
394 "(" + RE_EVEN_BACKSLASHES + ")" + QUOTE, r"\1\\" + QUOTE, naked_string
398 # Holds the CustomSplit objects that will later be added to the custom
402 # Temporary storage for the 'has_prefix' part of the CustomSplit objects.
405 # Sets the 'prefix' variable. This is the prefix that the final merged
407 next_str_idx = string_idx
411 and is_valid_index(next_str_idx)
412 and LL[next_str_idx].type == token.STRING
414 prefix = get_string_prefix(LL[next_str_idx].value)
417 # The next loop merges the string group. The final string will be
420 # The following convenience variables are used:
425 # NSS: naked next string
429 next_str_idx = string_idx
430 while is_valid_index(next_str_idx) and LL[next_str_idx].type == token.STRING:
433 SS = LL[next_str_idx].value
434 next_prefix = get_string_prefix(SS)
436 # If this is an f-string group but this substring is not prefixed
438 if "f" in prefix and "f" not in next_prefix:
439 # Then we must escape any braces contained in this substring.
440 SS = re.subf(r"(\{|\})", "{1}{1}", SS)
442 NSS = make_naked(SS, next_prefix)
444 has_prefix = bool(next_prefix)
445 prefix_tracker.append(has_prefix)
447 S = prefix + QUOTE + NS + NSS + BREAK_MARK + QUOTE
448 NS = make_naked(S, prefix)
452 S_leaf = Leaf(token.STRING, S)
453 if self.normalize_strings:
454 S_leaf.value = normalize_string_quotes(S_leaf.value)
456 # Fill the 'custom_splits' list with the appropriate CustomSplit objects.
457 temp_string = S_leaf.value[len(prefix) + 1 : -1]
458 for has_prefix in prefix_tracker:
459 mark_idx = temp_string.find(BREAK_MARK)
462 ), "Logic error while filling the custom string breakpoint cache."
464 temp_string = temp_string[mark_idx + len(BREAK_MARK) :]
465 breakpoint_idx = mark_idx + (len(prefix) if has_prefix else 0) + 1
466 custom_splits.append(CustomSplit(has_prefix, breakpoint_idx))
468 string_leaf = Leaf(token.STRING, S_leaf.value.replace(BREAK_MARK, ""))
470 if atom_node is not None:
471 replace_child(atom_node, string_leaf)
473 # Build the final line ('new_line') that this method will later return.
474 new_line = line.clone()
475 for (i, leaf) in enumerate(LL):
477 new_line.append(string_leaf)
479 if string_idx <= i < string_idx + num_of_strings:
480 for comment_leaf in line.comments_after(LL[i]):
481 new_line.append(comment_leaf, preformatted=True)
484 append_leaves(new_line, line, [leaf])
486 self.add_custom_splits(string_leaf.value, custom_splits)
490 def _validate_msg(line: Line, string_idx: int) -> TResult[None]:
491 """Validate (M)erge (S)tring (G)roup
493 Transform-time string validation logic for __merge_string_group(...).
496 * Ok(None), if ALL validation checks (listed below) pass.
498 * Err(CannotTransform), if any of the following are true:
499 - The target string group does not contain ANY stand-alone comments.
500 - The target string is not in a string group (i.e. it has no
502 - The string group has more than one inline comment.
503 - The string group has an inline comment that appears to be a pragma.
504 - The set of all string prefixes in the string group is of
505 length greater than one and is not equal to {"", "f"}.
506 - The string group consists of raw strings.
508 # We first check for "inner" stand-alone comments (i.e. stand-alone
509 # comments that have a string leaf before them AND after them).
512 found_sa_comment = False
513 is_valid_index = is_valid_index_factory(line.leaves)
514 while is_valid_index(i) and line.leaves[i].type in [
518 if line.leaves[i].type == STANDALONE_COMMENT:
519 found_sa_comment = True
520 elif found_sa_comment:
522 "StringMerger does NOT merge string groups which contain "
523 "stand-alone comments."
528 num_of_inline_string_comments = 0
529 set_of_prefixes = set()
531 for leaf in line.leaves[string_idx:]:
532 if leaf.type != token.STRING:
533 # If the string group is trailed by a comma, we count the
534 # comments trailing the comma to be one of the string group's
536 if leaf.type == token.COMMA and id(leaf) in line.comments:
537 num_of_inline_string_comments += 1
540 if has_triple_quotes(leaf.value):
541 return TErr("StringMerger does NOT merge multiline strings.")
544 prefix = get_string_prefix(leaf.value)
546 return TErr("StringMerger does NOT merge raw strings.")
548 set_of_prefixes.add(prefix)
550 if id(leaf) in line.comments:
551 num_of_inline_string_comments += 1
552 if contains_pragma_comment(line.comments[id(leaf)]):
553 return TErr("Cannot merge strings which have pragma comments.")
555 if num_of_strings < 2:
557 f"Not enough strings to merge (num_of_strings={num_of_strings})."
560 if num_of_inline_string_comments > 1:
562 f"Too many inline string comments ({num_of_inline_string_comments})."
565 if len(set_of_prefixes) > 1 and set_of_prefixes != {"", "f"}:
566 return TErr(f"Too many different prefixes ({set_of_prefixes}).")
571 class StringParenStripper(StringTransformer):
572 """StringTransformer that strips surrounding parentheses from strings.
575 The line contains a string which is surrounded by parentheses and:
576 - The target string is NOT the only argument to a function call.
577 - The target string is NOT a "pointless" string.
578 - If the target string contains a PERCENT, the brackets are not
579 preceeded or followed by an operator with higher precedence than
583 The parentheses mentioned in the 'Requirements' section are stripped.
586 StringParenStripper has its own inherent usefulness, but it is also
587 relied on to clean up the parentheses created by StringParenWrapper (in
588 the event that they are no longer needed).
591 def do_match(self, line: Line) -> TMatchResult:
594 is_valid_index = is_valid_index_factory(LL)
596 for (idx, leaf) in enumerate(LL):
597 # Should be a string...
598 if leaf.type != token.STRING:
601 # If this is a "pointless" string...
604 and leaf.parent.parent
605 and leaf.parent.parent.type == syms.simple_stmt
609 # Should be preceded by a non-empty LPAR...
611 not is_valid_index(idx - 1)
612 or LL[idx - 1].type != token.LPAR
613 or is_empty_lpar(LL[idx - 1])
617 # That LPAR should NOT be preceded by a function name or a closing
618 # bracket (which could be a function which returns a function or a
619 # list/dictionary that contains a function)...
620 if is_valid_index(idx - 2) and (
621 LL[idx - 2].type == token.NAME or LL[idx - 2].type in CLOSING_BRACKETS
627 # Skip the string trailer, if one exists.
628 string_parser = StringParser()
629 next_idx = string_parser.parse(LL, string_idx)
631 # if the leaves in the parsed string include a PERCENT, we need to
632 # make sure the initial LPAR is NOT preceded by an operator with
633 # higher or equal precedence to PERCENT
634 if is_valid_index(idx - 2):
635 # mypy can't quite follow unless we name this
636 before_lpar = LL[idx - 2]
637 if token.PERCENT in {leaf.type for leaf in LL[idx - 1 : next_idx]} and (
654 # only unary PLUS/MINUS
656 and before_lpar.parent.type == syms.factor
657 and (before_lpar.type in {token.PLUS, token.MINUS})
662 # Should be followed by a non-empty RPAR...
664 is_valid_index(next_idx)
665 and LL[next_idx].type == token.RPAR
666 and not is_empty_rpar(LL[next_idx])
668 # That RPAR should NOT be followed by anything with higher
669 # precedence than PERCENT
670 if is_valid_index(next_idx + 1) and LL[next_idx + 1].type in {
678 return Ok(string_idx)
680 return TErr("This line has no strings wrapped in parens.")
682 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
685 string_parser = StringParser()
686 rpar_idx = string_parser.parse(LL, string_idx)
688 for leaf in (LL[string_idx - 1], LL[rpar_idx]):
689 if line.comments_after(leaf):
691 "Will not strip parentheses which have comments attached to them."
695 new_line = line.clone()
696 new_line.comments = line.comments.copy()
698 append_leaves(new_line, line, LL[: string_idx - 1])
699 except BracketMatchError:
700 # HACK: I believe there is currently a bug somewhere in
701 # right_hand_split() that is causing brackets to not be tracked
702 # properly by a shared BracketTracker.
703 append_leaves(new_line, line, LL[: string_idx - 1], preformatted=True)
705 string_leaf = Leaf(token.STRING, LL[string_idx].value)
706 LL[string_idx - 1].remove()
707 replace_child(LL[string_idx], string_leaf)
708 new_line.append(string_leaf)
711 new_line, line, LL[string_idx + 1 : rpar_idx] + LL[rpar_idx + 1 :]
714 LL[rpar_idx].remove()
719 class BaseStringSplitter(StringTransformer):
721 Abstract class for StringTransformers which transform a Line's strings by splitting
722 them or placing them on their own lines where necessary to avoid going over
723 the configured line length.
726 * The target string value is responsible for the line going over the
727 line length limit. It follows that after all of black's other line
728 split methods have been exhausted, this line (or one of the resulting
729 lines after all line splits are performed) would still be over the
730 line_length limit unless we split this string.
732 * The target string is NOT a "pointless" string (i.e. a string that has
733 no parent or siblings).
735 * The target string is not followed by an inline comment that appears
738 * The target string is not a multiline (i.e. triple-quote) string.
742 def do_splitter_match(self, line: Line) -> TMatchResult:
744 BaseStringSplitter asks its clients to override this method instead of
745 `StringTransformer.do_match(...)`.
747 Follows the same protocol as `StringTransformer.do_match(...)`.
749 Refer to `help(StringTransformer.do_match)` for more information.
752 def do_match(self, line: Line) -> TMatchResult:
753 match_result = self.do_splitter_match(line)
754 if isinstance(match_result, Err):
757 string_idx = match_result.ok()
758 vresult = self._validate(line, string_idx)
759 if isinstance(vresult, Err):
764 def _validate(self, line: Line, string_idx: int) -> TResult[None]:
766 Checks that @line meets all of the requirements listed in this classes'
767 docstring. Refer to `help(BaseStringSplitter)` for a detailed
768 description of those requirements.
771 * Ok(None), if ALL of the requirements are met.
773 * Err(CannotTransform), if ANY of the requirements are NOT met.
777 string_leaf = LL[string_idx]
779 max_string_length = self._get_max_string_length(line, string_idx)
780 if len(string_leaf.value) <= max_string_length:
782 "The string itself is not what is causing this line to be too long."
785 if not string_leaf.parent or [L.type for L in string_leaf.parent.children] == [
790 f"This string ({string_leaf.value}) appears to be pointless (i.e. has"
794 if id(line.leaves[string_idx]) in line.comments and contains_pragma_comment(
795 line.comments[id(line.leaves[string_idx])]
798 "Line appears to end with an inline pragma comment. Splitting the line"
799 " could modify the pragma's behavior."
802 if has_triple_quotes(string_leaf.value):
803 return TErr("We cannot split multiline strings.")
807 def _get_max_string_length(self, line: Line, string_idx: int) -> int:
809 Calculates the max string length used when attempting to determine
810 whether or not the target string is responsible for causing the line to
811 go over the line length limit.
813 WARNING: This method is tightly coupled to both StringSplitter and
814 (especially) StringParenWrapper. There is probably a better way to
815 accomplish what is being done here.
818 max_string_length: such that `line.leaves[string_idx].value >
819 max_string_length` implies that the target string IS responsible
820 for causing this line to exceed the line length limit.
824 is_valid_index = is_valid_index_factory(LL)
826 # We use the shorthand "WMA4" in comments to abbreviate "We must
827 # account for". When giving examples, we use STRING to mean some/any
830 # Finally, we use the following convenience variables:
832 # P: The leaf that is before the target string leaf.
833 # N: The leaf that is after the target string leaf.
834 # NN: The leaf that is after N.
836 # WMA4 the whitespace at the beginning of the line.
837 offset = line.depth * 4
839 if is_valid_index(string_idx - 1):
840 p_idx = string_idx - 1
842 LL[string_idx - 1].type == token.LPAR
843 and LL[string_idx - 1].value == ""
846 # If the previous leaf is an empty LPAR placeholder, we should skip it.
850 if P.type == token.PLUS:
851 # WMA4 a space and a '+' character (e.g. `+ STRING`).
854 if P.type == token.COMMA:
855 # WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
858 if P.type in [token.COLON, token.EQUAL, token.NAME]:
859 # This conditional branch is meant to handle dictionary keys,
860 # variable assignments, 'return STRING' statement lines, and
861 # 'else STRING' ternary expression lines.
863 # WMA4 a single space.
866 # WMA4 the lengths of any leaves that came before that space,
867 # but after any closing bracket before that space.
868 for leaf in reversed(LL[: p_idx + 1]):
869 offset += len(str(leaf))
870 if leaf.type in CLOSING_BRACKETS:
873 if is_valid_index(string_idx + 1):
874 N = LL[string_idx + 1]
875 if N.type == token.RPAR and N.value == "" and len(LL) > string_idx + 2:
876 # If the next leaf is an empty RPAR placeholder, we should skip it.
877 N = LL[string_idx + 2]
879 if N.type == token.COMMA:
880 # WMA4 a single comma at the end of the string (e.g `STRING,`).
883 if is_valid_index(string_idx + 2):
884 NN = LL[string_idx + 2]
886 if N.type == token.DOT and NN.type == token.NAME:
887 # This conditional branch is meant to handle method calls invoked
888 # off of a string literal up to and including the LPAR character.
890 # WMA4 the '.' character.
894 is_valid_index(string_idx + 3)
895 and LL[string_idx + 3].type == token.LPAR
897 # WMA4 the left parenthesis character.
900 # WMA4 the length of the method's name.
901 offset += len(NN.value)
904 for comment_leaf in line.comments_after(LL[string_idx]):
907 # WMA4 two spaces before the '#' character.
910 # WMA4 the length of the inline comment.
911 offset += len(comment_leaf.value)
913 max_string_length = self.line_length - offset
914 return max_string_length
917 class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
919 StringTransformer that splits "atom" strings (i.e. strings which exist on
920 lines by themselves).
923 * The line consists ONLY of a single string (with the exception of a
924 '+' symbol which MAY exist at the start of the line), MAYBE a string
925 trailer, and MAYBE a trailing comma.
927 * All of the requirements listed in BaseStringSplitter's docstring.
930 The string mentioned in the 'Requirements' section is split into as
931 many substrings as necessary to adhere to the configured line length.
933 In the final set of substrings, no substring should be smaller than
934 MIN_SUBSTR_SIZE characters.
936 The string will ONLY be split on spaces (i.e. each new substring should
937 start with a space). Note that the string will NOT be split on a space
938 which is escaped with a backslash.
940 If the string is an f-string, it will NOT be split in the middle of an
941 f-expression (e.g. in f"FooBar: {foo() if x else bar()}", {foo() if x
942 else bar()} is an f-expression).
944 If the string that is being split has an associated set of custom split
945 records and those custom splits will NOT result in any line going over
946 the configured line length, those custom splits are used. Otherwise the
947 string is split as late as possible (from left-to-right) while still
948 adhering to the transformation rules listed above.
951 StringSplitter relies on StringMerger to construct the appropriate
952 CustomSplit objects and add them to the custom split map.
956 # Matches an "f-expression" (e.g. {var}) that might be found in an f-string.
958 (?<!\{) (?:\{\{)* \{ (?!\{)
965 (?<!\}) \} (?:\}\})* (?!\})
968 def do_splitter_match(self, line: Line) -> TMatchResult:
971 is_valid_index = is_valid_index_factory(LL)
975 # The first leaf MAY be a '+' symbol...
976 if is_valid_index(idx) and LL[idx].type == token.PLUS:
979 # The next/first leaf MAY be an empty LPAR...
980 if is_valid_index(idx) and is_empty_lpar(LL[idx]):
983 # The next/first leaf MUST be a string...
984 if not is_valid_index(idx) or LL[idx].type != token.STRING:
985 return TErr("Line does not start with a string.")
989 # Skip the string trailer, if one exists.
990 string_parser = StringParser()
991 idx = string_parser.parse(LL, string_idx)
993 # That string MAY be followed by an empty RPAR...
994 if is_valid_index(idx) and is_empty_rpar(LL[idx]):
997 # That string / empty RPAR leaf MAY be followed by a comma...
998 if is_valid_index(idx) and LL[idx].type == token.COMMA:
1001 # But no more leaves are allowed...
1002 if is_valid_index(idx):
1003 return TErr("This line does not end with a string.")
1005 return Ok(string_idx)
1007 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
1010 QUOTE = LL[string_idx].value[-1]
1012 is_valid_index = is_valid_index_factory(LL)
1013 insert_str_child = insert_str_child_factory(LL[string_idx])
1015 prefix = get_string_prefix(LL[string_idx].value)
1017 # We MAY choose to drop the 'f' prefix from substrings that don't
1018 # contain any f-expressions, but ONLY if the original f-string
1019 # contains at least one f-expression. Otherwise, we will alter the AST
1021 drop_pointless_f_prefix = ("f" in prefix) and re.search(
1022 self.RE_FEXPR, LL[string_idx].value, re.VERBOSE
1025 first_string_line = True
1026 starts_with_plus = LL[0].type == token.PLUS
1028 def line_needs_plus() -> bool:
1029 return first_string_line and starts_with_plus
1031 def maybe_append_plus(new_line: Line) -> None:
1034 If @line starts with a plus and this is the first line we are
1035 constructing, this function appends a PLUS leaf to @new_line
1036 and replaces the old PLUS leaf in the node structure. Otherwise
1037 this function does nothing.
1039 if line_needs_plus():
1040 plus_leaf = Leaf(token.PLUS, "+")
1041 replace_child(LL[0], plus_leaf)
1042 new_line.append(plus_leaf)
1045 is_valid_index(string_idx + 1) and LL[string_idx + 1].type == token.COMMA
1048 def max_last_string() -> int:
1051 The max allowed length of the string value used for the last
1052 line we will construct.
1054 result = self.line_length
1055 result -= line.depth * 4
1056 result -= 1 if ends_with_comma else 0
1057 result -= 2 if line_needs_plus() else 0
1060 # --- Calculate Max Break Index (for string value)
1061 # We start with the line length limit
1062 max_break_idx = self.line_length
1063 # The last index of a string of length N is N-1.
1065 # Leading whitespace is not present in the string value (e.g. Leaf.value).
1066 max_break_idx -= line.depth * 4
1067 if max_break_idx < 0:
1069 f"Unable to split {LL[string_idx].value} at such high of a line depth:"
1074 # Check if StringMerger registered any custom splits.
1075 custom_splits = self.pop_custom_splits(LL[string_idx].value)
1076 # We use them ONLY if none of them would produce lines that exceed the
1078 use_custom_breakpoints = bool(
1080 and all(csplit.break_idx <= max_break_idx for csplit in custom_splits)
1083 # Temporary storage for the remaining chunk of the string line that
1084 # can't fit onto the line currently being constructed.
1085 rest_value = LL[string_idx].value
1087 def more_splits_should_be_made() -> bool:
1090 True iff `rest_value` (the remaining string value from the last
1091 split), should be split again.
1093 if use_custom_breakpoints:
1094 return len(custom_splits) > 1
1096 return len(rest_value) > max_last_string()
1098 string_line_results: List[Ok[Line]] = []
1099 while more_splits_should_be_made():
1100 if use_custom_breakpoints:
1101 # Custom User Split (manual)
1102 csplit = custom_splits.pop(0)
1103 break_idx = csplit.break_idx
1105 # Algorithmic Split (automatic)
1106 max_bidx = max_break_idx - 2 if line_needs_plus() else max_break_idx
1107 maybe_break_idx = self._get_break_idx(rest_value, max_bidx)
1108 if maybe_break_idx is None:
1109 # If we are unable to algorithmically determine a good split
1110 # and this string has custom splits registered to it, we
1111 # fall back to using them--which means we have to start
1112 # over from the beginning.
1114 rest_value = LL[string_idx].value
1115 string_line_results = []
1116 first_string_line = True
1117 use_custom_breakpoints = True
1120 # Otherwise, we stop splitting here.
1123 break_idx = maybe_break_idx
1125 # --- Construct `next_value`
1126 next_value = rest_value[:break_idx] + QUOTE
1128 # Are we allowed to try to drop a pointless 'f' prefix?
1129 drop_pointless_f_prefix
1130 # If we are, will we be successful?
1131 and next_value != self._normalize_f_string(next_value, prefix)
1133 # If the current custom split did NOT originally use a prefix,
1134 # then `csplit.break_idx` will be off by one after removing
1138 if use_custom_breakpoints and not csplit.has_prefix
1141 next_value = rest_value[:break_idx] + QUOTE
1142 next_value = self._normalize_f_string(next_value, prefix)
1144 # --- Construct `next_leaf`
1145 next_leaf = Leaf(token.STRING, next_value)
1146 insert_str_child(next_leaf)
1147 self._maybe_normalize_string_quotes(next_leaf)
1149 # --- Construct `next_line`
1150 next_line = line.clone()
1151 maybe_append_plus(next_line)
1152 next_line.append(next_leaf)
1153 string_line_results.append(Ok(next_line))
1155 rest_value = prefix + QUOTE + rest_value[break_idx:]
1156 first_string_line = False
1158 yield from string_line_results
1160 if drop_pointless_f_prefix:
1161 rest_value = self._normalize_f_string(rest_value, prefix)
1163 rest_leaf = Leaf(token.STRING, rest_value)
1164 insert_str_child(rest_leaf)
1166 # NOTE: I could not find a test case that verifies that the following
1167 # line is actually necessary, but it seems to be. Otherwise we risk
1168 # not normalizing the last substring, right?
1169 self._maybe_normalize_string_quotes(rest_leaf)
1171 last_line = line.clone()
1172 maybe_append_plus(last_line)
1174 # If there are any leaves to the right of the target string...
1175 if is_valid_index(string_idx + 1):
1176 # We use `temp_value` here to determine how long the last line
1177 # would be if we were to append all the leaves to the right of the
1178 # target string to the last string line.
1179 temp_value = rest_value
1180 for leaf in LL[string_idx + 1 :]:
1181 temp_value += str(leaf)
1182 if leaf.type == token.LPAR:
1185 # Try to fit them all on the same line with the last substring...
1187 len(temp_value) <= max_last_string()
1188 or LL[string_idx + 1].type == token.COMMA
1190 last_line.append(rest_leaf)
1191 append_leaves(last_line, line, LL[string_idx + 1 :])
1193 # Otherwise, place the last substring on one line and everything
1194 # else on a line below that...
1196 last_line.append(rest_leaf)
1199 non_string_line = line.clone()
1200 append_leaves(non_string_line, line, LL[string_idx + 1 :])
1201 yield Ok(non_string_line)
1202 # Else the target string was the last leaf...
1204 last_line.append(rest_leaf)
1205 last_line.comments = line.comments.copy()
1208 def _get_break_idx(self, string: str, max_break_idx: int) -> Optional[int]:
1210 This method contains the algorithm that StringSplitter uses to
1211 determine which character to split each string at.
1214 @string: The substring that we are attempting to split.
1215 @max_break_idx: The ideal break index. We will return this value if it
1216 meets all the necessary conditions. In the likely event that it
1217 doesn't we will try to find the closest index BELOW @max_break_idx
1218 that does. If that fails, we will expand our search by also
1219 considering all valid indices ABOVE @max_break_idx.
1222 * assert_is_leaf_string(@string)
1223 * 0 <= @max_break_idx < len(@string)
1226 break_idx, if an index is able to be found that meets all of the
1227 conditions listed in the 'Transformations' section of this classes'
1232 is_valid_index = is_valid_index_factory(string)
1234 assert is_valid_index(max_break_idx)
1235 assert_is_leaf_string(string)
1237 _fexpr_slices: Optional[List[Tuple[Index, Index]]] = None
1239 def fexpr_slices() -> Iterator[Tuple[Index, Index]]:
1242 All ranges of @string which, if @string were to be split there,
1243 would result in the splitting of an f-expression (which is NOT
1246 nonlocal _fexpr_slices
1248 if _fexpr_slices is None:
1250 for match in re.finditer(self.RE_FEXPR, string, re.VERBOSE):
1251 _fexpr_slices.append(match.span())
1253 yield from _fexpr_slices
1255 is_fstring = "f" in get_string_prefix(string)
1257 def breaks_fstring_expression(i: Index) -> bool:
1260 True iff returning @i would result in the splitting of an
1261 f-expression (which is NOT allowed).
1266 for (start, end) in fexpr_slices():
1267 if start <= i < end:
1272 def passes_all_checks(i: Index) -> bool:
1275 True iff ALL of the conditions listed in the 'Transformations'
1276 section of this classes' docstring would be be met by returning @i.
1278 is_space = string[i] == " "
1280 is_not_escaped = True
1282 while is_valid_index(j) and string[j] == "\\":
1283 is_not_escaped = not is_not_escaped
1287 len(string[i:]) >= self.MIN_SUBSTR_SIZE
1288 and len(string[:i]) >= self.MIN_SUBSTR_SIZE
1294 and not breaks_fstring_expression(i)
1297 # First, we check all indices BELOW @max_break_idx.
1298 break_idx = max_break_idx
1299 while is_valid_index(break_idx - 1) and not passes_all_checks(break_idx):
1302 if not passes_all_checks(break_idx):
1303 # If that fails, we check all indices ABOVE @max_break_idx.
1305 # If we are able to find a valid index here, the next line is going
1306 # to be longer than the specified line length, but it's probably
1307 # better than doing nothing at all.
1308 break_idx = max_break_idx + 1
1309 while is_valid_index(break_idx + 1) and not passes_all_checks(break_idx):
1312 if not is_valid_index(break_idx) or not passes_all_checks(break_idx):
1317 def _maybe_normalize_string_quotes(self, leaf: Leaf) -> None:
1318 if self.normalize_strings:
1319 leaf.value = normalize_string_quotes(leaf.value)
1321 def _normalize_f_string(self, string: str, prefix: str) -> str:
1324 * assert_is_leaf_string(@string)
1327 * If @string is an f-string that contains no f-expressions, we
1328 return a string identical to @string except that the 'f' prefix
1329 has been stripped and all double braces (i.e. '{{' or '}}') have
1330 been normalized (i.e. turned into '{' or '}').
1332 * Otherwise, we return @string.
1334 assert_is_leaf_string(string)
1336 if "f" in prefix and not re.search(self.RE_FEXPR, string, re.VERBOSE):
1337 new_prefix = prefix.replace("f", "")
1339 temp = string[len(prefix) :]
1340 temp = re.sub(r"\{\{", "{", temp)
1341 temp = re.sub(r"\}\}", "}", temp)
1344 return f"{new_prefix}{new_string}"
1349 class StringParenWrapper(CustomSplitMapMixin, BaseStringSplitter):
1351 StringTransformer that splits non-"atom" strings (i.e. strings that do not
1352 exist on lines by themselves).
1355 All of the requirements listed in BaseStringSplitter's docstring in
1356 addition to the requirements listed below:
1358 * The line is a return/yield statement, which returns/yields a string.
1360 * The line is part of a ternary expression (e.g. `x = y if cond else
1361 z`) such that the line starts with `else <string>`, where <string> is
1364 * The line is an assert statement, which ends with a string.
1366 * The line is an assignment statement (e.g. `x = <string>` or `x +=
1367 <string>`) such that the variable is being assigned the value of some
1370 * The line is a dictionary key assignment where some valid key is being
1371 assigned the value of some string.
1374 The chosen string is wrapped in parentheses and then split at the LPAR.
1376 We then have one line which ends with an LPAR and another line that
1377 starts with the chosen string. The latter line is then split again at
1378 the RPAR. This results in the RPAR (and possibly a trailing comma)
1379 being placed on its own line.
1381 NOTE: If any leaves exist to the right of the chosen string (except
1382 for a trailing comma, which would be placed after the RPAR), those
1383 leaves are placed inside the parentheses. In effect, the chosen
1384 string is not necessarily being "wrapped" by parentheses. We can,
1385 however, count on the LPAR being placed directly before the chosen
1388 In other words, StringParenWrapper creates "atom" strings. These
1389 can then be split again by StringSplitter, if necessary.
1392 In the event that a string line split by StringParenWrapper is
1393 changed such that it no longer needs to be given its own line,
1394 StringParenWrapper relies on StringParenStripper to clean up the
1395 parentheses it created.
1398 def do_splitter_match(self, line: Line) -> TMatchResult:
1402 self._return_match(LL)
1403 or self._else_match(LL)
1404 or self._assert_match(LL)
1405 or self._assign_match(LL)
1406 or self._dict_match(LL)
1409 if string_idx is not None:
1410 string_value = line.leaves[string_idx].value
1411 # If the string has no spaces...
1412 if " " not in string_value:
1413 # And will still violate the line length limit when split...
1414 max_string_length = self.line_length - ((line.depth + 1) * 4)
1415 if len(string_value) > max_string_length:
1416 # And has no associated custom splits...
1417 if not self.has_custom_splits(string_value):
1418 # Then we should NOT put this string on its own line.
1420 "We do not wrap long strings in parentheses when the"
1421 " resultant line would still be over the specified line"
1422 " length and can't be split further by StringSplitter."
1424 return Ok(string_idx)
1426 return TErr("This line does not contain any non-atomic strings.")
1429 def _return_match(LL: List[Leaf]) -> Optional[int]:
1432 string_idx such that @LL[string_idx] is equal to our target (i.e.
1433 matched) string, if this line matches the return/yield statement
1434 requirements listed in the 'Requirements' section of this classes'
1439 # If this line is apart of a return/yield statement and the first leaf
1440 # contains either the "return" or "yield" keywords...
1441 if parent_type(LL[0]) in [syms.return_stmt, syms.yield_expr] and LL[
1443 ].value in ["return", "yield"]:
1444 is_valid_index = is_valid_index_factory(LL)
1446 idx = 2 if is_valid_index(1) and is_empty_par(LL[1]) else 1
1447 # The next visible leaf MUST contain a string...
1448 if is_valid_index(idx) and LL[idx].type == token.STRING:
1454 def _else_match(LL: List[Leaf]) -> Optional[int]:
1457 string_idx such that @LL[string_idx] is equal to our target (i.e.
1458 matched) string, if this line matches the ternary expression
1459 requirements listed in the 'Requirements' section of this classes'
1464 # If this line is apart of a ternary expression and the first leaf
1465 # contains the "else" keyword...
1467 parent_type(LL[0]) == syms.test
1468 and LL[0].type == token.NAME
1469 and LL[0].value == "else"
1471 is_valid_index = is_valid_index_factory(LL)
1473 idx = 2 if is_valid_index(1) and is_empty_par(LL[1]) else 1
1474 # The next visible leaf MUST contain a string...
1475 if is_valid_index(idx) and LL[idx].type == token.STRING:
1481 def _assert_match(LL: List[Leaf]) -> Optional[int]:
1484 string_idx such that @LL[string_idx] is equal to our target (i.e.
1485 matched) string, if this line matches the assert statement
1486 requirements listed in the 'Requirements' section of this classes'
1491 # If this line is apart of an assert statement and the first leaf
1492 # contains the "assert" keyword...
1493 if parent_type(LL[0]) == syms.assert_stmt and LL[0].value == "assert":
1494 is_valid_index = is_valid_index_factory(LL)
1496 for (i, leaf) in enumerate(LL):
1497 # We MUST find a comma...
1498 if leaf.type == token.COMMA:
1499 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1501 # That comma MUST be followed by a string...
1502 if is_valid_index(idx) and LL[idx].type == token.STRING:
1505 # Skip the string trailer, if one exists.
1506 string_parser = StringParser()
1507 idx = string_parser.parse(LL, string_idx)
1509 # But no more leaves are allowed...
1510 if not is_valid_index(idx):
1516 def _assign_match(LL: List[Leaf]) -> Optional[int]:
1519 string_idx such that @LL[string_idx] is equal to our target (i.e.
1520 matched) string, if this line matches the assignment statement
1521 requirements listed in the 'Requirements' section of this classes'
1526 # If this line is apart of an expression statement or is a function
1527 # argument AND the first leaf contains a variable name...
1529 parent_type(LL[0]) in [syms.expr_stmt, syms.argument, syms.power]
1530 and LL[0].type == token.NAME
1532 is_valid_index = is_valid_index_factory(LL)
1534 for (i, leaf) in enumerate(LL):
1535 # We MUST find either an '=' or '+=' symbol...
1536 if leaf.type in [token.EQUAL, token.PLUSEQUAL]:
1537 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1539 # That symbol MUST be followed by a string...
1540 if is_valid_index(idx) and LL[idx].type == token.STRING:
1543 # Skip the string trailer, if one exists.
1544 string_parser = StringParser()
1545 idx = string_parser.parse(LL, string_idx)
1547 # The next leaf MAY be a comma iff this line is apart
1548 # of a function argument...
1550 parent_type(LL[0]) == syms.argument
1551 and is_valid_index(idx)
1552 and LL[idx].type == token.COMMA
1556 # But no more leaves are allowed...
1557 if not is_valid_index(idx):
1563 def _dict_match(LL: List[Leaf]) -> Optional[int]:
1566 string_idx such that @LL[string_idx] is equal to our target (i.e.
1567 matched) string, if this line matches the dictionary key assignment
1568 statement requirements listed in the 'Requirements' section of this
1573 # If this line is apart of a dictionary key assignment...
1574 if syms.dictsetmaker in [parent_type(LL[0]), parent_type(LL[0].parent)]:
1575 is_valid_index = is_valid_index_factory(LL)
1577 for (i, leaf) in enumerate(LL):
1578 # We MUST find a colon...
1579 if leaf.type == token.COLON:
1580 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1582 # That colon MUST be followed by a string...
1583 if is_valid_index(idx) and LL[idx].type == token.STRING:
1586 # Skip the string trailer, if one exists.
1587 string_parser = StringParser()
1588 idx = string_parser.parse(LL, string_idx)
1590 # That string MAY be followed by a comma...
1591 if is_valid_index(idx) and LL[idx].type == token.COMMA:
1594 # But no more leaves are allowed...
1595 if not is_valid_index(idx):
1600 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
1603 is_valid_index = is_valid_index_factory(LL)
1604 insert_str_child = insert_str_child_factory(LL[string_idx])
1607 ends_with_comma = False
1608 if LL[comma_idx].type == token.COMMA:
1609 ends_with_comma = True
1611 leaves_to_steal_comments_from = [LL[string_idx]]
1613 leaves_to_steal_comments_from.append(LL[comma_idx])
1616 first_line = line.clone()
1617 left_leaves = LL[:string_idx]
1619 # We have to remember to account for (possibly invisible) LPAR and RPAR
1620 # leaves that already wrapped the target string. If these leaves do
1621 # exist, we will replace them with our own LPAR and RPAR leaves.
1622 old_parens_exist = False
1623 if left_leaves and left_leaves[-1].type == token.LPAR:
1624 old_parens_exist = True
1625 leaves_to_steal_comments_from.append(left_leaves[-1])
1628 append_leaves(first_line, line, left_leaves)
1630 lpar_leaf = Leaf(token.LPAR, "(")
1631 if old_parens_exist:
1632 replace_child(LL[string_idx - 1], lpar_leaf)
1634 insert_str_child(lpar_leaf)
1635 first_line.append(lpar_leaf)
1637 # We throw inline comments that were originally to the right of the
1638 # target string to the top line. They will now be shown to the right of
1640 for leaf in leaves_to_steal_comments_from:
1641 for comment_leaf in line.comments_after(leaf):
1642 first_line.append(comment_leaf, preformatted=True)
1644 yield Ok(first_line)
1646 # --- Middle (String) Line
1647 # We only need to yield one (possibly too long) string line, since the
1648 # `StringSplitter` will break it down further if necessary.
1649 string_value = LL[string_idx].value
1652 depth=line.depth + 1,
1653 inside_brackets=True,
1654 should_split_rhs=line.should_split_rhs,
1655 magic_trailing_comma=line.magic_trailing_comma,
1657 string_leaf = Leaf(token.STRING, string_value)
1658 insert_str_child(string_leaf)
1659 string_line.append(string_leaf)
1661 old_rpar_leaf = None
1662 if is_valid_index(string_idx + 1):
1663 right_leaves = LL[string_idx + 1 :]
1667 if old_parens_exist:
1669 right_leaves and right_leaves[-1].type == token.RPAR
1670 ), "Apparently, old parentheses do NOT exist?!"
1671 old_rpar_leaf = right_leaves.pop()
1673 append_leaves(string_line, line, right_leaves)
1675 yield Ok(string_line)
1678 last_line = line.clone()
1679 last_line.bracket_tracker = first_line.bracket_tracker
1681 new_rpar_leaf = Leaf(token.RPAR, ")")
1682 if old_rpar_leaf is not None:
1683 replace_child(old_rpar_leaf, new_rpar_leaf)
1685 insert_str_child(new_rpar_leaf)
1686 last_line.append(new_rpar_leaf)
1688 # If the target string ended with a comma, we place this comma to the
1689 # right of the RPAR on the last line.
1691 comma_leaf = Leaf(token.COMMA, ",")
1692 replace_child(LL[comma_idx], comma_leaf)
1693 last_line.append(comma_leaf)
1700 A state machine that aids in parsing a string's "trailer", which can be
1701 either non-existent, an old-style formatting sequence (e.g. `% varX` or `%
1702 (varX, varY)`), or a method-call / attribute access (e.g. `.format(varX,
1705 NOTE: A new StringParser object MUST be instantiated for each string
1706 trailer we need to parse.
1709 We shall assume that `line` equals the `Line` object that corresponds
1710 to the following line of python code:
1712 x = "Some {}.".format("String") + some_other_string
1715 Furthermore, we will assume that `string_idx` is some index such that:
1717 assert line.leaves[string_idx].value == "Some {}."
1720 The following code snippet then holds:
1722 string_parser = StringParser()
1723 idx = string_parser.parse(line.leaves, string_idx)
1724 assert line.leaves[idx].type == token.PLUS
1730 # String Parser States
1740 # Lookup Table for Next State
1741 _goto: Dict[Tuple[ParserState, NodeType], ParserState] = {
1742 # A string trailer may start with '.' OR '%'.
1743 (START, token.DOT): DOT,
1744 (START, token.PERCENT): PERCENT,
1745 (START, DEFAULT_TOKEN): DONE,
1746 # A '.' MUST be followed by an attribute or method name.
1747 (DOT, token.NAME): NAME,
1748 # A method name MUST be followed by an '(', whereas an attribute name
1749 # is the last symbol in the string trailer.
1750 (NAME, token.LPAR): LPAR,
1751 (NAME, DEFAULT_TOKEN): DONE,
1752 # A '%' symbol can be followed by an '(' or a single argument (e.g. a
1753 # string or variable name).
1754 (PERCENT, token.LPAR): LPAR,
1755 (PERCENT, DEFAULT_TOKEN): SINGLE_FMT_ARG,
1756 # If a '%' symbol is followed by a single argument, that argument is
1757 # the last leaf in the string trailer.
1758 (SINGLE_FMT_ARG, DEFAULT_TOKEN): DONE,
1759 # If present, a ')' symbol is the last symbol in a string trailer.
1760 # (NOTE: LPARS and nested RPARS are not included in this lookup table,
1761 # since they are treated as a special case by the parsing logic in this
1762 # classes' implementation.)
1763 (RPAR, DEFAULT_TOKEN): DONE,
1766 def __init__(self) -> None:
1767 self._state = self.START
1768 self._unmatched_lpars = 0
1770 def parse(self, leaves: List[Leaf], string_idx: int) -> int:
1773 * @leaves[@string_idx].type == token.STRING
1776 The index directly after the last leaf which is apart of the string
1777 trailer, if a "trailer" exists.
1779 @string_idx + 1, if no string "trailer" exists.
1781 assert leaves[string_idx].type == token.STRING
1783 idx = string_idx + 1
1784 while idx < len(leaves) and self._next_state(leaves[idx]):
1788 def _next_state(self, leaf: Leaf) -> bool:
1791 * On the first call to this function, @leaf MUST be the leaf that
1792 was directly after the string leaf in question (e.g. if our target
1793 string is `line.leaves[i]` then the first call to this method must
1794 be `line.leaves[i + 1]`).
1795 * On the next call to this function, the leaf parameter passed in
1796 MUST be the leaf directly following @leaf.
1799 True iff @leaf is apart of the string's trailer.
1801 # We ignore empty LPAR or RPAR leaves.
1802 if is_empty_par(leaf):
1805 next_token = leaf.type
1806 if next_token == token.LPAR:
1807 self._unmatched_lpars += 1
1809 current_state = self._state
1811 # The LPAR parser state is a special case. We will return True until we
1812 # find the matching RPAR token.
1813 if current_state == self.LPAR:
1814 if next_token == token.RPAR:
1815 self._unmatched_lpars -= 1
1816 if self._unmatched_lpars == 0:
1817 self._state = self.RPAR
1818 # Otherwise, we use a lookup table to determine the next state.
1820 # If the lookup table matches the current state to the next
1821 # token, we use the lookup table.
1822 if (current_state, next_token) in self._goto:
1823 self._state = self._goto[current_state, next_token]
1825 # Otherwise, we check if a the current state was assigned a
1827 if (current_state, self.DEFAULT_TOKEN) in self._goto:
1828 self._state = self._goto[current_state, self.DEFAULT_TOKEN]
1829 # If no default has been assigned, then this parser has a logic
1832 raise RuntimeError(f"{self.__class__.__name__} LOGIC ERROR!")
1834 if self._state == self.DONE:
1840 def insert_str_child_factory(string_leaf: Leaf) -> Callable[[LN], None]:
1842 Factory for a convenience function that is used to orphan @string_leaf
1843 and then insert multiple new leaves into the same part of the node
1844 structure that @string_leaf had originally occupied.
1847 Let `string_leaf = Leaf(token.STRING, '"foo"')` and `N =
1848 string_leaf.parent`. Assume the node `N` has the following
1855 Leaf(STRING, '"foo"'),
1859 We then run the code snippet shown below.
1861 insert_str_child = insert_str_child_factory(string_leaf)
1863 lpar = Leaf(token.LPAR, '(')
1864 insert_str_child(lpar)
1866 bar = Leaf(token.STRING, '"bar"')
1867 insert_str_child(bar)
1869 rpar = Leaf(token.RPAR, ')')
1870 insert_str_child(rpar)
1873 After which point, it follows that `string_leaf.parent is None` and
1874 the node `N` now has the following structure:
1881 Leaf(STRING, '"bar"'),
1886 string_parent = string_leaf.parent
1887 string_child_idx = string_leaf.remove()
1889 def insert_str_child(child: LN) -> None:
1890 nonlocal string_child_idx
1892 assert string_parent is not None
1893 assert string_child_idx is not None
1895 string_parent.insert_child(string_child_idx, child)
1896 string_child_idx += 1
1898 return insert_str_child
1901 def is_valid_index_factory(seq: Sequence[Any]) -> Callable[[int], bool]:
1907 is_valid_index = is_valid_index_factory(my_list)
1909 assert is_valid_index(0)
1910 assert is_valid_index(2)
1912 assert not is_valid_index(3)
1913 assert not is_valid_index(-1)
1917 def is_valid_index(idx: int) -> bool:
1920 True iff @idx is positive AND seq[@idx] does NOT raise an
1923 return 0 <= idx < len(seq)
1925 return is_valid_index