/// <summary> /// Fastforwards to the source code's end. /// </summary> /// <returns>The last token, namely the EOF token.</returns> /// <param name="ex">An UnexpectedTokenException.</param> private Token FastForwardToSourceEnd(UnexpectedTokenException ex) { // report the error notifyError(new SyntaxError(ex.Token, ex.ExpectedType, ex.ExpectationSet)); syntaxTreeBuilt = false; Token token; // ask for new token's until the end of file has been reached do { token = scanner.getNextToken(null); } while (token.Type != TokenType.END_OF_FILE); return(token); }
/// <summary> /// Fastforwards to the end of a for-loop block. /// </summary> /// <returns>The token that ends the for-loop block</returns> /// <param name="ex">An UnexpectedTokenException.</param> private Token FastForwardToEndOfBlock(UnexpectedTokenException ex) { notifyError(new SyntaxError(ex.Token, ex.ExpectedType, ex.ExpectationSet)); Token token = ex.Token; // since blocks can be nested, we must track the depth we're in to make sure // we find the correct end statement int blockDepth = 0; bool blockEndFound = false; while (token.Type != TokenType.END_OF_FILE) { if (token.Type == TokenType.END_OF_BLOCK) // if an end of block is found { if (blockDepth == 0) // and the depth matches { return(scanner.getNextToken(null)); // return the next token } blockEndFound = true; } else if (token.Type == TokenType.FOR_LOOP) // if we find another for-loop token { if (blockEndFound) // and the last token was an end block token { blockDepth--; // decrease the depth blockEndFound = false; } else { blockDepth++; // otherwise, increase the depth } } else { blockEndFound = false; } token = scanner.getNextToken(null); } return(token); }
/// <summary> /// Fastforwards to the statement's end. /// </summary> /// <returns>The token that ends the statement.</returns> /// <param name="ex">An UnexpectedTokenException.</param> private Token FastForwardToStatementEnd(UnexpectedTokenException ex) { notifyError(new SyntaxError(ex.Token, ex.ExpectedType, ex.ExpectationSet)); return(FastForwardTo(ParserConstants.STATEMENT_FASTFORWARD_TO, ex.Token)); }