0

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
TBH
  • 145
  • 1
  • 11

1 Answers1

0

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • If I have the following expression "let expression = try Expression(string: "1 - ($a - (3/4)) + ($a + 2)"), I get the message "function("add", [1.0 - $a - 3.0 / 4.0 + $a, 2.0])". My question: How do I know that $a-3/4 is in brackets ? – TBH Nov 29 '17 at 12:39
  • @TBH that looks like the `description`. As I mentioned, you'll want to `switch` on the `expression.kind`. That'll tell you it's a `.function` expression, and you'll get the 2 arguments as part of the enum payload. The two arguments will both be expressions: the first will be `.number(1.0)`, and the second will be what you're looking for – Dave DeLong Nov 30 '17 at 00:24