public static String reindentGameDescription ( String gameRules ) throws Exception { List < Symbol > tokens = ParserHelper . scan ( gameRules , true ) ; tokens = collapseWhitespaceTokens ( tokens ) ; StringBuilder result = new StringBuilder ( ) ; int parensLevel = 0 ; // "( so far" minus ") so far" int nestingLevel = 0 ; // "<=" and "or" level; when == parens level, use newlines for ( int i = 0 ; i < tokens . size ( ) ; i ++ ) { Symbol token = tokens . get ( i ) ; switch ( token . sym ) { case Symbols . CONSTANT : case Symbols . VARIABLE : result . append ( token . value ) ; break ; case Symbols . POPEN : result . append ( "(" ) ; parensLevel ++ ; break ; case Symbols . PCLOSE : result . append ( ")" ) ; if ( parensLevel > 0 ) { parensLevel -- ; } if ( nestingLevel > parensLevel ) { nestingLevel = parensLevel ; } break ; case Symbols . IMPLIES : result . append ( "<=" ) ; nestingLevel ++ ; break ; case Symbols . NOT : result . append ( "not" ) ; break ; case Symbols . DISTINCT : result . append ( "distinct" ) ; break ; case Symbols . OR : result . append ( "or" ) ; nestingLevel ++ ; break ; case Symbols . EOF : break ; case Symbols2 . COMMENT : result . append ( token . value ) ; break ; case Symbols2 . WHITESPACE : result . append ( rewriteWhitespace ( ( String ) token . value , tokens , i , parensLevel , nestingLevel ) ) ; break ; } } return result . toString ( ) ; }
Returns null if the indentation fails .