public String chompBalanced(char open,char close){
  int start=-1;
  int end=-1;
  int depth=0;
  char last=0;
  do {
    if (isEmpty())     break;
    Character c=consume();
    if (last == 0 || last != ESC) {
      if (c.equals(open)) {
        depth++;
        if (start == -1)         start=pos;
      }
 else       if (c.equals(close))       depth--;
    }
    if (depth > 0 && last != 0)     end=pos;
    last=c;
  }
 while (depth > 0);
  return (end >= 0) ? queue.substring(start,end) : "";
}

public String chompBalanced ( char open , char close ) { int start = - 1 ; int end = - 1 ; int depth = 0 ; char last = 0 ; do { if ( isEmpty ( ) ) break ; Character c = consume ( ) ; if ( last == 0 || last != ESC ) { if ( c . equals ( open ) ) { depth ++ ; if ( start == - 1 ) start = pos ; } else if ( c . equals ( close ) ) depth -- ; } if ( depth > 0 && last != 0 ) end = pos ; last = c ; } while ( depth > 0 ) ; return ( end >= 0 ) ? queue . substring ( start , end ) : "" ; }
pulls a balanced string off the queue . e . g . if queue is " ( one ( two ) three ) four " , ( , ) will return " one ( two ) three " , and leave " four " on the queue . unbalanced openers and closers can be escaped ( with \ ) . those escapes will be left in the returned string , which is suitable for regexes ( where we need to preserve the escape ) , but unsuitable for contains text strings ; use unescape for that .
