from typing import Optional
import re


def get_architecture(buffer: str) -> Optional[str]:
    """Parses the architecture out of an input string

    The input is expected to be a string representing vhdl file content. If an
    architecture is defined within this content, the architecture block is
    parsed if one is found. If nothing is found that could be parsed, the
    function returns None. If the architecture could be parsed, it is returned
    as a string beginning with "architecture" and ending on
    "end <name>;" or "end architecture;"

    Args:
        buffer (str): input string

    Returns:
        Optional[str]: architecture string
    """
    # (                 begin of capture group---------------------ARCHITECTURE
    #     architecture  "architecture"
    #     \s+           one or more whitespace characters
    #     \w+           one or more word characters
    #     \s+           one or more whitespace characters
    #     of            "of"
    #     \s+           one or more whitespace characters
    #     \w+           one or more word characters
    #     \s+           one or more whitespace characters
    #     is            "is"
    #     .*            any character zero or more times
    #     end           "end"
    #     \s+           one or more whitespace characters
    #     \w+           one or more word characters
    #     \s*           zero or more whitespace characters
    #     ;             semicolon
    # )                 end of capture group
    m = re.search(
        r"(architecture\s+\w+\s+of\s+\w+\s+is.*end\s+\w+\s*;)",
        buffer,
        flags=re.IGNORECASE,
    )
    if m is None:
        return None
    architecture = m.group(1)
    return architecture