def get_expression(text, begin, separator):
    """Find the end of a expression or statement
    An expression or statement ends at a new-line or at the separator,
    unless the new-line or separator is encountered inside a string-literal or
    inside matching bracket pairs.

    @param text The total text being parsed.
    @param begin The index at the start of the expression or statement.
    @param separator The character which ends an expression (not statement).
    @return (index beyond the terminator, the expression, The terminator or empty on end-of-text).
    """
    end_chars = separator + "\n\r\f"
    bracket_stack = 0
    in_string = None

    end = len(text)
    i = begin
    while i < end:
        c = text[i]
        if in_string:
            if c == in_string:
                in_string = None
            elif c == "\\":
                i += 1
        elif c in "'\"":
            in_string = c
        elif c in "[({":
            bracket_stack += 1
        elif c in "])}":
            bracket_stack -= 1
        elif c in end_chars and not bracket_stack:
            break

        i += 1

    expression = text[begin:i].strip()
    terminator = text[i:i + 1]
    return i + 1, expression, terminator