public class BufferedTreeNodeStream extends Object implements TreeNodeStream
CommonTreeNodeStream
Modifier and Type | Class and Description |
---|---|
protected class |
BufferedTreeNodeStream.StreamIterator |
Modifier and Type | Field and Description |
---|---|
protected IntArray |
calls
Stack of indexes used for push/pop calls
|
static int |
DEFAULT_INITIAL_BUFFER_SIZE |
protected Object |
down |
protected Object |
eof |
static int |
INITIAL_CALL_STACK_SIZE |
protected int |
lastMarker
Track the last mark() call result value for use in rewind().
|
protected List<Object> |
nodes
The complete mapping from stream index to tree node.
|
protected int |
p
The index into the nodes list of the current node (next node
to consume).
|
protected Object |
root
Pull nodes from which tree?
|
protected TokenStream |
tokens
IF this tree (root) was created from a token stream, track it.
|
protected boolean |
uniqueNavigationNodes
Reuse same DOWN, UP navigation nodes unless this is true
|
protected Object |
up |
Constructor and Description |
---|
BufferedTreeNodeStream(Object tree) |
BufferedTreeNodeStream(TreeAdaptor adaptor,
Object tree) |
BufferedTreeNodeStream(TreeAdaptor adaptor,
Object tree,
int initialBufferSize) |
Modifier and Type | Method and Description |
---|---|
protected void |
addNavigationNode(int ttype)
As we flatten the tree, we use UP, DOWN nodes to represent
the tree structure.
|
void |
consume() |
protected void |
fillBuffer()
Walk tree with depth-first-search and fill nodes buffer.
|
void |
fillBuffer(Object t) |
Object |
get(int i)
Get a tree node at an absolute index
i ; 0..n-1. |
Object |
getCurrentSymbol() |
protected int |
getNodeIndex(Object node)
What is the stream index for node? 0..n-1
Return -1 if node not found.
|
String |
getSourceName()
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.
|
TokenStream |
getTokenStream()
If the tree associated with this stream was created from a
TokenStream , you can specify it here. |
TreeAdaptor |
getTreeAdaptor()
What adaptor can tell me how to interpret/navigate nodes and
trees.
|
Object |
getTreeSource()
Where is this stream pulling nodes from? This is not the name, but
the object that provides node objects.
|
boolean |
hasUniqueNavigationNodes() |
int |
index()
Return the current input symbol index 0..n where n indicates the
last symbol has been read.
|
Iterator<Object> |
iterator() |
int |
LA(int i)
Get int at current input pointer + i ahead where i=1 is next int.
|
protected Object |
LB(int k)
Look backwards k nodes
|
Object |
LT(int k)
Get tree node at current input pointer +
k ahead where
k==1 is next node. |
int |
mark()
Tell the stream to start buffering if it hasn't already.
|
int |
pop()
Seek back to previous index saved during last push() call.
|
void |
push(int index)
Make stream jump to a new location, saving old location.
|
void |
release(int marker)
You may want to commit to a backtrack but don't want to force the
stream to keep bookkeeping objects around for a marker that is
no longer necessary.
|
void |
replaceChildren(Object parent,
int startChildIndex,
int stopChildIndex,
Object t)
Replace children of
parent from index startChildIndex to
stopChildIndex with t , which might be a list. |
void |
reset()
Reset the tree node stream in such a way that it acts like
a freshly constructed stream.
|
void |
rewind()
Rewind to the input position of the last marker.
|
void |
rewind(int marker)
Reset the stream so that next call to index would return marker.
|
void |
seek(int index)
Set the input cursor to the position indicated by index.
|
void |
setTokenStream(TokenStream tokens) |
void |
setTreeAdaptor(TreeAdaptor adaptor) |
void |
setUniqueNavigationNodes(boolean uniqueNavigationNodes)
As we flatten the tree, we use
Token.UP , Token.DOWN nodes
to represent the tree structure. |
int |
size()
Only makes sense for streams that buffer everything up probably, but
might be useful to display the entire stream or for testing.
|
String |
toString(Object start,
Object stop)
Return the text of all nodes from
start to stop ,
inclusive. |
String |
toTokenString(int start,
int stop)
Debugging
|
String |
toTokenTypeString()
Used for testing, just return the token type stream
|
public static final int DEFAULT_INITIAL_BUFFER_SIZE
public static final int INITIAL_CALL_STACK_SIZE
protected Object down
protected Object up
protected Object eof
protected List<Object> nodes
protected Object root
protected TokenStream tokens
protected boolean uniqueNavigationNodes
protected int p
protected int lastMarker
protected IntArray calls
public BufferedTreeNodeStream(Object tree)
public BufferedTreeNodeStream(TreeAdaptor adaptor, Object tree)
public BufferedTreeNodeStream(TreeAdaptor adaptor, Object tree, int initialBufferSize)
protected void fillBuffer()
public void fillBuffer(Object t)
protected int getNodeIndex(Object node)
protected void addNavigationNode(int ttype)
public Object get(int i)
TreeNodeStream
i
; 0..n-1.
If you don't want to buffer up nodes, then this method makes no
sense for you.get
in interface TreeNodeStream
public Object LT(int k)
TreeNodeStream
k
ahead where
k==1
is next node. k<0
indicates nodes in the past. So
LT(-1)
is previous node, but implementations are not required to
provide results for k < -1
. LT(0)
is undefined. For
k<=n
, return null
. Return null
for LT(0)
and any index that results in an absolute address that is negative.
This is analogous to TokenStream.LT(int)
, but this returns a tree node
instead of a Token
. Makes code generation identical for both
parser and tree grammars.
LT
in interface TreeNodeStream
public Object getCurrentSymbol()
protected Object LB(int k)
public Object getTreeSource()
TreeNodeStream
getTreeSource
in interface TreeNodeStream
public String getSourceName()
IntStream
getSourceName
in interface IntStream
public TokenStream getTokenStream()
TreeNodeStream
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.getTokenStream
in interface TreeNodeStream
public void setTokenStream(TokenStream tokens)
public TreeAdaptor getTreeAdaptor()
TreeNodeStream
getTreeAdaptor
in interface TreeNodeStream
public void setTreeAdaptor(TreeAdaptor adaptor)
public boolean hasUniqueNavigationNodes()
public void setUniqueNavigationNodes(boolean uniqueNavigationNodes)
TreeNodeStream
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
.setUniqueNavigationNodes
in interface TreeNodeStream
public int LA(int i)
IntStream
public int mark()
IntStream
public void release(int marker)
IntStream
public int index()
IntStream
public void rewind(int marker)
IntStream
public void rewind()
IntStream
public void seek(int index)
IntStream
public void push(int index)
public int pop()
public void reset()
TreeNodeStream
reset
in interface TreeNodeStream
public int size()
IntStream
public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t)
TreeNodeStream
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.
replaceChildren
in interface TreeNodeStream
public String toTokenTypeString()
public String toTokenString(int start, int stop)
public String toString(Object start, Object stop)
TreeNodeStream
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.toString
in interface TreeNodeStream
Copyright © 1992–2015 ANTLR. All rights reserved.