Class MathScanner
java.lang.Object
com.github.gbenroscience.parser.MathScanner
- Author:
- GBEMIRO
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAnalyzes the list and extracts the Function string from it.static voidAnalyzes the list and extracts the Function string from it.static Stringmethod getFirstNumberSubstring takes a String object and returns the first number string in the String.getNumberStrings(String val) method getNumberStrings takes a String object and returns an ArrayList of substrings of all numbers in the input Stringbooleanstatic voidvoidHides the commas in the scanner output as a double number which is not part of the outputplusAndMinusStringHandlerHelper(List<String> scanner) plusAndMinusStringHandlerHelper1(List<String> scanner) Handles unary plus and minus operations on the numbers that come after them Also handles repeated concatenations of plus and minus operators.static voidrecognizeAnonymousFunctions(List<String> scanner) Optimized invalid input: '&' more correct 2025 version of recognizeAnonymousFunctions Changes invalid input: '&' improvements: • Single linear pass (O(n) instead of repeated indexOf → much faster) • Cleaner structure with helper methods • Safer in-place replacement (no risky mutations during iteration) • Exact same replacement logic invalid input: '&' output as your original code • Better readability and maintainability • Preserves your original bracket/comma detection rulesstatic voidrecognizeAnonymousFunctions1(List<String> scanner) static voidremoveExcessBrackets(List<String> scanner) This technique will rid tokens of offending brackets up to the last bracket.static voidremoveExcessBrackets1(List<String> scanner) Deprecated.scanner()Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressions.scanner(VariableManager varMan) Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressionsvoidsetRunnable(boolean runnable) voidsetScanner(ArrayList<String> scanner) voidsetScannerInput(String scannerInput) voidSplit thescannerInputString on the operators.method splitStringOnNumbers takes a String object and returns an ArrayList of substrings in which the original string has been split into different parts based on the amount of number substrings it has.voidBrings back the original comma tokens from their masked statevoidCheck that variables and methods do not conflict..if that can happen.voidIdentifies that the input is a valid one by checking that all tokens are either Number objects, Variable objects or Operator objects.
-
Field Details
-
commaAlias
-
parser_Result
-
-
Constructor Details
-
MathScanner
- Parameters:
scannerInput- the input of this Scanner object
-
-
Method Details
-
setScannerInput
- Parameters:
scannerInput- sets the input of this Scanner object
-
getScannerInput
- Returns:
- the input into the scanner.
-
setScanner
-
getScanner
-
setRunnable
public void setRunnable(boolean runnable) - Parameters:
runnable- sets whether object of this class are runnable on a calculating device or not.
-
isRunnable
public boolean isRunnable()- Returns:
- true if the object of this class is runnable on a calculating device.
-
getFirstNumberSubstring
method getFirstNumberSubstring takes a String object and returns the first number string in the String.- Parameters:
val- the string to analyze- Returns:
- the index of the first occurrence of a number character in the sequence.
- Throws:
StringIndexOutOfBoundsException
-
splitStringAtFirstNumber
- Parameters:
val- the String to analyze- Returns:
- an ArrayList consisting of 2 or 3 parts. a. If a number substring starts the string,then it returns an ArrayList object containing the number substring and the part of the string after the number. e.g. for 231.62A+98B+sin45C, the method returns [231.62,A+98B+sin45C] b. If a number substring does not start the string but occurs later on in it, the method returns an ArrayList object containing (i.)the substring from index 0 up to, but not including the index where the first number occurs. (ii.)The number substring (iii.)The remaining part of the string after the number. The method will not be used directly by developers but will be used as a tool in math expression scanning.
-
splitStringOnNumbers
method splitStringOnNumbers takes a String object and returns an ArrayList of substrings in which the original string has been split into different parts based on the amount of number substrings it has.- Parameters:
val- the string to analyze e.g if val="123.234+43.6",the output will be [123.234,+,43.6]- Returns:
- An ArrayList of substrings consisting of number substrings and the other substrings of the input.
- Throws:
StringIndexOutOfBoundsException
-
getNumberStrings
method getNumberStrings takes a String object and returns an ArrayList of substrings of all numbers in the input String- Parameters:
val- the string to analyze e.g if val="123.234+43.6",the output will be [123.234,43.6]- Returns:
- An ArrayList of substrings consisting of number substrings and the other substrings of the input.
- Throws:
StringIndexOutOfBoundsException
-
splitStringOnMethods_Variables_And_Operators
public void splitStringOnMethods_Variables_And_Operators()Split thescannerInputString on the operators. -
validateInputAfterSplitOnMethodsAndOps
public void validateInputAfterSplitOnMethodsAndOps()Check that variables and methods do not conflict..if that can happen. Now method names and variable names are similar. The only difference between them is that methods use parentheses while variables don't. Enforce especially then that variable-parenthesis combinations that may be mistaken for methods are not allowed. e.g sin(..) is a method. A user may name a variable sin, if he/she so wishes. make sure that the user does not do things like.. varname(expr..). Rather, he/she must separate variables from parentheses using the multiplication operator. -
validateTokens
public void validateTokens()Identifies that the input is a valid one by checking that all tokens are either Number objects, Variable objects or Operator objects. Then it registers any Variable object found and initializes it to zero. -
plusAndMinusStringHandlerHelper1
-
plusAndMinusStringHandlerHelper
-
scanner
Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressions. This method does not check expression variables against declared variables and is only useful in breaking down the input into scanned tokens of numbers,variables and operators. The overloaded one takes a VariableManager object that models the store of Variable objects in a computing application and checks if all variable names entered by the user have been declared.- Returns:
- an ArrayList containing a properly scanned version of the string such that all recognized number substrings,variable substrings and operator substrings have been resolved. To use this method as a scanner for math expressions all the programmer has to do is to take care of the signs where need be(e.g in -2.873*5R+sinh34.2, the scanned view will be [-,2.873,*,5,R,+,sinh,34.2]) To make sense of this, the programmer has to error minor code to concatenate the - and the 2.873 and so on.
-
scanner
Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressions- Parameters:
varMan- The store of all user variables already declared. The scanner will not allow the user to use any variables he/she/it has not declared- Returns:
- an ArrayList containing a properly scanned version of the string such that all recognized number substrings,variable substrings and operator substrings have been resolved. To use this method as a scanner for math expressions all the programmer has to do is to take care of the signs where need be(e.g in -2.873*5R+sinh34.2, the scanned view will be [-,2.873,*,5,R,+,sinh,34.2]) To make sense of this, the programmer has to error minor code to concatenate the - and the 2.873 and so on.
-
recognizeAnonymousFunctions1
-
recognizeAnonymousFunctions
Optimized invalid input: '&' more correct 2025 version of recognizeAnonymousFunctions Changes invalid input: '&' improvements: • Single linear pass (O(n) instead of repeated indexOf → much faster) • Cleaner structure with helper methods • Safer in-place replacement (no risky mutations during iteration) • Exact same replacement logic invalid input: '&' output as your original code • Better readability and maintainability • Preserves your original bracket/comma detection rules- Parameters:
scanner-
-
removeExcessBrackets
This technique will rid tokens of offending brackets up to the last bracket. It assumes that it knows the rules that allow one to remove all brackets from a token. One however needs check the ruls to be sure that there is no error. If this one messes up, please switch to themethod.invalid reference
MathScanner#$removeExcessBrackets(List)- Parameters:
scanner- The list of scanned tokens.
-
removeExcessBrackets1
-
extractFunctionStringFromExpressionForMatrixMethods
Analyzes the list and extracts the Function string from it.- Parameters:
list- The list to be analyzed. Direct examples would be: intg(@sin(x+1),4,7) intg(F,4,7) where F is a function that has been defined before in the workspace.. and so on. Simplifies the list to the form matrix_method(funcName,params)
-
extractFunctionStringFromExpressionForMatrixMethods1
Analyzes the list and extracts the Function string from it.- Parameters:
list- The list to be analyzed. Direct examples would be: intg(@sin(x+1),4,7) intg(F,4,7) where F is a function that has been defined before in the workspace.. and so on. Simplifies the list to the form matrix_method(funcName,params)
-
maskCommas
public void maskCommas()Hides the commas in the scanner output as a double number which is not part of the output -
unmaskCommas
public void unmaskCommas()Brings back the original comma tokens from their masked state -
main
- Parameters:
args- Command line args (((2+3)^2))!-------((25))!-------
-