Package schrodinger :: Package application :: Package desmond :: Package antlr3 :: Module tree :: Class CommonTreeNodeStream
[hide private]
[frames] | no frames]

Class CommonTreeNodeStream

       object --+        
                |        
streams.IntStream --+    
                    |    
       TreeNodeStream --+
                        |
                       CommonTreeNodeStream

@brief A buffered stream of tree nodes.

Nodes can be from a tree of ANY kind.

This node stream sucks all nodes out of the tree specified in the constructor during construction and makes pointers into the tree using an array of Object pointers. The stream necessarily includes pointers to DOWN and UP and EOF nodes.

This stream knows how to mark/release for backtracking.

This stream is most suitable for tree interpreters that need to jump around a lot or for tree parsers requiring speed (at cost of memory). There is some duplicated functionality here with UnBufferedTreeNodeStream but just in bookkeeping, not tree walking etc...

@see UnBufferedTreeNodeStream

Instance Methods [hide private]
 
__init__(self, *args)
x.__init__(...) initializes x; see help(type(x)) for signature
 
fillBuffer(self)
Walk tree with depth-first-search and fill nodes buffer.
 
_fillBuffer(self, t)
 
getNodeIndex(self, node)
What is the stream index for node? 0..n-1 Return -1 if node not found.
 
addNavigationNode(self, ttype)
As we flatten the tree, we use UP, DOWN nodes to represent the tree structure.
 
get(self, i)
Get a tree node at an absolute index i; 0..n-1.
 
LT(self, k)
Get tree node at current input pointer + i ahead where i=1 is next node.
 
getCurrentSymbol(self)
 
LB(self, k)
Look backwards k nodes
 
getTreeSource(self)
Where is this stream pulling nodes from? This is not the name, but the object that provides node objects.
 
getSourceName(self)
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.
 
getTokenStream(self)
If the tree associated with this stream was created from a TokenStream, you can specify it here.
 
setTokenStream(self, tokens)
 
getTreeAdaptor(self)
What adaptor can tell me how to interpret/navigate nodes and trees.
 
hasUniqueNavigationNodes(self)
 
setUniqueNavigationNodes(self, uniqueNavigationNodes)
As we flatten the tree, we use UP, DOWN nodes to represent the tree structure.
 
consume(self)
 
LA(self, i)
Get int at current input pointer + i ahead where i=1 is next int.
 
mark(self)
Tell the stream to start buffering if it hasn't already.
 
release(self, marker=None)
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.
 
index(self)
Return the current input symbol index 0..n where n indicates the last symbol has been read.
 
rewind(self, marker=None)
Reset the stream so that next call to index would return marker.
 
seek(self, index)
Set the input cursor to the position indicated by index.
 
push(self, index)
Make stream jump to a new location, saving old location.
 
pop(self)
Seek back to previous index saved during last push() call.
 
reset(self)
 
size(self)
Only makes sense for streams that buffer everything up probably, but might be useful to display the entire stream or for testing.
 
replaceChildren(self, parent, startChildIndex, stopChildIndex, t)
Replace from start to stop child index of parent with t, which might be a list.
 
__str__(self)
Used for testing, just return the token type stream
 
toString(self, start, stop)
Return the text of all nodes from start to stop, inclusive.
 
__iter__(self)

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args)
(Constructor)

 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

fillBuffer(self)

 

Walk tree with depth-first-search and fill nodes buffer. Don't do DOWN, UP nodes if its a list (t is isNil).

addNavigationNode(self, ttype)

 

As we flatten the tree, we use UP, DOWN nodes to represent the tree structure. When debugging we need unique nodes so instantiate new ones when uniqueNavigationNodes is true.

get(self, i)

 

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.

Overrides: TreeNodeStream.get
(inherited documentation)

LT(self, k)

 

Get tree node at current input pointer + i ahead where i=1 is next node. i<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 i>=n, return null. Return null for LT(0) and any index that results in an absolute address that is negative.

This is analogus to the LT() method of the TokenStream, but this returns a tree node instead of a token. Makes code gen identical for both parser and tree grammars. :)

Overrides: TreeNodeStream.LT
(inherited documentation)

getTreeSource(self)

 

Where is this stream pulling nodes from? This is not the name, but the object that provides node objects.

Overrides: TreeNodeStream.getTreeSource
(inherited documentation)

getSourceName(self)

 

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.

Overrides: streams.IntStream.getSourceName
(inherited documentation)

getTokenStream(self)

 

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.

Overrides: TreeNodeStream.getTokenStream
(inherited documentation)

getTreeAdaptor(self)

 

What adaptor can tell me how to interpret/navigate nodes and trees. E.g., get text of a node.

Overrides: TreeNodeStream.getTreeAdaptor
(inherited documentation)

setUniqueNavigationNodes(self, uniqueNavigationNodes)

 

As we flatten the tree, we use UP, 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;

Overrides: TreeNodeStream.setUniqueNavigationNodes
(inherited documentation)

consume(self)

 
Overrides: streams.IntStream.consume

LA(self, i)

 

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.

Overrides: streams.IntStream.LA
(inherited documentation)

mark(self)

 

Tell the stream to start buffering if it hasn't already. Return current input position, index(), or some other marker so that when passed to rewind() you get back to the same spot. rewind(mark()) should not affect the input cursor. The Lexer track line/col info as well as input index so its markers are not pure input indexes. Same for tree node streams.

Overrides: streams.IntStream.mark
(inherited documentation)

release(self, marker=None)

 

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. This will have the same behavior as rewind() except it releases resources without the backward seek. This must throw away resources for all markers back to the marker argument. So if you're nested 5 levels of mark(), and then release(2) you have to release resources for depths 2..5.

Overrides: streams.IntStream.release
(inherited documentation)

index(self)

 

Return the current input symbol index 0..n where n indicates the last symbol has been read. The index is the symbol about to be read not the most recently read symbol.

Overrides: streams.IntStream.index
(inherited documentation)

rewind(self, marker=None)

 

Reset the stream so that next call to index would return marker. The marker will usually be index() but it doesn't have to be. It's just a marker to indicate what state the stream was in. This is essentially calling release() and seek(). If there are markers created after this marker argument, this routine must unroll them like a stack. Assume the state the stream was in when this marker was created.

If marker is None: Rewind to the input position of the last marker. Used currently only after a cyclic DFA and just before starting a sem/syn predicate to get the input position back to the start of the decision. Do not "pop" the marker off the state. mark(i) and rewind(i) should balance still. It is like invoking rewind(last marker) but it should not "pop" the marker off. It's like seek(last marker's input position).

Overrides: streams.IntStream.rewind
(inherited documentation)

seek(self, index)

 

Set the input cursor to the position indicated by index. This is normally used to seek ahead in the input stream. No buffering is required to do this unless you know your stream will use seek to move backwards such as when backtracking.

This is different from rewind in its multi-directional requirement and in that its argument is strictly an input cursor (index).

For char streams, seeking forward must update the stream state such as line number. For seeking backwards, you will be presumably backtracking using the mark/rewind mechanism that restores state and so this method does not need to update state when seeking backwards.

Currently, this method is only used for efficient backtracking using memoization, but in the future it may be used for incremental parsing.

The index is 0..n-1. A seek to position i means that LA(1) will return the ith symbol. So, seeking to 0 means LA(1) will return the first element in the stream.

Overrides: streams.IntStream.seek
(inherited documentation)

push(self, index)

 

Make stream jump to a new location, saving old location. Switch back with pop().

pop(self)

 

Seek back to previous index saved during last push() call. Return top of stack (return index).

size(self)

 

Only makes sense for streams that buffer everything up probably, but might be useful to display the entire stream or for testing. This value includes a single EOF.

Overrides: streams.IntStream.size
(inherited documentation)

replaceChildren(self, parent, startChildIndex, stopChildIndex, t)

 

Replace from start to stop child index of parent 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.

Overrides: TreeNodeStream.replaceChildren
(inherited documentation)

__str__(self)
(Informal representation operator)

 

Used for testing, just return the token type stream

Overrides: object.__str__

toString(self, start, stop)

 

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.

Overrides: TreeNodeStream.toString
(inherited documentation)