go.go

 1package lexers
 2
 3import (
 4	"strings"
 5
 6	. "github.com/alecthomas/chroma/v2" // nolint
 7)
 8
 9// Go lexer.
10var Go = Register(MustNewLexer(
11	&Config{
12		Name:      "Go",
13		Aliases:   []string{"go", "golang"},
14		Filenames: []string{"*.go"},
15		MimeTypes: []string{"text/x-gosrc"},
16	},
17	goRules,
18).SetAnalyser(func(text string) float32 {
19	if strings.Contains(text, "fmt.") && strings.Contains(text, "package ") {
20		return 0.5
21	}
22	if strings.Contains(text, "package ") {
23		return 0.1
24	}
25	return 0.0
26}))
27
28func goRules() Rules {
29	return Rules{
30		"root": {
31			{`\n`, Text, nil},
32			{`\s+`, Text, nil},
33			{`\\\n`, Text, nil},
34			{`//[^\n\r]*`, CommentSingle, nil},
35			{`/(\\\n)?[*](.|\n)*?[*](\\\n)?/`, CommentMultiline, nil},
36			{`(import|package)\b`, KeywordNamespace, nil},
37			{`(var|func|struct|map|chan|type|interface|const)\b`, KeywordDeclaration, nil},
38			{Words(``, `\b`, `break`, `default`, `select`, `case`, `defer`, `go`, `else`, `goto`, `switch`, `fallthrough`, `if`, `range`, `continue`, `for`, `return`), Keyword, nil},
39			{`(true|false|iota|nil)\b`, KeywordConstant, nil},
40			{Words(``, `\b(\()`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`, `print`, `println`, `panic`, `recover`, `close`, `complex`, `real`, `imag`, `len`, `cap`, `append`, `copy`, `delete`, `new`, `make`, `clear`, `min`, `max`), ByGroups(NameBuiltin, Punctuation), nil},
41			{Words(``, `\b`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`, `any`), KeywordType, nil},
42			{`\d+i`, LiteralNumber, nil},
43			{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
44			{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
45			{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
46			{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
47			{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
48			{`0[0-7]+`, LiteralNumberOct, nil},
49			{`0[xX][0-9a-fA-F_]+`, LiteralNumberHex, nil},
50			{`0b[01_]+`, LiteralNumberBin, nil},
51			{`(0|[1-9][0-9_]*)`, LiteralNumberInteger, nil},
52			{`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
53			{"(`)([^`]*)(`)", ByGroups(LiteralString, UsingLexer(TypeRemappingLexer(GoTextTemplate, TypeMapping{{Other, LiteralString, nil}})), LiteralString), nil},
54			{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
55			{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
56			{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
57			{`[|^<>=!()\[\]{}.,;:~]`, Punctuation, nil},
58			{`[^\W\d]\w*`, NameOther, nil},
59		},
60	}
61}
62
63var GoHTMLTemplate = Register(DelegatingLexer(HTML, MustNewXMLLexer(
64	embedded,
65	"embedded/go_template.xml",
66).SetConfig(
67	&Config{
68		Name:    "Go HTML Template",
69		Aliases: []string{"go-html-template"},
70	},
71)))
72
73var GoTextTemplate = Register(MustNewXMLLexer(
74	embedded,
75	"embedded/go_template.xml",
76).SetConfig(
77	&Config{
78		Name:    "Go Text Template",
79		Aliases: []string{"go-text-template"},
80	},
81))