public object ParseWithFixLogic(ITokenStream stream) { //WTM: Change: If the script is just a comment, resulting in only an EOF token, parser.ParseWithFixLogic fails. //The below check works around that. IToken[] firstTwoTokens = stream.Take(2).ToArray(); if (firstTwoTokens.Length == 1 && firstTwoTokens[0].Type == EOF_TOKEN_TYPE) { throw new EOFOnlyException(); } try { return(base.Parse(stream)); } catch (UnexpectedTokenException ex) when(ex.Token.Value == "endif") { bool isFixed = false; int nesting = 0; List <IToken> tokens = new List <IToken>(); foreach (var token in stream) { if (token.Type == "BranchStartToken") { ++nesting; tokens.Add(token); } else { if (token.Type == "BranchEndToken") { nesting = nesting - 1; if (nesting > -1) { tokens.Add(token); } else { isFixed = true; nesting = 0; //Clear up the token and nesting will be again 0 } } else { tokens.Add(token); } } } if (!isFixed) { throw; } ArrayTokenStream newTokenStream = new ArrayTokenStream(tokens); object newAST = this.Parse(newTokenStream); return(newAST); } }