getify / literalizer

Specialized heuristic lexer for JS to identify complex literals
16 stars 2 forks source link

identify number literals #11

Closed getify closed 11 years ago

getify commented 11 years ago

it would appear number literals are complex enough to need heuristic lexing (can't just be done with regexes).

getify commented 11 years ago

Examples of complications:

getify commented 11 years ago

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

mathiasbynens commented 11 years ago

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

ljharb commented 11 years ago

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.

getify commented 11 years ago

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.