Grammatica
Public Member Functions | Protected Member Functions | List of all members
PerCederberg.Grammatica.Runtime.Analyzer Class Reference

A parse tree analyzer. More...

Public Member Functions

 Analyzer ()
 Creates a new parse tree analyzer.
 
virtual void Reset ()
 Resets this analyzer when the parser is reset for another input stream. More...
 
Node Analyze (Node node)
 Analyzes a parse tree node by traversing all it's child nodes. More...
 
virtual Production NewProduction (ProductionPattern pattern)
 Factory method to create a new production node. More...
 
virtual void Enter (Node node)
 Called when entering a parse tree node. More...
 
virtual Node Exit (Node node)
 Called when exiting a parse tree node. More...
 
virtual void Child (Production node, Node child)
 Called when adding a child to a parse tree node. More...
 

Protected Member Functions

Node GetChildAt (Node node, int pos)
 Returns a child at the specified position. More...
 
Node GetChildWithId (Node node, int id)
 Returns the first child with the specified id. More...
 
object GetValue (Node node, int pos)
 Returns the node value at the specified position. More...
 
int GetIntValue (Node node, int pos)
 Returns the node integer value at the specified position. More...
 
string GetStringValue (Node node, int pos)
 Returns the node string value at the specified position. More...
 
ArrayList GetChildValues (Node node)
 Returns all the node values for all child nodes. More...
 

Detailed Description

A parse tree analyzer.

This class provides callback methods that may be used either during parsing, or for a parse tree traversal. This class should be subclassed to provide adequate handling of the parse tree nodes.

The general contract for the analyzer class does not guarantee a strict call order for the callback methods. Depending on the type of parser, the enter() and exit() methods for production nodes can be called either in a top-down or a bottom-up fashion. The only guarantee provided by this API, is that the calls for any given node will always be in the order enter(), child(), and exit(). If various child() calls are made, they will be made from left to right as child nodes are added (to the right).

Author
Per Cederberg
Version
1.5

Member Function Documentation

Node PerCederberg.Grammatica.Runtime.Analyzer.Analyze ( Node  node)
inline

Analyzes a parse tree node by traversing all it's child nodes.

The tree traversal is depth-first, and the appropriate callback methods will be called. If the node is a production node, a new production node will be created and children will be added by recursively processing the children of the specified production node. This method is used to process a parse tree after creation.

Parameters
nodethe parse tree node to process
Returns
the resulting parse tree node
Exceptions
ParserLogExceptionif the node analysis discovered errors
virtual void PerCederberg.Grammatica.Runtime.Analyzer.Child ( Production  node,
Node  child 
)
inlinevirtual

Called when adding a child to a parse tree node.

By default this method adds the child to the production node. A subclass can override this method to handle each node separately. Note that the child node may be null if the corresponding exit() method returned null.

Parameters
nodethe parent node
childthe child node, or null
Exceptions
ParseExceptionif the node analysis discovered errors
virtual void PerCederberg.Grammatica.Runtime.Analyzer.Enter ( Node  node)
inlinevirtual

Called when entering a parse tree node.

By default this method does nothing. A subclass can override this method to handle each node separately.

Parameters
nodethe node being entered
Exceptions
ParseExceptionif the node analysis discovered errors
virtual Node PerCederberg.Grammatica.Runtime.Analyzer.Exit ( Node  node)
inlinevirtual

Called when exiting a parse tree node.

By default this method returns the node. A subclass can override this method to handle each node separately. If no parse tree should be created, this method should return null.

Parameters
nodethe node being exited
Returns
the node to add to the parse tree, or null if no parse tree should be created
Exceptions
ParseExceptionif the node analysis discovered errors
Node PerCederberg.Grammatica.Runtime.Analyzer.GetChildAt ( Node  node,
int  pos 
)
inlineprotected

Returns a child at the specified position.

If either the node or the child node is null, this method will throw a parse exception with the internal error type.

Parameters
nodethe parent node
posthe child position
Returns
the child node
Exceptions
ParseExceptionif either the node or the child node was null
ArrayList PerCederberg.Grammatica.Runtime.Analyzer.GetChildValues ( Node  node)
inlineprotected

Returns all the node values for all child nodes.

Parameters
nodethe parse tree node
Returns
a list with all the child node values
Since
1.3
Node PerCederberg.Grammatica.Runtime.Analyzer.GetChildWithId ( Node  node,
int  id 
)
inlineprotected

Returns the first child with the specified id.

If the node is null, or no child with the specified id could be found, this method will throw a parse exception with the internal error type.

Parameters
nodethe parent node
idthe child node id
Returns
the child node
Exceptions
ParseExceptionif the node was null, or a child node couldn't be found
int PerCederberg.Grammatica.Runtime.Analyzer.GetIntValue ( Node  node,
int  pos 
)
inlineprotected

Returns the node integer value at the specified position.

If either the node is null, or the value is not an instance of the Integer class, this method will throw a parse exception with the internal error type.

Parameters
nodethe parse tree node
posthe child position
Returns
the value object
Exceptions
ParseExceptionif either the node was null, or the value wasn't an integer
string PerCederberg.Grammatica.Runtime.Analyzer.GetStringValue ( Node  node,
int  pos 
)
inlineprotected

Returns the node string value at the specified position.

If either the node is null, or the value is not an instance of the String class, this method will throw a parse exception with the internal error type.

Parameters
nodethe parse tree node
posthe child position
Returns
the value object
Exceptions
ParseExceptionif either the node was null, or the value wasn't a string
object PerCederberg.Grammatica.Runtime.Analyzer.GetValue ( Node  node,
int  pos 
)
inlineprotected

Returns the node value at the specified position.

If either the node or the value is null, this method will throw a parse exception with the internal error type.

Parameters
nodethe parse tree node
posthe child position
Returns
the value object
Exceptions
ParseExceptionif either the node or the value was null
virtual Production PerCederberg.Grammatica.Runtime.Analyzer.NewProduction ( ProductionPattern  pattern)
inlinevirtual

Factory method to create a new production node.

This method can be overridden to provide other production implementations than the default one.

Parameters
patternthe production pattern
Returns
the new production node
Since
1.5
virtual void PerCederberg.Grammatica.Runtime.Analyzer.Reset ( )
inlinevirtual

Resets this analyzer when the parser is reset for another input stream.

The default implementation of this method does nothing.

Since
1.5

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