nim-lang / NimLime

The official Nim programming language plugin for Sublime Text
MIT License
132 stars 34 forks source link

Add support for numeric suffixes. #153

Closed JohnAD closed 3 years ago

JohnAD commented 3 years ago

The next release of Nim is supporting numeric suffixes. For example:

import strutils
type nibble = distinct uint8 # a 4-bit unsigned integer
proc `'nibble`(n: string): nibble =
  # The leading ' is required.
  result = (parseInt(n) and 0x0F).nibble

var x = 5'nibble

Because the single quote now has dual purposes the current NimLime highlighter gets confused.

This PR will do two things:

  1. Make the numeric literal suffix rules more generic to handle custom suffixes.
  2. Change the syntax highlighting for char literals so that it does not include explicit calls to the suffix procedures. for example:
proc aFour(): nibble =
  4'nibble
var x = aFour()
var y = 'h'

As you can see, GitHub is also confused by this. This makes sense since I believe GitHub's filters are based on NimLimes'.

I've marked this as work-in-progress (WIP) because item 2 is not done yet. I'm still wrapping my head around a good way to do it. At the moment, the invalid.illegal.character.nim checker is completely disabled. I'd like to carefully add that back in. I might do it as a separate rule rather than as a pattern underneath the current one.

Any and all help is very welcome!

JohnAD commented 3 years ago

I've added the flawed char tests back as separate rules. The check is not nearly as inclusive since there is no longer the expectation that all uses of apostrophe always have a match. Instead it is more specifically looking for sequences of letters and digits without whitespace or non-escape punctuation that are incorrect. All proper escapes such as

let x = `\``

are accounted for and marked as a proper char.

While not stepping on anything like:

let y = 23'zing.bar('\e')
JohnAD commented 3 years ago

I don't know of any problem cases.

But, generically, the invalid.illegal.character.nim "Error highlighting" that was fairly inclusive before only does single line analysis and is a bit more limited. I'm not convinced getting back to that is ideal; but perhaps I need to live with the changes longer.

timotheecour commented 3 years ago

ping @Araq now that PRs + issues were successfully transfered to https://github.com/nim-lang/NimLime

(btw that's the version that currently runs on github, see https://github.com/github/linguist/pull/5306)

also, could i please get sufficient rights to edit labels?