internal static List <T> ParseMany <T>( ParserContext context, TokenKind openKind, Func <ParserContext, T> parser, TokenKind closeKind) { if (context.Current.Kind != openKind) { throw new SyntaxException(context, string.Format( CultureInfo.InvariantCulture, LangResources.ParseMany_InvalidOpenToken, openKind, context.Current)); } var list = new List <T>(); // skip opening token context.MoveNext(); while (context.Current.Kind != closeKind) { list.Add(parser(context)); } // skip closing token context.MoveNext(); return(list); }
private List <T> ParseMany <T>( ParserContext context, TokenKind openKind, Func <ParserContext, T> parser, TokenKind closeKind) { if (context.Current.Kind != openKind) { throw new SyntaxException(context, $"Expected a name token: {context.Current}."); } List <T> list = new List <T>(); // skip opening token context.MoveNext(); while (context.Current.Kind != closeKind) // todo : fix this { list.Add(parser(context)); } // skip closing token context.MoveNext(); return(list); }
private static IValueNode ParseEnumValue(ParserContext context) { SyntaxToken start = context.Current; Location location = context.CreateLocation(start); context.MoveNext(); switch (start.Value) { case Keywords.True: return(new BooleanValueNode(location, true)); case Keywords.False: return(new BooleanValueNode(location, false)); case Keywords.Null: return(new NullValueNode(location)); } return(new EnumValueNode ( location, start.Value )); }
public static bool SkipKeyword(this ParserContext context, string keyword) { if (context.Current.IsName() && context.Current.Value == keyword) { context.MoveNext(); return(true); } return(false); }
public static SyntaxToken SkipDescription(this ParserContext context) { if (context.Current.IsDescription()) { context.MoveNext(); return(context.Current); } return(context.Current); }
public static bool Skip(this ParserContext context, TokenKind kind) { bool match = context.Current.Kind == kind; if (match) { context.MoveNext(); } return(match); }
public static SyntaxToken ExpectScalarValue(this ParserContext context) { if (context.Current.IsScalarValue()) { context.MoveNext(); return(context.Current.Previous); } throw new SyntaxException(context, $"Expected a name token: {context.Current}."); }
public static SyntaxToken Expect(this ParserContext context, TokenKind kind) { if (context.Current.Kind == kind) { context.MoveNext(); return(context.Current.Previous); } throw new SyntaxException(context, $"Expected a name token: {context.Current}."); }
public static SyntaxToken ExpectKeyword(ParserContext context, string keyword) { SyntaxToken token = context.Current; if (token.IsName() && token.Value == keyword) { context.MoveNext(); return(token); } throw new SyntaxException(context, $"Expected \"{keyword}\", found {token}"); }
private static IValueNode ParseValue(string value) { SyntaxToken start = Lexer.Default.Read( new Source(value)); var context = new ParserContext( new Source(value), start, ParserOptions.Default, Parser.ParseName); context.MoveNext(); return(Parser.ParseValueLiteral(context, true)); }
private DocumentNode ParseDocument(ISource source, SyntaxToken start, ParserOptions options) { List <IDefinitionNode> definitions = new List <IDefinitionNode>(); ParserContext context = new ParserContext(source, start, options); context.MoveNext(); while (!context.IsEndOfFile()) { definitions.Add(ParseDefinition(context)); } Location location = context.CreateLocation(start); return(new DocumentNode(location, definitions.AsReadOnly())); }
/// <summary> /// Parses a fragment spred or inline fragment within a selection set. /// <see cref="ParseFragmentSpread" /> and /// <see cref="ParseInlineFragment" />. /// </summary> /// <param name="context">The parser context.</param> private static ISelectionNode ParseFragment(ParserContext context) { SyntaxToken start = context.Current; context.ExpectSpread(); var isOnKeyword = context.Current.IsOnKeyword(); if (!isOnKeyword && context.Current.IsName()) { return(ParseFragmentSpread(context, start)); } NamedTypeNode typeCondition = null; if (isOnKeyword) { context.MoveNext(); typeCondition = ParseNamedType(context); } return(ParseInlineFragment(context, start, typeCondition)); }
public IValueNode ParseJson(ISource source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } SyntaxToken start = _lexer.Read(source); if (start.Kind != TokenKind.StartOfFile) { throw new InvalidOperationException( "The first token must be a start of file token."); } ParserContext context = new ParserContext( source, start, ParserOptions.Default, ParseJsonName); context.MoveNext(); return(ParseValueLiteral(context, true)); }