Skip to content

Commit 506e36f

Browse files
authored
fix(lexers/go): "~" is a valid token (#926)
With the introduction of generics, tilde is a valid punctuation token in Go programs. https://go.dev/ref/spec#Operators_and_punctuation This updates the punctuation regex for the Go lexer, and adds a test to ensure that it's treated as such.
1 parent f4788c0 commit 506e36f

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lexers/go.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func goRules() Rules {
5555
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
5656
{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
5757
{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
58-
{`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
58+
{`[|^<>=!()\[\]{}.,;:~]`, Punctuation, nil},
5959
{`[^\W\d]\w*`, NameOther, nil},
6060
},
6161
}

lexers/testdata/go.actual

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ func hello(a int) {
1515
return func() int {
1616
return i
1717
}
18-
} // One last thing
18+
} // One last thing
19+
20+
type Int interface {
21+
~int | ~int8 | ~int16 | ~int32 | ~int64
22+
}

lexers/testdata/go.expected

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,39 @@
8484
{"type":"Text","value":"\n"},
8585
{"type":"Punctuation","value":"}"},
8686
{"type":"Text","value":" "},
87-
{"type":"CommentSingle","value":"// One last thing\n"}
87+
{"type":"CommentSingle","value":"// One last thing\n"},
88+
{"type":"Text","value":"\n"},
89+
{"type":"KeywordDeclaration","value":"type"},
90+
{"type":"Text","value":" "},
91+
{"type":"NameOther","value":"Int"},
92+
{"type":"Text","value":" "},
93+
{"type":"KeywordDeclaration","value":"interface"},
94+
{"type":"Text","value":" "},
95+
{"type":"Punctuation","value":"{"},
96+
{"type":"Text","value":"\n\t"},
97+
{"type":"Punctuation","value":"~"},
98+
{"type":"KeywordType","value":"int"},
99+
{"type":"Text","value":" "},
100+
{"type":"Punctuation","value":"|"},
101+
{"type":"Text","value":" "},
102+
{"type":"Punctuation","value":"~"},
103+
{"type":"KeywordType","value":"int8"},
104+
{"type":"Text","value":" "},
105+
{"type":"Punctuation","value":"|"},
106+
{"type":"Text","value":" "},
107+
{"type":"Punctuation","value":"~"},
108+
{"type":"KeywordType","value":"int16"},
109+
{"type":"Text","value":" "},
110+
{"type":"Punctuation","value":"|"},
111+
{"type":"Text","value":" "},
112+
{"type":"Punctuation","value":"~"},
113+
{"type":"KeywordType","value":"int32"},
114+
{"type":"Text","value":" "},
115+
{"type":"Punctuation","value":"|"},
116+
{"type":"Text","value":" "},
117+
{"type":"Punctuation","value":"~"},
118+
{"type":"KeywordType","value":"int64"},
119+
{"type":"Text","value":"\n"},
120+
{"type":"Punctuation","value":"}"},
121+
{"type":"Text","value":"\n"}
88122
]

0 commit comments

Comments
 (0)