public void CanParseFragmentWithParamBlockWithInitialValues() { // Arrange. Lexer lexer = new Lexer(null, @"fragment basic_material; [params] float alpha : ALPHA = 1.0f; float3 dir = float3(1.0f, -1.1f, 0.0); bool enable = false;" ); FragmentParser parser = new FragmentParser(null, lexer.GetTokens()); // Act. FragmentNode fragment = parser.Parse(); // Assert. Assert.AreEqual("basic_material", fragment.Name); Assert.IsNotNull(fragment.Parameters); Assert.AreEqual(3, fragment.Parameters.VariableDeclarations.Count); Assert.AreEqual(TokenType.Float, fragment.Parameters.VariableDeclarations[0].DataType); Assert.AreEqual("alpha", fragment.Parameters.VariableDeclarations[0].Name); Assert.AreEqual("ALPHA", fragment.Parameters.VariableDeclarations[0].Semantic); Assert.AreEqual("1", fragment.Parameters.VariableDeclarations[0].InitialValue); Assert.AreEqual(TokenType.Float3, fragment.Parameters.VariableDeclarations[1].DataType); Assert.AreEqual("dir", fragment.Parameters.VariableDeclarations[1].Name); Assert.AreEqual("float3(1,-1.1,0)", fragment.Parameters.VariableDeclarations[1].InitialValue); Assert.AreEqual(TokenType.Bool, fragment.Parameters.VariableDeclarations[2].DataType); Assert.AreEqual("enable", fragment.Parameters.VariableDeclarations[2].Name); Assert.AreEqual("false", fragment.Parameters.VariableDeclarations[2].InitialValue); }
public FragmentNode Parse() { TokenIndex = 0; FragmentNode fragmentNode = ParseFragmentDeclaration(); return(fragmentNode); }
public object VisitFragmentNode(FragmentNode node, Context parameter) { parameter.IsAvailable = true; var fragment = node.Fragment; parameter.AppendRawSql(fragment); parameter.AppendFormattedSql(fragment); return(null); }
protected override FragmentContent CreateContent(FragmentParser parser, ContentIdentity identity) { FragmentNode fragmentNode = parser.Parse(); return(new FragmentContent { FragmentNode = fragmentNode }); }
void CreateNodes() { foreach (ResourceMap.Node mapNode in this.selectedNodes) { FragmentNode fNode = new FragmentNode { Focus = mapNode }; this.fragmentNodes.Add(mapNode.Name, fNode); } }
private ICodeDescriptor GenerateObjectSelectionSet( OperationDefinitionNode operation, ObjectType objectType, IType fieldType, WithDirectives fieldOrOperation, FieldCollectionResult typeCase, Path path) { IFragmentNode returnType = HoistFragment( objectType, typeCase.SelectionSet, typeCase.Fragments); IReadOnlyList <IFragmentNode> fragments; string className; if (returnType is null) { fragments = typeCase.Fragments; className = CreateName(fieldOrOperation, objectType, GetClassName); } else { fragments = returnType.Children; className = CreateName(GetClassName(returnType.Fragment.Name)); } var modelSelectionSet = new SelectionSetNode( typeCase.Fields.Select(t => t.Selection).ToList()); var modelFragment = new FragmentNode(new Fragment( className, objectType, modelSelectionSet)); modelFragment.Children.AddRange(fragments); IInterfaceDescriptor modelInterface = CreateInterface(modelFragment, path); var modelClass = new ClassDescriptor( className, _namespace, typeCase.Type, modelInterface); RegisterDescriptor(modelInterface); RegisterDescriptor(modelClass); RegisterDescriptor( new ResultParserMethodDescriptor( GetPathName(path), operation, fieldType, fieldOrOperation as FieldNode, path, modelInterface, new[] { new ResultParserTypeDescriptor(modelClass) })); return(modelInterface); }
public static void Main() { var stream = new AntlrFileStream(Path); var lexer = new LeafLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new LeafParser(tokens); var fragment = new FragmentNode(parser.entry_point()); Console.WriteLine(fragment.ToString()); }
private ICodeDescriptor GenerateInterfaceSelectionSet( OperationDefinitionNode operation, InterfaceType interfaceType, IType fieldType, FieldNode fieldSelection, IReadOnlyCollection <FieldCollectionResult> typeCases, Path path) { FieldCollectionResult firstCase = typeCases.First(); IFragmentNode returnType = HoistFragment( interfaceType, firstCase.SelectionSet, firstCase.Fragments); InterfaceDescriptor interfaceDescriptor; if (returnType is null) { firstCase = _fieldCollector.CollectFields( interfaceType, firstCase.SelectionSet, path); string name = CreateName(fieldSelection, interfaceType, GetClassName); var interfaceSelectionSet = new SelectionSetNode( firstCase.Fields.Select(t => t.Selection).ToList()); returnType = new FragmentNode(new Fragment( name, interfaceType, interfaceSelectionSet)); _usedNames.Remove(name); } interfaceDescriptor = CreateInterface(returnType, path); var resultParserTypes = new List <ResultParserTypeDescriptor>(); foreach (FieldCollectionResult typeCase in typeCases) { GenerateInterfaceTypeCaseModel( typeCase, returnType, resultParserTypes, path); } RegisterDescriptor(interfaceDescriptor); RegisterDescriptor( new ResultParserMethodDescriptor( GetPathName(path), operation, fieldType, fieldSelection, path, interfaceDescriptor, resultParserTypes)); return(interfaceDescriptor); }
/// <summary> /// Initializes a new instance of the <see cref="QueryFragment" /> class. /// </summary> /// <param name="inlineFragmentNode">The inline fragment node.</param> public QueryFragment(FragmentNode inlineFragmentNode) { this.Node = Validation.ThrowIfNullOrReturn(inlineFragmentNode, nameof(inlineFragmentNode)); this.Name = string.Empty; this.ReferencedNamedFragments = new CharMemoryHashSet(); this.IsReferenced = true; this.TargetGraphTypeName = inlineFragmentNode.TargetType.ToString(); foreach (var node in this.Node.Children) { this.BuildReferencedFragmentList(node); } }
private void GenerateInterfaceTypeCaseModel( FieldCollectionResult typeCase, IFragmentNode returnType, ICollection <ResultParserTypeDescriptor> resultParser, Path path) { string className; IReadOnlyList <IFragmentNode> fragments; IFragmentNode modelType = HoistFragment( (ObjectType)typeCase.Type, typeCase.SelectionSet, typeCase.Fragments); if (modelType is null) { fragments = typeCase.Fragments; className = CreateName(GetClassName(typeCase.Type.Name)); } else { fragments = modelType.Children; className = CreateName(GetClassName(modelType.Fragment.Name)); } var modelSelectionSet = new SelectionSetNode( typeCase.Fields.Select(t => t.Selection).ToList()); var modelFragment = new FragmentNode(new Fragment( className, typeCase.Type, modelSelectionSet)); modelFragment.Children.AddRange(fragments); if (modelFragment.Children.All(t => t.Fragment.SelectionSet != returnType.Fragment.SelectionSet)) { modelFragment.Children.Add(returnType); } IInterfaceDescriptor modelInterface = CreateInterface(modelFragment, path); var modelClass = new ClassDescriptor( className, _namespace, typeCase.Type, modelInterface); RegisterDescriptor(modelInterface); RegisterDescriptor(modelClass); resultParser.Add(new ResultParserTypeDescriptor(modelClass)); }
void GraphNode(FragmentNode fragmentNode) { SvgEditor e = new SvgEditor(); SENodeGroup parentsGroup = new SENodeGroup("parents", false); SENodeGroup focusGroup = new SENodeGroup("focus", false); SENodeGroup childrenGroup = new SENodeGroup("children", false); parentsGroup.AppendChild(focusGroup); focusGroup.AppendChild(childrenGroup); { SENode node = this.CreateNode(fragmentNode.Focus, Color.Green, false); focusGroup.AppendNode(node); } foreach (ResourceMap.Node childNode in fragmentNode.Children) { SENode node = this.CreateNode(childNode, Color.LightBlue, true); childrenGroup.AppendNode(node); } foreach (ResourceMap.Node parentNode in fragmentNode.Parents) { SENode node = this.CreateNode(parentNode, Color.LightCyan, true); parentsGroup.AppendNode(node); } e.Render(parentsGroup, true); String svgName = FragmentMapName(fragmentNode.Focus); String outputSvgPath = Path.Combine(this.graphicsDir, svgName); this.fc?.Mark(outputSvgPath); e.Save(outputSvgPath); this.fragmentsBlock .AppendLine($"<p>") .AppendLine($"Fragment Diagram {fragmentNode.Focus.Name}") .AppendLine($"</p>") .AppendLine($"<object data=\"{svgName}\" type=\"image/svg+xml\">") .AppendLine($" <img src=\"{svgName}\" alt=\"{fragmentNode.Focus.Name}\"/>") .AppendLine($"</object>"); ; }
public void CanParseFragmentWithParamBlock() { // Arrange. Lexer lexer = new Lexer(null, @"fragment basic_material; [params] float alpha : ALPHA; float3 color;" ); FragmentParser parser = new FragmentParser(null, lexer.GetTokens()); // Act. FragmentNode fragment = parser.Parse(); // Assert. Assert.AreEqual("basic_material", fragment.Name); Assert.IsNotNull(fragment.Parameters); Assert.AreEqual(2, fragment.Parameters.VariableDeclarations.Count); Assert.AreEqual(TokenType.Float, fragment.Parameters.VariableDeclarations[0].DataType); Assert.AreEqual("alpha", fragment.Parameters.VariableDeclarations[0].Name); Assert.AreEqual("ALPHA", fragment.Parameters.VariableDeclarations[0].Semantic); Assert.AreEqual(TokenType.Float3, fragment.Parameters.VariableDeclarations[1].DataType); Assert.AreEqual("color", fragment.Parameters.VariableDeclarations[1].Name); }
/// <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) { tokenStream.MatchOrThrow(TokenType.SpreadOperator); var startLocation = tokenStream.Location; tokenStream.Next(); SyntaxNode node; SyntaxNode collection = null; ReadOnlyMemory <char> fragmentName = ReadOnlyMemory <char> .Empty; ReadOnlyMemory <char> restrictedToType = ReadOnlyMemory <char> .Empty; var directives = new List <SyntaxNode>(); // check for inline fragment first "on Type" if (tokenStream.Match(KEYWORDS.On)) { tokenStream.Next(); tokenStream.MatchOrThrow <NameToken>(); restrictedToType = tokenStream.ActiveToken.Text; tokenStream.Next(); } // might be a named fragment? if (tokenStream.Match <NameToken>()) { fragmentName = tokenStream.ActiveToken.Text; tokenStream.Next(); } // account for possible directives on this field while (tokenStream.Match(TokenType.AtSymbol)) { var dirMaker = NodeMakerFactory.CreateMaker <DirectiveNode>(); var directive = dirMaker.MakeNode(tokenStream); directives.Add(directive); } // may contain a field set if (tokenStream.Match(TokenType.CurlyBraceLeft)) { var filedColMaker = NodeMakerFactory.CreateMaker <FieldCollectionNode>(); collection = filedColMaker.MakeNode(tokenStream); } if (fragmentName.IsEmpty && restrictedToType.IsEmpty && directives.Count == 0 && collection == null) { throw new GraphQLSyntaxException( startLocation, "Invalid fragment syntax. No fragment could be created from the supplied block."); } if (!fragmentName.IsEmpty) { node = new FragmentSpreadNode(startLocation, fragmentName); } else { node = new FragmentNode(startLocation, restrictedToType); } if (collection != null && collection.Children.Any()) { node.AddChild(collection); } foreach (var directive in directives) { node.AddChild(directive); } return(node); }
public StitchedFragmentSymbol(string uniqueName, FragmentNode fragmentNode) { UniqueName = uniqueName; FragmentNode = fragmentNode; }
public static void Test() { var fragment = new FragmentNode(); fragment.AddImport(new ImportNode("C.Interop.Stdio", "stdio")); var typeDef = fragment.AddDefinition("Vec2"); var type = new StructTypeNode(); typeDef.DefinedNode = type; type.Members = new[] { new MemberNode("x", "f32"), new MemberNode("y", "f32"), }; var funcDef = fragment.AddDefinition("main"); var func = new FunctionNode(); funcDef.DefinedNode = func; func.Parameters = new[] { new ParameterNode("argc", new PointerNode("i8")), new ParameterNode("argv", "i32"), }; var scope = func.Scope = new ScopeNode(); scope.AppendStatement(new DeclarationNode { Name = "test", Mutable = true, Type = new PlainTypeNode("Vec2"), Allocator = new PlainTypeNode("__heap_allocator") }); scope.AppendStatement(new FunctionCallNode { LValue = new IdNode("printf"), Parameters = new ValueNode[] { new StringNode("%s\n") { CString = true }, new IdNode("test") { Child = new IdNode("x") { Child = new FunctionCallNode { LValue = new IdNode("toString"), Child = new FunctionCallNode { LValue = new IdNode("buffer") } } } }, } }); Console.Write(fragment); }