Beispiel #1
0
        public override SyntaxNode Visit(SyntaxNode node)
        {
            if (node == null)
            {
                return(null);
            }

            var result = node;

            foreach (var transformer in _transformers)
            {
                var before = result;
                result = transformer(result, _scope);

                if (result != before)
                {
                    result = RoslynCompiler.UpdateExcessId(result, before);
                }

                if (result == null)
                {
                    return(null);
                }
            }

            return(base.Visit(result));
        }
Beispiel #2
0
        //rewriters
        private Func <SyntaxNode, Scope, LookAheadResult> CheckCodeExtension(SyntaxNode original, SyntacticalExtension <SyntaxNode> extension)
        {
            return((node, scope) =>
            {
                var code = node as BlockSyntax;
                if (code == null)
                {
                    return new LookAheadResult {
                        Matched = false
                    }
                }
                ;

                _lookahead = null;
                extension.Body = base.Visit(code);

                SyntaxNode resulSyntaxNode = null;

                if (original is LocalDeclarationStatementSyntax)
                {
                    extension.Kind = ExtensionKind.Expression;

                    resulSyntaxNode = extension.Handler(node, scope, extension);
                    if (!(resulSyntaxNode is ExpressionSyntax))
                    {
                        //td: error, expecting expression
                        return new LookAheadResult {
                            Matched = false
                        };
                    }

                    var localDecl = original as LocalDeclarationStatementSyntax;
                    resulSyntaxNode = localDecl
                                      .WithDeclaration(localDecl.Declaration
                                                       .WithVariables(CSharp.SeparatedList(new[] {
                        localDecl.Declaration.Variables[0]
                        .WithInitializer(localDecl.Declaration.Variables[0].Initializer
                                         .WithValue((ExpressionSyntax)resulSyntaxNode))
                    })))
                                      .WithSemicolonToken(CSharp.ParseToken(";"));
                }
                else if (original is ExpressionStatementSyntax)
                {
                    var exprStatement = original as ExpressionStatementSyntax;
                    var assignment = exprStatement.Expression as AssignmentExpressionSyntax;
                    if (assignment != null)
                    {
                        extension.Kind = ExtensionKind.Expression;
                        resulSyntaxNode = extension.Handler(node, scope, extension);
                        if (!(resulSyntaxNode is ExpressionSyntax))
                        {
                            //td: error, expecting expression
                            return new LookAheadResult {
                                Matched = false
                            };
                        }

                        resulSyntaxNode = exprStatement
                                          .WithExpression(assignment
                                                          .WithRight((ExpressionSyntax)resulSyntaxNode))
                                          .WithSemicolonToken(CSharp.ParseToken(";"));
                    }
                    else
                    {
                        resulSyntaxNode = extension.Handler(node, scope, extension);
                        if (resulSyntaxNode != null)
                        {
                            resulSyntaxNode = RoslynCompiler.UpdateExcessId(resulSyntaxNode, node);
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }

                return new LookAheadResult
                {
                    Matched = true,
                    Result = resulSyntaxNode,
                };
            });
        }