Class CommonTreeNodeStream

All Implemented Interfaces:
IntStream, PositionTrackingStream<Object>, TreeNodeStream

public class CommonTreeNodeStream extends LookaheadStream<Object> implements TreeNodeStream, PositionTrackingStream<Object>
  • Field Details

  • Constructor Details

    • CommonTreeNodeStream

      public CommonTreeNodeStream(Object tree)
    • CommonTreeNodeStream

      public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree)
  • Method Details

    • reset

      public void reset()
      Description copied from interface: TreeNodeStream
      Reset the tree node stream in such a way that it acts like a freshly constructed stream.
      Specified by:
      reset in interface TreeNodeStream
      Overrides:
      reset in class LookaheadStream<Object>
    • nextElement

      public Object nextElement()
      Pull elements from tree iterator. Track tree level 0..max_level. If nil rooted tree, don't give initial nil and DOWN nor final UP.
      Specified by:
      nextElement in class LookaheadStream<Object>
      See Also:
    • remove

      public Object remove()
      Description copied from class: LookaheadStream
      Get and remove first element in queue; override FastQueue.remove(); it's the same, just checks for backtracking.
      Overrides:
      remove in class LookaheadStream<Object>
    • isEOF

      public boolean isEOF(Object o)
      Specified by:
      isEOF in class LookaheadStream<Object>
    • setUniqueNavigationNodes

      public void setUniqueNavigationNodes(boolean uniqueNavigationNodes)
      Description copied from interface: TreeNodeStream
      As we flatten the tree, we use Token.UP, Token.DOWN nodes to represent the tree structure. When debugging we need unique nodes so we have to instantiate new ones. When doing normal tree parsing, it's slow and a waste of memory to create unique navigation nodes. Default should be false.
      Specified by:
      setUniqueNavigationNodes in interface TreeNodeStream
    • getTreeSource

      public Object getTreeSource()
      Description copied from interface: TreeNodeStream
      Where is this stream pulling nodes from? This is not the name, but the object that provides node objects.
      Specified by:
      getTreeSource in interface TreeNodeStream
    • getSourceName

      public String getSourceName()
      Description copied from interface: IntStream
      Where are you getting symbols from? Normally, implementations will pass the buck all the way to the lexer who can ask its input stream for the file name or whatever.
      Specified by:
      getSourceName in interface IntStream
    • getTokenStream

      public TokenStream getTokenStream()
      Description copied from interface: TreeNodeStream
      If the tree associated with this stream was created from a TokenStream, you can specify it here. Used to do rule $text attribute in tree parser. Optional unless you use tree parser rule $text attribute or output=template and rewrite=true options.
      Specified by:
      getTokenStream in interface TreeNodeStream
    • setTokenStream

      public void setTokenStream(TokenStream tokens)
    • getTreeAdaptor

      public TreeAdaptor getTreeAdaptor()
      Description copied from interface: TreeNodeStream
      What adaptor can tell me how to interpret/navigate nodes and trees. E.g., get text of a node.
      Specified by:
      getTreeAdaptor in interface TreeNodeStream
    • setTreeAdaptor

      public void setTreeAdaptor(TreeAdaptor adaptor)
    • get

      public Object get(int i)
      Description copied from interface: TreeNodeStream
      Get a tree node at an absolute index i; 0..n-1. If you don't want to buffer up nodes, then this method makes no sense for you.
      Specified by:
      get in interface TreeNodeStream
    • LA

      public int LA(int i)
      Description copied from interface: IntStream
      Get int at current input pointer + i ahead where i=1 is next int. Negative indexes are allowed. LA(-1) is previous token (token just matched). LA(-i) where i is before first token should yield -1, invalid char / EOF.
      Specified by:
      LA in interface IntStream
    • push

      public void push(int index)
      Make stream jump to a new location, saving old location. Switch back with pop().
    • pop

      public int pop()
      Seek back to previous index saved during last push(int) call. Return top of stack (return index).
    • getKnownPositionElement

      public Object getKnownPositionElement(boolean allowApproximateLocation)
      Returns an element containing position information. If allowApproximateLocation is false, then this method will return the LT(1) element if it contains position information, and otherwise return null. If allowApproximateLocation is true, then this method will return the last known element containing position information.
      Specified by:
      getKnownPositionElement in interface PositionTrackingStream<Object>
      Parameters:
      allowApproximateLocation - if false, this method returns null if an element containing exact information about the current position is not available
      See Also:
    • hasPositionInformation

      public boolean hasPositionInformation(Object node)
      Description copied from interface: PositionTrackingStream
      Determines if the specified element contains concrete position information.
      Specified by:
      hasPositionInformation in interface PositionTrackingStream<Object>
      Parameters:
      node - the element to check
      Returns:
      true if element contains concrete position information, otherwise false
    • replaceChildren

      public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t)
      Description copied from interface: TreeNodeStream
      Replace children of parent from index startChildIndex to stopChildIndex with t, which might be a list. Number of children may be different after this call. The stream is notified because it is walking the tree and might need to know you are monkeying with the underlying tree. Also, it might be able to modify the node stream to avoid restreaming for future phases.

      If parent is null, don't do anything; must be at root of overall tree. Can't replace whatever points to the parent externally. Do nothing.

      Specified by:
      replaceChildren in interface TreeNodeStream
    • toString

      public String toString(Object start, Object stop)
      Description copied from interface: TreeNodeStream
      Return the text of all nodes from start to stop, inclusive. If the stream does not buffer all the nodes then it can still walk recursively from start until stop. You can always return null or "" too, but users should not access $ruleLabel.text in an action of course in that case.
      Specified by:
      toString in interface TreeNodeStream
    • toTokenTypeString

      public String toTokenTypeString()
      For debugging; destructive: moves tree iterator to end.