Class AstRoot

All Implemented Interfaces:
Comparable<AstNode>, Iterable<Node>

public class AstRoot extends ScriptNode
Node for the root of a parse tree. It contains the statements and functions in the script, and a list of Comment nodes associated with the script as a whole. Node type is Token.SCRIPT.

Note that the tree itself does not store errors. To collect the parse errors and warnings, pass an ErrorReporter to the Parser via the CompilerEnvirons.

  • Constructor Details

    • AstRoot

      public AstRoot()
    • AstRoot

      public AstRoot(int pos)
  • Method Details

    • getComments

      public SortedSet<Comment> getComments()
      Returns comment set
      Returns:
      comment set, sorted by start position. Can be null.
    • setComments

      public void setComments(SortedSet<Comment> comments)
      Sets comment list, and updates the parent of each entry to point to this node. Replaces any existing comments.
      Parameters:
      comments - comment list. can be null.
    • addComment

      public void addComment(Comment comment)
      Add a comment to the comment set.
      Parameters:
      comment - the comment node.
      Throws:
      IllegalArgumentException - if comment is null
    • visitComments

      public void visitComments(NodeVisitor visitor)
      Visits the comment nodes in the order they appear in the source code. The comments are not visited by the ScriptNode.visit(org.mozilla.javascript.ast.NodeVisitor) function - you must use this function to visit them.
      Parameters:
      visitor - the callback object. It is passed each comment node. The return value is ignored.
    • visitAll

      public void visitAll(NodeVisitor visitor)
      Visits the AST nodes, then the comment nodes. This method is equivalent to calling ScriptNode.visit(org.mozilla.javascript.ast.NodeVisitor), then visitComments(org.mozilla.javascript.ast.NodeVisitor). The return value is ignored while visiting comment nodes.
      Parameters:
      visitor - the callback object.
    • toSource

      public String toSource(int depth)
      Description copied from class: AstNode
      Emits source code for this node. Callee is responsible for calling this function recursively on children, incrementing indent as appropriate.

      Note: if the parser was in error-recovery mode, some AST nodes may have null children that are expected to be non-null when no errors are present. In this situation, the behavior of the toSource method is undefined: toSource implementations may assume that the AST node is error-free, since it is intended to be invoked only at runtime after a successful parse.

      Overrides:
      toSource in class Scope
      Parameters:
      depth - the current recursion depth, typically beginning at 0 when called on the root node.
    • debugPrint

      public String debugPrint()
      A debug-printer that includes comments (at the end).
      Overrides:
      debugPrint in class AstNode
      Returns:
      a very verbose indented printout of the tree. The format of each line is: abs-pos name position length [identifier]
    • checkParentLinks

      public void checkParentLinks()
      Debugging function to check that the parser has set the parent link for every node in the tree.
      Throws:
      IllegalStateException - if a parent link is missing