private static void close_frame_impl_(ErrorState state,
                                        Frame frame,
                                        PsiBuilder builder,
                                        PsiBuilder.Marker marker,
                                        IElementType elementType,
                                        boolean result,
                                        boolean pinned) {
    if (((frame.modifiers & _AND_) | (frame.modifiers & _NOT_)) != 0) {
      boolean resetLastPos = !state.suppressErrors && frame.lastVariantAt < 0 && frame.position < builder.rawTokenIndex();
      close_marker_impl_(frame, marker, null, false);
      state.predicateCount--;
      if ((frame.modifiers & _NOT_) != 0) state.predicateSign = !state.predicateSign;
      marker = elementType != null && marker != null && (result || pinned) ? builder.mark() : null;
      if (resetLastPos) frame.lastVariantAt = builder.rawTokenIndex();
    }
    if (elementType != null && marker != null) {
      if (result || pinned) {
        if ((frame.modifiers & _COLLAPSE_) != 0) {
          PsiBuilder.Marker last = (PsiBuilder.Marker)builder.getLatestDoneMarker();
          if (last != null &&
              last.getStartIndex() == frame.position &&
              state.typeExtends(last.getTokenType(), elementType) &&
              wasAutoSkipped(builder, builder.rawTokenIndex() - last.getEndIndex())) {
            elementType = last.getTokenType();
            last.drop();
          }
        }
        if ((frame.modifiers & _UPPER_) != 0) {
          marker.drop();
          for (Frame f = frame.parentFrame; f != null; f = f.parentFrame) {
            if (f.elementType == null) continue;
            f.elementType = elementType;
            break;
          }
        }
        else if ((frame.modifiers & _LEFT_INNER_) != 0 && frame.leftMarker != null) {
          marker.done(elementType);
          extend_marker_impl(frame.leftMarker);
        }
        else if ((frame.modifiers & _LEFT_) != 0 && frame.leftMarker != null) {
          marker.drop();
          frame.leftMarker.precede().done(elementType);
        }
        else {
          if (frame.level == 0) builder.eof(); // skip whitespaces
          marker.done(elementType);
        }
      }
      else {
        close_marker_impl_(frame, marker, null, false);
      }
    }
    else if (result || pinned) {
      if (marker != null) marker.drop();
      if ((frame.modifiers & _LEFT_INNER_) != 0 && frame.leftMarker != null) {
        extend_marker_impl(frame.leftMarker);
      }
    }
    else {
      close_marker_impl_(frame, marker, null, false);
    }
  }
private static void close_frame_impl_(ErrorState state,
                                        Frame frame,
                                        PsiBuilder builder,
                                        PsiBuilder.Marker marker,
                                        IElementType elementType,
                                        boolean result,
                                        boolean pinned)