コード例 #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);
                }
コード例 #2
0
        public override XmlSchemaObject VisitASTChoice(ASTChoice astChoice)
        {
            /*
             * <xs:simpleType name="color" final="restriction" >
             * <xs:restriction base="xs:string">
             * <xs:enumeration value="green" />
             * <xs:enumeration value="red" />
             * <xs:enumeration value="blue" />
             * </xs:restriction>
             * </xs:simpleType>
             */
            var enumeration = new XmlSchemaSimpleType
            {
                Name = astChoice.Name
            };
            var restriction = new XmlSchemaSimpleTypeRestriction();

            restriction.BaseTypeName = new XmlQualifiedName("string", DefaultSchemaNamespace);
            foreach (ASTOption option in astChoice.Options)
            {
                var facet = new XmlSchemaEnumerationFacet
                {
                    Value = option.Value
                };
                restriction.Facets.Add(facet);
            }
            enumeration.Content = restriction;
            this.Schema.Items.Add(enumeration);

            return(enumeration);
        }
コード例 #3
0
        public override string VisitASTChoice(ASTChoice astChoice)
        {
            string template = $@"
class {astChoice.Name} {{
{string.Join("\n", astChoice.Options.Select(o => o.Value).ToList())}
}}

";

            this.Parts.Add(template);
            return(template);
        }
コード例 #4
0
        private JSchema MapASTChoice(ASTChoice astChoice)
        {
            var result = new JSchema
            {
                Title = astChoice.Name
            };

            astChoice.Options.ToList().ForEach(option =>
            {
                result.Enum.Add(option.Value);
            });
            result.Title = astChoice.Name;
            return(result);
        }
コード例 #5
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
         };
     }));
 }
コード例 #6
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);
        }
コード例 #7
0
        public override IEnumerable <Descriptor> VisitASTChoice(ASTChoice astChoice)
        {
            var description = $@"
{MapAnnotations(astChoice.Annotations)}

Options:
{String.Join(Environment.NewLine, astChoice.Options.Select(o => o.Value))}
";

            yield return(new Descriptor(astChoice.Name)
            {
                Module = ModuleName,
                Name = astChoice.Name,
                Description = description,
                DescriptorType = DescriptorType.Choice.ToString("g")
            });
        }
コード例 #8
0
 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),
     });
コード例 #9
0
        public void BasicChoiceTest()
        {
            var code      = @"
choice Gender =
    | ""Male""
    | ""Female""
    | ""Other""
";
            var tokens    = new Lexer().Lex(code);
            var parseTree = new Parser(tokens).Parse().ToList();

            Assert.NotNull(parseTree);
            Assert.Single(parseTree);

            ASTChoice choice = parseTree[0] as ASTChoice;

            Assert.Equal("Gender", choice.Name);
            Assert.Equal(3, choice.Options.Count);
            Assert.Equal("Male", choice.Options[0].Value);
            Assert.Equal("Female", choice.Options[1].Value);
            Assert.Equal("Other", choice.Options[2].Value);
        }
コード例 #10
0
        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.
                }
コード例 #11
0
        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);
        }
    }
コード例 #12
0
        public void ChoicesAnnotations()
        {
            var code      = @"
choice Gender =
    | ""Male""
    | ""Female""

    @ Non-binary option
    | ""Other""
";
            var tokens    = new Lexer().Lex(code);
            var parseTree = new Parser(tokens).Parse().ToList();

            Assert.NotNull(parseTree);
            Assert.Single(parseTree);

            ASTChoice choice = parseTree[0] as ASTChoice;
            ASTOption option = choice.Options[2];

            Assert.Equal("Other", option.Value);
            Assert.Single(option.Annotations);
            Assert.Equal("Non-binary option", option.Annotations.First().Value);
        }
コード例 #13
0
ファイル: Parser.cs プロジェクト: lucashorward/ZDragon.NET
        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;
        }
コード例 #14
0
 public override string VisitASTChoice(ASTChoice astChoice)
 {
     return("");
 }
コード例 #15
0
 public override T VisitASTChoice(ASTChoice astChoice) => d;
コード例 #16
0
        private dynamic GenerateEnum(ASTChoice choice)
        {
            var faker = new Faker();

            return(faker.Random.ListItem(choice.Options.ToList()).Value);
        }