Closed getify closed 11 years ago
Examples of complications:
{x:4}-4
this is either a { .. }
statement block followed by a -4
number literal expression statement, or it's an object value followed by a -
subtraction operator followed by 4
number literal.42.e3
is valid just like 42e3
is.Eh, this might not need to happen after all. -42
or +42
is what makes it contextually sensitive, but JS doesn't see a -42
, it always sees a -
operator followed by a 42
, which means JS doesn't treat it context-sensitive. We probably shouldn't either, if for no other reason than to avoid unnecessary work. All those devs (like me) who want -42
to be treated as a literal are just out of luck.
Outstanding question: does that also go for -Infinity
and -0
? @mathiasbynens
Outstanding question: does that also go for
-Infinity
and-0
?
Yes. Esprima confirms this: http://esprima.org/demo/parse.html?code=%5B%0A%20%20-Infinity%2C%0A%20%20-0%0A%5D
At the very least, I think it would be a useful start to identify number literals even if the unary + and - weren't included - especially with the "e" syntax. I know that I'd love (in my own projects) to enforce that numbers ending in zeroes use scientific notation, for example.
Interesting. Since the regex for identifying all the variations of number formats is non-trivial, it might make sense to have in this lib, although this is definitely not a full lexer, it's really only for the "tough parts" of lexing. Number lexing seems like it almost meets that standard. I'll think about it.
it would appear number literals are complex enough to need heuristic lexing (can't just be done with regexes).