/// <summary> /// Parses a field. /// <see cref="FieldNode" />: /// Alias? : Name Arguments? Directives? SelectionSet? /// </summary> /// <param name="context">The parser context.</param> private FieldNode ParseField() { ISyntaxToken start = _reader.Token; NameNode name = ParseName(); NameNode?alias = null; if (SkipColon()) { alias = name; name = ParseName(); } List <ArgumentNode> arguments = ParseArguments(false); List <DirectiveNode> directives = ParseDirectives(false); SelectionSetNode? selectionSet = _reader.Kind == TokenKind.LeftBrace ? ParseSelectionSet() : null; var location = new Location(start, _reader.Token); return(new FieldNode ( location, name, alias, directives, arguments, selectionSet )); }
/// <summary> /// Parses input value definitions. /// <see cref="InputValueDefinitionNode" />: /// Description? Name : Type DefaultValue? Directives[isConstant=true]? /// </summary> /// <param name="context">The parser context.</param> private InputValueDefinitionNode ParseInputValueDefinition() { ISyntaxToken start = _reader.Token; StringValueNode?description = ParseDescription(); NameNode name = ParseName(); ExpectColon(); ITypeNode type = ParseTypeReference(); IValueNode?defaultValue = SkipEqual() ? ParseValueLiteral(true) : null; List <DirectiveNode> directives = ParseDirectives(true); var location = new Location(start, _reader.Token); return(new InputValueDefinitionNode ( location, name, description, type, defaultValue, directives )); }
private DirectiveDefinitionNode ParseDirectiveDefinition() { ISyntaxToken start = _reader.Token; StringValueNode?description = ParseDescription(); ExpectDirectiveKeyword(); ExpectAt(); NameNode name = ParseName(); List <InputValueDefinitionNode> arguments = ParseArgumentDefinitions(); bool isRepeatable = SkipRepeatableKeyword(); ExpectOnKeyword(); List <NameNode> locations = ParseDirectiveLocations(); var location = new Location(start, _reader.Token); return(new DirectiveDefinitionNode ( location, name, description, isRepeatable, arguments, locations )); }
private unsafe void ParseDirectiveLocation() { ISyntaxToken token = _reader.Token; if (token.Kind == TokenKind.Name) { fixed(char *c = _reader.Value) { string name = new string(c, 0, _reader.Value.Length); if (DirectiveLocation.IsValidName(name)) { _classifications.AddClassification( SyntaxClassificationKind.DirectiveLocation, _reader.Token); MoveNext(); return; } } } _classifications.AddClassification( SyntaxClassificationKind.Error, _reader.Token); MoveNext(); }
public static ISyntaxToken GetPreviousToken(ISyntaxToken token, bool includeZeroLength, bool includeSkippedTokens) { var tokenPredicate = GetTokenPredicate(includeZeroLength); var triviaPredicate = GetTriviaPredicate(includeSkippedTokens); return(GetPreviousToken(token, true, tokenPredicate, triviaPredicate)); }
private ObjectTypeExtensionNode ParseObjectTypeExtension( ISyntaxToken start) { MoveNext(); NameNode name = ParseName(); List <NamedTypeNode> interfaces = ParseImplementsInterfaces(); List <DirectiveNode> directives = ParseDirectives(true); List <FieldDefinitionNode> fields = ParseFieldsDefinition(); var location = new Location(start, _reader.Token); if (interfaces.Count == 0 && directives.Count == 0 && fields.Count == 0) { throw Unexpected(_reader.Kind); } return(new ObjectTypeExtensionNode ( location, name, directives, interfaces, fields )); }
private static ISyntaxToken GetPreviousTokenFromTrivia(ISyntaxToken token, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate) { if (token == null) { return(null); } var tt = GetPreviousToken(token.TrailingTrivia, token, tokenPredicate, triviaPredicate); if (tt != null) { return(tt); } var t = GetPreviousToken(token, false, tokenPredicate, triviaPredicate); if (t != null) { return(t); } var lt = GetPreviousToken(token.LeadingTrivia, token, tokenPredicate, triviaPredicate); if (lt != null) { return(lt); } return(null); }
/// <summary> /// Parse a variable. /// <see cref="VariableNode" />: /// $Name /// </summary> private void ParseVariableName(bool isReference) { ISyntaxToken start = _reader.Token; SyntaxClassificationKind classificationKind = isReference ? SyntaxClassificationKind.VariableReference : SyntaxClassificationKind.VariableIdentifier; if (_reader.Kind == TokenKind.Dollar) { MoveNext(); _classifications.AddClassification( _reader.Kind == TokenKind.Name ? classificationKind : SyntaxClassificationKind.Error, new Location(start, _reader.Token)); } else { _classifications.AddClassification( SyntaxClassificationKind.Error, _reader.Token); } MoveNext(); }
/// <summary> /// Parses a list value. /// <see cref="ListValueNode" />: /// - [ ] /// - [ Value[isConstant]+ ] /// </summary> /// <param name="context">The parser context.</param> /// <param name="isConstant"> /// Defines if only constant values are allowed; /// otherwise, variables are allowed. /// </param> private ListValueNode ParseList(bool isConstant) { ISyntaxToken start = _reader.Token; if (_reader.Kind != TokenKind.LeftBracket) { throw new SyntaxException(_reader, string.Format( CultureInfo.InvariantCulture, LangResources.ParseMany_InvalidOpenToken, TokenKind.LeftBracket, TokenVisualizer.Visualize(in _reader))); } var items = new List <IValueNode>(); // skip opening token MoveNext(); while (_reader.Kind != TokenKind.RightBracket) { items.Add(ParseValueLiteral(isConstant)); } // skip closing token Expect(TokenKind.RightBracket); var location = new Location(start, _reader.Token); return(new ListValueNode ( location, items )); }
/// <summary> /// Parses a field. /// <see cref="FieldNode" />: /// Alias? : Name Arguments? Directives? SelectionSet? /// </summary> private void ParseField() { ISyntaxToken start = _reader.Token; if (start.Kind == TokenKind.Name) { MoveNext(); if (SkipColon()) { _classifications.AddClassification( SyntaxClassificationKind.FieldAlias, start); ParseName(SyntaxClassificationKind.FieldReference); } else { _classifications.AddClassification( SyntaxClassificationKind.FieldReference, start); } } ParseArguments(false); ParseDirectives(false); if (_reader.Kind == TokenKind.LeftBrace) { ParseSelectionSet(); } }
protected async Task <QuickInfoContent> CreateContentAsync( Workspace workspace, ISyntaxToken token, SemanticModelBase semanticModel, IEnumerable <ISymbol> symbols, CancellationToken cancellationToken) { var descriptionService = workspace.Services.GetLanguageServices(token.Language).GetService <ISymbolDisplayService>(); var sections = await descriptionService.ToDescriptionGroupsAsync(workspace, semanticModel, token.FileSpan.Span.Start, symbols.ToImmutableArray(), cancellationToken).ConfigureAwait(false); var mainDescriptionBuilder = new List <TaggedText>(); if (sections.ContainsKey(SymbolDescriptionGroups.MainDescription)) { mainDescriptionBuilder.AddRange(sections[SymbolDescriptionGroups.MainDescription]); } var documentationContent = GetDocumentationContent(symbols, sections); return(new QuickInfoDisplayContent( semanticModel.Language, glyph: symbols?.First().GetGlyph() ?? Glyph.None, mainDescription: ImmutableArray.CreateRange(mainDescriptionBuilder), documentation: documentationContent)); }
/// <summary> /// Parses a selection set. /// <see cref="SelectionSetNode" />: /// { Selection+ } /// </summary> /// <param name="context">The parser context.</param> private SelectionSetNode ParseSelectionSet() { ISyntaxToken start = _reader.Token; if (_reader.Kind != TokenKind.LeftBrace) { throw new SyntaxException(_reader, string.Format( CultureInfo.InvariantCulture, LangResources.ParseMany_InvalidOpenToken, TokenKind.LeftBrace, TokenVisualizer.Visualize(in _reader))); } var selections = new List <ISelectionNode>(); // skip opening token MoveNext(); while (_reader.Kind != TokenKind.RightBrace) { selections.Add(ParseSelection()); } // skip closing token ExpectRightBrace(); var location = new Location(start, _reader.Token); return(new SelectionSetNode ( location, selections )); }
private StringValueNode ParseStringLiteral() { ISyntaxToken start = _reader.Token; bool isBlock = _reader.Kind == TokenKind.BlockString; string value = ExpectString(); var location = new Location(start, _reader.Token); return(new StringValueNode(location, value, isBlock)); }
public ISymbol GetDeclaredSymbol(SemanticModelBase semanticModel, ISyntaxToken token, CancellationToken cancellationToken) { var location = token.SourceRange; var q = from node in ((SyntaxToken)token).Ancestors() let symbol = semanticModel.GetDeclaredSymbol(node) where symbol != null && symbol.Locations.Contains(location) select symbol; return(q.FirstOrDefault()); }
/// <summary> /// Parses a named type. /// <see cref="NamedTypeNode" />: /// Name /// </summary> /// <param name="context">The parser context.</param> private NamedTypeNode ParseNamedType() { ISyntaxToken start = _reader.Token; NameNode name = ParseName(); var location = new Location(start, _reader.Token); return(new NamedTypeNode ( location, name )); }
private NameNode ParseName() { ISyntaxToken start = _reader.Token; string name = ExpectName(); var location = new Location(start, _reader.Token); return(new NameNode ( location, name )); }
public Location( ISyntaxToken start, ISyntaxToken end) { Start = start.Start; StartToken = start; End = end.End; EndToken = end; Length = end.End - start.Start; Line = start.Line; Column = start.Column; }
/// <summary> /// Parses a fragment spread. /// <see cref="FragmentSpreadNode" />: /// ... FragmentName Directives? /// </summary> /// <param name="context">The parser context.</param> /// <param name="start"> /// The start token of the current fragment node. /// </param> private FragmentSpreadNode ParseFragmentSpread(ISyntaxToken start) { NameNode name = ParseFragmentName(); List <DirectiveNode> directives = ParseDirectives(false); var location = new Location(start, _reader.Token); return(new FragmentSpreadNode ( location, name, directives )); }
/// <summary> /// Parse a variable. /// <see cref="VariableNode" />: /// $Name /// </summary> /// <param name="context">The parser context.</param> private VariableNode ParseVariable() { ISyntaxToken start = _reader.Token; ExpectDollar(); NameNode name = ParseName(); var location = new Location(start, _reader.Token); return(new VariableNode ( location, name )); }
private ITypeSystemExtensionNode ParseTypeExtension() { ISyntaxToken start = _reader.Token; // extensions do not have a description TakeDescription(); MoveNext(); if (_reader.Kind == TokenKind.Name) { if (_reader.Value.SequenceEqual(GraphQLKeywords.Schema)) { return(ParseSchemaExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Scalar)) { return(ParseScalarTypeExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Type)) { return(ParseObjectTypeExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Interface)) { return(ParseInterfaceTypeExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Union)) { return(ParseUnionTypeExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Enum)) { return(ParseEnumTypeExtension(start)); } if (_reader.Value.SequenceEqual(GraphQLKeywords.Input)) { return(ParseInputObjectTypeExtension(start)); } } throw Unexpected(_reader.Kind); }
/// <summary> /// Parses an inline fragment. /// <see cref="FragmentSpreadNode" />: /// ... TypeCondition? Directives? SelectionSet /// </summary> /// <param name="context">The parser context.</param> /// <param name="start"> /// The start token of the current fragment node. /// </param> /// <param name="typeCondition"> /// The fragment type condition. /// </param> private InlineFragmentNode ParseInlineFragment( ISyntaxToken start, NamedTypeNode?typeCondition) { List <DirectiveNode> directives = ParseDirectives(false); SelectionSetNode selectionSet = ParseSelectionSet(); var location = new Location(start, _reader.Token); return(new InlineFragmentNode ( location, typeCondition, directives, selectionSet )); }
/// <summary> /// Parses a short-hand form operation definition. /// <see cref="OperationDefinitionNode" />: /// SelectionSet /// </summary> /// <param name="context">The parser context.</param> private OperationDefinitionNode ParseShortOperationDefinition() { ISyntaxToken start = _reader.Token; SelectionSetNode selectionSet = ParseSelectionSet(); var location = new Location(start, _reader.Token); return(new OperationDefinitionNode ( location, null, OperationType.Query, Array.Empty <VariableDefinitionNode>(), Array.Empty <DirectiveNode>(), selectionSet )); }
private static ISyntaxToken GetNextToken(ISyntaxToken token, bool searchTrailingTrivia, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate) { if (searchTrailingTrivia) { var tt = GetFirstToken(token.TrailingTrivia, tokenPredicate, triviaPredicate); if (tt != null) { return(tt); } } if (token.Parent == null) { return(null); } var returnNext = false; foreach (var nodeOrToken in token.Parent.ChildNodes) { if (returnNext) { if (nodeOrToken.IsToken) { var t = GetFirstToken((ISyntaxToken)nodeOrToken, tokenPredicate, triviaPredicate); if (t != null) { return(t); } } else { var t = GetFirstToken(nodeOrToken, tokenPredicate, triviaPredicate); if (t != null) { return(t); } } } else if (nodeOrToken.IsToken && nodeOrToken == token) { returnNext = true; } } return(GetNextToken(token.Parent, tokenPredicate, triviaPredicate)); }
private static ISyntaxToken GetPreviousToken(ISyntaxToken token, bool searchLeadingTrivia, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate) { if (searchLeadingTrivia) { var lt = GetLastToken(token.LeadingTrivia, tokenPredicate, triviaPredicate); if (lt != null) { return(lt); } } if (token.Parent == null) { return(null); } var returnNext = false; foreach (var nodeOrToken in token.Parent.ChildNodes.Reverse()) { if (returnNext) { if (nodeOrToken.IsToken) { var t = GetLastToken((ISyntaxToken)nodeOrToken, tokenPredicate, triviaPredicate); if (t != null) { return(t); } } else { var t = GetLastToken(nodeOrToken, tokenPredicate, triviaPredicate); if (t != null) { return(t); } } } else if (nodeOrToken.IsToken && nodeOrToken == token) { returnNext = true; } } return(GetPreviousToken(token.Parent, tokenPredicate, triviaPredicate)); }
private DirectiveNode ParseDirective(bool isConstant) { ISyntaxToken start = _reader.Token; ExpectAt(); NameNode name = ParseName(); List <ArgumentNode> arguments = ParseArguments(isConstant); var location = new Location(start, _reader.Token); return(new DirectiveNode ( location, name, arguments )); }
public DocumentNode Parse() { var definitions = new List <IDefinitionNode>(); ISyntaxToken start = _reader.Token; MoveNext(); while (_reader.Kind != TokenKind.EndOfFile) { definitions.Add(ParseDefinition()); } var location = new Location(start, _reader.Token); return(new DocumentNode(location, definitions)); }
protected override async Task <QuickInfoContent> BuildContentAsync( Document document, ISyntaxToken token, CancellationToken cancellationToken) { var modelAndSymbols = await this.BindTokenAsync(document, token, cancellationToken).ConfigureAwait(false); if (modelAndSymbols.Item2.Length == 0) { return(null); } return(await CreateContentAsync(document.Workspace, token, modelAndSymbols.Item1, modelAndSymbols.Item2, cancellationToken : cancellationToken).ConfigureAwait(false)); }
/// <summary> /// Parses an enum value definitions. /// <see cref="EnumValueDefinitionNode" />: /// Description? EnumValue Directives[isConstant=true]? /// </summary> /// <param name="context">The parser context.</param> private EnumValueDefinitionNode ParseEnumValueDefinition() { ISyntaxToken start = _reader.Token; StringValueNode? description = ParseDescription(); NameNode name = ParseName(); List <DirectiveNode> directives = ParseDirectives(true); var location = new Location(start, _reader.Token); return(new EnumValueDefinitionNode ( location, name, description, directives )); }
/// <summary> /// Parses an argument. /// <see cref="ArgumentNode" />: /// Name : Value[isConstant] /// </summary> /// <param name="context">The parser context.</param> private ArgumentNode ParseArgument(bool isConstant) { ISyntaxToken start = _reader.Token; NameNode name = ParseName(); ExpectColon(); IValueNode value = ParseValueLiteral(isConstant); var location = new Location(start, _reader.Token); return(new ArgumentNode ( location, name, value )); }
/// <summary> /// Parses an operation type definition. /// <see cref="OperationTypeDefinitionNode" />: /// OperationType : NamedType /// </summary> /// <param name="context">The parser context.</param> private OperationTypeDefinitionNode ParseOperationTypeDefinition() { ISyntaxToken start = _reader.Token; OperationType operation = ParseOperationType(); ExpectColon(); NamedTypeNode type = ParseNamedType(); var location = new Location(start, _reader.Token); return(new OperationTypeDefinitionNode ( location, operation, type )); }