def split_segments(text, closing_paren=False):
buf = StringIO()<EOL>segments = []<EOL>combinators = []<EOL>last_group = False<EOL>iterator = iter(text)<EOL>last_negation = False<EOL>for character in iterator:<EOL><INDENT>if character in COMBINATORS:<EOL><INDENT>if last_negation:<EOL><INDENT>buf.write(constants.OPERATOR_NEGATION)<EOL><DEDENT>val = buf.getvalue()<EOL>reset_stringio(buf)<EOL>if not last_group and not len(val):<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>if len(val):<EOL><INDENT>segments.append(parse_segment(val))<EOL><DEDENT>combinators.append(COMBINATORS[character])<EOL><DEDENT>elif character == constants.GROUP_BEGIN:<EOL><INDENT>if buf.tell():<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>seg = split_segments(iterator, True)<EOL>if last_negation:<EOL><INDENT>seg = UnarySegmentCombinator(seg)<EOL><DEDENT>segments.append(seg)<EOL>last_group = True<EOL>continue<EOL><DEDENT>elif character == constants.GROUP_END:<EOL><INDENT>val = buf.getvalue()<EOL>if not buf.tell() or not closing_paren:<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>segments.append(parse_segment(val))<EOL>return combine(segments, combinators)<EOL><DEDENT>elif character == constants.OPERATOR_NEGATION and not buf.tell():<EOL><INDENT>last_negation = True<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>if last_negation:<EOL><INDENT>buf.write(constants.OPERATOR_NEGATION)<EOL><DEDENT>if last_group:<EOL><INDENT>raise ValueError('<STR_LIT>' % character)<EOL><DEDENT>buf.write(character)<EOL><DEDENT>last_negation = False<EOL>last_group = False<EOL><DEDENT>else:<EOL><INDENT>if closing_paren:<EOL><INDENT>raise ValueError('<STR_LIT>' % constants.GROUP_END)<EOL><DEDENT>if not last_group:<EOL><INDENT>segments.append(parse_segment(buf.getvalue()))<EOL><DEDENT><DEDENT>return combine(segments, combinators)<EOL>
Return objects representing segments.