Commit Graph

11 Commits

Author SHA1 Message Date
Simon Lydell 9a0babf5b1 Treat Infinity and NaN as reserved words
Fixes #4218.
2016-03-06 11:41:48 +01:00
Simon Lydell 021d2e4376 Refactor `Literal` into several subtypes
Previously, the parser created `Literal` nodes for many things. This resulted in
information loss. Instead of being able to check the node type, we had to use
regexes to tell the different types of `Literal`s apart. That was a bit like
parsing literals twice: Once in the lexer, and once (or more) in the compiler.
It also caused problems, such as `` `this` `` and `this` being indistinguishable
(fixes #2009).

Instead returning `new Literal` in the grammar, subtypes of it are now returned
instead, such as `NumberLiteral`, `StringLiteral` and `IdentifierLiteral`. `new
Literal` by itself is only used to represent code chunks that fit no category.
(While mentioning `NumberLiteral`, there's also `InfinityLiteral` now, which is
a subtype of `NumberLiteral`.)

`StringWithInterpolations` has been added as a subtype of `Parens`, and
`RegexWithInterpolations` as a subtype of `Call`. This makes it easier for other
programs to make use of CoffeeScript's "AST" (nodes). For example, it is now
possible to distinguish between `"a #{b} c"` and `"a " + b + " c"`. Fixes #4192.

`SuperCall` has been added as a subtype of `Call`.

Note, though, that some information is still lost, especially in the lexer. For
example, there is no way to distinguish a heredoc from a regular string, or a
heregex without interpolations from a regular regex. Binary and octal number
literals are indistinguishable from hexadecimal literals.

After the new subtypes were added, they were taken advantage of, removing most
regexes in nodes.coffee. `SIMPLENUM` (which matches non-hex integers) had to be
kept, though, because such numbers need special handling in JavaScript (for
example in `1..toString()`).

An especially nice hack to get rid of was using `new String()` for the token
value for reserved identifiers (to be able to set a property on them which could
survive through the parser). Now it's a good old regular string.

In range literals, slices, splices and for loop steps when number literals
are involved, CoffeeScript can do some optimizations, such as precomputing the
value of, say, `5 - 3` (outputting `2` instead of `5 - 3` literally). As a side
bonus, this now also works with hexadecimal number literals, such as `0x02`.

Finally, this also improves the output of `coffee --nodes`:

    # Before:
    $ bin/coffee -ne 'while true
      "#{a}"
      break'
    Block
      While
        Value
          Bool
        Block
          Value
            Parens
              Block
                Op +
                  Value """"
                  Value
                    Parens
                      Block
                        Value "a" "break"

    # After:
    $ bin/coffee -ne 'while true
      "#{a}"
      break'
    Block
      While
        Value BooleanLiteral: true
        Block
          Value
            StringWithInterpolations
              Block
                Op +
                  Value StringLiteral: ""
                  Value
                    Parens
                      Block
                        Value IdentifierLiteral: a
          StatementLiteral: break
2016-03-05 17:08:11 +01:00
Michael Ficarra 6a88ce7d1e fixes #2224: various issues related to number lexing
This was... embarrassing. I'm just really glad we didn't cut a release
before this got fixed.
2012-03-27 21:31:20 -04:00
clutchski 40a9196c7f Removing tab indentation. 2012-02-03 19:33:03 -05:00
Gerald Lewis 34e517de09 Issue #2060 Disallow uppercase radix prefixes and exponential notation 2012-01-20 17:23:50 -05:00
Gerald Lewis 4372138fdd Issue #1547 'use strict' Python-style octal literal notation 0o777
Allows octals in the form '0o777' and '0O777'

Case insensitive

Disallows decimals prefixed with '0'
2012-01-16 17:22:19 -05:00
Michael Ficarra 6d33a2e1a0 allowing capital B in binary literals; see #2021; thanks @BrendanEich 2012-01-12 13:21:29 -05:00
Revence Kalibwani 938abae4b5 One test; viz., for binary literal. 2011-10-21 22:27:08 +03:00
Revence Kalibwani 264f881a81 Binary notation integers (0b100 as 4). 2011-10-21 21:44:56 +03:00
Michael Ficarra c8845643e5 fixes #1385: property access on parenthesized number literals 2011-05-24 16:27:07 -04:00
Jeremy Ashkenas 19849e66d4 renaming wordy test titles. 2011-04-23 13:35:15 -04:00