import re


def _data_comment_from_line(line, in_block=False):
    """Parse a line from a CASTEP input file

    Args:
        line (str): line from CASTEP .cell or .param file
        in_block (bool): Include all tokens before comment in
            data and leave tag as None.

    Returns:
        (tag, data, comment) where tag is a str, data is a list
        of str and comment is a str

    """
    comment_split_line = re.split("[#;!]", line)

    line_content = comment_split_line[0]
    if len(comment_split_line) == 1:
        comment = ""
    else:
        comment = line[len(line_content) :]

    if in_block:
        tag, data = None, line.split()
    else:
        tag, *data = line_content.split()
        if len(data) == 0:
            data = [""]

        # Clean up delimiter: 'TAG DAT' 'TAG= DAT' 'TAG : DAT'
        # are all possible so check both symbols in both places
        data = [item for item in data if item not in ":="]
        if tag[-1] in ":=":
            tag = tag[:-1]

    return tag, data, comment