c# - AST walker doesn't seem to work fine -
i using roslyn , have created ast walker in order detect when nodes traversed.
the source trying parse following:
var source = string.format(@" using system; using system.collections; using system.linq; using system.text; namespace helloworld {{ class {0} : {1} {{ static void main(string[] args) {{ console.writeline(""hello, world!""); }} }} }}", "myclass", "mybaseclass");
and walker:
namespace mystuff { using system; using microsoft.codeanalysis; using microsoft.codeanalysis.csharp; using microsoft.codeanalysis.csharp.syntax; public class mywalker : csharpsyntaxwalker { public mywalker(syntaxnode node) : base(syntaxwalkerdepth.structuredtrivia) { this.root = node; } public syntaxnode root { get; private set; } public void start() { this.visit(this.root); } sealed public override void visitaccessordeclaration(accessordeclarationsyntax node) { this.dosomething(node, this.type.isinstanceoftype(node)); } ... sealed public override void visityieldstatement(yieldstatementsyntax node) { this.dosomething(node, this.type.isinstanceoftype(node)); } // breakpoint in body of function! private void dosomething(syntaxnode node) { ... } } }
i overriding visit methods, alphabetically visitaccessordeclaration
visityieldstatement
. doing try stuff, not real applications, know quite nonsense.
visiting
i run it:
syntaxnode root = csharpsyntaxtree.parsetext(source).getroot();
and run walker:
var astwalker = new mywalker(this.root); astexecutor.start(); // next operation
what happens? seta breakpoint on function dosomething
, expect it lot of times. not happen. happens 1 node: visitcompilationunit
. after control flow passes on on // next operation
, that's it.
what missing? thanks
in overrides, need call base version of method overriding. allow stop walk. not calling base if trying visit specific nodes, can control it.
Comments
Post a Comment