/// <summary> /// Processes the stream as far as it needs to to generate a fully qualiffied /// <see cref="SyntaxNode" /> based on its internal ruleset. All implementations /// are expected to validate the stream is at a location which it can consume /// and throw an <see cref="GraphQLSyntaxException" /> as appropriate and consume /// the last character in their phrase, leaving the stream primed for the next /// maker in the series. /// </summary> /// <param name="tokenStream">The token stream.</param> /// <returns>LexicalToken.</returns> public SyntaxNode MakeNode(TokenStream tokenStream) { SyntaxNode node = null; if (tokenStream.Match <NullToken>()) { node = new ScalarValueNode( tokenStream.Location, ScalarValueType.Boolean, ParserConstants.Keywords.Null); } else { tokenStream.MatchOrThrow <NameToken>(); if (tokenStream.Match(ParserConstants.Keywords.True, ParserConstants.Keywords.False)) { node = new ScalarValueNode( tokenStream.Location, ScalarValueType.Boolean, tokenStream.ActiveToken.Text); } else { GraphQLSyntaxException.ThrowFromExpectation( tokenStream.Location, "{true|false}", tokenStream.ActiveToken.Text.ToString()); } } tokenStream.Next(); return(node); }
/// <summary> /// Matchs the text of the currnetly <see cref="ActiveToken"/> to the given keyword /// or throws an exception. /// </summary> /// <param name="keyword">The keyword.</param> public void MatchOrThrow(ReadOnlyMemory <char> keyword) { if (!this.Match(keyword)) { GraphQLSyntaxException.ThrowFromExpectation( this.Location, keyword, this.ActiveToken?.Text ?? ReadOnlyMemory <char> .Empty); } }
/// <summary> /// Matches the currently <see cref="ActiveToken"/> to the provided <see cref="TokenType"/> /// or throws an exception. /// </summary> /// <param name="tokenType">Type of the token.</param> public void MatchOrThrow(TokenType tokenType) { if (!this.Match(tokenType)) { GraphQLSyntaxException.ThrowFromExpectation( this.Location, tokenType.Description(), this.ActiveTokenTypeDescrption); } }
/// <summary> /// Matches the type of the <see cref="ActiveToken"/> to the provided type or throws /// an exception. /// </summary> /// <typeparam name="TLexicalType">The type of the t lexical type.</typeparam> public void MatchOrThrow <TLexicalType>() where TLexicalType : LexicalToken { if (!this.Match <TLexicalType>()) { var text = this.EndOfStream ? TokenType.EndOfFile.Description().ToString() : this.ActiveToken.GetType().FriendlyName(); GraphQLSyntaxException.ThrowFromExpectation( this.Location, typeof(TLexicalType).FriendlyName(), text); } }
/// <summary> /// Processes the queue as far as it needs to to generate a fully qualiffied /// <see cref="SyntaxNode" /> based on its ruleset. /// </summary> /// <param name="tokenStream">The token stream.</param> /// <returns>LexicalToken.</returns> public SyntaxNode MakeNode(TokenStream tokenStream) { var maker = ValueMakerFactory.CreateMaker(tokenStream.ActiveToken); if (maker != null) { return(maker.MakeNode(tokenStream)); } GraphQLSyntaxException.ThrowFromExpectation( tokenStream.Location, "<value>", tokenStream.TokenType.Description().ToString()); throw new InvalidOperationException("Critical Failure, this exception should never be reached."); }
/// <summary> /// Matches the currently <see cref="ActiveToken" /> to any of the provided <see cref="TokenType" />. /// The active token must match one of the provided values or an exception is thrown. /// </summary> /// <param name="primaryTokenType">Type of the primary token to check for.</param> /// <param name="additionalTypes">Any additional tokens to check for.</param> public void MatchOrThrow(TokenType primaryTokenType, params TokenType[] additionalTypes) { if (additionalTypes == null || additionalTypes.Length == 0) { this.MatchOrThrow(primaryTokenType); } else { var typeCollection = primaryTokenType.AsEnumerable().Concat(additionalTypes); var tokensMatched = typeCollection.Where(this.Match); if (!tokensMatched.Any()) { var errors = "[" + string.Join(", ", typeCollection.Select(TokenTypeTextForErrorMessage).Select(x => x.ToString())) + "]"; GraphQLSyntaxException.ThrowFromExpectation( this.Location, errors, this.ActiveTokenTypeDescrption.ToString()); } } }