net.percederberg.grammatica.parser
Class ProductionPatternAlternative

java.lang.Object
  extended bynet.percederberg.grammatica.parser.ProductionPatternAlternative

public class ProductionPatternAlternative
extends java.lang.Object

A production pattern alternative. This class represents a list of production pattern elements. In order to provide productions that cannot be represented with the element occurance counters, multiple alternatives must be created and added to the same production pattern. A production pattern alternative is always contained within a production pattern.


Constructor Summary
ProductionPatternAlternative()
          Creates a new production pattern alternative.
 
Method Summary
 void addElement(ProductionPatternElement elem)
          Adds a production pattern element to this alternative.
 void addElement(ProductionPatternElement elem, int min, int max)
          Adds a production pattern element to this alternative.
 void addProduction(int id, int min, int max)
          Adds a production to this alternative.
 void addToken(int id, int min, int max)
          Adds a token to this alternative.
 boolean equals(java.lang.Object obj)
          Checks if this object is equal to another.
 ProductionPatternElement getElement(int pos)
          Returns an element in this alternative.
 int getElementCount()
          Returns the number of elements in this alternative.
 int getMaxElementCount()
          Returns the maximum number of elements needed to satisfy this alternative.
 int getMinElementCount()
          Returns the minimum number of elements needed to satisfy this alternative.
 ProductionPattern getPattern()
          Returns the production pattern containing this alternative.
 boolean isLeftRecursive()
          Checks if this alternative is recursive on the left-hand side.
 boolean isMatchingEmpty()
          Checks if this alternative would match an empty stream of tokens.
 boolean isRightRecursive()
          Checks if this alternative is recursive on the right-hand side.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ProductionPatternAlternative

public ProductionPatternAlternative()
Creates a new production pattern alternative.

Method Detail

isLeftRecursive

public boolean isLeftRecursive()
Checks if this alternative is recursive on the left-hand side. This method checks all the possible left side elements and returns true if the pattern itself is among them.

Returns:
true if the alternative is left side recursive, or false otherwise

isRightRecursive

public boolean isRightRecursive()
Checks if this alternative is recursive on the right-hand side. This method checks all the possible right side elements and returns true if the pattern itself is among them.

Returns:
true if the alternative is right side recursive, or false otherwise

isMatchingEmpty

public boolean isMatchingEmpty()
Checks if this alternative would match an empty stream of tokens. This check is equivalent of getMinElementCount() returning zero (0).

Returns:
true if the rule can match an empty token stream, or false otherwise

getPattern

public ProductionPattern getPattern()
Returns the production pattern containing this alternative.

Returns:
the production pattern for this alternative

getElementCount

public int getElementCount()
Returns the number of elements in this alternative.

Returns:
the number of elements in this alternative

getMinElementCount

public int getMinElementCount()
Returns the minimum number of elements needed to satisfy this alternative. The value returned is the sum of all the elements minimum count.

Returns:
the minimum number of elements

getMaxElementCount

public int getMaxElementCount()
Returns the maximum number of elements needed to satisfy this alternative. The value returned is the sum of all the elements maximum count.

Returns:
the maximum number of elements

getElement

public ProductionPatternElement getElement(int pos)
Returns an element in this alternative.

Parameters:
pos - the element position, 0 <= pos < count
Returns:
the element found

addToken

public void addToken(int id,
                     int min,
                     int max)
Adds a token to this alternative. The token is appended to the end of the element list. The multiplicity values specified define if the token is optional or required, and if it can be repeated.

Parameters:
id - the token (pattern) id
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

addProduction

public void addProduction(int id,
                          int min,
                          int max)
Adds a production to this alternative. The production is appended to the end of the element list. The multiplicity values specified define if the production is optional or required, and if it can be repeated.

Parameters:
id - the production (pattern) id
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

addElement

public void addElement(ProductionPatternElement elem)
Adds a production pattern element to this alternative. The element is appended to the end of the element list.

Parameters:
elem - the production pattern element

addElement

public void addElement(ProductionPatternElement elem,
                       int min,
                       int max)
Adds a production pattern element to this alternative. The multiplicity values in the element will be overridden with the specified values. The element is appended to the end of the element list.

Parameters:
elem - the production pattern element
min - the minimum number of occurancies
max - the maximum number of occurancies, or -1 for infinite

equals

public boolean equals(java.lang.Object obj)
Checks if this object is equal to another. This method only returns true for another production pattern alternative with identical elements in the same order.

Parameters:
obj - the object to compare with
Returns:
true if the object is identical to this one, or false otherwise

toString

public java.lang.String toString()
Returns a string representation of this object.

Returns:
a token string representation