public static boolean NeedsParentheses(Vector vec){
  if (vec.size() == 0) {
    return false;
  }
  ;
  int curCharNum=0;
  int curLineNum=0;
  int parenDepth=0;
  boolean inString=false;
  boolean needParen=false;
  while ((curLineNum < vec.size()) && (!needParen)) {
    String curLine=(String)vec.elementAt(0);
    while ((curCharNum < curLine.length()) && (!needParen)) {
      char curChar=curLine.charAt(curCharNum);
      if (inString) {
switch (curChar) {
case '\"':
          inString=false;
        break;
case '\\':
      curCharNum++;
    break;
}
;
}
 else {
boolean leftParen=false;
boolean rightParen=false;
boolean mayNeedParen=false;
char nextChar=' ';
if (curCharNum < curLine.length() - 1) {
  nextChar=curLine.charAt(curCharNum + 1);
}
switch (curChar) {
case '\"':
  inString=true;
break;
case '=':
mayNeedParen=true;
break;
case '#':
mayNeedParen=true;
break;
case '<':
if (nextChar == '<') {
curCharNum++;
leftParen=true;
}
 else {
mayNeedParen=true;
}
;
break;
case '>':
if (nextChar == '>') {
curCharNum++;
rightParen=true;
}
 else {
mayNeedParen=true;
}
;
break;
case '|':
if ((nextChar == '-') || ((curCharNum > 0) && (curLine.charAt(curCharNum - 1) == '-'))) {
mayNeedParen=true;
}
;
break;
case '\\':
if (!((nextChar == ' ') || (nextChar == 'o') || (nextChar == 'X'))) {
mayNeedParen=true;
}
;
break;
case '/':
if (nextChar == '\\') {
mayNeedParen=true;
}
;
break;
case '(':
case '[':
case '{':
leftParen=true;
break;
case ')':
case ']':
case '}':
rightParen=true;
break;
}
;
if (mayNeedParen && (parenDepth == 0)) {
needParen=true;
}
;
if (leftParen) {
parenDepth++;
}
;
if (rightParen) {
if (parenDepth == 0) {
needParen=true;
}
;
parenDepth--;
}
}
;
curCharNum++;
}
;
if (inString) {
needParen=true;
}
;
curLineNum++;
curCharNum=0;
}
return needParen;
}

public static boolean NeedsParentheses ( Vector vec ) { if ( vec . size ( ) == 0 ) { return false ; } ; int curCharNum = 0 ; int curLineNum = 0 ; int parenDepth = 0 ; boolean inString = false ; boolean needParen = false ; while ( ( curLineNum < vec . size ( ) ) && ( ! needParen ) ) { String curLine = ( String ) vec . elementAt ( 0 ) ; while ( ( curCharNum < curLine . length ( ) ) && ( ! needParen ) ) { char curChar = curLine . charAt ( curCharNum ) ; if ( inString ) { switch ( curChar ) { case '\"' : inString = false ; break ; case '\\' : curCharNum ++ ; break ; } ; } else { boolean leftParen = false ; boolean rightParen = false ; boolean mayNeedParen = false ; char nextChar = ' ' ; if ( curCharNum < curLine . length ( ) - 1 ) { nextChar = curLine . charAt ( curCharNum + 1 ) ; } switch ( curChar ) { case '\"' : inString = true ; break ; case '=' : mayNeedParen = true ; break ; case '#' : mayNeedParen = true ; break ; case '<' : if ( nextChar == '<' ) { curCharNum ++ ; leftParen = true ; } else { mayNeedParen = true ; } ; break ; case '>' : if ( nextChar == '>' ) { curCharNum ++ ; rightParen = true ; } else { mayNeedParen = true ; } ; break ; case '|' : if ( ( nextChar == '-' ) || ( ( curCharNum > 0 ) && ( curLine . charAt ( curCharNum - 1 ) == '-' ) ) ) { mayNeedParen = true ; } ; break ; case '\\' : if ( ! ( ( nextChar == ' ' ) || ( nextChar == 'o' ) || ( nextChar == 'X' ) ) ) { mayNeedParen = true ; } ; break ; case '/' : if ( nextChar == '\\' ) { mayNeedParen = true ; } ; break ; case '(' : case '[' : case '{' : leftParen = true ; break ; case ')' : case ']' : case '}' : rightParen = true ; break ; } ; if ( mayNeedParen && ( parenDepth == 0 ) ) { needParen = true ; } ; if ( leftParen ) { parenDepth ++ ; } ; if ( rightParen ) { if ( parenDepth == 0 ) { needParen = true ; } ; parenDepth -- ; } } ; curCharNum ++ ; } ; if ( inString ) { needParen = true ; } ; curLineNum ++ ; curCharNum = 0 ; } return needParen ; }
as part of adding the tla to pcal translation code , ll removedseparated the code that decides if parentheses are needed from the parenthesize method and put it into this method . the parenthesize method itself will not be needed .
