Class LexicalPreservingPrinter


  • public class LexicalPreservingPrinter
    extends java.lang.Object
    A Lexical Preserving Printer is used to capture all the lexical information while parsing, update them when operating on the AST and then used them to reproduce the source code in its original formatting including the AST changes.
    • Field Detail

      • NODE_TEXT_DATA

        public static final DataKey<NodeText> NODE_TEXT_DATA
        The nodetext for a node is stored in the node's data field. This is the key to set and retrieve it.
    • Constructor Detail

      • LexicalPreservingPrinter

        public LexicalPreservingPrinter()
    • Method Detail

      • setup

        public static <N extends Node> N setup​(N node)
        Prepares the node so it can be used in the print methods. The correct order is:
        1. Parse some code
        2. Call this setup method on the result
        3. Make changes to the AST as desired
        4. Use one of the print methods on this class to print out the original source code with your changes added
        Returns:
        the node passed as a parameter for your convenience.
      • createObserver

        private static AstObserver createObserver()
      • storeInitialText

        private static void storeInitialText​(Node root)
      • findNodeForToken

        private static Node findNodeForToken​(Node node,
                                             Range tokenRange)
      • storeInitialTextForOneNode

        private static void storeInitialTextForOneNode​(Node node,
                                                       java.util.List<JavaToken> nodeTokens)
      • tokensPreceeding

        private static java.util.Iterator<TokenTextElement> tokensPreceeding​(Node node)
      • print

        public static java.lang.String print​(Node node)
        Print a Node into a String, preserving the lexical information.
      • print

        public static void print​(Node node,
                                 java.io.Writer writer)
                          throws java.io.IOException
        Print a Node into a Writer, preserving the lexical information.
        Throws:
        java.io.IOException
      • prettyPrintingTextNode

        private static void prettyPrintingTextNode​(Node node,
                                                   NodeText nodeText)
      • interpret

        private static NodeText interpret​(Node node,
                                          CsmElement csm,
                                          NodeText nodeText)
        TODO: Process CsmIndent and CsmUnindent before reaching this point
      • getComment

        public static java.util.Optional<Comment> getComment​(Node node)
        Returns the comment or an Optional.empty if there is no comment on this method call. In case of MethodCallExpr, Comment are setted in the parent ExpressionStmt comment attribute.
      • getTokenKind

        private static int getTokenKind​(Comment comment)
      • getOrCreateNodeText

        static NodeText getOrCreateNodeText​(Node node)
      • isReturningOptionalNodeList

        private static boolean isReturningOptionalNodeList​(java.lang.reflect.Method m)