public SourceSpan GetApplicableSpanFromLastToken(Node containingNode) { if (containingNode != null && Position >= containingNode.EndIndex) { var token = TokenSource.Tokens.LastOrDefault(); if (token.Key.End >= Position) { return(TokenSource.GetTokenSpan(token.Key)); } } return(default);
private IEnumerable <Token> ReadExpressionTokens(IEnumerable <KeyValuePair <IndexSpan, Token> > tokens) { var nesting = 0; var exprTokens = new Stack <Token>(); var currentLine = -1; foreach (var t in tokens) { var p = _tokenSource.GetTokenSpan(t.Key).Start; if (p.Line > currentLine) { currentLine = p.Line; } else if (p.Line < currentLine && nesting == 0) { break; } exprTokens.Push(t.Value); switch (t.Value.Kind) { case TokenKind.RightParenthesis: case TokenKind.RightBracket: case TokenKind.RightBrace: nesting += 1; break; case TokenKind.LeftParenthesis: case TokenKind.LeftBracket: case TokenKind.LeftBrace: if (--nesting < 0) { exprTokens.Pop(); return(exprTokens); } break; case TokenKind.Comment: exprTokens.Pop(); break; case TokenKind.Name: case TokenKind.Constant: case TokenKind.Dot: case TokenKind.Ellipsis: case TokenKind.MatMultiply: case TokenKind.KeywordAwait: break; case TokenKind.Assign: case TokenKind.LeftShiftEqual: case TokenKind.RightShiftEqual: case TokenKind.BitwiseAndEqual: case TokenKind.BitwiseOrEqual: case TokenKind.ExclusiveOrEqual: exprTokens.Pop(); return(exprTokens); default: if (t.Value.Kind >= TokenKind.FirstKeyword || nesting == 0) { exprTokens.Pop(); return(exprTokens); } break; } } return(exprTokens); }