none
Syntax nodes for storing comments in Syntax tree

    问题

  • Hello,

     

    I couldn't find the syntax node for storing nodes in the abstract syntax tree. Are they stored in some trivia structure? If they aren't stored in the AST, how are they printed in the GetText() method.

    Thanks!

    2011年10月25日 18:37

答案

  • Hi Rishabh - You can use the Syntax Visualizer Tool Window sample to inspect the tree structure and see where the comments are stored. (This blog post has more details about the Syntax Visualizer samples.)

    For example, in the code you posted, the '// comment1' above 'int b = 1' is stored as a Leading "SingleLineCommentTrivia" under the "IntKeyword" SyntaxToken for 'int b = 1' in the tree.

    If you right click on the "VariableDeclaration" node above and select "Directed Syntax Graph" then you can also view a graphical representation of the sub-tree that looks as follows. Notice again that the "SingleLineCommentTrivia" shows up under the "IntKeyword" amidst other Leading SyntaxTrivia such as "WhiteSpaceTrivia" and "EndOfLineTrivia".

    Using the public API, you can get at the "SingleLineCommentTrivia" for '// comment1' by calling the "LeadingTrivia" property on the SyntaxToken corresponding to the "IntKeyword" above or by calling "GetLeadingTrivia()" on the following ancsestor SyntaxNodes of this SyntaxToken - "PredefinedType" / "VariableDeclaration" / "LocalDeclarationStatement".

    Hope that helps!


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    2011年10月25日 19:21
    所有者:
  • Thanks for the feedback Rishabh!

    Yes you should be able to use the SyntaxRewriter for what you are trying to accomplish. Just override the VisitTrivia() method - you can detect and process comments by checking the 'Kind' of the input SyntaxTrivias.

    class MyRewriter : SyntaxRewriter
    {
        protected override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
        {
            var newTrivia = default(SyntaxTrivia);
            if (trivia.Kind == SyntaxKind.SingleLineCommentTrivia ||
                trivia.Kind == SyntaxKind.MultiLineCommentTrivia || ... )
            {
                // Handle comments.
            }
            else
            {
                // Handle other types of trivia.
                newTrivia = base.VisitTrivia(trivia);
            }
            return newTrivia;
        }
    }

     


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    • 已标记为答案 RishabhS 2011年10月25日 20:05
    2011年10月25日 19:58
    所有者:

全部回复

  • They are stored as SyntaxTrivia that hangs from the node as leading trivia or trailing trivia.

    Bill

     

    2011年10月25日 18:39
    所有者:
  • Thanks, I tried looking for SyntaxTrivia nodes but was unable to find the comment string in the trivia nodes. Is it the case that these nodes are not accessible from public interface?

     

    I am trying this very simple example

     

    int method1(){

    int a = 1;

    // comment1

    int b = 1;

    // comment 2

    }

     

    The comment statements comment1 and comment2 should be present in SyntaxTrivia of which nodes: BlockSyntax, MethodDeclarationSyntax or LocalStatementDeclarationSyntax nodes? 

     

    Thanks

    2011年10月25日 18:54
  • Hi Rishabh - You can use the Syntax Visualizer Tool Window sample to inspect the tree structure and see where the comments are stored. (This blog post has more details about the Syntax Visualizer samples.)

    For example, in the code you posted, the '// comment1' above 'int b = 1' is stored as a Leading "SingleLineCommentTrivia" under the "IntKeyword" SyntaxToken for 'int b = 1' in the tree.

    If you right click on the "VariableDeclaration" node above and select "Directed Syntax Graph" then you can also view a graphical representation of the sub-tree that looks as follows. Notice again that the "SingleLineCommentTrivia" shows up under the "IntKeyword" amidst other Leading SyntaxTrivia such as "WhiteSpaceTrivia" and "EndOfLineTrivia".

    Using the public API, you can get at the "SingleLineCommentTrivia" for '// comment1' by calling the "LeadingTrivia" property on the SyntaxToken corresponding to the "IntKeyword" above or by calling "GetLeadingTrivia()" on the following ancsestor SyntaxNodes of this SyntaxToken - "PredefinedType" / "VariableDeclaration" / "LocalDeclarationStatement".

    Hope that helps!


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    2011年10月25日 19:21
    所有者:
  • The heuristic we use to attach trivia is that most trivia is attached to the token following it, unless it is after the last token on a line, in which case it is attached to the token following it.

    In your example above, "// comment1" would be attached as LeadingTrivia to the "int" keyword token in the "int a = 1;" statement, which also "bubbles up" so that you can see it on the LocalDeclarationStatementSyntax node.

    "//comment 2" would be attached as LeadingTrivia for the "}" token of the method.  Since it doesn't either "Lead" or "Trail" any nodes, it wouldn't bubble up, so you'd have to look at the trivia directly on the token to see it.

    Hope this helps!

    --

    Kevin

    2011年10月25日 19:21
    所有者:
  • Thanks Shyam and Kevin! The Syntax visualizer tool looks amazingly helpful (previously I had to learn the syntax structure all through debugger which wasn't easy :)). Now I am able to figure out where the comments are stored in the tree, Thanks a lot for the helpful information!

    My goal is to look for comments and to remove them from the code. Is there a rewriter function available in SyntaxRewriter class that lets me visit these comments statements recursively to modify/delete them from the tree similar to the VisitMethods in the class.

    Thanks again!

    2011年10月25日 19:45
  • Thanks for the feedback Rishabh!

    Yes you should be able to use the SyntaxRewriter for what you are trying to accomplish. Just override the VisitTrivia() method - you can detect and process comments by checking the 'Kind' of the input SyntaxTrivias.

    class MyRewriter : SyntaxRewriter
    {
        protected override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
        {
            var newTrivia = default(SyntaxTrivia);
            if (trivia.Kind == SyntaxKind.SingleLineCommentTrivia ||
                trivia.Kind == SyntaxKind.MultiLineCommentTrivia || ... )
            {
                // Handle comments.
            }
            else
            {
                // Handle other types of trivia.
                newTrivia = base.VisitTrivia(trivia);
            }
            return newTrivia;
        }
    }

     


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    • 已标记为答案 RishabhS 2011年10月25日 20:05
    2011年10月25日 19:58
    所有者:
  • This was exactly what I was looking for, thanks a lot Shyam!
    2011年10月25日 20:06