Package tools
Class ParsingTools
Object
ParsingTools
public class ParsingTools
extends Object
The
ParsingTools
class contains methods related to parsing user input, and is used heavily by parsing
.-
Field Summary
Fields Modifier and Type Field Description static Pattern
newline
Matches newlines with supported carriage returnsstatic Pattern
validNames
Matches all valid variable, function, and constant names -
Method Summary
Modifier and Type Method Description static char
getCharacter(String input)
Converts the string to achar
, supporting legitimate single-character strings like"x"
and LaTeX-escaped characters like"\epsilon"
static double
getConstant(String infix)
Evaluates infix corresponding to a constant, such aspi/3
static boolean
isAlmostInteger(double d)
Checks if adouble
is within Settings.integerMargin of an integerstatic boolean
parseBoolean(String s)
Parses a string to a boolean using the following rules, ignoring case: TRUE:true, t, 1, yes, y
FALSE:false, f, 0, no, n
static String
processEscapes(String input)
Returns the input string with LaTeX escapes removed (if they exist) ifSettings.removeEscapes
is enabledstatic GeneralFunction
toFunction(Object input)
If the input is aGeneralFunction
, returns the input.static int
toInteger(double d)
-
Field Details
-
newline
public static final Pattern newlineMatches newlines with supported carriage returns -
validNames
public static final Pattern validNamesMatches all valid variable, function, and constant names
-
-
Method Details
-
toFunction
If the input is aGeneralFunction
, returns the input. If the input is aDouble
, returns a newConstant
of that value. If the input is aString
, parses it withFunctionParser.parseInfix(String)
.- Parameters:
input
- the input to be parsed as described above- Returns:
- the parsed input as described above
-
getConstant
public static double getConstant(String infix)Evaluates infix corresponding to a constant, such aspi/3
- Parameters:
infix
- the infix string of the constant- Returns:
- a
double
corresponding to the evaluated constant to be evaluated
-
parseBoolean
public static boolean parseBoolean(String s)Parses a string to a boolean using the following rules, ignoring case: TRUE:true, t, 1, yes, y
FALSE:false, f, 0, no, n
- Parameters:
s
- the string to be parsed- Returns:
- the string parsed to a boolean
-
toInteger
public static int toInteger(double d) throws IllegalArgumentException- Parameters:
d
- thedouble
to be converted- Returns:
- the
double
rounded to an integer - Throws:
IllegalArgumentException
- if thedouble
is not withinSettings.integerMargin
of an integer
-
isAlmostInteger
public static boolean isAlmostInteger(double d) throws IllegalArgumentExceptionChecks if adouble
is within Settings.integerMargin of an integer- Parameters:
d
- thedouble
to be checked- Returns:
- true if the
double
is withinSettings.integerMargin
of an integer - Throws:
IllegalArgumentException
-
getCharacter
public static char getCharacter(String input)Converts the string to achar
, supporting legitimate single-character strings like"x"
and LaTeX-escaped characters like"\epsilon"
- Parameters:
input
- the string containing the character- Returns:
- the character represented by the string
- Throws:
IllegalArgumentException
- if the input is not in one of the formats specified above
-
processEscapes
public static String processEscapes(String input)Returns the input string with LaTeX escapes removed (if they exist) ifSettings.removeEscapes
is enabled- Parameters:
input
- the possibly-escaped string- Returns:
- the input with escapes processed
-