Beispiel #1
0
        public static (TextSpan, IIdentifierToken) ExpectIdentifier(this ITokenIterator tokens)
        {
            if (tokens.Current is IIdentifierToken identifier)
            {
                tokens.Next();
                return(identifier.Span, identifier);
            }

            tokens.Context.Diagnostics.Add(
                ParseError.MissingToken(tokens.Context.File, typeof(IIdentifierToken), tokens.Current));
            return(new TextSpan(tokens.Current.Span.Start, 0), null);
        }
Beispiel #2
0
        public static T RequiredToken <T>(this ITokenIterator <IToken> tokens)
            where T : IToken
        {
            if (tokens.Current is T token)
            {
                tokens.Next();
                return(token);
            }

            tokens.Context.Diagnostics.Add(
                ParseError.MissingToken(tokens.Context.File, typeof(T), tokens.Current));
            throw new ParseFailedException($"Requires {typeof(T).GetFriendlyName()}, found {tokens.Current.GetType().GetFriendlyName()}");
        }
Beispiel #3
0
        public static TextSpan Expect <T>(this ITokenIterator <IToken> tokens)
            where T : IToken
        {
            if (tokens.Current is T token)
            {
                tokens.Next();
                return(token.Span);
            }

            tokens.Context.Diagnostics.Add(
                ParseError.MissingToken(tokens.Context.File, typeof(T), tokens.Current));
            // An empty span at the current location
            return(new TextSpan(tokens.Current.Span.Start, 0));
        }