protected String resolveAvailable ( CommonTokenStream tokens , EvaluationContext context ) { boolean hasMissing = false ; List < Object > outputComponents = new ArrayList <> ( ) ; for ( int t = 0 ; t < tokens . size ( ) - 1 ; t ++ ) { // we can ignore the final EOF token Token token = tokens . get ( t ) ; Token nextToken = tokens . get ( t + 1 ) ; // if token is a NAME not followed by ( then it's a context reference if ( token . getType ( ) == ExcellentParser . NAME && nextToken . getType ( ) != ExcellentParser . LPAREN ) { try { outputComponents . add ( context . resolveVariable ( token . getText ( ) ) ) ; } catch ( EvaluationError ex ) { hasMissing = true ; outputComponents . add ( token ) ; } } else { outputComponents . add ( token ) ; } } // if we don't have missing context references, perform evaluation as normal if ( ! hasMissing ) { return null ; } // re-combine the tokens and context values back into an expression StringBuilder output = new StringBuilder ( String . valueOf ( m_expressionPrefix ) ) ; for ( Object outputComponent : outputComponents ) { String compVal ; if ( outputComponent instanceof Token ) { compVal = ( ( Token ) outputComponent ) . getText ( ) ; } else { compVal = Conversions . toRepr ( outputComponent , context ) ; } output . append ( compVal ) ; } return output . toString ( ) ; }
Checks the token stream for context references and if there are missing references - substitutes available references and returns a partially evaluated expression .