Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: Fix hex and octal int and errors highlight #2584

Merged
merged 13 commits into from
Nov 18, 2019
Merged
19 changes: 19 additions & 0 deletions autoload/go/highlight_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,37 @@ function! Test_numeric_literal_highlight() abort
let tests = {
\ 'lone zero': {'group': 'goDecimalInt', 'value': '0'},
\ 'integer': {'group': 'goDecimalInt', 'value': '1234567890'},
\ 'integerGrouped': {'group': 'goDecimalInt', 'value': '1_234_567_890'},
\ 'integerErrorLeadingUnderscore': {'group': 'goDecimalErrorInt', 'value': '_1234_567_890'},
\ 'integerErrorTrailingUnderscore': {'group': 'goDecimalErrorInt', 'value': '1_234_567890_'},
\ 'integerErrorDoubleUnderscore': {'group': 'goDecimalErrorInt', 'value': '1_234__567_890'},
\ 'integerErrorDoubleTrailingUnderscore': {'group': 'goDecimalErrorInt', 'value': '1_234_567_890__'},
\ 'hexadecimal': {'group': 'goHexadecimalInt', 'value': '0x0123456789abdef'},
\ 'hexadecimalGrouped': {'group': 'goHexadecimalInt', 'value': '0x012_345_678_9ab_def'},
\ 'hexadecimalErrorLeading': {'group': 'goHexadecimalError', 'value': '0xg0123456789abdef'},
\ 'hexadecimalErrorTrailing': {'group': 'goHexadecimalError', 'value': '0x0123456789abdefg'},
\ 'hexadecimalErrorDoubleUnderscore': {'group': 'goHexadecimalError', 'value': '0x__0123456789abdef'},
\ 'hexadecimalErrorDoubleTrailingUnderscore': {'group': 'goHexadecimalError', 'value': '0x_0123456789abdef__'},
\ 'hexadecimalErrorTrailingUnderscore': {'group': 'goHexadecimalError', 'value': '0x0123456789abdef_'},
\ 'heXadecimal': {'group': 'goHexadecimalInt', 'value': '0X0123456789abdef'},
\ 'heXadecimalErrorLeading': {'group': 'goHexadecimalError', 'value': '0Xg0123456789abdef'},
\ 'heXadecimalErrorTrailing': {'group': 'goHexadecimalError', 'value': '0X0123456789abdefg'},
\ 'octal': {'group': 'goOctalInt', 'value': '01234567'},
\ 'octalPrefix': {'group': 'goOctalInt', 'value': '0o1234567'},
\ 'octalGrouped': {'group': 'goOctalInt', 'value': '0o1_234_567'},
\ 'octalErrorLeading': {'group': 'goOctalError', 'value': '081234567'},
\ 'octalErrorTrailing': {'group': 'goOctalError', 'value': '012345678'},
\ 'octalErrorDoubleUnderscore': {'group': 'goOctalError', 'value': '0o__1234567'},
\ 'octalErrorDoubleTrailingUnderscore': {'group': 'goOctalError', 'value': '0o_1234567__'},
\ 'octalErrorTrailingUnderscore': {'group': 'goOctalError', 'value': '0o_123456_7_'},
\ 'octalErrorTrailingO': {'group': 'goOctalError', 'value': '0o_123456_7o'},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value does this add over octalErrorTrailing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For checking a specific subgroup that matches on "oO". We need this group cuz we allow 'oO' in this subgroup [^ \t0-7oOxX_/]. And just realized that we need to expand that specific subgroup to [oOxX] to match on 0123x.

\ 'OctalPrefix': {'group': 'goOctalInt', 'value': '0O1234567'},
\ 'binaryInt': {'group': 'goBinaryInt', 'value': '0b0101'},
\ 'binaryIntGrouped': {'group': 'goBinaryInt', 'value': '0b_01_01'},
\ 'binaryErrorLeading': {'group': 'goBinaryError', 'value': '0b20101'},
\ 'binaryErrorTrailing': {'group': 'goBinaryError', 'value': '0b01012'},
\ 'binaryErrorDoubleUnderscore': {'group': 'goBinaryError', 'value': '0b_01__01'},
\ 'binaryErrorTrailingUnderscore': {'group': 'goBinaryError', 'value': '0b_01_01_'},
\ 'BinaryInt': {'group': 'goBinaryInt', 'value': '0B0101'},
\ 'BinaryErrorLeading': {'group': 'goBinaryError', 'value': '0B20101'},
\ 'BinaryErrorTrailing': {'group': 'goBinaryError', 'value': '0B01012'},
Expand Down
16 changes: 9 additions & 7 deletions syntax/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,17 @@ endif
syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst

" Integers
syn match goDecimalInt "\<-\=\(0\|[1-9]\d*\)\%([Ee][-+]\=\d\+\)\=\>"
syn match goHexadecimalInt "\<-\=0[xX]\x\+\>"
syn match goHexadecimalError "\<-\=0[xX]\x*[^ \t0-9A-Fa-f]\S*\>"
syn match goOctalInt "\<-\=0\o\+\>"
syn match goOctalError "\<-\=0[XxBb\]]\@!\o*[^ \t0-7]\S*\>"
syn match goBinaryInt "\<-\=0[bB][01]\+\>"
syn match goBinaryError "\<-\=0[bB][01]*[^ \t01]\S*\>"
syn match goDecimalInt "\<-\=\(0\|[1-9]_\?\(\d\+_\?\)*\)\%([Ee][-+]\=\d\+\)\=\>"
syn match goDecimalErrorInt "\<-\=\(_\(\d\+_*\)\+\|\([1-9]\d*_*\)\+_\{2,\}\(\d\+_*\)\+\|\([1-9]\d*_*\)\+_\+\)\%([Ee][-+]\=\d\+\)\=\>"
syn match goHexadecimalInt "\<-\=0[xX]_\?\(\x\+_\?\)\+\>"
syn match goHexadecimalError "\<-\=0[xX]_\?\(\x\+_\?\)*\(\([^ \t0-9A-Fa-f_]\|_\{2,\}\)\S*\|_\)\>"
syn match goOctalInt "\<-\=0[oO]\?_\?\(\o\+_\?\)\+\>"
syn match goOctalError "\<-\=0[0-7oO_]*\(\([^ \t0-7oOxX_/]\|[oO]\{2,\}\|_\{2,\}\)\S*\|_\|[oO]\)\>"
syn match goBinaryInt "\<-\=0[bB]_\?\([01]\+_\?\)\+\>"
syn match goBinaryError "\<-\=0[bB]_\?\([01]\+_\?\)*\([^ \t01_]\S*\|_\{2,\}\S*\|_\)\>"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for both goBinaryInt and goBinaryError, wouldn't [01_]\+ suffic instead of [01]\+_\??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[01_]\+ matches more than one sequential underscore, and that is syntaxly incorrect.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's syntactically in incorrect, but that's what goBinaryError is trying to catch. Can goBinaryError be simplified to \<-\=0[bB]\([01_]*[^ \t01_]\S*\|[01]*__\S*\)\>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Technically no, cuz it doesn't match on 0b_01__01 or 0b_01_01_ but I'll try to simplify it.


hi def link goDecimalInt Integer
hi def link goDecimalErrorInt Error
hi def link goHexadecimalInt Integer
hi def link goHexadecimalError Error
hi def link goOctalInt Integer
Expand Down