Repository navigation
"2m.6" is interpreted as multiplication #3689
|
Hello! I just wanted to ask if this is intended behavior: I would have expected both of these to be considered invalid input, so I was surprised to find out that "2m.6" (with no parentheses) is interpreted as the product (2 m)(.6). |
Replies: 1 comment 1 reply
|
It is not a special case anyone designed — it falls out of two rules that are each deliberate on their own, and the difference between your two examples is purely about which characters may continue an identifier. A symbol starts on an alpha character and then keeps going while the next one is alpha or a digit: if (parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state))) {
while (parse.isAlpha(...) || parse.isDigit(currentCharacter(state))) {A dot is neither, and parse.isDigitDot = function isDigitDot (c) {
return ((c >= '0' && c <= '9') || c === '.')
}So The joining is intentional and the source says so in as many words: // symbol: implicit multiplication like '2a', '(2+3)a', 'a b'
// number: implicit multiplication like '(2+3)2'Worth noticing the guard on the number case, because it is what makes the behaviour look inconsistent from outside: (state.tokenType === TOKENTYPE.NUMBER &&
!isConstantNode(last) &&
(!isOperatorNode(last) || last.op === '!')) ||A number only multiplies implicitly when the thing before it is not a constant. That is why a bare So both of your expectations are half right: |
It is not a special case anyone designed — it falls out of two rules that are each deliberate on their own, and the difference between your two examples is purely about which characters may continue an identifier.
A symbol starts on an alpha character and then keeps going while the next one is alpha or a digit:
A dot is neither, and
isDigitDotputs it firmly on the number side instead:So
2m6tokenises as2followed by the single symbolm6