edu.neu.ccs.parser
Class AbstractParser

java.lang.Object
  |
  +--edu.neu.ccs.parser.AbstractParser
All Implemented Interfaces:
Parser
Direct Known Subclasses:
JPTParser

public abstract class AbstractParser
extends Object
implements Parser

Abstract superclass for classes of objects that provide functionality for evaluating Strings into primitive types and objects using a language with simple syntactic structure.

Since:
1.0

Nested Class Summary
static class AbstractParser.ObjectOperationPair
          Pair class used by a Parser to store a value and an associated operation.
static class AbstractParser.Operation
          Class encapsulating an operation and its corresponding String symbol for use with a parser.
static class AbstractParser.Procedure
          Class encapsulating a procedure and its corresponding String symbol for use with a parser.
 
Field Summary
protected  String ARGUMENT_LIST_END
          String token representing the end of an argument list.
protected  String ARGUMENT_LIST_START
          String token representing the start of an argument list.
protected  String ARGUMENT_SEPARATOR
          String token representing the argument separator.
protected  Hashtable constants
          Table of constant variable identifiers.
protected  String data
          String to be evaluated by this parser
protected  Hashtable environment
          Table of variable identifiers and their corresponding values.
protected static int FLOATING
          Value designating string data that has form of a floating number.
protected  AbstractParser.Operation identity
          The identity operation, equivalent to the function f(x,y) = y, inserted with the lowest level of precedence and used as the base for evaluation of an expression.
protected static int INTEGRAL
          Value designating string data that has form of an integer number.
protected  String NESTED_EXPRESSION_END
          String token representing the end of a nested expression.
protected  String NESTED_EXPRESSION_START
          String token representing the start of a nested expression.
protected  int next
          Index of the next character in the data String to be parsed by this parser.
protected static AbstractParser.Operation OPERATION_PREFIX
          Singleton operation object designating that a symbol is a prefix for a known operation.
protected  Hashtable operations
          Table of operation symbols and their corresponding operations.
protected  Vector precedence
          List of hashtables storing the precedence relationship between operations.
protected  Hashtable prefixes
          Table of prefixes of operation symbols.
protected  Hashtable procedures
          Table of procedure identifiers and their corresponding procedures.
protected  String RADIX_POINT
          String token representing the radix point.
protected  char UNDERSCORE
          Char with UNDERSCORE character for identifiers.
 
Constructor Summary
AbstractParser()
          Constructs a new parser by adding the standard operations, procedures, and constants available for this parser.
 
Method Summary
 void addConstant(String id, Object value)
          Adds the given constant to the parser environment with the given identifier and value.
protected  void addConstants()
          Adds the standard constants for this parser to the environment.
protected  void addOperation(AbstractParser.Operation op, int pos)
          Adds the given operation to the operation table at the given index in the precedence list.
 AbstractParser.Operation addOperationAfterPrecedenceOf(AbstractParser.Operation compare, AbstractParser.Operation op)
          Adds the given operation to the table of recognized operations, at the level of precedence immediately after the precendence of the given existing operation.
 AbstractParser.Operation addOperationAtPrecedenceOf(AbstractParser.Operation compare, AbstractParser.Operation op)
          Adds the given operation to the table of recognized operations, at the same level of precedence as the given existing operation.
 AbstractParser.Operation addOperationBeforePrecedenceOf(AbstractParser.Operation compare, AbstractParser.Operation op)
          Adds the given operation to the table of recognized operations, at the level of precedence immediately before the precendence as the given existing operation.
protected  void addOperations()
          Adds the standard operations for this parser to the operation table.
 void addProcedure(AbstractParser.Procedure proc)
          Adds the given procedure to the table of recognized procedures.
protected  void addProcedures()
          Adds the standard procedures for this parser to the procedure table.
protected  int afterDigits(int start)
          Return the first character position at or after start that is not a digit.
protected  int afterSign(int start)
          If the character at position start is '+' or '-' return (start + 1) otherwise return start.
protected  void assign(String id, Object value)
          Assigns the given value to the given identifier in the environment, replacing the previous value in the environment with the same identifier.
protected  boolean isExponentAt(int start)
          Return true if the character at start is 'E' or 'e'.
protected  AbstractParser.Operation isOperationOrPrefix(String symbol)
          Performs a lookup in the operation and prefix tables to determine if the given String symbol represents an existing operation or is a prefix of the symbol for an existing operation.
protected  boolean isSignAt(int start)
          Return true if the character at start is '+' or '-'.
protected  AbstractParser.Operation nextOperation()
          If the next token in the data represents an operation then return the corresponding Operation otherwise return null.
protected  boolean nextTokenIs(String pattern)
          Returns true if the given String exactly matches the next non-whitespace characters in the data, or false if it does not.
protected  boolean nextTokenIs(String pattern, int start)
          Returns true if the given String exactly matches the characters in the data starting at the given index, or false if it does not.
abstract  Object parse(String data)
          Parses the given data and returns the Object it represents, given this parsing scheme.
protected  Object[] parseArgumentList()
          Parses the next argument list in the data String and returns an array of objects representing the list of arguments.
protected abstract  AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
          Parses the next expression in the data String.
protected  String parseIdentifier()
          Parses the next identifier in the data String and returns it.
protected  Object parseNumber()
          Parses the next numeric token in the data String and returns an object representation of its value.
protected  int precedenceOf(AbstractParser.Operation op)
          Returns the index in the precedence list corresponding to the precedence of the given operation, or -1 if the given operation is null or is not in the precedence list.
protected  void setArgumentSeparatorToken(String token)
          Sets the token representing the argument separator in this parsing scheme to the given String token.
protected  void setLeftParenthesisToken(String token)
          Sets the token representing the start of an argument list in this parsing scheme to the given String token.
protected  void setRadixPointToken(String token)
          Sets the token representing the radix point in this parsing scheme to the given String token.
protected  void setRightParenthesisToken(String token)
          Sets the token representing the end of an argument list in this parsing scheme to the given String token.
protected  void skipWhitespace()
          Skips over whitespace characters starting at the current parse position, until a non-whitespace character or the end of the data String is found.
protected  boolean startsIdentifier()
          Returns true if the next non-whitespace character starts an identifier.
protected  boolean startsNumber()
          Returns true if the next non-whitespace character starts a number.
protected  boolean withinIdentifier()
          Returns true if the next character is within an identifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPERATION_PREFIX

protected static final AbstractParser.Operation OPERATION_PREFIX
Singleton operation object designating that a symbol is a prefix for a known operation.


INTEGRAL

protected static final int INTEGRAL
Value designating string data that has form of an integer number.

See Also:
Constant Field Values

FLOATING

protected static final int FLOATING
Value designating string data that has form of a floating number.

See Also:
Constant Field Values

NESTED_EXPRESSION_START

protected String NESTED_EXPRESSION_START
String token representing the start of a nested expression.


NESTED_EXPRESSION_END

protected String NESTED_EXPRESSION_END
String token representing the end of a nested expression.


ARGUMENT_LIST_START

protected String ARGUMENT_LIST_START
String token representing the start of an argument list.


ARGUMENT_LIST_END

protected String ARGUMENT_LIST_END
String token representing the end of an argument list.


RADIX_POINT

protected String RADIX_POINT
String token representing the radix point.


ARGUMENT_SEPARATOR

protected String ARGUMENT_SEPARATOR
String token representing the argument separator.


UNDERSCORE

protected char UNDERSCORE
Char with UNDERSCORE character for identifiers.


environment

protected Hashtable environment
Table of variable identifiers and their corresponding values.


constants

protected Hashtable constants
Table of constant variable identifiers.


procedures

protected Hashtable procedures
Table of procedure identifiers and their corresponding procedures.


operations

protected Hashtable operations
Table of operation symbols and their corresponding operations.


prefixes

protected Hashtable prefixes
Table of prefixes of operation symbols.


precedence

protected Vector precedence
List of hashtables storing the precedence relationship between operations. Operations with the same precedence are placed in the same hashtable. The list is set up from lowest precedence to highest.


identity

protected final AbstractParser.Operation identity
The identity operation, equivalent to the function f(x,y) = y, inserted with the lowest level of precedence and used as the base for evaluation of an expression.


data

protected String data
String to be evaluated by this parser


next

protected int next
Index of the next character in the data String to be parsed by this parser.

Constructor Detail

AbstractParser

public AbstractParser()
Constructs a new parser by adding the standard operations, procedures, and constants available for this parser.

Method Detail

parse

public abstract Object parse(String data)
                      throws ParseException
Description copied from interface: Parser
Parses the given data and returns the Object it represents, given this parsing scheme.

Specified by:
parse in interface Parser
Parameters:
data - the String data to be parsed
Throws:
ParseException - if the data is malformed

addConstant

public void addConstant(String id,
                        Object value)
Adds the given constant to the parser environment with the given identifier and value.

Parameters:
id - the identifier for the constant
value - the value for the constant

addProcedure

public void addProcedure(AbstractParser.Procedure proc)
Adds the given procedure to the table of recognized procedures.

Parameters:
proc - the procedure to be added

addOperationAtPrecedenceOf

public AbstractParser.Operation addOperationAtPrecedenceOf(AbstractParser.Operation compare,
                                                           AbstractParser.Operation op)
Adds the given operation to the table of recognized operations, at the same level of precedence as the given existing operation. Replaces the method named addOperatorAtPrecedenceOf.

Parameters:
compare - an existing operation at the desired level of precedence
op - the operation to be added
Returns:
the operation object that was added to the operation table
Throws:
JPTError - if the given operation is not an existing operation for this parser
Since:
2.2

addOperationBeforePrecedenceOf

public AbstractParser.Operation addOperationBeforePrecedenceOf(AbstractParser.Operation compare,
                                                               AbstractParser.Operation op)
Adds the given operation to the table of recognized operations, at the level of precedence immediately before the precendence as the given existing operation. Replaces the method named addOperatorBeforePrecedenceOf.

Parameters:
compare - an existing operation at the desired level of precedence
op - the operation to be added
Returns:
the operation object that was added to the operation table
Throws:
JPTError - if the given operation is not an existing operation for this parser
Since:
2.2

addOperationAfterPrecedenceOf

public AbstractParser.Operation addOperationAfterPrecedenceOf(AbstractParser.Operation compare,
                                                              AbstractParser.Operation op)
Adds the given operation to the table of recognized operations, at the level of precedence immediately after the precendence of the given existing operation. Replaces the method named addOperatorAfterPrecedenceOf.

Parameters:
compare - an existing operation at the desired level of precedence
op - the operation to be added
Returns:
the operation object that was added to the operation table
Throws:
JPTError - if the given operation is not an existing operation for this parser
Since:
2.2

parseExpression

protected abstract AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
                                                               throws ParseException
Parses the next expression in the data String. This method is to be overridden by subclasses of this class. Replaces method with the same name due to changes in the parameter and return type name.

Parameters:
standing - the value of the left operand and the operation immediately preceding the expression to be parsed
Returns:
the value of the parsed expression and the operation immediately following it
Throws:
ParseException - if the expression is malformed
Since:
2.2

addOperations

protected void addOperations()
Adds the standard operations for this parser to the operation table. Replaces the method named addOperators. This method is to be overridden by subclasses of this class, if the subclass makes use of operations.

Since:
2.2

addProcedures

protected void addProcedures()
Adds the standard procedures for this parser to the procedure table. This method is to be overridden by subclasses of this class, if the subclass makes use of procedures.


addConstants

protected void addConstants()
Adds the standard constants for this parser to the environment. This method is to be overridden by subclasses of this class, if the subclass has constants defined in its environment.


setLeftParenthesisToken

protected void setLeftParenthesisToken(String token)
Sets the token representing the start of an argument list in this parsing scheme to the given String token.

Parameters:
token - the desired String token

setRightParenthesisToken

protected void setRightParenthesisToken(String token)
Sets the token representing the end of an argument list in this parsing scheme to the given String token.

Parameters:
token - a String token

setRadixPointToken

protected void setRadixPointToken(String token)
Sets the token representing the radix point in this parsing scheme to the given String token.

Parameters:
token - a String token

setArgumentSeparatorToken

protected void setArgumentSeparatorToken(String token)
Sets the token representing the argument separator in this parsing scheme to the given String token.

Parameters:
token - a String token

assign

protected void assign(String id,
                      Object value)
Assigns the given value to the given identifier in the environment, replacing the previous value in the environment with the same identifier.

Parameters:
id - the identifier for the variable
value - the value of the variable

addOperation

protected void addOperation(AbstractParser.Operation op,
                            int pos)
Adds the given operation to the operation table at the given index in the precedence list. Replaces the method named addOperator.

Parameters:
op - an operation to be added
pos - the position in the precedence list
Throws:
JPTError - if the symbol for the given operation is already an operation for this parser
Since:
2.2

isOperationOrPrefix

protected AbstractParser.Operation isOperationOrPrefix(String symbol)
Performs a lookup in the operation and prefix tables to determine if the given String symbol represents an existing operation or is a prefix of the symbol for an existing operation. Replaces the method named isOperatorOrPrefix.

Parameters:
symbol - the symbol to look up in the operation table
Returns:
the operation object if it is represented by the given symbol, or OPERATION_PREFIX if the symbol is a prefix, or null if the symbol is neither an operation or a prefix
Since:
2.2

nextOperation

protected AbstractParser.Operation nextOperation()
If the next token in the data represents an operation then return the corresponding Operation otherwise return null.

Since:
2.2

precedenceOf

protected int precedenceOf(AbstractParser.Operation op)
Returns the index in the precedence list corresponding to the precedence of the given operation, or -1 if the given operation is null or is not in the precedence list. Replaces method with the same name due to changes in the parameter type name.

Parameters:
op - the operation whose precedence is needed
Since:
2.2

nextTokenIs

protected boolean nextTokenIs(String pattern)
Returns true if the given String exactly matches the next non-whitespace characters in the data, or false if it does not.


nextTokenIs

protected boolean nextTokenIs(String pattern,
                              int start)
Returns true if the given String exactly matches the characters in the data starting at the given index, or false if it does not.


startsNumber

protected boolean startsNumber()
Returns true if the next non-whitespace character starts a number.

Since:
2.2

startsIdentifier

protected boolean startsIdentifier()
Returns true if the next non-whitespace character starts an identifier. By convention, this means the character is a letter or an underscore.

Since:
2.2

withinIdentifier

protected boolean withinIdentifier()
Returns true if the next character is within an identifier. By convention, this means the character is a letter or a digit or an underscore.

Since:
2.2

parseIdentifier

protected String parseIdentifier()
Parses the next identifier in the data String and returns it.


parseArgumentList

protected Object[] parseArgumentList()
                              throws ParseException
Parses the next argument list in the data String and returns an array of objects representing the list of arguments. By default, an argument list begins with an argument list start token, followed by a possibly zero-length list of expressions separated by argument separator tokens, ending with an argument list end token.

Throws:
ParseException - if the argument list is malformed

parseNumber

protected Object parseNumber()
                      throws ParseException
Parses the next numeric token in the data String and returns an object representation of its value. By default, a number is either of the form s(d*), where s is an optional sign in {+-} and each d is in {0 .. 9} or of the form s(d*).(d*)etx where s is an optional sign in {+-}, each d is in {0 .. 9}, . is the radix point token, E is a character in {Ee}, t is an optional sign in {+-}, and x is one to three characters in {0 .. 9}. Integral values are represented by XBigInteger objects, and floating point values are represented by XDouble objects.

Throws:
ParseException - if the number is malformed

afterSign

protected int afterSign(int start)
If the character at position start is '+' or '-' return (start + 1) otherwise return start.


isSignAt

protected boolean isSignAt(int start)
Return true if the character at start is '+' or '-'.


isExponentAt

protected boolean isExponentAt(int start)
Return true if the character at start is 'E' or 'e'.


afterDigits

protected int afterDigits(int start)
Return the first character position at or after start that is not a digit.


skipWhitespace

protected void skipWhitespace()
Skips over whitespace characters starting at the current parse position, until a non-whitespace character or the end of the data String is found. By default, whitespace is defined as a character considered to be whitespace by the Java Character class.

See Also:
Character