private static String formatParseExceptionDetails ( String errorToken , List < String > expectedTokens ) { // quotes/normalize the expected tokens before rendering, just in case after normalization some // can be deduplicated. ImmutableSet . Builder < String > normalizedTokensBuilder = ImmutableSet . builder ( ) ; for ( String t : expectedTokens ) { normalizedTokensBuilder . add ( maybeQuoteForParseError ( t ) ) ; } expectedTokens = normalizedTokensBuilder . build ( ) . asList ( ) ; StringBuilder details = new StringBuilder ( ) ; int numExpectedTokens = expectedTokens . size ( ) ; if ( numExpectedTokens != 0 ) { details . append ( ": expected " ) ; for ( int i = 0 ; i < numExpectedTokens ; i ++ ) { details . append ( expectedTokens . get ( i ) ) ; if ( i < numExpectedTokens - 2 ) { details . append ( ", " ) ; } if ( i == numExpectedTokens - 2 ) { if ( numExpectedTokens > 2 ) { details . append ( ' ' ) ; } details . append ( " or " ) ; } } } return String . format ( "parse error at '%s'%s" , escapeWhitespaceForErrorPrinting ( errorToken ) , details . toString ( ) ) ; }
A helper method for formatting javacc ParseExceptions .