if (result == null) {
return new ExtendedParseResult<T>(null, Context.NOT_PARSED);
}
JsonArray<Token> tokens = result.getTokens();
Token lastToken = tokens.peek();
Preconditions.checkNotNull(lastToken,
"Last token expected to be non-null; text='%s', position=%s", text, position);
TokenType lastTokenType = lastToken.getType();
String lastTokenValue = lastToken.getValue();
if (!addSpace) {
if (lastTokenType == STRING || lastTokenType == REGEXP) {
return new ExtendedParseResult<T>(result, Context.IN_STRING);
} else if (lastTokenType == TokenType.COMMENT) {
return new ExtendedParseResult<T>(result, Context.IN_COMMENT);
}
// Python parser, for a purpose of simplicity, parses period and variable
// name as a single token. If period is not followed by identifier, parser
// states that this is and error, which is, generally, not truth.
if ((lastTokenType == TokenType.ERROR) && LITERAL_PERIOD.equals(lastTokenValue)) {
tokens.pop();
tokens.add(new Token(lastToken.getMode(), TokenType.NULL, LITERAL_PERIOD));
}
return new ExtendedParseResult<T>(result, Context.IN_CODE);
}
// Remove / shorten last token to omit added whitespace.
tokens.pop();
if (lastTokenType == STRING || lastTokenType == REGEXP || lastTokenType == TokenType.COMMENT) {
// Whitespace stuck to token - strip it.
lastTokenValue = lastTokenValue.substring(0, lastTokenValue.length() - 1);
tokens.add(new Token(lastToken.getMode(), lastTokenType, lastTokenValue));
if (lastTokenType == STRING || lastTokenType == REGEXP) {
return new ExtendedParseResult<T>(result, Context.IN_STRING);
} else {
return new ExtendedParseResult<T>(result, Context.IN_COMMENT);
}