Package org.antlr.runtime.tree
Class CommonTreeNodeStream
- java.lang.Object
-
- org.antlr.runtime.misc.FastQueue<T>
-
- org.antlr.runtime.misc.LookaheadStream<Object>
-
- org.antlr.runtime.tree.CommonTreeNodeStream
-
- All Implemented Interfaces:
IntStream,PositionTrackingStream<Object>,TreeNodeStream
public class CommonTreeNodeStream extends LookaheadStream<Object> implements TreeNodeStream, PositionTrackingStream<Object>
-
-
Field Summary
Fields Modifier and Type Field Description protected IntArraycallsStack of indexes used for push/pop calls.static intDEFAULT_INITIAL_BUFFER_SIZEprotected booleanhasNilRootTree(nil A B C)trees like flatA B Cstreamsstatic intINITIAL_CALL_STACK_SIZEprotected TreeIteratoritTheTreeIteratorwe using.protected intlevelTracks tree depth.protected ObjectpreviousLocationElementTracks the last node before the start ofFastQueue.datawhich contains position information to provide information for error reporting.protected ObjectrootPull nodes from which tree?protected TokenStreamtokensIf this tree (root) was created from aTokenStream, track it.-
Fields inherited from class org.antlr.runtime.misc.LookaheadStream
currentElementIndex, eof, lastMarker, markDepth, prevElement, UNINITIALIZED_EOF_ELEMENT_INDEX
-
-
Constructor Summary
Constructors Constructor Description CommonTreeNodeStream(Object tree)CommonTreeNodeStream(TreeAdaptor adaptor, Object tree)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectget(int i)Get a tree node at an absolute indexi; 0..n-1.ObjectgetKnownPositionElement(boolean allowApproximateLocation)Returns an element containing position information.StringgetSourceName()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.TokenStreamgetTokenStream()If the tree associated with this stream was created from aTokenStream, you can specify it here.TreeAdaptorgetTreeAdaptor()What adaptor can tell me how to interpret/navigate nodes and trees.ObjectgetTreeSource()Where is this stream pulling nodes from? This is not the name, but the object that provides node objects.booleanhasPositionInformation(Object node)Determines if the specifiedelementcontains concrete position information.booleanisEOF(Object o)intLA(int i)Get int at current input pointer + i ahead where i=1 is next int.ObjectnextElement()Pull elements from tree iterator.intpop()Seek back to previous index saved during lastpush(int)call.voidpush(int index)Make stream jump to a new location, saving old location.Objectremove()Get and remove first element in queue; overrideFastQueue.remove(); it's the same, just checks for backtracking.voidreplaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t)Replace children ofparentfrom indexstartChildIndextostopChildIndexwitht, which might be a list.voidreset()Reset the tree node stream in such a way that it acts like a freshly constructed stream.voidsetTokenStream(TokenStream tokens)voidsetTreeAdaptor(TreeAdaptor adaptor)voidsetUniqueNavigationNodes(boolean uniqueNavigationNodes)As we flatten the tree, we useToken.UP,Token.DOWNnodes to represent the tree structure.StringtoString(Object start, Object stop)Return the text of all nodes fromstarttostop, inclusive.StringtoTokenTypeString()For debugging; destructive: moves tree iterator to end.-
Methods inherited from class org.antlr.runtime.misc.LookaheadStream
consume, fill, index, LB, LT, mark, release, rewind, rewind, seek, size, syncAhead
-
Methods inherited from class org.antlr.runtime.misc.FastQueue
add, clear, elementAt, head, range, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.antlr.runtime.IntStream
consume, index, mark, release, rewind, rewind, seek, size
-
Methods inherited from interface org.antlr.runtime.tree.TreeNodeStream
LT
-
-
-
-
Field Detail
-
DEFAULT_INITIAL_BUFFER_SIZE
public static final int DEFAULT_INITIAL_BUFFER_SIZE
- See Also:
- Constant Field Values
-
INITIAL_CALL_STACK_SIZE
public static final int INITIAL_CALL_STACK_SIZE
- See Also:
- Constant Field Values
-
root
protected Object root
Pull nodes from which tree?
-
tokens
protected TokenStream tokens
If this tree (root) was created from aTokenStream, track it.
-
it
protected TreeIterator it
TheTreeIteratorwe using.
-
calls
protected IntArray calls
Stack of indexes used for push/pop calls.
-
hasNilRoot
protected boolean hasNilRoot
Tree(nil A B C)trees like flatA B Cstreams
-
level
protected int level
Tracks tree depth. Level=0 means we're at root node level.
-
previousLocationElement
protected Object previousLocationElement
Tracks the last node before the start ofFastQueue.datawhich contains position information to provide information for error reporting. This is tracked in addition toLookaheadStream.prevElementwhich may or may not contain position information.
-
-
Constructor Detail
-
CommonTreeNodeStream
public CommonTreeNodeStream(Object tree)
-
CommonTreeNodeStream
public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree)
-
-
Method Detail
-
reset
public void reset()
Description copied from interface:TreeNodeStreamReset the tree node stream in such a way that it acts like a freshly constructed stream.- Specified by:
resetin interfaceTreeNodeStream- Overrides:
resetin classLookaheadStream<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:
nextElementin classLookaheadStream<Object>- See Also:
LookaheadStream.isEOF(T)
-
remove
public Object remove()
Description copied from class:LookaheadStreamGet and remove first element in queue; overrideFastQueue.remove(); it's the same, just checks for backtracking.- Overrides:
removein classLookaheadStream<Object>
-
isEOF
public boolean isEOF(Object o)
- Specified by:
isEOFin classLookaheadStream<Object>
-
setUniqueNavigationNodes
public void setUniqueNavigationNodes(boolean uniqueNavigationNodes)
Description copied from interface:TreeNodeStreamAs we flatten the tree, we useToken.UP,Token.DOWNnodes 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 befalse.- Specified by:
setUniqueNavigationNodesin interfaceTreeNodeStream
-
getTreeSource
public Object getTreeSource()
Description copied from interface:TreeNodeStreamWhere is this stream pulling nodes from? This is not the name, but the object that provides node objects.- Specified by:
getTreeSourcein interfaceTreeNodeStream
-
getSourceName
public String getSourceName()
Description copied from interface:IntStreamWhere 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:
getSourceNamein interfaceIntStream
-
getTokenStream
public TokenStream getTokenStream()
Description copied from interface:TreeNodeStreamIf the tree associated with this stream was created from aTokenStream, you can specify it here. Used to do rule$textattribute in tree parser. Optional unless you use tree parser rule$textattribute oroutput=templateandrewrite=trueoptions.- Specified by:
getTokenStreamin interfaceTreeNodeStream
-
setTokenStream
public void setTokenStream(TokenStream tokens)
-
getTreeAdaptor
public TreeAdaptor getTreeAdaptor()
Description copied from interface:TreeNodeStreamWhat adaptor can tell me how to interpret/navigate nodes and trees. E.g., get text of a node.- Specified by:
getTreeAdaptorin interfaceTreeNodeStream
-
setTreeAdaptor
public void setTreeAdaptor(TreeAdaptor adaptor)
-
get
public Object get(int i)
Description copied from interface:TreeNodeStreamGet a tree node at an absolute indexi; 0..n-1. If you don't want to buffer up nodes, then this method makes no sense for you.- Specified by:
getin interfaceTreeNodeStream
-
LA
public int LA(int i)
Description copied from interface:IntStreamGet 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.
-
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 lastpush(int)call. Return top of stack (return index).
-
getKnownPositionElement
public Object getKnownPositionElement(boolean allowApproximateLocation)
Returns an element containing position information. IfallowApproximateLocationisfalse, then this method will return theLT(1)element if it contains position information, and otherwise returnnull. IfallowApproximateLocationistrue, then this method will return the last known element containing position information.- Specified by:
getKnownPositionElementin interfacePositionTrackingStream<Object>- Parameters:
allowApproximateLocation- iffalse, this method returnsnullif an element containing exact information about the current position is not available- See Also:
hasPositionInformation(java.lang.Object)
-
hasPositionInformation
public boolean hasPositionInformation(Object node)
Description copied from interface:PositionTrackingStreamDetermines if the specifiedelementcontains concrete position information.- Specified by:
hasPositionInformationin interfacePositionTrackingStream<Object>- Parameters:
node- the element to check- Returns:
trueifelementcontains concrete position information, otherwisefalse
-
replaceChildren
public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t)
Description copied from interface:TreeNodeStreamReplace children ofparentfrom indexstartChildIndextostopChildIndexwitht, 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
parentisnull, don't do anything; must be at root of overall tree. Can't replace whatever points to the parent externally. Do nothing.- Specified by:
replaceChildrenin interfaceTreeNodeStream
-
toString
public String toString(Object start, Object stop)
Description copied from interface:TreeNodeStreamReturn the text of all nodes fromstarttostop, inclusive. If the stream does not buffer all the nodes then it can still walk recursively from start until stop. You can always returnnullor""too, but users should not access$ruleLabel.textin an action of course in that case.- Specified by:
toStringin interfaceTreeNodeStream
-
toTokenTypeString
public String toTokenTypeString()
For debugging; destructive: moves tree iterator to end.
-
-