/// <summary> /// Throws an error if the current token does not match the given expression. /// </summary> /// <param name="expression">The expression to check.</param> public void Expect(string expression) { var match = CurrentToken.Equals(expression, StringComparison.OrdinalIgnoreCase); if (!match) { throw new ParserException($"'{expression}' expected but found '{CurrentToken}'", CurrentLineNumber); } Move(); }
/// <summary> /// Throws an error if the current token does not match any of the given expressions. /// </summary> /// <param name="expression">The expressions to check.</param> /// <returns>When this method returns, contains the index of the matching expression.</returns> public int ExpectAny(params string[] expressions) { for (int i = 0; i < expressions.Length; i++) { var expression = expressions[i]; var match = CurrentToken.Equals(expression, StringComparison.OrdinalIgnoreCase); if (match) { Move(); return(i); } } var expressionsStr = string.Join(", ", expressions.Select(o => $"'{o}'")); throw new ParserException($"'{expressionsStr}' expected but found '{CurrentToken}'", CurrentLineNumber); }