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