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

PerCederberg.Grammatica.Runtime
Class Parser

System.Object
   |
   +--Parser

   in Parser.cs
Direct Known Subclasses:
RecursiveDescentParser

public abstract class Parser
extends System.Object

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


Field Summary
  href="../PerCederberg.Grammatica.Runtime/Analyzer.html">Analyzer Analyzer
          The analyzer property (read-only).
  href="../PerCederberg.Grammatica.Runtime/Tokenizer.html">Tokenizer Tokenizer
          The tokenizer property (read-only).
 
Constructor Summary
internal Parser( TextReader input )
          Creates a new parser.
internal Parser( TextReader input, Analyzer analyzer )
          Creates a new parser.
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()
          Deprecated. Use the Analyzer property instead.
 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()
          Deprecated. Use the Tokenizer property instead.
protected virtual Analyzer NewAnalyzer()
          Creates a new analyzer for this parser.
protected virtual Production NewProduction( ProductionPattern pattern )
          Factory method to create a new production node.
protected virtual Tokenizer NewTokenizer( TextReader input )
          Creates a new tokenizer for 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.
 void Reset( TextReader input )
          Resets this parser for usage with another input stream.
 internal void SetInitialized( bool initialized )
          Sets the parser initialized flag.
 override string ToString()
          Returns a string representation of this parser.
 

Field Detail

Analyzer

public Analyzer Analyzer;
The analyzer property (read-only). This property contains the analyzer in use by this parser.
Since:
1.5

Tokenizer

public Tokenizer Tokenizer;
The tokenizer property (read-only). This property contains the tokenizer in use by this parser.
Since:
1.5


Constructor Detail

Parser

 internal Parser( TextReader input );
Creates a new parser.
Parameters:
input - the input stream to read from
Throws:
ParserCreationException - if the tokenizer couldn't be initialized correctly
Since:
1.5

Parser

 internal Parser( TextReader input, Analyzer analyzer );
Creates a new parser.
Parameters:
input - the input stream to read from
analyzer - the analyzer callback to use
Throws:
ParserCreationException - if the tokenizer couldn't be initialized correctly
Since:
1.5

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();
Deprecated. Use the Analyzer property instead.

Returns the analyzer in use by this parser.

Returns:
the analyzer in use by this parser
Since:
1.4
See Also:
Analyzer

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();
Deprecated. Use the Tokenizer property instead.

Returns the tokenizer in use by this parser.

Returns:
the tokenizer in use by this parser
Since:
1.4
See Also:
Tokenizer

NewAnalyzer

protected virtual Analyzer NewAnalyzer();
Creates a new analyzer for this parser. Can be overridden by a subclass to provide a custom implementation.
Returns:
the analyzer created
Since:
1.5

NewProduction

protected virtual Production NewProduction( ProductionPattern pattern );
Factory method to create a new production node. This method can be overridden to provide other production implementations than the default one.
Parameters:
pattern - the production pattern
Returns:
the new production node
Since:
1.5

NewTokenizer

protected virtual Tokenizer NewTokenizer( TextReader input );
Creates a new tokenizer for this parser. Can be overridden by a subclass to provide a custom implementation.
Parameters:
in - the input stream to read from
Returns:
the tokenizer created
Throws:
ParserCreationException - if the tokenizer couldn't be initialized correctly
Since:
1.5

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. It will also call the Reset() method, to make sure that only the Tokenizer.Reset() method must be explicitly called in order to reuse a parser for multiple input streams. 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, Reset, Tokenizer.Reset

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

Reset

public void Reset( TextReader input );
Resets this parser for usage with another input stream. The associated tokenizer and analyzer will also be reset. This method will clear all the internal state and the error log in the parser. It is normally called in order to reuse a parser and tokenizer pair with multiple input streams, thereby avoiding the cost of re-analyzing the grammar structures.
Parameters:
input - the new input stream to read
Since:
1.5
See Also:
Tokenizer.Reset, Analyzer.Reset

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.5 Documentation
FRAMES    NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD