コード例 #1
0
ファイル: Resolver.cs プロジェクト: lucashorward/ZDragon.NET
 private void ResolveType(ASTType t, List <IASTNode> nodes, List <ASTError> errors)
 {
     t.Extensions.ToList().ForEach(e =>
     {
         if (!(FindNode(e) is ASTType extendedFrom))
         {
             // TODO: Handle error gracefully.
             //throw new System.Exception($"Cannot find type {e} to extend from");
         }
コード例 #2
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),
     });
コード例 #3
0
        public override string VisitASTType(ASTType astType)
        {
            List <string> parts = new List <string>();

            parts.AddRange(astType.Annotations.Select(Visit));
            parts.AddRange(astType.Directives.Select(Visit));

            if (astType.Fields.Count() == 0)
            {
                parts.Add($"type {astType.Name}");
            }
            else
            {
                parts.Add($"type {astType.Name} =");
                parts.AddRange(astType.Fields.Select(Visit));
            }

            return(string.Join("\n", parts.ToArray()));
        }
コード例 #4
0
        public IEnumerable <IASTNode> Resolve()
        {
            foreach (IASTNode node in ParseTree)
            {
                if (node is ASTData)
                {
                    foreach (ASTDataOption option in ((ASTData)node).Options)
                    {
                        var existingNode = FindNode(option.Name);
                        if (existingNode is null)
                        {
                            yield return(new ASTType(
                                             option.Name,
                                             option.Parameters,
                                             Enumerable.Empty <ASTTypeField>(),
                                             Enumerable.Empty <ASTAnnotation>(),
                                             Enumerable.Empty <ASTDirective>()));
                        }
                    }
                    yield return(node);
                }
                else if (node is ASTType)
                {
                    ASTType t = (ASTType)node;
                    t.Extensions.ToList().ForEach(e =>
                    {
                        var extendedFrom = FindNode(e) as ASTType;
                        if (extendedFrom is null)
                        {
                            throw new System.Exception($"Cannot find type {e} to extend from");
                        }

                        var clones = extendedFrom.Fields.Select(f => f.Clone()).ToList();
                        t.AddFields(clones);
                    });
                    yield return(t);
                }
                else
                {
                    yield return(node);
                }
            }
        }
コード例 #5
0
        public IEnumerable <IASTNode> Parse()
        {
            List <ASTAnnotation> annotations = new List <ASTAnnotation>();
            List <ASTDirective>  directives  = new List <ASTDirective>();

            while (HasNext())
            {
                if (Current.TokenType == TokenType.KW_Type)
                {
                    var(errors, t) = ASTType.Parse(this, annotations, directives);
                    this.Errors.AddRange(errors);
                    yield return(t);
                }
                else if (Current.TokenType == TokenType.KW_Alias)
                {
                    yield return(new ASTAlias(this));
                }
                else if (Current.TokenType == TokenType.KW_Choice)
                {
                    yield return(new ASTChoice(this));
                }
                else if (Current.TokenType == TokenType.Annotation)
                {
                    annotations = ASTAnnotation.Parse(this).ToList();
                }
                else if (Current.TokenType == TokenType.Directive)
                {
                    directives = ASTDirective.Parse(this).ToList();
                }
                else
                {
                    Next();
                }
            }
            yield break;
        }
コード例 #6
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;
        }