Example #1
0
        private void ResolveImports()
        {
            this.Imports = new List <IASTNode>();
            var imports = Generator.AST.FindAll(n => n is ASTImport).ToList();

            imports.ForEach(node =>
            {
                ASTImport import = (ASTImport)node;
                var ast          = this.Project.GetAstForModule(import.Name);
                if (!import.Imports.Any())
                {
                    var copies = ast
                                 .FindAll(a => a is ASTType || a is ASTAlias || a is ASTData || a is ASTChoice)
                                 .Select(a => {
                        return(a switch
                        {
                            ASTType t => t.Clone() as IASTNode,
                            ASTAlias t => t.Clone() as IASTNode,
                            ASTData t => t.Clone() as IASTNode,
                            ASTChoice t => t.Clone() as IASTNode,
                            _ => throw new Exception("Can only serialize real AST nodes.")
                        });
                    })
                                 .ToList();
                    this.Imports.AddRange(copies);
                }
        public override string VisitASTData(ASTData astData)
        {
            var typeReferences = astData.Options.Select(f =>
            {
                string _type = f.Name;
                if (_type != "String" &&
                    _type != "Number" &&
                    _type != "Boolean" &&
                    _type != "Date" &&
                    _type != "DateTime" &&
                    _type != "Time")
                {
                    return($@"{astData.Name} --* {_type}");
                }
                else
                {
                    return("");
                }
            });
            var template = $@"
class {astData.Name} {{
{string.Join("\n", astData.Options.Select(o => o.ToMermaidString()).ToList())}
}}
{string.Join("\n", typeReferences)}
";

            this.Parts.Add(template);
            return(template);
        }
Example #3
0
        public override string VisitASTData(ASTData astData)
        {
            var typeReferences = astData.Options.Select(f =>
            {
                var _type = f.Name;
                if (_type != "String" &&
                    _type != "Number" &&
                    _type != "Decimal" &&
                    _type != "Boolean" &&
                    _type != "Date" &&
                    _type != "DateTime" &&
                    _type != "Time" &&
                    !_type.StartsWith("'", StringComparison.Ordinal))
                {
                    return($@"{astData.Name} --* {_type}");
                }
                else
                {
                    return("");
                }
            });
            var template = $@"
class {astData.Name} {{
{string.Join(Environment.NewLine, astData.Options.Select(o => o.ToMermaidString()).ToList())}
}}
{string.Join(Environment.NewLine, typeReferences)}
";

            this.Parts.Add(template);
            return(template);
        }
 public override IEnumerable <Descriptor> VisitASTData(ASTData astData)
 {
     yield return(new Descriptor(astData.Name)
     {
         Module = ModuleName,
         Name = astData.Name,
         Description = MapAnnotations(astData.Annotations),
         DescriptorType = DescriptorType.Data.ToString("g")
     });
 }
Example #5
0
        public JSchema Create(ASTData astData, IEnumerable <IASTNode> nodes)
        {
            this.Nodes = nodes.ToList();
            var schema = MapAstNode(astData.Name);

            schema.SchemaVersion = new Uri("http://json-schema.org/draft-07/schema#");
            schema.Title         = astData.Name;
            schema.Description   = JsonMapper.Annotate(astData.Annotations);
            schema.ExtensionData.Add("references", References);
            return(schema);
        }
        private JSchema MapASTData(ASTData astData)
        {
            var result = new JSchema
            {
                Title = astData.Name
            };

            astData.Options.ToList().ForEach(option =>
            {
                var temp = MapAstNode(option.Name);
                result.AnyOf.Add(temp);
            });
            return(result);
        }
Example #7
0
 internal static IASTNode?Find(IEnumerable <IASTNode> nodes, string name)
 {
     return(nodes.FirstOrDefault(n =>
     {
         return n switch
         {
             ASTType node => node.Name == name,
             ASTAlias node => node.Name == name,
             ASTData node => node.Name == name,
             ASTChoice node => node.Name == name,
             _ => false
         };
     }));
 }
Example #8
0
        private JSchema MapAstNode(string name)
        {
            var refNode = JsonMapper.Find(Nodes, name);
            var result  = refNode switch
            {
                ASTType n => MapASTType(n),
                ASTAlias n => MapASTAlias(n),
                ASTChoice n => MapASTChoice(n),
                ASTData n => MapASTData(n),
                _ => throw new NotImplementedException("Not implemented.")
            };

            AddReference(name, result);
            return(result);
        }
 public T Visit(IASTNode node)
 {
     return(node switch
     {
         ASTType n => VisitASTType(n),
         ASTTypeField n => VisitASTTypeField(n),
         ASTTypeDefinition n => VisitASTTypeDefinition(n),
         ASTRestriction n => VisitASTRestriction(n),
         ASTAlias n => VisitASTAlias(n),
         ASTData n => VisitASTData(n),
         ASTAnnotation n => VisitASTAnnotation(n),
         ASTDirective n => VisitASTDirective(n),
         ASTChoice n => VisitASTChoice(n),
         ASTOption n => VisitASTOption(n),
         ASTChapter n => VisitASTChapter(n),
         ASTParagraph n => VisitASTParagraph(n),
         _ => VisitDefault(node),
     });
Example #10
0
        public override XmlSchemaObject?VisitASTData(ASTData astData)
        {
            var xmlSchemaComplexType = new XmlSchemaComplexType();

            xmlSchemaComplexType.Name = astData.Name;

            var xmlSchemaChoice = new XmlSchemaChoice();

            foreach (ASTDataOption option in astData.Options)
            {
                var o = new XmlSchemaElement
                {
                    RefName = new System.Xml.XmlQualifiedName("self:" + option.Name)
                };
                xmlSchemaChoice.Items.Add(o);
            }

            xmlSchemaComplexType.Particle = xmlSchemaChoice;
            this.Schema.Items.Add(xmlSchemaComplexType);
            return(xmlSchemaComplexType);
        }
        public static List <IASTNode> ResolveImports(ASTGenerator generator)
        {
            var imports  = new List <IASTNode>();
            var _imports = generator.AST.FindAll(n => n is ASTImport).ToList();

            _imports.ForEach(node =>
            {
                var import = (ASTImport)node;
                var ast    = ProjectContext.Instance?.GetAstForModule(import.ModuleName);
                if (!import.Imports.Any())
                {
                    var copies = ast?
                                 .FindAll(a => a is ASTType || a is ASTAlias || a is ASTData || a is ASTChoice || a is ASTView)
                                 .Select(a =>
                    {
                        return(a switch
                        {
                            ASTType t => t.Clone() as IASTNode,
                            ASTAlias t => t.Clone() as IASTNode,
                            ASTData t => t.Clone() as IASTNode,
                            ASTChoice t => t.Clone() as IASTNode,
                            ASTView t => t.Clone() as IASTNode,
                            _ => null
                        });
                    })
                                 .ToList()
                                 .Where(n => n != null)
                                 .OfType <IASTNode>()
                                 .ToList() ?? Enumerable.Empty <IASTNode>();

                    // For some reason the compiler cannot find that I filter out all of the
                    // null's from the list and so I will only have a list of reference types
                    // of type IASTNode...

#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
                    imports.AddRange(copies);
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
                }
        private JSchema?MapAstNode(string name)
        {
            if (NodeNames.Contains(name))
            {
                return((JSchema)References.GetValue(name));
            }
            var refNode = JsonMapper.Find(Nodes, name);
            var result  = refNode switch
            {
                ASTType n => MapASTType(n),
                ASTAlias n => MapASTAlias(n),
                ASTChoice n => MapASTChoice(n),
                ASTData n => MapASTData(n),
                _ => new JSchema()
            };

            if (result != null)
            {
                AddReference(name, result);
            }
            return(result);
        }
    }
Example #13
0
        public IEnumerable <IASTNode> Parse()
        {
            var annotations = new List <ASTAnnotation>();
            var directives  = new List <ASTDirective>();

            while (HasNext() && Current.TokenType != TokenType.EndOfFile)
            {
                if (Current.TokenType == TokenType.KW_Type)
                {
                    var(errors, t) = ASTType.Parse(this, annotations, directives);
                    Errors.AddRange(errors);
                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                    yield return(t);
                }
                else if (Current.TokenType == TokenType.KW_Alias)
                {
                    var(errors, alias) = ASTAlias.Parse(this, annotations, directives);
                    Errors.AddRange(errors);
                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                    yield return(alias);
                }
                else if (Current.TokenType == TokenType.KW_Choice)
                {
                    var(errors, result) = ASTChoice.Parse(this);
                    Errors.AddRange(errors);
                    yield return(result);

                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                }
                else if (Current.TokenType == TokenType.KW_Data)
                {
                    var(errors, data) = ASTData.Parse(this, annotations, directives);
                    Errors.AddRange(errors);
                    yield return(data);

                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                }
                else if (Current.TokenType == TokenType.KW_View)
                {
                    var(errors, data) = ASTView.Parse(this, annotations, directives);
                    Errors.AddRange(errors);
                    yield return(data);

                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                }
                else if (Current.TokenType == TokenType.KW_Open)
                {
                    var(errors, data) = ASTImport.Parse(this);
                    Errors.AddRange(errors);
                    yield return(data);

                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                }
                else if (Current.TokenType == TokenType.KW_Flow)
                {
                    var(errors, data) = ASTFlow.Parse(this);
                    Errors.AddRange(errors);
                    yield return(data);

                    annotations = new List <ASTAnnotation>();
                    directives  = new List <ASTDirective>();
                }
                else if (Current.TokenType == TokenType.Annotation)
                {
                    annotations = ASTAnnotation.Parse(this).ToList();
                }
                else if (Current.TokenType == TokenType.Directive)
                {
                    var(errors, dirs) = ASTDirective.Parse(this);
                    Errors.AddRange(errors.ToList());
                    directives = dirs.ToList();
                }
                else if (Current.TokenType == TokenType.Chapter)
                {
                    yield return(new ASTChapter(Current.Value));

                    Next();
                }
                else if (Current.TokenType == TokenType.Paragraph)
                {
                    yield return(new ASTParagraph(Current.Value));

                    Next();
                }
                else
                {
                    Next();
                }
            }
            yield break;
        }
Example #14
0
 public override string VisitASTData(ASTData astData)
 {
     //ErdParts
     return("");
 }
 public override string VisitASTData(ASTData astData)
 {
     return("");
 }
Example #16
0
 public override T VisitASTData(ASTData astData) => d;