Opened 13 months ago

Closed 11 months ago

Last modified 7 months ago

#14473 closed task (fixed)

Implement Underscores in Numeric Literals Proposal (NumericUnderscores extension)

Reported by: takenobu Owned by: takenobu
Priority: normal Milestone: 8.6.1
Component: Compiler Version: 8.2.1
Keywords: GHCProposal Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #13126 #9224 Differential Rev(s): Phab:D4235
Wiki Page:

Description

Implement Underscores in Numeric Literals Proposal.

GHC supports various numeric literals such as decimal, octal, hexadecimal, binary, and floating point numbers. However, large numeric literals are hard to read. This proposal improves the readability, quality, expressiveness of numeric literals.

This proposal allows underscores to numeric literals when the NumericUnderscores language extension is enabled. Underscores (_) in numeric literals are simply ignored.

The specification of the feature is available here:
https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0009-numeric-underscores.rst

For a discussion:
https://github.com/ghc-proposals/ghc-proposals/pull/76

Change History (11)

comment:1 Changed 13 months ago by takenobu

I am planning to submit a patch. This is the patch of the current version:
https://github.com/takenobu-hs/ghc/compare/master...takenobu-hs:wip/numeric-underscores

About it, Iavor gave me good advice:
https://github.com/ghc-proposals/ghc-proposals/pull/76#issuecomment-344347463

That is, I unify the definition of Lexer and add a validation function. After investigating how to implement the function for validation, I will submit the patch.

comment:2 Changed 13 months ago by takenobu

Owner: set to takenobu

comment:3 Changed 13 months ago by takenobu

Differential Rev(s): Phab:D4235
Status: newpatch

comment:4 Changed 13 months ago by takenobu

I submitted a patch Phab:D4235.

comment:5 Changed 11 months ago by takenobu

For the later people, I record about the correspondence status of the syntax highlight on editors and code browsers for BinaryLiterals, HexFloatLiterals and NumericUnderscores extensions.

  • Emacs (haskell-mode)
    • Lowercase literals are already almost correctly displayed.
    • haskell-lexeme.el and haskell-font-lock.el
  • Linguist (which is used from github)
    • Linguis uses language-haskell submodule of atom.
    • Linguis will bump it at new year release.
Last edited 11 months ago by takenobu (previous) (diff)

comment:6 Changed 11 months ago by _recursion

I've submitted an issue for this for SublimeHaskell (Sublime Text Haskell plugin) which can be found here: https://github.com/SublimeHaskell/SublimeHaskell/issues/387

comment:7 Changed 11 months ago by takenobu

_recursion,thank you so much!

comment:8 Changed 11 months ago by Ben Gamari <ben@…>

In 4a13c5b1/ghc:

Implement underscores in numeric literals (NumericUnderscores extension)

Implement the proposal of underscores in numeric literals.
Underscores in numeric literals are simply ignored.

The specification of the feature is available here:
https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/000
9-numeric-underscores.rst

For a discussion of the various choices:
https://github.com/ghc-proposals/ghc-proposals/pull/76

Implementation detail:

* Added dynamic flag
  * `NumericUnderscores` extension flag is added for this feature.

* Alex "Regular expression macros" in Lexer.x
  * Add `@numspc` (numeric spacer) macro to represent multiple
    underscores.
  * Modify `@decimal`, `@decimal`, `@binary`, `@octal`, `@hexadecimal`,
    `@exponent`, and `@bin_exponent` macros to include `@numspc`.

* Alex "Rules" in Lexer.x
  * To be simpler, we have only the definitions with underscores.
    And then we have a separate function (`tok_integral` and `tok_frac`)
    that validates the literals.

* Validation functions in Lexer.x
  * `tok_integral` and `tok_frac` functions validate
    whether contain underscores or not.
    If `NumericUnderscores` extensions are not enabled,
    check that there are no underscores.
  * `tok_frac` function is created by merging `strtoken` and
    `init_strtoken`.
  * `init_strtoken` is deleted. Because it is no longer used.

* Remove underscores from target literal string
  * `parseUnsignedInteger`, `readRational__`, and `readHexRational} use
    the customized `span'` function to remove underscores.

* Added Testcase
  * testcase for NumericUnderscores enabled.
      NumericUnderscores0.hs and NumericUnderscores1.hs
  * testcase for NumericUnderscores disabled.
      NoNumericUnderscores0.hs and NoNumericUnderscores1.hs
  * testcase to invalid pattern for NumericUnderscores enabled.
      NumericUnderscoresFail0.hs and NumericUnderscoresFail1.hs

Test Plan: `validate` including the above testcase

Reviewers: goldfire, bgamari

Reviewed By: bgamari

Subscribers: carter, rwbarton, thomie

GHC Trac Issues: #14473

Differential Revision: https://phabricator.haskell.org/D4235

comment:9 Changed 11 months ago by bgamari

Resolution: fixed
Status: patchclosed

Thanks for taking this on, takenobu!

comment:10 Changed 11 months ago by takenobu

It's my pleasure :)

comment:11 Changed 7 months ago by RyanGlScott

Keywords: GHCProposal added
Note: See TracTickets for help on using tickets.