Beispiel #1
0
        public void TestGenerateConstant()
        {
            _constantGenerator.Results = new[] { "\"foo\"" };

            var php = _generator.Generate(new Constant("foo"));

            Assert.AreEqual("\"foo\"", php);
        }
        public void RawScriptStatement()
        {
            var block = ScriptStatementHelper.CreateScriptBlockStatement();

            block.Statements.Add(ScriptStatementHelper.CreateScriptRawStatement("hello"));

            var action = statementGenerator.Generate <string, string>(new ExpressContext(), block);

            var compiled = action.Compile();

            StringBuilder sb = new StringBuilder();

            compiled(sb, "", "");

            sb.ToString().ShouldBe("hello");
        }
Beispiel #3
0
        private SourceCode GetConstructors(SemanticModel semanticModel, HashSet <string> typeReferences, IEnumerable <ConstructorDeclarationSyntax> constructorDeclarations)
        {
            var generatedConstructors = new SourceCode {
                MainPart = ""
            };

            foreach (var constructorDeclaration in constructorDeclarations)
            {
                string identifier = constructorDeclaration.Identifier.ValueText;
                var    parameters = new List <Var>();
                foreach (var parameter in constructorDeclaration.ParameterList.Parameters)
                {
                    var typeReference = TypeReferenceGenerator.GenerateTypeReference(parameter.Type, semanticModel);
                    parameters.Add(new Var
                    {
                        Name = parameter.Identifier.ValueText,
                        Type = typeReference
                    });
                }
                var constructorAccessModifier = GetAccessModifier(constructorDeclaration.Modifiers.ToList());
                var statements = new List <string>();
                foreach (var statement in constructorDeclaration.Body.Statements)
                {
                    string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                    statements.Add(generatedStatement);
                }
                var generatedConstructor = ConstructorGenerator.Generate(identifier, constructorAccessModifier, parameters,
                                                                         statements, semanticModel);
                generatedConstructors.MainPart += "\n" + generatedConstructor.MainPart;
            }
            return(generatedConstructors);
        }
Beispiel #4
0
        public void RawTextBlock()
        {
            var statementGenerator = new StatementGenerator(new NullLogger <StatementGenerator>());

            var templateText = @"This is a World from scriban!";
            var template     = Template.Parse(templateText, null, null, null);

            var result = statementGenerator.Generate <string, object>(new ExpressContext(), template.Page.Body);

            var functor = result.Compile();
            var sb      = new StringBuilder();

            functor(sb, "doesn'tm atter", null);

            Assert.Equal("This is a World from scriban!", sb.ToString());
        }
Beispiel #5
0
        private SourceCode GetProperties(SemanticModel semanticModel, HashSet <string> typeReferences,
                                         IEnumerable <PropertyDeclarationSyntax> propertyDeclarations, bool isFromInterface)
        {
            var generatedProperties = new SourceCode {
                MainPart = ""
            };

            foreach (var propertyDeclaration in propertyDeclarations)
            {
                string identifier    = propertyDeclaration.Identifier.ValueText;
                var    propertyType  = propertyDeclaration.Type;
                var    typeReference = TypeReferenceGenerator.GenerateTypeReference(propertyType, semanticModel);
                if (!typeReference.IsPredefined)
                {
                    typeReferences.Add(typeReference.Text);
                }

                bool isStatic  = HasStaticModifier(propertyDeclaration.Modifiers.ToList());
                bool isVirtual = isFromInterface || HasVirtualModifier(propertyDeclaration.Modifiers.ToList());

                var propertyAccessModifier = GetAccessModifier(propertyDeclaration.Modifiers.ToList());
                var accessors   = propertyDeclaration.AccessorList.Accessors;
                var getAccessor = accessors.FirstOrDefault(syntax => syntax.Keyword.Text == "get");
                var setAccessor = accessors.FirstOrDefault(syntax => syntax.Keyword.Text == "set");

                if (propertyDeclaration.AccessorList.Accessors.Any(syntax => syntax.Body != null))
                {
                    var complexPropertyDescription = new ComplexPropertyDescription
                    {
                        PropertyName      = identifier,
                        PropertyType      = typeReference,
                        GetAccessModifier = new Optional <AccessModifier>(),
                        SetAccessModifier = new Optional <AccessModifier>(),
                        IsStatic          = isStatic,
                        IsVirtual         = isVirtual
                    };
                    if (getAccessor != null)
                    {
                        var getAccessModifier = GetAccessModifier(getAccessor.Modifiers);
                        var getModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, getAccessModifier);
                        complexPropertyDescription.GetAccessModifier = getModifier;
                        if (getAccessor.Body != null)
                        {
                            var statements = new List <string>();
                            foreach (var statement in getAccessor.Body.Statements)
                            {
                                string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                                statements.Add(generatedStatement);
                            }
                            complexPropertyDescription.GetStatements = statements;
                        }
                    }
                    if (setAccessor != null)
                    {
                        var setAccessModifier = GetAccessModifier(setAccessor.Modifiers);
                        var setModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, setAccessModifier);
                        complexPropertyDescription.SetAccessModifier = setModifier;
                        if (setAccessor.Body != null)
                        {
                            var statements = new List <string>();
                            foreach (var statement in setAccessor.Body.Statements)
                            {
                                string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                                statements.Add(generatedStatement);
                            }
                            complexPropertyDescription.SetStatements = statements;
                        }
                    }

                    SourceCode generatedProperty = PropertyGenerator.GenerateComplexProperty(complexPropertyDescription, semanticModel);
                    generatedProperties.MainPart += "\n" + generatedProperty.MainPart;
                }
                else
                {
                    var simplePropertyDescription = new SimplePropertyDescription
                    {
                        PropertyName      = identifier,
                        PropertyType      = typeReference,
                        GetAccessModifier = new Optional <AccessModifier>(),
                        SetAccessModifier = new Optional <AccessModifier>(),
                        IsStatic          = isStatic,
                        IsVirtual         = isVirtual,
                        IsFromInterface   = isFromInterface
                    };
                    if (getAccessor != null)
                    {
                        var getAccessModifier = GetAccessModifier(getAccessor.Modifiers);
                        var getModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, getAccessModifier);
                        simplePropertyDescription.GetAccessModifier = getModifier;
                    }
                    if (setAccessor != null)
                    {
                        var setAccessModifier = GetAccessModifier(setAccessor.Modifiers);
                        var setModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, setAccessModifier);
                        simplePropertyDescription.SetAccessModifier = setModifier;
                    }

                    SourceCode generatedProperty = PropertyGenerator
                                                   .GenerateSimpleProperty(simplePropertyDescription, semanticModel, isFromInterface);
                    generatedProperties.MainPart += "\n" + generatedProperty.MainPart;
                }
            }
            return(generatedProperties);
        }
Beispiel #6
0
        private SourceCode GetMethods(SemanticModel semanticModel, HashSet <string> typeReferences, IEnumerable <MethodDeclarationSyntax> methodDeclarations)
        {
            var generatedMethods = new SourceCode {
                MainPart = ""
            };

            foreach (var methodDeclaration in methodDeclarations)
            {
                string identifier          = methodDeclaration.Identifier.ValueText;
                var    returnType          = methodDeclaration.ReturnType;
                var    returnTypeReference = TypeReferenceGenerator.GenerateTypeReference(returnType, semanticModel);
                if (!returnTypeReference.IsPredefined)
                {
                    typeReferences.Add(returnTypeReference.Text);
                }
                var typeParameters = new List <string>();
                if (methodDeclaration.TypeParameterList != null)
                {
                    foreach (var typeParameter in methodDeclaration.TypeParameterList.Parameters)
                    {
                        typeParameters.Add(typeParameter.Identifier.ValueText);
                    }
                }
                var parameters = new List <Var>();
                foreach (var parameter in methodDeclaration.ParameterList.Parameters)
                {
                    var typeReference = TypeReferenceGenerator.GenerateTypeReference(parameter.Type, semanticModel);
                    parameters.Add(new Var
                    {
                        Name = parameter.Identifier.ValueText,
                        Type = typeReference
                    });
                }
                var  accessModifier = GetAccessModifier(methodDeclaration.Modifiers.ToList());
                bool isStatic       = HasStaticModifier(methodDeclaration.Modifiers.ToList());
                Optional <List <string> > body;
                if (methodDeclaration.Body == null)
                {
                    body = new Optional <List <string> >();
                }
                else
                {
                    var statements = new List <string>();
                    foreach (var statement in methodDeclaration.Body.Statements)
                    {
                        string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                        statements.Add(generatedStatement);
                        new Optional <List <string> >();
                    }
                    body = new Optional <List <string> >(statements);
                }

                bool isVirtual = !body.HasValue || HasVirtualModifier(methodDeclaration.Modifiers.ToList());

                var generatedMethod = MethodGenerator.Generate(identifier,
                                                               returnTypeReference,
                                                               typeParameters,
                                                               accessModifier,
                                                               parameters,
                                                               body,
                                                               isStatic,
                                                               isVirtual,
                                                               semanticModel);
                generatedMethods.MainPart += "\n" + generatedMethod.MainPart;
            }
            return(generatedMethods);
        }