Skip to content

Commit

Permalink
fix(lexers/go): "~" is a valid token (#926)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abhinav committed Feb 12, 2024
1 parent f4788c0 commit 506e36f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lexers/go.go
Expand Up @@ -55,7 +55,7 @@ func goRules() Rules {
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
{`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
{`[|^<>=!()\[\]{}.,;:~]`, Punctuation, nil},
{`[^\W\d]\w*`, NameOther, nil},
},
}
Expand Down
6 changes: 5 additions & 1 deletion lexers/testdata/go.actual
Expand Up @@ -15,4 +15,8 @@ func hello(a int) {
return func() int {
return i
}
} // One last thing
} // One last thing

type Int interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
36 changes: 35 additions & 1 deletion lexers/testdata/go.expected
Expand Up @@ -84,5 +84,39 @@
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// One last thing\n"}
{"type":"CommentSingle","value":"// One last thing\n"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"type"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Int"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"interface"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n\t"},
{"type":"Punctuation","value":"~"},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"~"},
{"type":"KeywordType","value":"int8"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"~"},
{"type":"KeywordType","value":"int16"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"~"},
{"type":"KeywordType","value":"int32"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"~"},
{"type":"KeywordType","value":"int64"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"}
]

0 comments on commit 506e36f

Please sign in to comment.