schrodinger.application.desmond.antlr3.tree module

@package antlr3.tree @brief ANTLR3 runtime package, tree module

This module contains all support classes for AST construction and tree parsers.

class schrodinger.application.desmond.antlr3.tree.BaseTree(node=None)

Bases: schrodinger.application.desmond.antlr3.tree.Tree

@brief A generic tree implementation with no payload.

You must subclass to actually have any user data. ANTLR v3 uses a list of children approach instead of the child-sibling approach in v2. A flat tree (a list) is an empty node whose children represent the list. An empty, but non-null node is called “nil”.

addChild(childTree)

Add t as child of this node.

Warning: if t has no children, but child does and child isNil then this routine moves children to t via t.children = child.children; i.e., without copying the array.

addChildren(children)

Add all elements of kids list as children of this node

deleteChild(i)
freshenParentAndChildIndexes(offset=0)
getAncestor(ttype)

Walk upwards and get first ancestor with this token type.

getAncestors()

Return a list of all ancestors of this node.

The first node of list is the root and the last is the parent of this node.

getCharPositionInLine()
getChild(i)
getChildCount()
getChildIndex()

BaseTree doesn’t track child indexes.

getChildren()

@brief Get the children internal List

Note that if you directly mess with the list, do so at your own risk.

getFirstChildWithType(treeType)
getLine()
getParent()

BaseTree doesn’t track parent pointers.

hasAncestor(ttype)

Walk upwards looking for ancestor with this token type.

isNil()
replaceChildren(startChildIndex, stopChildIndex, newTree)

Delete children from start to stop and replace with t even if t is a list (nil-root tree). num of children can increase or decrease. For huge child lists, inserting children can force walking rest of children to set their childindex; could be slow.

sanityCheckParentAndChildIndexes(parent=None, i=-1)
setChild(i, t)
setChildIndex(index)

BaseTree doesn’t track child indexes.

setParent(t)

BaseTree doesn’t track parent pointers.

toString()

Override to say how a node (not a tree) should look as text

toStringTree()

Print out a whole tree not just a node

class schrodinger.application.desmond.antlr3.tree.BaseTreeAdaptor

Bases: schrodinger.application.desmond.antlr3.tree.TreeAdaptor

@brief A TreeAdaptor that works with any Tree implementation.

addChild(tree, child)

Add a child to the tree t. If child is a flat tree (a list), make all in list children of t. Warning: if t has no children, but child does and child isNil then you can decide it is ok to move children to t via t.children = child.children; i.e., without copying the array. Just make sure that this is consistent with have the user will build ASTs.

becomeRoot(newRoot, oldRoot)

If oldRoot is a nil root, just copy or move the children to newRoot. If not a nil root, make oldRoot a child of newRoot.

old=^(nil a b c), new=r yields ^(r a b c) old=^(a b c), new=r yields ^(r ^(a b c))

If newRoot is a nil-rooted single child tree, use the single child as the new root node.

old=^(nil a b c), new=^(nil r) yields ^(r a b c) old=^(a b c), new=^(nil r) yields ^(r ^(a b c))

If oldRoot was null, it’s ok, just return newRoot (even if isNil).

old=null, new=r yields r old=null, new=^(nil r) yields ^(nil r)

Return newRoot. Throw an exception if newRoot is not a simple node or nil root with a single child node–it must be a root node. If newRoot is ^(nil x) return x as newRoot.

Be advised that it’s ok for newRoot to point at oldRoot’s children; i.e., you don’t have to copy the list. We are constructing these nodes so we should have this control for efficiency.

createFromToken(tokenType, fromToken, text=None)
createFromType(tokenType, text)
createToken(fromToken=None, tokenType=None, text=None)

Tell me how to create a token for use with imaginary token nodes. For example, there is probably no input symbol associated with imaginary token DECL, but you need to create it as a payload or whatever for the DECL node as in ^(DECL type ID).

If you care what the token payload objects’ type is, you should override this method and any other createToken variant.

deleteChild(t, i)
dupTree(t, parent=None)

This is generic in the sense that it will work with any kind of tree (not just Tree interface). It invokes the adaptor routines not the tree node routines to do the construction.

errorNode(input, start, stop, exc)

create tree node that holds the start and stop tokens associated with an error.

If you specify your own kind of tree nodes, you will likely have to override this method. CommonTree returns Token.INVALID_TOKEN_TYPE if no token payload but you might have to set token type for diff node type.

You don’t have to subclass CommonErrorNode; you will likely need to subclass your own tree node class to avoid class cast exception.

getChild(t, i)
getChildCount(t)
getText(t)
getType(t)
getUniqueID(node)
isNil(tree)
nil()
rulePostProcessing(root)

Transform ^(nil x) to x and nil to null

setChild(t, i, child)
setText(t, text)
setType(t, type)
class schrodinger.application.desmond.antlr3.tree.CommonErrorNode(input, start, stop, exc)

Bases: schrodinger.application.desmond.antlr3.tree.CommonTree

A node representing erroneous token range in token stream

getText()
getType()
isNil()
toString()
class schrodinger.application.desmond.antlr3.tree.CommonTree(payload)

Bases: schrodinger.application.desmond.antlr3.tree.BaseTree

@brief A tree node that is wrapper for a Token object.

After 3.0 release while building tree rewrite stuff, it became clear that computing parent and child index is very difficult and cumbersome. Better to spend the space in every tree node. If you don’t want these extra fields, it’s easy to cut them out in your own BaseTree subclass.

charPositionInLine
dupNode()
getCharPositionInLine()
getChildIndex()
getLine()
getParent()
getText()
getToken()
getTokenStartIndex()
getTokenStopIndex()
getType()
isNil()
line
setChildIndex(idx)
setParent(t)
setTokenStartIndex(index)
setTokenStopIndex(index)
setUnknownTokenBoundaries()

For every node in this subtree, make sure it’s start/stop token’s are set. Walk depth first, visit bottom up. Only updates nodes with at least one token index < 0.

text
toString()
toStringTree()
tokenStartIndex
tokenStopIndex
type
class schrodinger.application.desmond.antlr3.tree.CommonTreeAdaptor

Bases: schrodinger.application.desmond.antlr3.tree.BaseTreeAdaptor

@brief A TreeAdaptor that works with any Tree implementation.

It provides really just factory methods; all the work is done by BaseTreeAdaptor. If you would like to have different tokens created than ClassicToken objects, you need to override this and then set the parser tree adaptor to use your subclass.

To get your parser to build nodes of a different type, override create(Token), errorNode(), and to be safe, YourTreeClass.dupNode(). dupNode is called to duplicate nodes during rewrite operations.

createToken(fromToken=None, tokenType=None, text=None)

Tell me how to create a token for use with imaginary token nodes. For example, there is probably no input symbol associated with imaginary token DECL, but you need to create it as a payload or whatever for the DECL node as in ^(DECL type ID).

If you care what the token payload objects’ type is, you should override this method and any other createToken variant.

createWithPayload(payload)
dupNode(treeNode)

Duplicate a node. This is part of the factory; override if you want another kind of node to be built.

I could use reflection to prevent having to override this but reflection is slow.

getChild(t, i)
getChildCount(t)
getChildIndex(t)
getParent(t)
getText(t)
getToken(t)

What is the Token associated with this node? If you are not using CommonTree, then you must override this in your own adaptor.

getTokenStartIndex(t)
getTokenStopIndex(t)
getType(t)
replaceChildren(parent, startChildIndex, stopChildIndex, t)
setChildIndex(t, index)
setParent(t, parent)
setTokenBoundaries(t, startToken, stopToken)

Track start/stop token for subtree root created for a rule. Only works with Tree nodes. For rules that match nothing, seems like this will yield start=i and stop=i-1 in a nil node. Might be useful info so I’ll not force to be i..i.

class schrodinger.application.desmond.antlr3.tree.CommonTreeNodeStream(*args)

Bases: schrodinger.application.desmond.antlr3.tree.TreeNodeStream

@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

LA(i)
LB(k)

Look backwards k nodes

LT(k)
addNavigationNode(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.

consume()
fillBuffer()

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

get(i)
getCurrentSymbol()
getNodeIndex(node)

What is the stream index for node? 0..n-1 Return -1 if node not found.

getSourceName()
getTokenStream()
getTreeAdaptor()
getTreeSource()
hasUniqueNavigationNodes()
index()
mark()
pop()

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

push(index)

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

release(marker=None)
replaceChildren(parent, startChildIndex, stopChildIndex, t)
reset()
rewind(marker=None)
seek(index)
setTokenStream(tokens)
setUniqueNavigationNodes(uniqueNavigationNodes)
size()
toString(start, stop)
exception schrodinger.application.desmond.antlr3.tree.RewriteCardinalityException(elementDescription)

Bases: exceptions.RuntimeError

@brief Base class for all exceptions thrown during AST rewrite construction.

This signifies a case where the cardinality of two or more elements in a subrule are different: (ID INT)+ where |ID|!=|INT|

getMessage()
exception schrodinger.application.desmond.antlr3.tree.RewriteEarlyExitException(elementDescription=None)

Bases: schrodinger.application.desmond.antlr3.tree.RewriteCardinalityException

@brief No elements within a (…)+ in a rewrite rule

exception schrodinger.application.desmond.antlr3.tree.RewriteEmptyStreamException(elementDescription)

Bases: schrodinger.application.desmond.antlr3.tree.RewriteCardinalityException

@brief Ref to ID or expr but no tokens in ID stream or subtrees in expr stream

class schrodinger.application.desmond.antlr3.tree.RewriteRuleElementStream(adaptor, elementDescription, elements=None)

Bases: object

@brief Internal helper class.

A generic list of elements tracked in an alternative to be used in a -> rewrite rule. We need to subclass to fill in the next() method, which returns either an AST node wrapped around a token payload or an existing subtree.

Once you start next()ing, do not try to add more elements. It will break the cursor tracking I believe.

:see org.antlr.runtime.tree.RewriteRuleSubtreeStream :see org.antlr.runtime.tree.RewriteRuleTokenStream

TODO: add mechanism to detect/puke on modification after reading from stream

add(el)
dup(el)

When constructing trees, sometimes we need to dup a token or AST subtree. Dup’ing a token means just creating another AST node around it. For trees, you must call the adaptor.dupTree() unless the element is for a tree root; then it must be a node dup.

getDescription()

Deprecated. Directly access elementDescription attribute

hasNext()
nextTree()

Return the next element in the stream. If out of elements, throw an exception unless size()==1. If size is 1, then return elements[0].

Return a duplicate node/subtree if stream is out of elements and size==1. If we’ve already used the element, dup (dirty bit set).

reset()

Reset the condition of this stream so that it appears we have not consumed any of its elements. Elements themselves are untouched. Once we reset the stream, any future use will need duplicates. Set the dirty bit.

size()
toTree(el)

Ensure stream emits trees; tokens must be converted to AST nodes. AST nodes can be passed through unmolested.

class schrodinger.application.desmond.antlr3.tree.RewriteRuleNodeStream(adaptor, elementDescription, elements=None)

Bases: schrodinger.application.desmond.antlr3.tree.RewriteRuleElementStream

Queues up nodes matched on left side of -> in a tree parser. This is the analog of RewriteRuleTokenStream for normal parsers.

dup(el)
nextNode()
toTree(el)
class schrodinger.application.desmond.antlr3.tree.RewriteRuleSubtreeStream(adaptor, elementDescription, elements=None)

Bases: schrodinger.application.desmond.antlr3.tree.RewriteRuleElementStream

@brief Internal helper class.

dup(el)
nextNode()

Treat next element as a single node even if it’s a subtree. This is used instead of next() when the result has to be a tree root node. Also prevents us from duplicating recently-added children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration must dup the type node, but ID has been added.

Referencing a rule result twice is ok; dup entire tree as we can’t be adding trees as root; e.g., expr expr.

Hideous code duplication here with super.next(). Can’t think of a proper way to refactor. This needs to always call dup node and super.next() doesn’t know which to call: dup node or dup tree.

class schrodinger.application.desmond.antlr3.tree.RewriteRuleTokenStream(adaptor, elementDescription, elements=None)

Bases: schrodinger.application.desmond.antlr3.tree.RewriteRuleElementStream

@brief Internal helper class.

dup(el)
nextNode()
nextToken()
toTree(el)
class schrodinger.application.desmond.antlr3.tree.Tree

Bases: object

@brief Abstract baseclass for tree nodes.

What does a tree look like? ANTLR has a number of support classes such as CommonTreeNodeStream that work on these kinds of trees. You don’t have to make your trees implement this interface, but if you do, you’ll be able to use more support code.

NOTE: When constructing trees, ANTLR can build any kind of tree; it can even use Token objects as trees if you add a child list to your tokens.

This is a tree node without any payload; just navigation and factory stuff.

addChild(t)

Add t as a child to this node. If t is null, do nothing. If t is nil, add all children of t to this’ children.

deleteChild(i)
dupNode()
freshenParentAndChildIndexes()

Set the parent and child index values for all children

getAncestor(ttype)

Walk upwards and get first ancestor with this token type.

getAncestors()

Return a list of all ancestors of this node.

The first node of list is the root and the last is the parent of this node.

getCharPositionInLine()
getChild(i)
getChildCount()
getChildIndex()

This node is what child index? 0..n-1

getLine()

In case we don’t have a token payload, what is the line for errors?

getParent()

Tree tracks parent and child index now > 3.0

getText()
getTokenStartIndex()
What is the smallest token index (indexing from 0) for this node
and its children?
getTokenStopIndex()

What is the largest token index (indexing from 0) for this node and its children?

getType()

Return a token type; needed for tree parsing.

hasAncestor(ttype)

Walk upwards looking for ancestor with this token type.

isNil()

Indicates the node is a nil node but may still have children, meaning the tree is a flat list.

replaceChildren(startChildIndex, stopChildIndex, t)

Delete children from start to stop and replace with t even if t is a list (nil-root tree). num of children can increase or decrease. For huge child lists, inserting children can force walking rest of children to set their childindex; could be slow.

setChild(i, t)

Set ith child (0..n-1) to t; t must be non-null and non-nil node

setChildIndex(index)

This node is what child index? 0..n-1

setParent(t)

Tree tracks parent and child index now > 3.0

setTokenStartIndex(index)
setTokenStopIndex(index)
toString()
toStringTree()
class schrodinger.application.desmond.antlr3.tree.TreeAdaptor

Bases: object

@brief Abstract baseclass for tree adaptors.

How to create and navigate trees. Rather than have a separate factory and adaptor, I’ve merged them. Makes sense to encapsulate.

This takes the place of the tree construction code generated in the generated code in 2.x and the ASTFactory.

I do not need to know the type of a tree at all so they are all generic Objects. This may increase the amount of typecasting needed. :(

addChild(t, child)

Add a child to the tree t. If child is a flat tree (a list), make all in list children of t. Warning: if t has no children, but child does and child isNil then you can decide it is ok to move children to t via t.children = child.children; i.e., without copying the array. Just make sure that this is consistent with have the user will build ASTs. Do nothing if t or child is null.

becomeRoot(newRoot, oldRoot)

If oldRoot is a nil root, just copy or move the children to newRoot. If not a nil root, make oldRoot a child of newRoot.

old=^(nil a b c), new=r yields ^(r a b c) old=^(a b c), new=r yields ^(r ^(a b c))

If newRoot is a nil-rooted single child tree, use the single child as the new root node.

old=^(nil a b c), new=^(nil r) yields ^(r a b c) old=^(a b c), new=^(nil r) yields ^(r ^(a b c))

If oldRoot was null, it’s ok, just return newRoot (even if isNil).

old=null, new=r yields r old=null, new=^(nil r) yields ^(nil r)

Return newRoot. Throw an exception if newRoot is not a simple node or nil root with a single child node–it must be a root node. If newRoot is ^(nil x) return x as newRoot.

Be advised that it’s ok for newRoot to point at oldRoot’s children; i.e., you don’t have to copy the list. We are constructing these nodes so we should have this control for efficiency.

create(*args)

Deprecated, use createWithPayload, createFromToken or createFromType.

This method only exists to mimic the Java interface of TreeAdaptor.

createFromToken(tokenType, fromToken, text=None)

Create a new node derived from a token, with a new token type and (optionally) new text.

This is invoked from an imaginary node ref on right side of a rewrite rule as IMAG[$tokenLabel] or IMAG[$tokenLabel “IMAG”].

This should invoke createToken(Token).

createFromType(tokenType, text)

Create a new node derived from a token, with a new token type.

This is invoked from an imaginary node ref on right side of a rewrite rule as IMAG[“IMAG”].

This should invoke createToken(int,String).

createWithPayload(payload)

Create a tree node from Token object; for CommonTree type trees, then the token just becomes the payload. This is the most common create call.

Override if you want another kind of node to be built.

deleteChild(t, i)

Remove ith child and shift children down from right.

dupNode(treeNode)

Duplicate a single tree node.

Override if you want another kind of node to be built.

dupTree(tree)

Duplicate tree recursively, using dupNode() for each node

errorNode(input, start, stop, exc)

Return a tree node representing an error. This node records the tokens consumed during error recovery. The start token indicates the input symbol at which the error was detected. The stop token indicates the last symbol consumed during recovery.

You must specify the input stream so that the erroneous text can be packaged up in the error node. The exception could be useful to some applications; default implementation stores ptr to it in the CommonErrorNode.

This only makes sense during token parsing, not tree parsing. Tree parsing should happen only when parsing and tree construction succeed.

getChild(t, i)

Get a child 0..n-1 node

getChildCount(t)

How many children? If 0, then this is a leaf node

getChildIndex(t)

What index is this node in the child list? Range: 0..n-1 If your node type doesn’t handle this, it’s ok but the tree rewrites in tree parsers need this functionality.

getParent(t)

Who is the parent node of this node; if null, implies node is root. If your node type doesn’t handle this, it’s ok but the tree rewrites in tree parsers need this functionality.

getText(t)
getToken(t)

Return the token object from which this node was created.

Currently used only for printing an error message. The error display routine in BaseRecognizer needs to display where the input the error occurred. If your tree of limitation does not store information that can lead you to the token, you can create a token filled with the appropriate information and pass that back. See BaseRecognizer.getErrorMessage().

getTokenStartIndex(t)

Get the token start index for this subtree; return -1 if no such index

getTokenStopIndex(t)

Get the token stop index for this subtree; return -1 if no such index

getType(t)

For tree parsing, I need to know the token type of a node

getUniqueID(node)

For identifying trees.

How to identify nodes so we can say “add node to a prior node”? Even becomeRoot is an issue. Use System.identityHashCode(node) usually.

isNil(tree)

Is tree considered a nil node used to make lists of child nodes?

nil()

Return a nil node (an empty but non-null node) that can hold a list of element as the children. If you want a flat tree (a list) use “t=adaptor.nil(); t.addChild(x); t.addChild(y);”

replaceChildren(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.

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.

rulePostProcessing(root)

Given the root of the subtree created for this rule, post process it to do any simplifications or whatever you want. A required behavior is to convert ^(nil singleSubtree) to singleSubtree as the setting of start/stop indexes relies on a single non-nil root for non-flat trees.

Flat trees such as for lists like “idlist : ID+ ;” are left alone unless there is only one ID. For a list, the start/stop indexes are set in the nil node.

This method is executed after all rule tree construction and right before setTokenBoundaries().

setChild(t, i, child)

Set ith child (0..n-1) to t; t must be non-null and non-nil node

setChildIndex(t, index)

What index is this node in the child list? Range: 0..n-1 If your node type doesn’t handle this, it’s ok but the tree rewrites in tree parsers need this functionality.

setParent(t, parent)

Who is the parent node of this node; if null, implies node is root. If your node type doesn’t handle this, it’s ok but the tree rewrites in tree parsers need this functionality.

setText(t, text)

Node constructors can set the text of a node

setTokenBoundaries(t, startToken, stopToken)

Where are the bounds in the input token stream for this node and all children? Each rule that creates AST nodes will call this method right before returning. Flat trees (i.e., lists) will still usually have a nil root node just to hold the children list. That node would contain the start/stop indexes then.

setType(t, type)

Node constructors can set the type of a node

class schrodinger.application.desmond.antlr3.tree.TreeNodeStream

Bases: schrodinger.application.desmond.antlr3.streams.IntStream

@brief A stream of tree nodes

It accessing nodes from a tree of some kind.

LT(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. :)

get(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.

getTokenStream()

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.

getTreeAdaptor()

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

getTreeSource()

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

replaceChildren(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.

setUniqueNavigationNodes(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;

toString(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.

class schrodinger.application.desmond.antlr3.tree.TreeParser(input, state=None)

Bases: schrodinger.application.desmond.antlr3.recognizers.BaseRecognizer

@brief Baseclass for generated tree parsers.

A parser for a stream of tree nodes. “tree grammars” result in a subclass of this. All the error reporting and recovery is shared with Parser via the BaseRecognizer superclass.

dotdot = '.*[^.]\\.\\.[^.].*'
dotdotPattern = <_sre.SRE_Pattern object>
doubleEtc = '.*\\.\\.\\.\\s+\\.\\.\\..*'
doubleEtcPattern = <_sre.SRE_Pattern object>
getCurrentInputSymbol(input)
getErrorHeader(e)

Prefix error message with the grammar name because message is always intended for the programmer because the parser built the input tree not the user.

getErrorMessage(e, tokenNames)

Tree parsers parse nodes they usually have a token object as payload. Set the exception token and do the default behavior.

getMissingSymbol(input, e, expectedTokenType, follow)
getSourceName()
getTreeNodeStream()
inContext(context, adaptor=None, tokenName=None, t=None)

Check if current node in input has a context.

Context means sequence of nodes towards root of tree. For example, you might say context is “MULT” which means my parent must be MULT. “CLASS VARDEF” says current node must be child of a VARDEF and whose parent is a CLASS node. You can use “…” to mean zero-or-more nodes. “METHOD … VARDEF” means my parent is VARDEF and somewhere above that is a METHOD node. The first node in the context is not necessarily the root. The context matcher stops matching and returns true when it runs out of context. There is no way to force the first node to be the root.

matchAny(ignore)

Match ‘.’ in tree parser has special meaning. Skip node or entire tree if node has children. If children, scan until corresponding UP node.

mismatch(input, ttype, follow)

We have DOWN/UP nodes in the stream that have no line info; override. plus we want to alter the exception type. Don’t try to recover from tree parser errors inline…

reset()
setTreeNodeStream(input)

Set the input stream

traceIn(ruleName, ruleIndex)
traceOut(ruleName, ruleIndex)
class schrodinger.application.desmond.antlr3.tree.TreeRuleReturnScope

Bases: schrodinger.application.desmond.antlr3.recognizers.RuleReturnScope

This is identical to the ParserRuleReturnScope except that the start property is a tree nodes not Token object when you are parsing trees. To be generic the tree node types have to be Object.

getStart()
getTree()
class schrodinger.application.desmond.antlr3.tree.TreeVisitor(adaptor=None)

Bases: object

Do a depth first walk of a tree, applying pre() and post() actions we go.

visit(t, pre_action=None, post_action=None)

Visit every node in tree t and trigger an action for each node before/after having visited all of its children. Bottom up walk. Execute both actions even if t has no children. Ignore return results from transforming children since they will have altered the child list of this node (their parent). Return result of applying post action to this node.

The Python version differs from the Java version by taking two callables ‘pre_action’ and ‘post_action’ instead of a class instance that wraps those methods. Those callables must accept a TreeNode as their single argument and return the (potentially transformed or replaced) TreeNode.