Antlr.Runtime.Tree.TreeWizard Class Reference

Build and navigate trees with this object. Must know about the names of tokens so you have to pass in a map or array of token names (from which this class can build the map). I.e., Token DECL means nothing unless the class can translate it to a token type. More...

Collaboration diagram for Antlr.Runtime.Tree.TreeWizard:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 TreeWizard (ITreeAdaptor adaptor)
 TreeWizard (ITreeAdaptor adaptor, IDictionary tokenNameToTypeMap)
 TreeWizard (ITreeAdaptor adaptor, string[] tokenNames)
 TreeWizard (string[] tokenNames)
IDictionary ComputeTokenTypes (string[] tokenNames)
 Compute a Map<String, Integer> that is an inverted index of tokenNames (which maps int token types to names).
int GetTokenType (string tokenName)
 Using the map of token names to token types, return the type.
IDictionary Index (object t)
 Walk the entire tree and make a node name to nodes mapping.
IList Find (object t, int ttype)
 Return a List of tree nodes with token type ttype.
IList Find (object t, string pattern)
 Return a List of subtrees matching pattern.
object FindFirst (object t, int ttype)
object FindFirst (object t, string pattern)
void Visit (object t, int ttype, ContextVisitor visitor)
 Visit every ttype node in t, invoking the visitor.
void Visit (object t, string pattern, ContextVisitor visitor)
 For all subtrees that match the pattern, execute the visit action.
bool Parse (object t, string pattern, IDictionary labels)
 Given a pattern like (ASSIGN lhs:ID rhs:.) with optional labels on the various nodes and '.' (dot) as the node/subtree wildcard, return true if the pattern matches and fill the labels Map with the labels pointing at the appropriate nodes. Return false if the pattern is malformed or the tree does not match.
bool Parse (object t, string pattern)
object Create (string pattern)
 Create a tree or node from the indicated tree pattern that closely follows ANTLR tree grammar tree element syntax:.
new bool Equals (object t1, object t2)
 Compare type, structure, and text of two trees, assuming adaptor in this instance of a TreeWizard.

Static Public Member Functions

static bool Equals (object t1, object t2, ITreeAdaptor adaptor)
 Compare t1 and t2; return true if token types/text, structure match exactly. The trees are examined in their entirety so that (A B) does not match (A B C) nor (A (B C)).

Protected Member Functions

void _Index (object t, IDictionary m)
 Do the work for index.
void _Visit (object t, object parent, int childIndex, int ttype, ContextVisitor visitor)
 Do the recursive work for visit.
bool _Parse (object t1, TreePattern t2, IDictionary labels)
 Do the work for Parse(). Check to see if the t2 pattern fits the structure and token types in t1. Check text if the pattern has text arguments on nodes. Fill labels map with pointers to nodes in tree matched against nodes in pattern with labels.

Static Protected Member Functions

static bool _Equals (object t1, object t2, ITreeAdaptor adaptor)

Protected Attributes

ITreeAdaptor adaptor
IDictionary tokenNameToTypeMap

Classes

interface  ContextVisitor
class  InvokeVisitorOnPatternMatchContextVisitor
class  PatternMatchingContextVisitor
class  RecordAllElementsVisitor
class  TreePattern
 When using label:TOKENNAME in a tree for parse(), we must track the label. More...
class  TreePatternTreeAdaptor
 This adaptor creates TreePattern objects for use during scan(). More...
class  Visitor
class  WildcardTreePattern


Detailed Description

Build and navigate trees with this object. Must know about the names of tokens so you have to pass in a map or array of token names (from which this class can build the map). I.e., Token DECL means nothing unless the class can translate it to a token type.

In order to create nodes and navigate, this class needs a TreeAdaptor.

This class can build a token type -> node index for repeated use or for iterating over the various nodes with a particular type.

This class works in conjunction with the TreeAdaptor rather than moving all this functionality into the adaptor. An adaptor helps build and navigate trees using methods. This class helps you do it with string patterns like "(A B C)". You can create a tree from that pattern or match subtrees against it.

Definition at line 64 of file TreeWizard.cs.


Constructor & Destructor Documentation

Antlr.Runtime.Tree.TreeWizard.TreeWizard ( ITreeAdaptor  adaptor  ) 

Definition at line 189 of file TreeWizard.cs.

Antlr.Runtime.Tree.TreeWizard.TreeWizard ( ITreeAdaptor  adaptor,
IDictionary  tokenNameToTypeMap 
)

Definition at line 194 of file TreeWizard.cs.

Antlr.Runtime.Tree.TreeWizard.TreeWizard ( ITreeAdaptor  adaptor,
string[]  tokenNames 
)

Definition at line 200 of file TreeWizard.cs.

Antlr.Runtime.Tree.TreeWizard.TreeWizard ( string[]  tokenNames  ) 

Definition at line 206 of file TreeWizard.cs.


Member Function Documentation

IDictionary Antlr.Runtime.Tree.TreeWizard.ComputeTokenTypes ( string[]  tokenNames  ) 

Compute a Map<String, Integer> that is an inverted index of tokenNames (which maps int token types to names).

Definition at line 215 of file TreeWizard.cs.

int Antlr.Runtime.Tree.TreeWizard.GetTokenType ( string  tokenName  ) 

Using the map of token names to token types, return the type.

Definition at line 232 of file TreeWizard.cs.

IDictionary Antlr.Runtime.Tree.TreeWizard.Index ( object  t  ) 

Walk the entire tree and make a node name to nodes mapping.

For now, use recursion but later nonrecursive version may be more efficient. Returns Map<Integer, List> where the List is of your AST node type. The Integer is the token type of the node.

TODO: save this index so that find and visit are faster

Definition at line 256 of file TreeWizard.cs.

void Antlr.Runtime.Tree.TreeWizard._Index ( object  t,
IDictionary  m 
) [protected]

Do the work for index.

Definition at line 264 of file TreeWizard.cs.

IList Antlr.Runtime.Tree.TreeWizard.Find ( object  t,
int  ttype 
)

Return a List of tree nodes with token type ttype.

Definition at line 287 of file TreeWizard.cs.

IList Antlr.Runtime.Tree.TreeWizard.Find ( object  t,
string  pattern 
)

Return a List of subtrees matching pattern.

Definition at line 295 of file TreeWizard.cs.

object Antlr.Runtime.Tree.TreeWizard.FindFirst ( object  t,
int  ttype 
)

Definition at line 312 of file TreeWizard.cs.

object Antlr.Runtime.Tree.TreeWizard.FindFirst ( object  t,
string  pattern 
)

Definition at line 317 of file TreeWizard.cs.

void Antlr.Runtime.Tree.TreeWizard.Visit ( object  t,
int  ttype,
ContextVisitor  visitor 
)

Visit every ttype node in t, invoking the visitor.

This is a quicker version of the general visit(t, pattern) method. The labels arg of the visitor action method is never set (it's null) since using a token type rather than a pattern doesn't let us set a label.

Definition at line 331 of file TreeWizard.cs.

void Antlr.Runtime.Tree.TreeWizard._Visit ( object  t,
object  parent,
int  childIndex,
int  ttype,
ContextVisitor  visitor 
) [protected]

Do the recursive work for visit.

Definition at line 337 of file TreeWizard.cs.

void Antlr.Runtime.Tree.TreeWizard.Visit ( object  t,
string  pattern,
ContextVisitor  visitor 
)

For all subtrees that match the pattern, execute the visit action.

The implementation uses the root node of the pattern in combination with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. Patterns with wildcard roots are also not allowed.

Definition at line 363 of file TreeWizard.cs.

bool Antlr.Runtime.Tree.TreeWizard.Parse ( object  t,
string  pattern,
IDictionary  labels 
)

Given a pattern like (ASSIGN lhs:ID rhs:.) with optional labels on the various nodes and '.' (dot) as the node/subtree wildcard, return true if the pattern matches and fill the labels Map with the labels pointing at the appropriate nodes. Return false if the pattern is malformed or the tree does not match.

If a node specifies a text arg in pattern, then that must match for that node in t.

TODO: what's a better way to indicate bad pattern? Exceptions are a hassle

Definition at line 393 of file TreeWizard.cs.

bool Antlr.Runtime.Tree.TreeWizard.Parse ( object  t,
string  pattern 
)

Definition at line 403 of file TreeWizard.cs.

bool Antlr.Runtime.Tree.TreeWizard._Parse ( object  t1,
TreePattern  t2,
IDictionary  labels 
) [protected]

Do the work for Parse(). Check to see if the t2 pattern fits the structure and token types in t1. Check text if the pattern has text arguments on nodes. Fill labels map with pointers to nodes in tree matched against nodes in pattern with labels.

Definition at line 414 of file TreeWizard.cs.

object Antlr.Runtime.Tree.TreeWizard.Create ( string  pattern  ) 

Create a tree or node from the indicated tree pattern that closely follows ANTLR tree grammar tree element syntax:.

(root child1 ... child2).

You can also just pass in a node: ID

Any node can have a text argument: ID[foo] (notice there are no quotes around foo--it's clear it's a string).

nil is a special name meaning "give me a nil node". Useful for making lists: (nil A B C) is a list of A B C.

Definition at line 473 of file TreeWizard.cs.

static bool Antlr.Runtime.Tree.TreeWizard.Equals ( object  t1,
object  t2,
ITreeAdaptor  adaptor 
) [static]

Compare t1 and t2; return true if token types/text, structure match exactly. The trees are examined in their entirety so that (A B) does not match (A B C) nor (A (B C)).

TODO: allow them to pass in a comparator TODO: have a version that is nonstatic so it can use instance adaptor

I cannot rely on the tree node's equals() implementation as I make no constraints at all on the node types nor interface etc...

Definition at line 493 of file TreeWizard.cs.

new bool Antlr.Runtime.Tree.TreeWizard.Equals ( object  t1,
object  t2 
)

Compare type, structure, and text of two trees, assuming adaptor in this instance of a TreeWizard.

Definition at line 503 of file TreeWizard.cs.

static bool Antlr.Runtime.Tree.TreeWizard._Equals ( object  t1,
object  t2,
ITreeAdaptor  adaptor 
) [static, protected]

Definition at line 508 of file TreeWizard.cs.


Member Data Documentation

Definition at line 66 of file TreeWizard.cs.

Definition at line 67 of file TreeWizard.cs.


The documentation for this class was generated from the following file:

Generated on Wed Oct 1 14:13:35 2008 for ANTLR API by  doxygen 1.5.5