/// <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); }
private Token ReadToken(int distance) { while (distance >= _readTokens.Count) { _readTokens.Add(_tokenIterator.Next()); } return(_readTokens[distance]); }
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); }
public static bool Accept <T>(this ITokenIterator <IToken> tokens) where T : class, IToken { if (tokens.Current is T) { tokens.Next(); return(true); } return(false); }
public bool Next() { do { if (!tokens.Next()) { return(false); } } while (tokens.Current is ITriviaToken); return(true); }
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); }
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(); }
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()}"); }
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)); }