Exemplo n.º 1
0
        public SyntaxNode setCode(ExcessContext ctx, DSLContext dctx, BlockSyntax code)
        {
            if (_parseCode == null)
            {
                throw new InvalidOperationException("This dsl does not support code");
            }

            SyntaxNode          node       = dctx.MainNode;
            string              identifier = "";
            ParameterListSyntax args       = null;

            var invocation = node as InvocationExpressionSyntax;

            if (invocation != null)
            {
                args = toParameterList(invocation.ArgumentList.Arguments);
            }
            else
            {
                var varDeclarator = node as VariableDeclaratorSyntax;
                if (varDeclarator == null)
                {
                    //td: error
                    return(node);
                }

                identifier = varDeclarator.Identifier.ToString();
                args       = toParameterList(varDeclarator.ArgumentList.Arguments);
            }

            code = (BlockSyntax)ctx.Rewriter.Visit(code);
            return((SyntaxNode)_parseCode.Invoke(_parser, new object[] { code, identifier, args, code, dctx.Assign }));
        }
Exemplo n.º 2
0
        public ResolveArrayArgument(ExcessContext ctx, SyntaxNode node, ArrayCreationExpressionSyntax arg)
        {
            ctx_  = ctx;
            node_ = node;

            args_.Add(SyntaxFactory.Argument(arg));
        }
Exemplo n.º 3
0
 public ResolveEventArguments(string name, ExcessContext ctx, List <MemberDeclarationSyntax> extraMembers, SyntaxTokenList modifiers)
 {
     name_         = name;
     ctx_          = ctx;
     extraMembers_ = extraMembers;
     modifiers_    = modifiers;
 }
Exemplo n.º 4
0
        public static SyntaxTree Compile(ExcessContext ctx, string code, string file = null)
        {
            Compiler compiler = new Compiler(ctx);

            ctx.FileName = file;
            ctx.Rewriter = compiler;

            SyntaxTree tree = compiler.compile(code);

            bool needsRecompile = true; //td: !!!

            if (needsRecompile)
            {
                var root = tree.GetRoot() as CompilationUnitSyntax;
                if (root == null)
                {
                    root = SyntaxFactory.CompilationUnit().
                           WithMembers(SyntaxFactory.List(new MemberDeclarationSyntax[] {
                        (MemberDeclarationSyntax)tree.GetRoot()
                    }));
                }

                var usings = ctx.GetUsings().Select <string, UsingDirectiveSyntax>(
                    @using => SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName(
                                                               @using)));

                root = root.WithUsings(SyntaxFactory.List(usings)).
                       NormalizeWhitespace();

                //root = root.WithUsings(SyntaxFactory.List(new UsingDirectiveSyntax[]
                //        {
                //            SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System")),
                //            SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Collections")),
                //            SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Collections.Generic")),
                //        })).NormalizeWhitespace();

                var    linkerNodes = root.GetAnnotatedNodes(Compiler.LinkerAnnotationId);
                string currVersion = root.ToFullString();

                root = SyntaxFactory.ParseCompilationUnit(currVersion);

                foreach (var linkNode in linkerNodes)
                {
                    SyntaxNode rn = root.FindNode(linkNode.Span);
                    if (rn != null)
                    {
                        var annotation = linkNode.GetAnnotations(Compiler.LinkerAnnotationId).First();
                        annotation = new SyntaxAnnotation(annotation.Kind, annotation.Data);

                        root = root.ReplaceNode(rn, rn.WithAdditionalAnnotations(annotation));
                    }
                }

                tree = root.SyntaxTree;
            }

            ctx.FileName = null;
            return(tree);
        }
Exemplo n.º 5
0
        public SyntaxNode link(ExcessContext ctx, SyntaxNode node, SemanticModel model)
        {
            if (_link == null)
            {
                throw new InvalidOperationException("This dsl does not support linking");
            }

            return((SyntaxNode)_link.Invoke(_linker, new object[] { node, model }));
        }
Exemplo n.º 6
0
        private SyntaxNode linkArray(ExcessContext ctx, SyntaxNode linkNode, SyntaxNode newNode, SemanticModel model)
        {
            ArrayCreationExpressionSyntax ace = newNode as ArrayCreationExpressionSyntax;
            ArgumentSyntax arg     = null;
            bool           asParam = newNode is ArgumentSyntax;

            if (asParam)
            {
                arg = (ArgumentSyntax)newNode;
                ace = (ArrayCreationExpressionSyntax)arg.Expression;
            }

            ITypeSymbol arrayType = null;

            foreach (var expr in ace.Initializer.Expressions)
            {
                ITypeSymbol type = model.GetSpeculativeTypeInfo(expr.SpanStart, expr, SpeculativeBindingOption.BindAsExpression).Type;

                if (arrayType == null)
                {
                    arrayType = type;
                }
                else if (type != arrayType)
                {
                    if (isSuperClass(type, arrayType))
                    {
                        arrayType = type; //downcast
                    }
                    else if (!isSuperClass(arrayType, type))
                    {
                        //td: error
                        return(newNode); //unable to refine
                    }
                }
            }

            if (arrayType == null)
            {
                return(newNode);
            }

            ace = ace.WithType(SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName(arrayType.Name + "[]")));
            if (asParam)
            {
                return(arg.WithExpression(ace));
            }

            return(ace);
        }
Exemplo n.º 7
0
        public static Compilation Link(ExcessContext ctx, Compilation compilation, Dictionary <SyntaxTree, SyntaxTree> track = null)
        {
            Compilation result = compilation;

            foreach (var tree in compilation.SyntaxTrees)
            {
                SyntaxTree curr = ctx.fixErrors(tree, result, out result);

                var linker     = new Linker(ctx, result);
                var resultTree = linker.link(curr.GetRoot(), out result).SyntaxTree;
                if (track != null)
                {
                    track[tree] = resultTree;
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        public static Compilation Link(ExcessContext ctx, Compilation compilation, IEnumerable <SyntaxTree> trees, Dictionary <SyntaxTree, SyntaxTree> track = null)
        {
            Compilation result = compilation;

            foreach (var tree in trees)
            {
                //add usings
                var        root       = tree.GetRoot() as CompilationUnitSyntax;
                var        actualTree = root.SyntaxTree;
                SyntaxTree curr       = ctx.fixErrors(actualTree, result, out result);

                Linker linker     = new Linker(ctx, result);
                var    resultTree = linker.link(curr.GetRoot(), out result).SyntaxTree;

                if (track != null)
                {
                    track[tree] = resultTree;
                }
            }

            return(result);
        }
Exemplo n.º 9
0
 public ResolveDSLCode(IDSLHandler dsl, ExcessContext ctx, DSLContext dctx)
 {
     dsl_  = dsl;
     ctx_  = ctx;
     dctx_ = dctx;
 }
Exemplo n.º 10
0
 public ResolveTypedef(FieldDeclarationSyntax field, ExcessContext ctx)
 {
     field_ = field;
     ctx_   = ctx;
 }
Exemplo n.º 11
0
 public static SyntaxTree Compile(string code, IDSLFactory factory, out ExcessContext ctx, string file = null)
 {
     ctx = new ExcessContext(factory);
     return(Compile(ctx, code, file));
 }
Exemplo n.º 12
0
        public SyntaxNode compile(ExcessContext ctx, DSLContext dctx)
        {
            setContext(_parser, ctx);
            setContext(_linker, ctx);

            var node = dctx.MainNode;

            switch (dctx.Surroundings)
            {
            case DSLSurroundings.Global:
            {
                if (node is ClassDeclarationSyntax)
                {
                    if (_parseClass == null)
                    {
                        throw new InvalidOperationException("This dsl does not support types");
                    }

                    var classDeclaration = (ClassDeclarationSyntax)ctx.Rewriter.Visit(node);

                    var id     = classDeclaration.Identifier.ToString();
                    var result = ctx.Rewriter.Visit(node);
                    return((SyntaxNode)_parseClass.Invoke(_parser, new object[] { classDeclaration, id, SyntaxFactory.ParameterList() }));
                }
                else if (node is MethodDeclarationSyntax)
                {
                    if (_parseNamespace == null)
                    {
                        throw new InvalidOperationException("This dsl does not support namespaces");
                    }

                    var method = (MethodDeclarationSyntax)node;
                    var id     = method.ReturnType.IsMissing? "" :  method.Identifier.ToString();
                    var code   = (BlockSyntax)ctx.Rewriter.Visit(method.Body);

                    return((SyntaxNode)_parseNamespace.Invoke(_parser, new object[] { method, id, method.ParameterList, code }));
                }

                //td: error
                break;
            }

            case DSLSurroundings.TypeBody:
            {
                if (_parseMethod == null)
                {
                    throw new InvalidOperationException("This dsl does not support methods");
                }

                var method = (MethodDeclarationSyntax)node;
                var code   = (BlockSyntax)ctx.Rewriter.Visit(method.Body);

                return((SyntaxNode)_parseMethod.Invoke(_parser, new object[] { method, "", method.ParameterList, code }));
            }

            case DSLSurroundings.Code:
            {
                if (_parseCodeHeader == null)
                {
                    return(null);
                }

                return((SyntaxNode)_parseCodeHeader.Invoke(_parser, new object[] { node }));
            }
            }

            return(node);
        }
Exemplo n.º 13
0
 public ResolveTypedFunction(FieldDeclarationSyntax field, ExcessContext ctx, bool asPublic)
 {
     field_    = field;
     ctx_      = ctx;
     asPublic_ = asPublic;
 }
Exemplo n.º 14
0
 public ResolveDSLClass(IDSLHandler dsl, ExcessContext ctx, List <MemberDeclarationSyntax> extraMembers)
 {
     dsl_          = dsl;
     ctx_          = ctx;
     extraMembers_ = extraMembers;
 }
Exemplo n.º 15
0
 public void SetContext(ExcessContext ctx)
 {
     _ctx  = ctx;
     _mark = new SyntaxAnnotation(ctx.GetUnique(_name));
 }
Exemplo n.º 16
0
 public Linker(ExcessContext ctx, Compilation compilation)
 {
     ctx_         = ctx;
     compilation_ = compilation;
 }
Exemplo n.º 17
0
 public ExcessParamListRewriter(ExcessContext ctx)
 {
     ctx_ = ctx;
 }
Exemplo n.º 18
0
 public Compiler(ExcessContext ctx)
 {
     ctx_ = ctx;
 }
Exemplo n.º 19
0
        private void setContext(object obj, ExcessContext ctx)
        {
            var method = obj.GetType().GetMethod("SetContext");

            method.Invoke(obj, new object[] { ctx });
        }