Example #1
0
        public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            if (node.Declaration.Variables.Count == 1)
            {
                IDSLHandler dsl = null;
                dsl = ctx_.CreateDSL(node.Declaration.Type.ToString());
                if (dsl != null)
                {
                    if (node.Declaration.Variables.Count != 1)
                    {
                        //td: error
                        return(node);
                    }

                    var        variable = node.Declaration.Variables[0];
                    DSLContext dctx     = new DSLContext {
                        MainNode = variable, Surroundings = DSLSurroundings.Code, Assign = false, ExtraMembers = members_
                    };
                    Debug.Assert(pending_ == null);
                    pending_ = new ResolveDSLCode(dsl, ctx_, dctx);
                    return(dsl.compile(ctx_, dctx));
                }
            }

            return(base.VisitLocalDeclarationStatement(node));
        }
Example #2
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 }));
        }
Example #3
0
        public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            //handle language constructs
            string typeName = node.ReturnType.ToString();

            switch (typeName)
            {
            case "on":       return(rewriteEventHandler(node));

            case "function": return(rewriteFunction(node, false));

            case "method":   return(rewriteFunction(node, true));

            case "typedef":  return(rewriteTypedef(node));

            case "":
            {
                switch (node.Identifier.ToString())
                {
                case "constructor": return(rewriteConstructor(node));
                }
                break;
            }
            }

            //handle dsls
            IDSLHandler     dsl = null;
            DSLSurroundings ds  = node.Parent is CompilationUnitSyntax ? DSLSurroundings.Global : DSLSurroundings.TypeBody;
            string          id  = null;

            if (!node.ReturnType.IsMissing)
            {
                dsl = ctx_.CreateDSL(typeName);
                id  = node.Identifier.ToString();
            }
            else
            {
                dsl = ctx_.CreateDSL(node.Identifier.ToString());
            }

            if (dsl != null)
            {
                DSLContext dctx = new DSLContext {
                    MainNode = node, Surroundings = ds, Id = id, ExtraMembers = members_
                };
                return(dsl.compile(ctx_, dctx));
            }

            return(node.WithBody((BlockSyntax)base.Visit(node.Body)));
        }
Example #4
0
        public SyntaxNode rewrite(CSharpSyntaxRewriter transform, SyntaxNode node, out LookAheadAction action)
        {
            if (node is ClassDeclarationSyntax)
            {
                action = LookAheadAction.SUCCEDED;
                ClassDeclarationSyntax clazz = (ClassDeclarationSyntax)node;
                DSLContext             dctx  = new DSLContext {
                    MainNode = node, Surroundings = DSLSurroundings.Global, ExtraMembers = extraMembers_
                };
                return(dsl_.compile(ctx_, dctx));
            }

            action = LookAheadAction.FAILED;
            return(null);
        }
Example #5
0
        private static IEnumerable <SyntaxNode> _defaultMemberHandler(CompleteInfo complete)
        {
            foreach (var member in complete.Members)
            {
                var method = member as MethodDeclarationSyntax;
                if (method == null)
                {
                    yield return(member);

                    continue;
                }

                var ctx = complete.Context;
                var dsl = null as IDSLHandler;
                var id  = string.Empty;
                if (!method.ReturnType.IsMissing)
                {
                    dsl = ctx.CreateDSL(method.ReturnType.ToString());
                    id  = method.Identifier.ToString();
                }
                else
                {
                    dsl = ctx.CreateDSL(method.Identifier.ToString());
                }

                if (dsl != null)
                {
                    var        additional = new List <MemberDeclarationSyntax>();
                    DSLContext dctx       = new DSLContext {
                        MainNode = method, Surroundings = DSLSurroundings.Global, Id = id, ExtraMembers = additional
                    };
                    var dslResult = dsl.compile(ctx, dctx);

                    if (dslResult != null)
                    {
                        complete.Additional.Add(dslResult);
                    }

                    complete.Additional.AddRange(additional);
                }
                else
                {
                    yield return(method);
                }
            }
        }
Example #6
0
        private SyntaxNode rewriteInvocation(InvocationExpressionSyntax node, bool assign)
        {
            IDSLHandler dsl = ctx_.CreateDSL(node.Expression.ToString());

            if (dsl != null)
            {
                DSLContext dctx = new DSLContext {
                    MainNode = node, Surroundings = DSLSurroundings.Code, Assign = assign, ExtraMembers = members_
                };

                Debug.Assert(pending_ == null);
                pending_ = new ResolveDSLCode(dsl, ctx_, dctx);
                return(dsl.compile(ctx_, dctx));
            }
            else if (node.ArgumentList.GetDiagnostics().Any())
            {
                return(SyntaxFactory.InvocationExpression(node.Expression, (ArgumentListSyntax)Visit(node.ArgumentList)));
            }

            return(node);
        }
Example #7
0
 public ResolveDSLCode(IDSLHandler dsl, ExcessContext ctx, DSLContext dctx)
 {
     dsl_  = dsl;
     ctx_  = ctx;
     dctx_ = dctx;
 }
Example #8
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);
        }