private Func <SyntaxNode, Scope, LookAheadResult> MatchTypeExtension(IncompleteMemberSyntax incomplete, SyntacticalExtension <SyntaxNode> extension) { return((node, scope) => { var resulSyntaxNode = node; if (node is ClassDeclarationSyntax) { ClassDeclarationSyntax clazz = (ClassDeclarationSyntax)node; clazz = clazz .WithAttributeLists(incomplete.AttributeLists) .WithModifiers(incomplete.Modifiers); resulSyntaxNode = extension.Handler(node, scope, extension); } //td: error?, expecting class return new LookAheadResult { Matched = resulSyntaxNode != null, Result = resulSyntaxNode }; }); }
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax method) { SyntacticalExtension <SyntaxNode> extension = methodExtension(method); if (extension != null) { switch (extension.Kind) { case ExtensionKind.Member: case ExtensionKind.Type: { return(extension.Handler(method, new Scope(_scope), extension)); } default: { //td: error, incorrect extension (i.e. a code extension being used inside a type) return(null); } } } return(base.VisitMethodDeclaration(method)); }
//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, }; }); }
private Func<SyntaxNode, Scope, LookAheadResult> MatchTypeExtension(IncompleteMemberSyntax incomplete, SyntacticalExtension<SyntaxNode> extension) { return (node, scope) => { var resulSyntaxNode = node; if (node is ClassDeclarationSyntax) { ClassDeclarationSyntax clazz = (ClassDeclarationSyntax)node; clazz = clazz .WithAttributeLists(incomplete.AttributeLists) .WithModifiers(incomplete.Modifiers); resulSyntaxNode = extension.Handler(node, scope, extension); } //td: error?, expecting class return new LookAheadResult { Matched = resulSyntaxNode != null, Result = resulSyntaxNode }; }; }
//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, }; }; }