/// <summary> /// Creates a new parser /// </summary> /// <param name="tokens">The tokens</param> /// <param name="binaryOperatorPrecedences">The binary operator precedence table</param> public Parser(IEnumerable<Token> tokens, IDictionary<char, int> binaryOperatorPrecedences = null) { this.tokens = tokens; this.currentToken = null; if (binaryOperatorPrecedences == null) { this.binaryOperatorPrecedence = new Dictionary<char, int>(); } else { this.binaryOperatorPrecedence = binaryOperatorPrecedences; } //This operators must exist if (!this.binaryOperatorPrecedence.ContainsKey('<')) { this.binaryOperatorPrecedence.Add('<', 10); } if (!this.binaryOperatorPrecedence.ContainsKey('+')) { this.binaryOperatorPrecedence.Add('+', 20); } if (!this.binaryOperatorPrecedence.ContainsKey('-')) { this.binaryOperatorPrecedence.Add('-', 20); } if (!this.binaryOperatorPrecedence.ContainsKey('*')) { this.binaryOperatorPrecedence.Add('*', 40); } }
/// <summary> /// Advances to the next token /// </summary> private void NextToken() { this.currentToken = this.tokens.FirstOrDefault(); this.tokens = this.tokens.Skip(1); }