private void EmitEnum(TransportModelEnum enumModel, ICodeStream output)
        {
            var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(enumModel, ModelType.Transport)}.cs");

            var unit = CompilationUnit();

            unit = unit.AddUsings(
                UsingDirective(ParseName(nameof(System))));

            var enumDeclaration = EnumDeclaration(CsEmitterHelper.GetCSharpModelShortName(enumModel, ModelType.Transport));

            enumDeclaration = enumDeclaration.AddModifiers(Token(SyntaxKind.PublicKeyword));

            enumDeclaration = enumDeclaration.WithMembers(SeparatedList <EnumMemberDeclarationSyntax>(enumModel.Members.Values.Select(EmitEnumMember)));

            if (enumModel.IsFlags)
            {
                enumDeclaration = enumDeclaration.WithAttributeLists(
                    List <AttributeListSyntax>(
                        new[] { AttributeList(
                                    SingletonSeparatedList <AttributeSyntax>(
                                        Attribute(
                                            IdentifierName(typeof(System.FlagsAttribute).FullName)))) }));
            }

            var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsTransportModelNamespace));

            nsContainer           = nsContainer.AddMembers(enumDeclaration);
            unit                  = unit.AddMembers(nsContainer);
            outputFile.SyntaxTree = unit.SyntaxTree;
        }
        private void EmitInterface(TransportModelInterface model, ICodeStream output)
        {
            var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(model, ModelType.Transport)}.cs");

            var unit = CompilationUnit();

            unit = unit.AddUsings(
                UsingDirective(ParseName(nameof(System))));

            var entityInterface = InterfaceDeclaration(CsEmitterHelper.GetCSharpModelShortName(model, ModelType.Transport));

            entityInterface = entityInterface.AddModifiers(Token(SyntaxKind.PublicKeyword));

            if (model.GenericParameters.Count > 0)
            {
                entityInterface = entityInterface.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(model.GenericParameters.Select(p => TypeParameter(p.Key)))));

                entityInterface = entityInterface.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>(
                                                                            model.GenericParameters.Select(p =>
                                                                                                           TypeParameterConstraintClause(
                                                                                                               IdentifierName(p.Key),
                                                                                                               SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Transport))) })))));
            }

            List <string> baseTypes = new List <string>();

            foreach (var interfaceModel in model.Interfaces)
            {
                baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Transport));
            }

            if (baseTypes.Count > 0)
            {
                entityInterface = entityInterface.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t))))));
            }

            entityInterface = entityInterface.WithMembers(List <MemberDeclarationSyntax>(model.Members.Select(m =>
                                                                                                              PropertyDeclaration(ParseTypeName(CsEmitterHelper.GetPropertyTypeName(m.Value, this.settings, ModelType.Transport)), NameHelper.GetSafeVariableName(m.Value.Name))
                                                                                                              .WithAccessorList(
                                                                                                                  SyntaxFactory.AccessorList(
                                                                                                                      SyntaxFactory.List <AccessorDeclarationSyntax>(new AccessorDeclarationSyntax[]
            {
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
            })))
                                                                                                              )));

            var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsTransportModelNamespace));

            nsContainer           = nsContainer.AddMembers(entityInterface);
            unit                  = unit.AddMembers(nsContainer);
            outputFile.SyntaxTree = unit.SyntaxTree;
        }
        public void Emit(Parameters parameters, ICodeStream output)
        {
            var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{ConverterClassName}.cs");

            var unit = CompilationUnit();

            unit = unit.AddUsings(
                UsingDirective(ParseName(nameof(System))));

            var converterClass = ClassDeclaration(ConverterClassName);

            converterClass = converterClass.AddModifiers(Token(SyntaxKind.PublicKeyword));

            var baseNodeEntity    = parameters.TransportModel.TransportModelEntities.First(e => e.Key == settings.AstNodeBaseTypeQualified.Split('.').Last());
            var baseNodeInterface = parameters.TransportModel.TransportModelInterfaces.First(e => e.Key == settings.AstNodeBaseTypeQualified.Split('.').Last());
            var syntaxKindEnum    = parameters.TransportModel.TransportModelEnums.First(e => e.Key == "SyntaxKind");

            var conversionMethod = MethodDeclaration(ParseTypeName(CsEmitterHelper.GetCSharpModelFullyQualifiedName(baseNodeInterface.Value, this.settings, ModelType.Ast)), this.convertFromNodeMethodName)
                                   .AddModifiers(Token(SyntaxKind.PublicKeyword))
                                   .WithParameterList(ParameterList(SeparatedList <ParameterSyntax>(new[] { Parameter(Identifier(this.nodeParameterName)).WithType(ParseTypeName(CsEmitterHelper.GetCSharpModelFullyQualifiedName(baseNodeInterface.Value, this.settings, ModelType.Transport))) })))
                                   .WithBody(Block());

            conversionMethod = conversionMethod.AddBodyStatements(
                IfStatement(
                    BinaryExpression(SyntaxKind.EqualsExpression, IdentifierName(this.nodeParameterName), IdentifierName("null")),
                    ReturnStatement(IdentifierName("null")))
                );

            foreach (var entityModel in parameters.TransportModel.TransportModelEntities.Where(e => e.Value.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind))
            {
                conversionMethod = conversionMethod.AddBodyStatements(this.EmitForEntity(entityModel.Value, syntaxKindEnum.Value));
            }

            conversionMethod = conversionMethod.AddBodyStatements(
                ExpressionStatement(
                    ThrowExpression(
                        ObjectCreationExpression(ParseTypeName(typeof(InvalidOperationException).FullName),
                                                 ArgumentList(SeparatedList <ArgumentSyntax>(new[] { Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("Unable to convert node."))) })),
                                                 null
                                                 ))));

            converterClass = converterClass.AddMembers(conversionMethod);

            converterClass = converterClass.AddMembers(this.EmitCollectionConversionMethod(baseNodeInterface.Value));

            var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsAstModelNamespace));

            nsContainer           = nsContainer.AddMembers(converterClass);
            unit                  = unit.AddMembers(nsContainer);
            outputFile.SyntaxTree = unit.SyntaxTree;
        }
        public void Emit(Parameters parameters, ICodeStream output)
        {
            var singleOutputFile = (CodeFileTs)output.CreateCodeFile($"TransportToAstConverter.ts");

            var name = "Converter";

            var classDefinition = new StClassDeclaration()
                                  .WithModifier(new StExportKeywordToken())
                                  .WithName(new StIdentifier().WithEscapedText(name));

            classDefinition.members.Add(this.EmitNodeConversionFunction(parameters, singleOutputFile));
            classDefinition.members.Add(this.EmitNodeCollectionConversionFunction(singleOutputFile));

            var importStatement = new StImportDeclaration()
                                  .WithModuleSpecifier(new StStringLiteral().WithText(typescriptModuleName))
                                  .WithImportClause(c =>
                                                    c
                                                    .WithNamedBindings(new StNamespaceImport().WithName(new StIdentifier().WithEscapedText(typescriptAliasName)))
                                                    );

            singleOutputFile.Model = new StRoot()
                                     .WithStatement(importStatement)
                                     .WithStatement(classDefinition);
        }
        private void EmitEntity(TransportModelEntity entityModel, ICodeStream output)
        {
            var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport)}.cs");

            var unit = CompilationUnit();

            unit = unit.AddUsings(
                UsingDirective(ParseName(nameof(System))));

            var entityClass = ClassDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport));

            entityClass = entityClass.AddModifiers(Token(SyntaxKind.PublicKeyword));

            if (entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind kindDiscriminant)
            {
                entityClass = entityClass.AddMembers(
                    ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport))
                    .AddModifiers(Token(SyntaxKind.PublicKeyword))
                    .WithBody(Block().AddStatements(
                                  ExpressionStatement(
                                      AssignmentExpression(
                                          SyntaxKind.SimpleAssignmentExpression,
                                          MemberAccessExpression(
                                              SyntaxKind.SimpleMemberAccessExpression,
                                              ThisExpression(),
                                              IdentifierName("kind")),
                                          MemberAccessExpression(
                                              SyntaxKind.SimpleMemberAccessExpression,
                                              IdentifierName($"{this.settings.CsTransportModelNamespace}.SyntaxKind"),
                                              IdentifierName(kindDiscriminant.SyntaxKindValueName)))))));
            }

            if (entityModel.GenericParameters.Count > 0)
            {
                entityClass = entityClass.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(entityModel.GenericParameters.Select(p => TypeParameter(p.Key)))));

                entityClass = entityClass.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>(
                                                                    entityModel.GenericParameters.Select(p =>
                                                                                                         TypeParameterConstraintClause(
                                                                                                             IdentifierName(p.Key),
                                                                                                             SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Transport))) })))));
            }

            List <string> baseTypes = new List <string>();

            if (entityModel.BaseEntity != null)
            {
                baseTypes.Add(CsEmitterHelper.GetCSharpModelReferenceName(entityModel.BaseEntity, this.settings, ModelType.Transport));
            }

            foreach (var interfaceModel in entityModel.Interfaces)
            {
                baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Transport));
            }

            if (baseTypes.Count > 0)
            {
                entityClass = entityClass.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t))))));
            }

            entityClass = entityClass.AddMembers(entityModel.Members.Select(m =>
                                                                            PropertyDeclaration(ParseTypeName(CsEmitterHelper.GetPropertyTypeName(m.Value, this.settings, ModelType.Transport)), NameHelper.GetSafeVariableName(m.Value.Name))
                                                                            .WithAccessorList(
                                                                                SyntaxFactory.AccessorList(
                                                                                    SyntaxFactory.List <AccessorDeclarationSyntax>(new AccessorDeclarationSyntax[]
            {
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
            })))
                                                                            .AddModifiers(Token(SyntaxKind.PublicKeyword))
                                                                            ).ToArray <MemberDeclarationSyntax>());

            var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsTransportModelNamespace));

            nsContainer           = nsContainer.AddMembers(entityClass);
            unit                  = unit.AddMembers(nsContainer);
            outputFile.SyntaxTree = unit.SyntaxTree;
        }
        private void EmitEntity(TransportModelEntity entityModel, ICodeStream output)
        {
            var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast)}.cs");

            var unit = CompilationUnit();

            unit = unit.AddUsings(
                UsingDirective(ParseName(nameof(System))));

            var entityClass = ClassDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast));

            entityClass = entityClass.AddModifiers(Token(SyntaxKind.PublicKeyword));

            if (entityModel.TsDiscriminant == null || !(entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind))
            {
                entityClass = entityClass.AddModifiers(Token(SyntaxKind.AbstractKeyword));
            }

            List <StatementSyntax> propertyInitializers     = null;
            List <ArgumentSyntax>  baseConstructorArguments = null;

            var constructorDeclaration =
                ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast))
                .AddModifiers(Token(SyntaxKind.PublicKeyword))
                .WithParameterList(ParameterList(SeparatedList <ParameterSyntax>(this.GetConstructorParameters(entityModel, out propertyInitializers, out baseConstructorArguments))));

            if (baseConstructorArguments != null && baseConstructorArguments.Count > 0)
            {
                constructorDeclaration = constructorDeclaration.WithInitializer(
                    ConstructorInitializer(SyntaxKind.BaseConstructorInitializer)
                    .AddArgumentListArguments(baseConstructorArguments.ToArray()));
            }

            var constructorBody = Block();

            constructorBody = this.PopulateRequiredConstructorBodyInitializers(constructorBody, entityModel);

            if (propertyInitializers != null && propertyInitializers.Count > 0)
            {
                constructorBody = constructorBody.AddStatements(propertyInitializers.ToArray());
            }

            constructorDeclaration = constructorDeclaration.WithBody(constructorBody);
            entityClass            = entityClass.AddMembers(constructorDeclaration);

            if (constructorDeclaration.ParameterList.Parameters.Count > 0)
            {
                var parameterlessConstructorDeclaration =
                    ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast))
                    .AddModifiers(Token(SyntaxKind.PublicKeyword));
                var parameterlessConstructorBody = Block();
                parameterlessConstructorBody        = this.PopulateRequiredConstructorBodyInitializers(parameterlessConstructorBody, entityModel);
                parameterlessConstructorDeclaration = parameterlessConstructorDeclaration.WithBody(parameterlessConstructorBody);
                entityClass = entityClass.AddMembers(parameterlessConstructorDeclaration);
            }

            if (entityModel.GenericParameters.Count > 0)
            {
                entityClass = entityClass.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(entityModel.GenericParameters.Select(p => TypeParameter(p.Key)))));

                entityClass = entityClass.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>(
                                                                    entityModel.GenericParameters.Select(p =>
                                                                                                         TypeParameterConstraintClause(
                                                                                                             IdentifierName(p.Key),
                                                                                                             SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Ast))) })))));
            }

            List <string> baseTypes = new List <string>();

            if (entityModel.Name == this.settings.AstNodeBaseTypeQualified.Split('.').Last())
            {
                baseTypes.Add($"{settings.CsAstModelNamespace}.{baseNodeType}");
            }

            if (entityModel.BaseEntity != null)
            {
                baseTypes.Add(CsEmitterHelper.GetCSharpModelReferenceName(entityModel.BaseEntity, this.settings, ModelType.Ast));
            }

            foreach (var interfaceModel in entityModel.Interfaces)
            {
                baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Ast));
            }

            if (baseTypes.Count > 0)
            {
                entityClass = entityClass.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t))))));
            }

            entityClass = entityClass.AddMembers(
                entityModel
                .Members
                .Where(m => !CsEmitterHelper.IsNodeCollection(m.Value.Type))
                .Select(m =>
                        FieldDeclaration(VariableDeclaration(ParseTypeName(CsEmitterHelper.GetAstModelPropertyTypeName(m.Value, this.settings)), SeparatedList <VariableDeclaratorSyntax>(new[] { VariableDeclarator(this.GetFieldName(m.Key)) })))
                        ).ToArray <MemberDeclarationSyntax>());

            entityClass = entityClass.AddMembers(entityModel.Members.Select(m => this.CreatePropertyDeclaration(m.Value)).ToArray <MemberDeclarationSyntax>());

            if (entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind)
            {
                entityClass = entityClass.AddMembers(this.GenerateConversionMethod(entityModel));
            }

            var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsAstModelNamespace));

            nsContainer           = nsContainer.AddMembers(entityClass);
            unit                  = unit.AddMembers(nsContainer);
            outputFile.SyntaxTree = unit.SyntaxTree;
        }
        public void Emit(Parameters parameters, ICodeStream output)
        {
            var singleOutputFile = (CodeFileTs)output.CreateCodeFile($"TransportTypeMarker.ts");

            this.EmitTypeMarkerFunction(parameters, singleOutputFile);
        }