private bool ExpectIdentifier(string name, out Token token) { return ExpectToken( TokenType.Identifier, (value) => { return value.Equals(name, StringComparison.OrdinalIgnoreCase); }, out token); }
private bool ExpectToken(TokenType type, Func<string, bool> tokenPredicate, out Token token) { token = null; Token testToken = mLexer.Peek(); if (testToken.Type == type && tokenPredicate(testToken.Value)) { token = mLexer.Next(); } return (token != null); }
// Used by the parser to read and consume the next token public Token Next() { // If the next token has already been Peek()'d, return it if (mNext != null) { Token next = mNext; mNext = null; return next; } if (mCurrentPosition >= mInput.Length) { // Already parsed entire input string return Token.EndOfFile(); } char c = mInput[mCurrentPosition]; if (Char.IsWhiteSpace(c)) { return Token.Whitespace(ReadSimpleToken(Char.IsWhiteSpace)); } else if (Char.IsLetter(c)) { return Token.Identifier(ReadSimpleToken(Char.IsLetterOrDigit)); } else if (Char.IsDigit(c)) { return Token.Number(ReadSimpleToken(Char.IsDigit)); } else if (IsQuoteChar(c)) { return Token.String(ReadString(c)); } else if (!Char.IsControl(c)) { // Anything that fell through the above checks, and that is not a control character // is something we'll try to treat as a symbol string symbol = ReadSymbol(); if (!string.IsNullOrEmpty(symbol)) { return Token.Symbol(symbol); } } // Failed to match a token return Token.Error(mInput, mCurrentPosition); }
// Used by the parser to look at the next token, without consuming it public Token Peek() { // Implement peek by using a cache value for the next token if (mNext == null) { mNext = Next(); } return mNext; }
private bool ExpectToken(TokenType type, out Token token) { token = null; Token testToken = mLexer.Peek(); if (testToken.Type == type) { token = mLexer.Next(); } return (token != null); }
private bool ExpectString(out Token token) { return ExpectToken(TokenType.String, out token); }
private bool ExpectNumber(out Token token) { return ExpectToken(TokenType.Number, out token); }