def pattern_of()

in fluent/migrate/transforms.py [0:0]


    def pattern_of(*elements):
        normalized = []

        # Normalize text content: convert text content to TextElements, join
        # adjacent text and prune empty. Text content is either existing
        # TextElements or whitespace-only StringLiterals. This may result in
        # leading and trailing whitespace being put back into TextElements if
        # the new Pattern is built from existing Patterns (CONCAT(COPY...)).
        # The leading and trailing whitespace of the new Pattern will be
        # extracted later into new StringLiterals.
        for element in chain_elements(elements):
            if isinstance(element, FTL.TextElement):
                text_content = element.value
            elif (
                isinstance(element, FTL.Placeable)
                and isinstance(element.expression, FTL.StringLiteral)
                and re.match(r"^ *$", element.expression.value)
            ):
                text_content = element.expression.value
            else:
                # The element does not contain text content which should be
                # normalized. It may be a number, a reference, or
                # a StringLiteral which should be preserved in the Pattern.
                normalized.append(element)
                continue

            previous = normalized[-1] if len(normalized) else None
            if isinstance(previous, FTL.TextElement):
                # Join adjacent TextElements.
                previous.value += text_content
            elif len(text_content) > 0:
                # Normalize non-empty text to a TextElement.
                normalized.append(FTL.TextElement(text_content))
            else:
                # Prune empty text.
                pass

        # Store empty values explicitly as {""}.
        if len(normalized) == 0:
            empty = FTL.Placeable(FTL.StringLiteral(""))
            return FTL.Pattern([empty])

        # Extract explicit leading whitespace into a StringLiteral.
        if isinstance(normalized[0], FTL.TextElement):
            ws, text = extract_whitespace(re_leading_ws, normalized[0])
            normalized[:1] = [ws, text]

        # Extract explicit trailing whitespace into a StringLiteral.
        if isinstance(normalized[-1], FTL.TextElement):
            ws, text = extract_whitespace(re_trailing_ws, normalized[-1])
            normalized[-1:] = [text, ws]

        return FTL.Pattern([element for element in normalized if element is not None])