This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
Add support for changes to number literals introduced in Go 1.13 #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Requirements
The updated grammar should support syntax highlighting for the following:
(taken from Go 1.13 Release Notes)
Description of the Change
All the regular expressions are manually converted from the EBNF (Extended Backus-Naur Form) expressions for each number literal type found in The Go Programming Language Specification:
Playground containing code snippet to test with: https://play.golang.org/p/IuWyvmHjHs0
EBNF to Regular Expression Conversion
Decimal literals
"0" … "9" .
/\d/
decimal_digit { [ "_" ] decimal_digit } .
/\d(_?\d)*/
"0" | ( "1" … "9" ) [ [ "_" ] decimal_digits ] .
/0|[1-9](_?\d(_?\d)*)?/
Binary literals
"0" | "1" .
/[01]/
binary_digit { [ "_" ] binary_digit } .
/[01](_?[01])*/
"0" ( "b" | "B" ) [ "_" ] binary_digits .
/0[bB]_?[01](_?[01])*/
Octal literals
"0" … "7" .
/[0-7]/
octal_digit { [ "_" ] octal_digit } .
/[0-7](_?[0-7])*/
"0" [ "o" | "O" ] [ "_" ] octal_digits .
/0[oO]?_?[0-7](_?[0-7])*/
Hexadecimal literals
"0" … "9" | "A" … "F" | "a" … "f" .
/[\da-fA-F]/
hex_digit { [ "_" ] hex_digit } .
/[\da-fA-F](_?[\da-fA-F])*/
"0" ( "x" | "X" ) [ "_" ] hex_digits .
/0[xX]_?[\da-fA-F](_?[\da-fA-F])*/
Decimal floating-point literals
( "e" | "E" ) [ "+" | "-" ] decimal_digits .
/[eE][+-]?\d(_?\d)*/
decimal_digits "." [ decimal_digits ] [ decimal_exponent ] .
/\d(_?\d)*\.(\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?/
decimal_digits decimal_exponent .
/\d(_?\d)*[eE][+-]?\d(_?\d)*/
"." decimal_digits [ decimal_exponent ] .
/\.\d(_?\d)*([eE][+-]?\d(_?\d)*)?/
Hexadecimal floating-point literals
( "p" | "P" ) [ "+" | "-" ] decimal_digits .
/[pP][+-]?\d(_?\d)*/
[ "_" ] hex_digits "." [ hex_digits ] .
/_?[\da-fA-F](_?[\da-fA-F])*\.([\da-fA-F](_?[\da-fA-F])*)?/
[ "_" ] hex_digits .
/_?[\da-fA-F](_?[\da-fA-F])*/
"." hex_digits .
/\.[\da-fA-F](_?[\da-fA-F])*/
"0" ( "x" | "X" ) hex_mantissa hex_exponent .
/0[xX](_?[\da-fA-F](_?[\da-fA-F])*\.([\da-fA-F](_?[\da-fA-F])*)?|_?[\da-fA-F](_?[\da-fA-F])*|\.[\da-fA-F](_?[\da-fA-F])*)[pP][+-]?\d(_?\d)*/
Imaginary literals
Since the imaginary suffix
i
may now be used with any (binary, decimal, hexadecimal) integer or floating-point literal, we just need to append ani
to the various set of regular expressions above.There's only a slight difference in the pattern for imaginary literals (with octal integer literal), which we modify it to not collide with the old imaginary literals pattern (pre Go 1.12):
Alternate Designs
Benefits
Patterns & scopes have been broken down by each number literal types for the following benefits:
Possible Drawbacks
This change introduces the following scopes:
constant.numeric.integer.decimal.go
constant.numeric.integer.binary.go
constant.numeric.integer.octal.go
constant.numeric.integer.hexadecimal.go
constant.numeric.floating-point.decimal.go
constant.numeric.floating-point.hexadecimal.go
constant.numeric.imaginary.decimal-integer.go
constant.numeric.imaginary.binary-integer.go
constant.numeric.imaginary.octal-integer.go
constant.numeric.imaginary.hexadecimal-integer.go
constant.numeric.imaginary.decimal-floating-point.go
constant.numeric.imaginary.hexadecimal-floating-point.go
constant.numeric.imaginary.backwards-compatibility.go
The only "breaking" change I'm aware is that imaginary literal expressions currently matched under
constant.numeric.integer
would now be matched with scopes prefixed withconstant.numeric.imaginary.*
.But as far as I know, majority of the color themes don't use anything beyond
constant.numeric
: https://github.com/search?l=Less&q=syntax--integer&type=CodeApplicable Issues
Fixes #174
Related tree-sitter/tree-sitter-go#31 (PR: tree-sitter/tree-sitter-go#32)