|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--edu.neu.ccs.parser.AbstractParser
Abstract superclass for classes of objects
that provide functionality for evaluating
s
into primitive types and objects
using a language with simple syntactic structure.String
| Nested Class Summary | |
static class |
AbstractParser.ObjectOperationPair
Pair class used by a
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 |
protected static final AbstractParser.Operation OPERATION_PREFIX
protected static final int INTEGRAL
protected static final int FLOATING
protected String NESTED_EXPRESSION_START
protected String NESTED_EXPRESSION_END
protected String ARGUMENT_LIST_START
protected String ARGUMENT_LIST_END
protected String RADIX_POINT
protected String ARGUMENT_SEPARATOR
protected char UNDERSCORE
protected Hashtable environment
protected Hashtable constants
protected Hashtable procedures
protected Hashtable operations
protected Hashtable prefixes
protected Vector precedence
protected final AbstractParser.Operation identity
protected String data
String to be evaluated by this parser
protected int next
String
to be parsed by this parser.
| Constructor Detail |
public AbstractParser()
| Method Detail |
public abstract Object parse(String data)
throws ParseException
ParserObject it represents,
given this parsing scheme.
parse in interface Parserdata - the String data to be parsed
ParseException - if the data is malformed
public void addConstant(String id,
Object value)
id - the identifier for the constantvalue - the value for the constantpublic void addProcedure(AbstractParser.Procedure proc)
proc - the procedure to be added
public AbstractParser.Operation addOperationAtPrecedenceOf(AbstractParser.Operation compare,
AbstractParser.Operation op)
compare - an existing operation
at the desired level of precedenceop - the operation to be added
JPTError - if the given operation
is not an existing operation for this parser
public AbstractParser.Operation addOperationBeforePrecedenceOf(AbstractParser.Operation compare,
AbstractParser.Operation op)
compare - an existing operation
at the desired level of precedenceop - the operation to be added
JPTError - if the given operation
is not an existing operation for this parser
public AbstractParser.Operation addOperationAfterPrecedenceOf(AbstractParser.Operation compare,
AbstractParser.Operation op)
compare - an existing operation
at the desired level of precedenceop - the operation to be added
JPTError - if the given operation
is not an existing operation for this parser
protected abstract AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
throws ParseException
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.
standing - the value of the left operand
and the operation immediately preceding
the expression to be parsed
ParseException - if the expression is malformedprotected void addOperations()
protected void addProcedures()
protected void addConstants()
protected void setLeftParenthesisToken(String token)
String token.
token - the desired String tokenprotected void setRightParenthesisToken(String token)
String token.
token - a String tokenprotected void setRadixPointToken(String token)
String token.
token - a String tokenprotected void setArgumentSeparatorToken(String token)
String token.
token - a String token
protected void assign(String id,
Object value)
id - the identifier for the variablevalue - the value of the variable
protected void addOperation(AbstractParser.Operation op,
int pos)
op - an operation to be addedpos - the position in the precedence list
JPTError - if the symbol for the given operation
is already an operation for this parserprotected AbstractParser.Operation isOperationOrPrefix(String symbol)
String symbol
represents an existing operation
or is a prefix of the symbol for an existing operation.
Replaces the method named isOperatorOrPrefix.
symbol - the symbol to look up in the operation table
OPERATION_PREFIX if the symbol is a prefix,
or null if the symbol is neither an operation or
a prefixprotected AbstractParser.Operation nextOperation()
null.
protected int precedenceOf(AbstractParser.Operation op)
null or
is not in the precedence list.
Replaces method with the same name due to changes in
the parameter type name.
op - the operation whose precedence is neededprotected boolean nextTokenIs(String pattern)
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)
true
if the given String exactly matches
the characters in the data starting at the given index,
or false if it does not.
protected boolean startsNumber()
protected boolean startsIdentifier()
protected boolean withinIdentifier()
protected String parseIdentifier()
String
and returns it.
protected Object[] parseArgumentList()
throws ParseException
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.
ParseException - if the argument list is malformed
protected Object parseNumber()
throws ParseException
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.
ParseException - if the number is malformedprotected int afterSign(int start)
'+'
or '-' return (start + 1)
otherwise return start.
protected boolean isSignAt(int start)
'+'
or '-'.
protected boolean isExponentAt(int start)
'E'
or 'e'.
protected int afterDigits(int start)
protected void skipWhitespace()
String is found.
By default, whitespace is defined as
a character considered to be whitespace
by the Java Character class.
Character
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||