edu.neu.ccs.parser
Class SimpleFunction

java.lang.Object
  extended byedu.neu.ccs.parser.AbstractFunction
      extended byedu.neu.ccs.parser.SimpleFunction

public class SimpleFunction
extends AbstractFunction

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:

Version:
2.6.0

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

parser

private BaseParser parser
The parser that will be used to evaluate this function.


parameters

private String[] parameters
The function formal parameters.


body

private String body
The function body that will be parsed during evaluation.

Constructor Detail

SimpleFunction

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:

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.

Parameters:
name - the function name
parameters - the function formal parameters
body - the function body
Throws:
IllegalArgumentException

SimpleFunction

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:

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.

Parameters:
name - the function name
parameters - the function formal parameters
body - the function body
Throws:
IllegalArgumentException

SimpleFunction

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:

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.

Parameters:
parser - the parser to use to parse this function
name - the function name
parameters - the function formal parameters
body - the function body
Throws:
IllegalArgumentException

SimpleFunction

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:

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.

Parameters:
parser - the parser to use to parse this function
name - the function name
parameters - the function formal parameters
body - the function body
Throws:
IllegalArgumentException
Method Detail

functionCall

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.

Specified by:
functionCall in class AbstractFunction
Parameters:
values - the actual parameter values
Throws:
ParseException

parameters

public final String[] parameters()

Returns a copy of this function's formal parameter list.


parametersAsString

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.


body

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.


parser

public final BaseParser parser()

Returns this function's parser.


testFunctionName

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.

Parameters:
fcnName - the proposed function name

testFunctionName

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.

Parameters:
parser - the parser context
fcnName - the proposed function name

testParameterName

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.

Parameters:
paramName - the proposed parameter name
fcnName - the proposed function name

testParameterName

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.

Parameters:
parser - the parser context
paramName - the proposed parameter name
fcnName - the proposed function name

testParameterArrayNames

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:
parameters - the proposed array of parameter names
fcnName - the proposed function name

testParameterArrayNames

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.

Parameters:
parser - the parser context
parameters - the proposed array of parameter names
fcnName - the proposed function name

testFunctionBody

public 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.

Parameters:
body - the proposed function body

toString

public 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.