private static SyntaxNode LinkSettings(SettingsRoot root, Func <SettingsModel, Scope, SyntaxNode> parse, Scope scope) { var calls = new List <StatementSyntax>(); foreach (var header in root.Headers) { foreach (var value in header.Values) { if (value.Left.GetType() != typeof(IdentifierNameSyntax)) { //td: error continue; } var valueId = value.Left.ToString(); calls.Add(AddSetting.Get <StatementSyntax>( RoslynCompiler.Quoted(valueId), value.Right)); } } var result = SettingClass.Get <ClassDeclarationSyntax>("__settings"); return(result .ReplaceNodes(result .DescendantNodes() .OfType <MethodDeclarationSyntax>(), (on, nn) => nn .AddBodyStatements(calls.ToArray()))); }
private static Func <SyntaxNode, Scope, SyntaxNode> FunctionInjection(SyntaxNode toReplace, SyntaxList <StatementSyntax>?toInject) { return((node, scope) => { var scopeParameter = node .Ancestors() .OfType <MethodDeclarationSyntax>() .FirstOrDefault() ?.ParameterList .Parameters .LastOrDefault(); if (scopeParameter == null || scopeParameter.Type.ToString() != "__Scope") { //td: error return node; } var block = node as BlockSyntax; Debug.Assert(block != null); //td: error var variables = new List <StatementSyntax>(); foreach (var injectionStatement in toInject.Value) { var injectionDeclaration = (injectionStatement as LocalDeclarationStatementSyntax); if (injectionDeclaration == null) { //td: error continue; } if (injectionDeclaration.Declaration.Variables.Count != 1) { //td: error continue; } var injectionVariable = injectionDeclaration .Declaration .Variables .Single(); var type = injectionDeclaration.Declaration.Type; if (type.ToString() == "var") { //td: error continue; } var name = injectionVariable.Identifier; variables.Add(injectionDeclaration .WithDeclaration(injectionDeclaration.Declaration .WithVariables(CSharp.SeparatedList(new[] { injectionVariable.WithInitializer(CSharp.EqualsValueClause( Templates.ScopeGet.Get <ExpressionSyntax>( type, RoslynCompiler.Quoted(name.ToString())))) })))); } return block.WithStatements(CSharp.List( FunctionInjectionStatements(block, toReplace, variables))); }); }
private static SyntaxNode TransformHeaderModel(RootModel root, Func <MockIdentGrammarModel, Scope, SyntaxNode> parse, Scope scope) { var statements = new List <StatementSyntax>(); foreach (var header in root.Headers) { foreach (var value in header.Values) { statements.Add(HeaderValue.Get <StatementSyntax>( RoslynCompiler.Quoted(header.Name), RoslynCompiler.Quoted(value.Left.ToString()), value.Right)); } foreach (var contact in header.Contacts) { statements.Add(Contact.Get <StatementSyntax>( RoslynCompiler.Quoted(header.Name), RoslynCompiler.Quoted(contact.Name), RoslynCompiler.Quoted(contact.Telephone))); foreach (var phone in contact.OtherNumbers) { statements.Add(OtherNumber.Get <StatementSyntax>( RoslynCompiler.Quoted(header.Name), RoslynCompiler.Quoted(contact.Name), CSharp.LiteralExpression( SyntaxKind.NumericLiteralExpression, CSharp.Literal(phone.AreaCode)), CSharp.LiteralExpression( SyntaxKind.NumericLiteralExpression, CSharp.Literal(phone.FirstThree)), CSharp.LiteralExpression( SyntaxKind.NumericLiteralExpression, CSharp.Literal(phone.LastFour)))); } } if (header.Location != null) { statements.Add(Location.Get <StatementSyntax>( RoslynCompiler.Quoted(header.Name), RoslynCompiler.Quoted(header.Location.Place ?? ""), RoslynCompiler.Quoted(header.Location.City ?? ""))); } } foreach (var forStatement in root.Statements) { statements.Add(CSharp .ForEachStatement( CSharp.ParseTypeName("var"), forStatement.Iterator, CSharp.ParseExpression(forStatement.Iterable), CSharp.Block(forStatement .Statements .ToArray()))); } return(CSharp.Block(statements.ToArray())); }