Ejemplo n.º 1
0
        /// <summary>
        /// If the next token is a keyword with the given value, return that token after
        /// advancing the parser. Otherwise, do not change the parser state and return
        /// false.
        /// </summary>
        /// <param name="kind">The kind.</param>
        /// <returns></returns>
        /// <exception cref="SyntaxError"></exception>
        protected Token Expect(TokenKind kind)
        {
            var token = Token;

            if (token.Kind == kind)
            {
                Advance();
                return(token);
            }
            throw new SyntaxError(Source, token.Start,
                                  $"Expected {TokenKindHelpers.GetTokenKindDesc(kind)}, found {token.GetTokenDesc()}");
        }
Ejemplo n.º 2
0
 /// <summary>
 /// A helper function to describe a token as a string for debugging
 /// </summary>
 /// <returns></returns>
 public string GetTokenDesc()
 {
     return(String.IsNullOrEmpty(Value)
         ? TokenKindHelpers.GetTokenKindDesc(Kind)
         : $"{TokenKindHelpers.GetTokenKindDesc(Kind)} \"{Value}\"");
 }