/// <summary> /// Lookahead N /// </summary> /// <param name="index"></param> /// <returns></returns> protected Token Peek(int index = 0) { while (TokenBuf.Count <= index) { TokenBuf.Enqueue(NextToken()); } Queue <Token> .Enumerator iter = TokenBuf.GetEnumerator(); for (int i = 0; i <= index; ++i) { iter.MoveNext(); } return(iter.Current); }
/// <summary> /// types为空表示接受任何Token /// </summary> /// <param name="types"></param> /// <returns></returns> protected Token Consume(params TokenType[] types) { if (TokenBuf.Count == 0) { TokenBuf.Enqueue(NextToken()); } if (types.Length != 0 && !Check(types)) { Token t = TokenBuf.Dequeue(); StringBuilder sb = new StringBuilder("Expect ").Append(types[0].ToString()); for (int i = 1; i < types.Length; ++i) { sb.Append("/").Append(types[i].ToString()); } throw new SyntaxError(sb.ToString(), t); } return(TokenBuf.Dequeue()); }