# Buggy code
def match(
    self,
    segments,
    match_depth=0,
    parse_depth=0,
    verbosity=0,
    dialect=None,
    match_segment=None,
):
    """The match function for `bracketed` implements bracket counting."""

    # 1. work forwards to find the first bracket.
    #    If we find something other that whitespace, then fail out.
    # 2. Once we have the first bracket, we need to bracket count forward to find it's partner.
    # 3. Assuming we find it's partner then we try and match what goes between them.
    #    If we match, great. If not, then we return an empty match.
    #    If we never find it's partner then we return an empty match but should probably
    #    log a parsing warning, or error?

    sub_bracket_count = 0
    pre_content_segments = tuple()
    unmatched_segs = segments
    matched_segs = tuple()
    current_bracket_segment = None

    # Step 1. Find the first useful segment
    # Work through to find the first code segment...
    if self.code_only:
        for idx, seg in enumerate(segments):
            if seg.is_code:
                break
            else:
                matched_segs += (seg,)
                unmatched_segs = unmatched_segs[1:]
        else:
            # We've trying to match on a sequence of segments which contain no code.
            # That means this isn't a match.
            return MatchResult.from_unmatched(segments)

    # is it a bracket?
    m = self.start_bracket._match(
        segments=unmatched_segs,
        match_depth=match_depth + 1,
        parse_depth=parse_depth,
        verbosity=verbosity,
        dialect=dialect,
        match_segment=match_segment,
    )

    if m.has_match():
        # We've got the first bracket.
        # Update the seg_idx by the length of the match
        current_bracket_segment = m.matched_segments[0]
        # No indexing to allow mutation
        matched_segs += m.matched_segments
        unmatched_segs = m.unmatched_segments
    else:
        # Whatever we have, it doesn't start with a bracket.
        return MatchResult.from_unmatched(segments)

    # Step 2: Bracket count forward to find it's pair
    content_segments = tuple()
    pre_content_segments = matched_segs

    while True:
        # Are we at the end of the sequence?
        if len(unmatched_segs) == 0:
            # We've got to the end without finding the closing bracket
            # this isn't just parsing issue this is probably a syntax
            # error.
            # TODO: Format this better
            raise SQLParseError(
                "Couldn't find closing bracket for opening bracket.",
                segment=current_bracket_segment,
            )

        # Is it a closing bracket?
        m = self.end_bracket._match(
            segments=unmatched_segs,
            match_depth=match_depth + 1,
            parse_depth=parse_depth,
            verbosity=verbosity,
            dialect=dialect,
            match_segment=match_segment,
        )
        if m.has_match():
            if sub_bracket_count == 0:
                # We're back to the bracket pair!
                matched_segs += m.matched_segments
                unmatched_segs = m.unmatched_segments
                closing_bracket_segs = m.matched_segments
                break
            else:
                # reduce the bracket count and then advance the counter.
                sub_bracket_count -= 1
                matched_segs += m.matched_segments
                unmatched_segs = m.unmatched_segments
                continue

        # Is it an opening bracket?
        m = self.start_bracket._match(
            segments=unmatched_segs,
            match_depth=match_depth + 1,
            parse_depth=parse_depth,
            verbosity=verbosity,
            dialect=dialect,
            match_segment=match_segment,
        )
        if m.has_match():
            # increment the open bracket counter and proceed
            sub_bracket_count += 1
            matched_segs += m.matched_segments
            unmatched_segs = m.unmatched_segments
            continue

        # If we get here it's not an opening bracket or a closing bracket
        # so we should carry on our merry way
        matched_segs += (unmatched_segs[0],)
        content_segments += (unmatched_segs[0],)
        unmatched_segs = unmatched_segs[1:]

    # If we get to here then we've found our closing bracket.
    # Let's identify the section to match for our content matchers
    # and then try it against each of them.

    for elem in self._elements:
        elem_match = elem._match(
            content_segments,
            match_depth=match_depth + 1,
            parse_depth=parse_depth,
            verbosity=verbosity,
            dialect=dialect,
            match_segment=match_segment,
        )
        # Matches at this stage must be complete, because we've got nothing
        # to do with any leftovers within the brackets.
        if elem_match.is_complete():
            # We're also returning the *mutated* versions from the sub-matcher
            return MatchResult(
                pre_content_segments
                + elem_match.matched_segments
                + closing_bracket_segs,
                unmatched_segs,
            )
        else:
            # Not matched this element, move on.
            # NB, a partial match here isn't helpful. We're matching
            # BETWEEN two delimiters and so it must be a complete match.
            # Incomplete matches are only possible at the end
            continue
    else:
        # If we're here we haven't matched any of the elements, then we have a problem
        return MatchResult.from_unmatched(segments)


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/user/sql/.venv/bin/sqlfluff", line 11, in <module>
load_entry_point('sqlfluff==0.1.3', 'console_scripts', 'sqlfluff')()
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/cli/commands.py", line 85, in lint
result = lnt.lint_paths(paths, verbosity=verbose)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 324, in lint_paths
result.add(self.lint_path(path, verbosity=verbosity, fix=fix))
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 312, in lint_path
linted_path.add(self.lint_file(f, fname=fname, verbosity=verbosity, fix=fix))
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 212, in lint_file
parsed, vs, time_dict = self.parse_file(f=f, fname=fname, verbosity=verbosity)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 193, in parse_file
parsed = fs.parse(recurse=recurse, verbosity=verbosity, dialect=self.dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 186, in parse
dialect=dialect, match_segment=self.__class__.__name__)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 273, in match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 485, in match
match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 273, in match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 236, in match
dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 361, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 320, in match
dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 586, in match
segment=segments[start_bracket_idx])
TypeError: tuple indices must be integers or slices, not NoneType

# Fixed code
def match(self, segments, parse_context):
    """The match function for `bracketed` implements bracket counting."""

    # 1. work forwards to find the first bracket.
    #    If we find something other that whitespace, then fail out.
    # 2. Once we have the first bracket, we need to bracket count forward to find it's partner.
    # 3. Assuming we find it's partner then we try and match what goes between them.
    #    If we match, great. If not, then we return an empty match.
    #    If we never find it's partner then we return an empty match but should probably
    #    log a parsing warning, or error?

    seg_buff = segments
    matched_segs = ()

    # Look for the first bracket
    start_match = self._code_only_sensitive_match(
        seg_buff,
        self.start_bracket,
        parse_context=parse_context.copy(incr="match_depth"),
        code_only=self.code_only,
    )
    if start_match:
        seg_buff = start_match.unmatched_segments
    else:
        # Can't find the opening bracket. No Match.
        return MatchResult.from_unmatched(segments)

    # Look for the closing bracket
    pre, end_match, _ = self._bracket_sensitive_look_ahead_match(
        segments=seg_buff,
        matchers=[self.end_bracket],
        parse_context=parse_context,
        code_only=self.code_only,
    )
    if not end_match:
        raise SQLParseError(
            "Couldn't find closing bracket for opening bracket.", segment=matched_segs
        )

    # Match the content now we've confirmed the brackets. We use the
    # _longest helper function mostly just because it deals with multiple
    # matchers.
    content_match, _ = self._longest_code_only_sensitive_match(
        pre,
        self._elements,
        parse_context=parse_context.copy(incr="match_depth"),
        code_only=self.code_only,
    )

    # We require a complete match for the content (hopefully for obvious reasons)
    if content_match.is_complete():
        return MatchResult(
            start_match.matched_segments
            + content_match.matched_segments
            + end_match.matched_segments,
            end_match.unmatched_segments,
        )
    else:
        return MatchResult.from_unmatched(segments)