InseeFr / Trevas-TS

JavaScript engine for the Validation and Transformation Language
MIT License
9 stars 8 forks source link

Arithmetics with `instr` is buggy #134

Open romaintailhurat opened 7 months ago

romaintailhurat commented 7 months ago

I'm trying to implement a simple feat with a duration string, that is extracting the month value for:

P100Y11M

We don't have yet a duration type implemented in Trevas JS, i'm trying to simply parse the string*.

One way to do it is to use substr and instr (here DD is a string with value P100Y11M):

substr(
    DD, 
    instr(DD, "Y") + 1, 
    (instr(DD, "M")-instr(DD, "Y"))-1
)

But i'm getting the following error:

Type errors: Error: extraneous input '-1' expecting ')'

If i try the same formula using a scalar as the length param, it works:

substr(
    DD, 
    instr(DD, "Y") + 1, 
    2
)

It's probably not a problem with substr, because this is also raising the same error as previously:

instr(DD, "M")-(instr(DD, "Y")-1)

@NicoLaval we can discuss that and even pair programming over this when you find time 😃

romaintailhurat commented 6 months ago

More

With the same binding as above:

{"DD": "P100Y11M"}

we can check that:

romaintailhurat commented 6 months ago

Even more

So:

substr(
    DD, 
    instr(DD, "Y") + 1, 
    (instr(DD, "M") - instr(DD, "Y")) - 1
)

gives 11.

Meaning in arithmetics spaces between operands are important, but probably not handled correctly for users, we should provide errors instead of wrong / curious results.

romaintailhurat commented 6 months ago

Truly, the problem is with this type of expression: 10 -9, that seems to be parsed as the two integers 10 and 9 in sequence, not as the subtracting operation.

NicoLaval commented 6 months ago

Seems to be a grammar issue.

Try here: a := 10 -9; is not valid (but has to be) and a := 10 - 9; is valid.