I already found a solution to my problem on stackoverflow but it is in Objective-C. See this link DDMathParser - Getting tokens
I translated this into Swift as shown below. So what is the latest way to get tokens from a string and to get grouped tokens?
For example: 1 + ($a - (3 / 4))
Here is my try:
do{
let token = try Tokenizer(string: "1 + ($a - (3 /4))").tokenize()
for element in token {
print(element)
}
} catch {
print(error)
}
But I get the following messages:
MathParser.DecimalNumberToken
MathParser.OperatorToken
MathParser.OperatorToken
MathParser.VariableToken
MathParser.OperatorToken
MathParser.OperatorToken
MathParser.DecimalNumberToken
MathParser.OperatorToken
MathParser.DecimalNumberToken
MathParser.OperatorToken
MathParser.OperatorToken
How do I get the specific tokens from my string?
You're getting all of the tokens.
OperatorToken,DecimalNumberToken, etc all inherit from aRawTokensuperclass.RawTokendefines a.stringand a.rangeproperty.The
.stringis the actualString, as extracted or inferred from the source. The.rangeis where in the original string the token is located.It's important to note, however, that there may be tokens produced by the tokenizer that are not present in the original string. For example, tokens get injected by the Tokenizer when resolving implicit multiplication (
3xturns in to3 * x).Added later:
If you want the final tree of how it all gets parsed, then you want the
Expression:At this point, you
switchonexpression.kind. It'll either be anumber, avariable, or afunction.functionexpressions have child expressions, representing the arguments to the function.