|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.neu.ccs.parser.AbstractFunction
edu.neu.ccs.parser.SimpleFunction
Class SimpleFunction is an
implementation of AbstractFunction
that is defined by providing its information as
strings.
The function name is a string, the formal parameter list is an array of string with the parameter identifiers, and the function body is a string that will be parsed in a context in which the formal parameters are bound via a let to the actual argument values.
The parameter list may also be provided to the constructors as a comma separated list of parameter names in a single string.
In the constructors, the user may supply a specific parser but if that is omitted then the current default parser is used.
The function is automatically installed as a function in its parser. This is both convenient for the caller and essential since a simple function cannot be parsed without a parser.
A SimpleFunction may replace another SimpleFunction with the same name that has already been installed in the parser but it may not replace an AbstractFunction that is not a SimpleFunction. This design prevents the user from replacing a function that has been built into the parser algorithmically.
See also:
SimpleFunctionPane
in package edu.neu.ccs.guiSimpleFunctionBuilder
in package edu.neu.ccs.guiSimpleFunctionPaneWithIO
in package edu.neu.ccs.guiSimpleFunctionBuilderWithIO
in package edu.neu.ccs.gui
| Field Summary | |
private String |
body
The function body that will be parsed during evaluation. |
private String[] |
parameters
The function formal parameters. |
private BaseParser |
parser
The parser that will be used to evaluate this function. |
| Fields inherited from class edu.neu.ccs.parser.AbstractFunction |
|
| Constructor Summary | |
SimpleFunction(BaseParser parser,
String name,
String[] parameters,
String body)
Constructor for SimpleFunction
that uses the given parser. |
|
SimpleFunction(BaseParser parser,
String name,
String parameters,
String body)
Constructor for SimpleFunction
that uses the given parser. |
|
SimpleFunction(String name,
String[] parameters,
String body)
Constructor for SimpleFunction
that uses the current default parser. |
|
SimpleFunction(String name,
String parameters,
String body)
Constructor for SimpleFunction
that uses the current default parser. |
|
| Method Summary | |
String |
body()
Returns this function's body. |
Object |
functionCall(Object[] values)
Parses the body of this function in a context in which the formal parameters are bound via let to the given values and returns the object computed. |
String[] |
parameters()
Returns a copy of this function's formal parameter list. |
String |
parametersAsString()
Returns a copy of this function's formal parameter list as a comma separated string. |
BaseParser |
parser()
Returns this function's parser. |
static String |
testFunctionBody(String body)
Tests if the proposed function body is non- null and non-empty. |
static String |
testFunctionName(BaseParser parser,
String fcnName)
Tests if the given name is a possible simple function name in the context of the given parser; returns a non- null error message
if a problem is detected;
otherwise returns null. |
static String |
testFunctionName(String fcnName)
Tests if the given name is a possible simple function name in the context of the current default parser; returns a non- null error message
if a problem is detected;
otherwise returns null. |
static String |
testParameterArrayNames(BaseParser parser,
String[] parameters,
String fcnName)
Tests if the given array of names is a possible set of parameter names in the context of the given parser and the given function name; returns a non- null error message
if a problem is detected;
otherwise returns null. |
static String |
testParameterArrayNames(String[] parameters,
String fcnName)
Tests if the given array of names is a possible set of parameter names in the context of the current default parser and the given function name; returns a non- null error message
if a problem is detected;
otherwise returns null. |
static String |
testParameterName(BaseParser parser,
String paramName,
String fcnName)
Tests if the given name is a possible parameter name in the context of the given parser and the given function name; returns a non- null error message
if a problem is detected;
otherwise returns null. |
static String |
testParameterName(String paramName,
String fcnName)
Tests if the given name is a possible parameter name in the context of the current default parser and the given function name; returns a non- null error message
if a problem is detected;
otherwise returns null. |
String |
toString()
Returns a string with 3 lines consisting of the function name, the function parameters as a comma separated list, and the function body on one line. |
| Methods inherited from class edu.neu.ccs.parser.AbstractFunction |
arguments, checkValues, checkValuesAsNumeric, checkValuesAsXObject, makeFourArg, makeNoArg, makeOneArg, makeThreeArg, makeTwoArg, name |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
private BaseParser parser
private String[] parameters
private String body
| Constructor Detail |
public SimpleFunction(String name,
String[] parameters,
String body)
Constructor for SimpleFunction
that uses the current default parser.
The function name is a string, the formal parameter list is an array of string with the parameter identifiers, and the function body is a string that will be parsed in a context in which the formal parameters are bound via a let to the actual argument values.
If parameters is null, it is
converted to an array of length 0 which means
that the function will take no parameters.
Throws IllegalArgumentException
if:
null.No effort is made to assess the validity of the body as a parsable string. This is done when the function is called and the context is fully known.
The body will be trimmed and also passed through
Strings.flatten so it may later be
displayed on a single line if desired.
name - the function nameparameters - the function formal parametersbody - the function body
IllegalArgumentException
public SimpleFunction(String name,
String parameters,
String body)
Constructor for SimpleFunction
that uses the current default parser.
The function name is a string, the formal parameter list is a comma separated string with the parameter identifiers, and the function body is a string that will be parsed in a context in which the formal parameters are bound via a let to the actual argument values.
If parameters is null, the
function will take no parameters.
Throws IllegalArgumentException
if:
null.No effort is made to assess the validity of the body as a parsable string. This is done when the function is called and the context is fully known.
The body will be trimmed and also passed through
Strings.flatten so it may later be
displayed on a single line if desired.
name - the function nameparameters - the function formal parametersbody - the function body
IllegalArgumentException
public SimpleFunction(BaseParser parser,
String name,
String[] parameters,
String body)
Constructor for SimpleFunction
that uses the given parser.
If the given parser is null,
then the current default parser will be used.
The function name is a string, the formal parameter list is an array of string with the parameter identifiers, and the function body is a string that will be parsed in a context in which the formal parameters are bound via a let to the actual argument values.
If parameters is null, it is
converted to an array of length 0 which means
that the function will take no parameters.
Throws IllegalArgumentException
if:
null.No effort is made to assess the validity of the body as a parsable string. This is done when the function is called and the context is fully known.
The body will be trimmed and also passed through
Strings.flatten so it may later be
displayed on a single line if desired.
parser - the parser to use to parse this functionname - the function nameparameters - the function formal parametersbody - the function body
IllegalArgumentException
public SimpleFunction(BaseParser parser,
String name,
String parameters,
String body)
Constructor for SimpleFunction
that uses the given parser.
If the given parser is null,
then the current default parser will be used.
The function name is a string, the formal parameter list is a comma separated string with the parameter identifiers, and the function body is a string that will be parsed in a context in which the formal parameters are bound via a let to the actual argument values.
If parameters is null, the
function will take no parameters.
Throws IllegalArgumentException
if:
null.No effort is made to assess the validity of the body as a parsable string. This is done when the function is called and the context is fully known.
The body will be trimmed and also passed through
Strings.flatten so it may later be
displayed on a single line if desired.
parser - the parser to use to parse this functionname - the function nameparameters - the function formal parametersbody - the function body
IllegalArgumentException| Method Detail |
public final Object functionCall(Object[] values)
throws ParseException
Parses the body of this function in a context in which the formal parameters are bound via let to the given values and returns the object computed.
The number of values should equal the number of expected function parameters.
functionCall in class AbstractFunctionvalues - the actual parameter values
ParseExceptionpublic final String[] parameters()
Returns a copy of this function's formal parameter list.
public final String parametersAsString()
Returns a copy of this function's formal parameter list as a comma separated string.
If the function has 0 parameters, this method will return a string with one blank to act as a placeholder for parsing purposes.
public final String body()
Returns this function's body.
The string that is returned has been trimmed
and flattened (via Strings.flatten)
so it may viewed on a single line.
public final BaseParser parser()
Returns this function's parser.
public static String testFunctionName(String fcnName)
Tests if the given name
is a possible simple function name
in the context of the current default parser;
returns a non-null error message
if a problem is detected;
otherwise returns null.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
fcnName - the proposed function name
public static String testFunctionName(BaseParser parser,
String fcnName)
Tests if the given name
is a possible simple function name
in the context of the given parser;
returns a non-null error message
if a problem is detected;
otherwise returns null.
If the given parser is null,
then the current default parser will be used.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
parser - the parser contextfcnName - the proposed function name
public static String testParameterName(String paramName,
String fcnName)
Tests if the given name is a possible parameter name
in the context of the current default parser
and the given function name;
returns a non-null error message
if a problem is detected;
otherwise returns null.
Note that a parameter name may not be the same as its function name.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
paramName - the proposed parameter namefcnName - the proposed function name
public static String testParameterName(BaseParser parser,
String paramName,
String fcnName)
Tests if the given name is a possible parameter name
in the context of the given parser
and the given function name;
returns a non-null error message
if a problem is detected;
otherwise returns null.
Note that a parameter name may not be the same as its function name.
If the given parser is null,
then the current default parser will be used.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
parser - the parser contextparamName - the proposed parameter namefcnName - the proposed function name
public static String testParameterArrayNames(String[] parameters,
String fcnName)
Tests if the given array of names
is a possible set of parameter names
in the context of the current default parser
and the given function name;
returns a non-null error message
if a problem is detected;
otherwise returns null.
Note that a parameter name may not be the same as its function name.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
parameters - the proposed array of parameter namesfcnName - the proposed function name
public static String testParameterArrayNames(BaseParser parser,
String[] parameters,
String fcnName)
Tests if the given array of names
is a possible set of parameter names
in the context of the given parser
and the given function name;
returns a non-null error message
if a problem is detected;
otherwise returns null.
Note that a parameter name may not be the same as its function name.
If the given parser is null,
then the current default parser will be used.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
parser - the parser contextparameters - the proposed array of parameter namesfcnName - the proposed function namepublic static String testFunctionBody(String body)
Tests if the proposed function body is
non-null and non-empty.
Note that it is not possible to make more sophisticated tests until runtime so this method does only the most obvious tests.
There is no corresponding method to this method that accepts a parser argument since we do not wish to execute the body in the context of a parser since functions may have side effects.
Used in the constructors for this class and may be used by a caller to pretest prior to invoking one of the constructors.
body - the proposed function bodypublic String toString()
Returns a string with 3 lines consisting of the function name, the function parameters as a comma separated list, and the function body on one line.
If the function has 0 parameters, the line for its parameters will contain a blank to act as a placeholder for parsing purposes.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||