jeff-hykin / better-go-syntax

💾 📦 ✅
MIT License
35 stars 6 forks source link

Integer and float literals with underscores #3

Closed SteelPhase closed 4 years ago

SteelPhase commented 4 years ago

Integer, and floating point literals allow underscores, and the regex associated with them here doesn't seem to support underscores. Is there a way these could be updated to support underscores.

{
    "comment": "Floating-point literals",
    "match": "(\\.\\d+([Ee][-+]\\d+)?i?)\\b|\\b\\d+\\.\\d*(([Ee][-+]\\d+)?i?\\b)?",
    "name": "constant.numeric.floating-point.go"
},
{
    "comment": "Integers",
    "match": "\\b((0x[0-9a-fA-F]+)|(0[0-7]+i?)|(\\d+([Ee]\\d+)?i?)|(\\d+[Ee][-+]\\d+i?))\\b",
    "name": "constant.numeric.integer.go"
},
jeff-hykin commented 4 years ago

Could you link to official go documentation on valid numeric literal syntax?

Allowing _ isn't quite is straightforward as it sounds since there are many cases that are unclear 1__0, 1_0_1, 1_, 1_e10, 1_e__10, 1__0e10_, etc. Trial-and-error is not a very quick way to figure out exactly what is valid/invalid but I'm unaware of the official specification of Go's numeric literals

I did however reformat this repo today to make the change easy to implement

SteelPhase commented 4 years ago

I linked to integer, and floating point literals in my message above. They are both located here https://golang.org/ref/spec

egonelbre commented 4 years ago

For numbers there are test cases here https://github.com/golang/go/blob/master/src/go/scanner/scanner_test.go#L916

jeff-hykin commented 4 years ago

I linked to integer, and floating point literals in my message above. They are both located here https://golang.org/ref/spec

>.< my bad I completely read over the links, thanks! That'll make it a lot easier to implement

hyangah commented 4 years ago

@jeff-hykin Is it possible to reuse some of the numeric handling in cpp-textmate-grammar/source/shared_patterns?

I am not familiar with ruby/gem at all and don't know how to importtextmate_grammar the cpp-textmate-grammar uses. Is there a pointer to the instruction?

jeff-hykin commented 4 years ago

@hyangah

Is it possible to reuse some of the numeric handling?

Yes, BUT a straight copy-paste probably will cause problems because C++ numbers contain a lot of junk (C++ allows quotes ' instead of underscores as separators, allows custom units, etc). Using the cpp-textmate-grammar as a guide is generally a good idea though.

I am not familiar with ruby/gem at all don't know how to import textmate_grammar

This repo should automatically import textmate_grammar which is an older version of the one cpp-textmate-grammar. I apologize for the broken CONTRIBUTING.md though, since it didn't explain how to setup the ruby/gem part.

Is there a pointer to the instruction?

I pushed a commit just now that updated the CONTRIBUTING.md which should explain how to get this repo (better-go-syntax) running, and explains how this repo works

I hoped to finish a full guide on the textmate_grammar library this summer but had to postpone it.

jeff-hykin commented 4 years ago

Also, if anyone can simply come up with regex that passes all the test cases then I can merge it into the grammar

egonelbre commented 4 years ago

I tried translating the spec directly to a regex https://play.golang.org/p/hbqmflTLVUu, but it should be possible to run them through a regex minifier.

jeff-hykin commented 4 years ago

This should be fixed now! Thanks @egonelbre and @hyangah