Beispiel #1
0
        /// <summary>
        /// The current token is unexpected, report an error and consume it.
        /// </summary>
        public static TextSpan UnexpectedToken(this ITokenIterator tokens)
        {
            // TODO shouldn't we ignore or combine unexpected token errors until we parse something successfully?
            var span = tokens.Current.Span;

            tokens.Context.Diagnostics.Add(ParseError.UnexpectedToken(tokens.Context.File, span));
            tokens.Next();
            return(span);
        }
Beispiel #2
0
        private Token ReadToken(int distance)
        {
            while (distance >= _readTokens.Count)
            {
                _readTokens.Add(_tokenIterator.Next());
            }

            return(_readTokens[distance]);
        }
Beispiel #3
0
        public static T?AcceptToken <T>(this ITokenIterator <IToken> tokens)
            where T : class, IToken
        {
            if (tokens.Current is T token)
            {
                tokens.Next();
                return(token);
            }

            return(null);
        }
Beispiel #4
0
        public static bool Accept <T>(this ITokenIterator <IToken> tokens)
            where T : class, IToken
        {
            if (tokens.Current is T)
            {
                tokens.Next();
                return(true);
            }

            return(false);
        }
Beispiel #5
0
            public bool Next()
            {
                do
                {
                    if (!tokens.Next())
                    {
                        return(false);
                    }
                } while (tokens.Current is ITriviaToken);

                return(true);
            }
Beispiel #6
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 #7
0
        public LexResult(ITokenIterator iterator)
        {
            var tokens = new List <IToken>();

            do
            {
                tokens.Add(iterator.Current);
            } while (iterator.Next());

            File        = iterator.Context.File;
            Tokens      = tokens.ToFixedList();
            Diagnostics = iterator.Context.Diagnostics.Build();
        }
Beispiel #8
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 #9
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));
        }