Overview   Project   Class   Tree   Deprecated   Index 
Grammatica 1.4 Documentation
FRAMES    NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD

PerCederberg.Grammatica.Parser
Class Parser

System.Object
   |
   +--Parser

   in Parser.cs
Direct Known Subclasses:
RecursiveDescentParser

class Parser
extends System.Object

A base parser class. This class provides the standard parser interface, as well as token handling.


Constructor Summary
internal Parser( Tokenizer tokenizer )
          Creates a new parser.
internal Parser( Tokenizer tokenizer, Analyzer analyzer )
          Creates a new parser.
 
Method Summary
 internal void AddError( ParseException e, bool recovery )
          Adds an error to the error log.
 internal void AddNode( Production node, Node child )
          Handles the parser adding a child node to a production.
 virtual void AddPattern( ProductionPattern pattern )
          Adds a new production pattern to the parser.
 internal void EnterNode( Node node )
          Handles the parser entering a production.
 internal Node ExitNode( Node node )
          Handles the parser leaving a production.
 Analyzer GetAnalyzer()
          Returns the analyzer in use by this parser.
 internal ProductionPattern GetPattern( int id )
          Returns the production pattern with the specified id.
 internal ICollection GetPatterns()
          Returns the ordered set of production patterns.
 internal ProductionPattern GetStartPattern()
          Returns the production pattern for the starting production.
 internal string GetTokenDescription( int token )
          Returns a token description for a specified token.
 Tokenizer GetTokenizer()
          Returns the tokenizer in use by this parser.
 internal Token NextToken()
          Reads and consumes the next token in the queue.
 internal Token NextToken( int id )
          Reads and consumes the next token in the queue.
 Node Parse()
          Parses the token stream and returns a parse tree.
protected abstract Node ParseStart()
          Parses the token stream and returns a parse tree.
 internal Token PeekToken( int steps )
          Returns a token from the queue.
 virtual void Prepare()
          Initializes the parser.
 internal void SetInitialized( bool initialized )
          Sets the parser initialized flag.
 override string ToString()
          Returns a string representation of this parser.
 

Constructor Detail

Parser

 internal Parser( Tokenizer tokenizer );
Creates a new parser.
Parameters:
tokenizer - the tokenizer to use

Parser

 internal Parser( Tokenizer tokenizer, Analyzer analyzer );
Creates a new parser.
Parameters:
tokenizer - the tokenizer to use
analyzer - the analyzer callback to use


Method Detail

AddError

 internal void AddError( ParseException e, bool recovery );
Adds an error to the error log. If the parser is in error recovery mode, the error will not be added to the log. If the recovery flag is set, this method will set the error recovery counter thus enter error recovery mode. Only lexical or syntactical errors require recovery, so this flag shouldn't be set otherwise.
Parameters:
e - the error to add
recovery - the recover flag

AddNode

 internal void AddNode( Production node, Node child );
Handles the parser adding a child node to a production. This method calls the appropriate analyzer callback. Note that this method will not call any callback if an error requiring recovery has ocurred.
Parameters:
node - the parent parse tree node
child - the child parse tree node, or null

AddPattern

public virtual void AddPattern( ProductionPattern pattern );
Adds a new production pattern to the parser. The first pattern added is assumed to be the starting point in the grammar. The patterns added may be validated to some extent.
Parameters:
pattern - the pattern to add
Throws:
ParserCreationException - if the pattern couldn't be added correctly to the parser

EnterNode

 internal void EnterNode( Node node );
Handles the parser entering a production. This method calls the appropriate analyzer callback if the node is not hidden. Note that this method will not call any callback if an error requiring recovery has ocurred.
Parameters:
node - the parse tree node

ExitNode

 internal Node ExitNode( Node node );
Handles the parser leaving a production. This method calls the appropriate analyzer callback if the node is not hidden, and returns the result. Note that this method will not call any callback if an error requiring recovery has ocurred.
Parameters:
node - the parse tree node
Returns:
the parse tree node, or null if no parse tree should be created

GetAnalyzer

public Analyzer GetAnalyzer();
Returns the analyzer in use by this parser.
Returns:
the analyzer in use by this parser
Since:
1.4

GetPattern

 internal ProductionPattern GetPattern( int id );
Returns the production pattern with the specified id.
Parameters:
id - the production pattern id
Returns:
the production pattern found, or null if non-existent

GetPatterns

 internal ICollection GetPatterns();
Returns the ordered set of production patterns.
Returns:
the ordered set of production patterns

GetStartPattern

 internal ProductionPattern GetStartPattern();
Returns the production pattern for the starting production.
Returns:
the start production pattern, or null if no patterns have been added

GetTokenDescription

 internal string GetTokenDescription( int token );
Returns a token description for a specified token.
Parameters:
token - the token to describe
Returns:
the token description

GetTokenizer

public Tokenizer GetTokenizer();
Returns the tokenizer in use by this parser.
Returns:
the tokenizer in use by this parser
Since:
1.4

NextToken

 internal Token NextToken();
Reads and consumes the next token in the queue. If no token was available for consumation, a parse error will be thrown.
Returns:
the token consumed
Throws:
ParseException - if the input stream couldn't be read or parsed correctly

NextToken

 internal Token NextToken( int id );
Reads and consumes the next token in the queue. If no token was available for consumation, a parse error will be thrown. A parse error will also be thrown if the token id didn't match the specified one.
Parameters:
id - the expected token id
Returns:
the token consumed
Throws:
ParseException - if the input stream couldn't be parsed correctly, or if the token wasn't expected

Parse

public Node Parse();
Parses the token stream and returns a parse tree. This method will call prepare() if not previously called. In case of a parse error, the parser will attempt to recover and throw all the errors found in a parser log exception in the end.
Returns:
the parse tree
Throws:
ParserCreationException - if the parser couldn't be initialized correctly
ParserLogException - if the input couldn't be parsed correctly
See Also:
prepare

ParseStart

protected abstract Node ParseStart();
Parses the token stream and returns a parse tree.
Returns:
the parse tree
Throws:
ParseException - if the input couldn't be parsed correctly

PeekToken

 internal Token PeekToken( int steps );
Returns a token from the queue. This method is used to check coming tokens before they have been consumed. Any number of tokens forward can be checked.
Parameters:
steps - the token queue number, zero (0) for first
Returns:
the token in the queue, or null if no more tokens in the queue

Prepare

public virtual void Prepare();
Initializes the parser. All the added production patterns will be analyzed for ambiguities and errors. This method also initializes internal data structures used during the parsing.
Throws:
ParserCreationException - if the parser couldn't be initialized correctly

SetInitialized

 internal void SetInitialized( bool initialized );
Sets the parser initialized flag. Normally this flag is set by the prepare() method, but this method allows further modifications to it.
Parameters:
initialized - the new initialized flag

ToString

public override string ToString();
Returns a string representation of this parser. The string will contain all the production definitions and various additional information.
Returns:
a detailed string representation of this parser

 Overview   Project   Class   Tree   Deprecated   Index 
Grammatica 1.4 Documentation
FRAMES    NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD