Search code examples
swiftddmathparser

Getting DDMathParser tokens and group tokens


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?


Solution

  • You're getting all of the tokens.

    OperatorToken, DecimalNumberToken, etc all inherit from a RawToken superclass. RawToken defines a .string and a .range property.

    The .string is the actual String, as extracted or inferred from the source. The .range is 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 (3x turns in to 3 * x).


    Added later:

    If you want the final tree of how it all gets parsed, then you want the Expression:

    let expression = try Expression(string: "1 + ($a - (3 / 4))")
    

    At this point, you switch on expression.kind. It'll either be a number, a variable, or a function. function expressions have child expressions, representing the arguments to the function.