* This is where we do a lookahead to see if we find some token and if we do find it, but not on the correct
* position, we skip some tokens to go to it.
*/
public ISpecialStr createSpecialStr(String token, boolean searchOnLast, boolean throwException)
throws ParseException {
final Token currentToken = grammar.getCurrentToken();
Token firstTokenToIterate;
if (searchOnLast) {
firstTokenToIterate = grammar.getJJLastPos();
} else {
firstTokenToIterate = currentToken;
}
Token foundToken = null;
int foundAtPos = 0;
//lot's of tokens, but we'll bail out on an indent, or dedent, so, that's OK.
TokensIterator iterTokens = grammar.getTokensIterator(firstTokenToIterate, 50, true);
while (iterTokens.hasNext()) {
foundAtPos += 1;
Token next = iterTokens.next();
if (next.image != null && next.image.equals(token)) {
//Found what we were looking for!
foundToken = next;
break;
}
}
if (foundToken != null) {
if (foundAtPos <= 2 //found at correct position.
|| searchOnLast //we already matched it... right now we're just adding it to the stack!
) {
return foundToken.asSpecialStr();
}
}
if (throwException) {
ParseException e = createException(token, currentToken);
//we found it at the wrong position!
if (foundToken != null) {
//we found it, but not on the position we were expecting, so, we must skip some tokens to get to it --
//and report the needed exception)
grammar.addAndReport(e, "Found at wrong position: " + foundToken);
Token beforeLastReturned = iterTokens.getBeforeLastReturned();
grammar.setCurrentToken(beforeLastReturned);
return foundToken.asSpecialStr();
}
//create a 'synthetic token' in the place we were expecting it.