1package g
  2
  3import (
  4	"strings"
  5
  6	. "github.com/alecthomas/chroma" // nolint
  7	"github.com/alecthomas/chroma/lexers/h"
  8	"github.com/alecthomas/chroma/lexers/internal"
  9)
 10
 11// Go lexer.
 12var Go = internal.Register(MustNewLexer(
 13	&Config{
 14		Name:      "Go",
 15		Aliases:   []string{"go", "golang"},
 16		Filenames: []string{"*.go"},
 17		MimeTypes: []string{"text/x-gosrc"},
 18	},
 19	Rules{
 20		"root": {
 21			{`\n`, Text, nil},
 22			{`\s+`, Text, nil},
 23			{`\\\n`, Text, nil},
 24			{`//(.*?)\n`, CommentSingle, nil},
 25			{`/(\\\n)?[*](.|\n)*?[*](\\\n)?/`, CommentMultiline, nil},
 26			{`(import|package)\b`, KeywordNamespace, nil},
 27			{`(var|func|struct|map|chan|type|interface|const)\b`, KeywordDeclaration, nil},
 28			{Words(``, `\b`, `break`, `default`, `select`, `case`, `defer`, `go`, `else`, `goto`, `switch`, `fallthrough`, `if`, `range`, `continue`, `for`, `return`), Keyword, nil},
 29			{`(true|false|iota|nil)\b`, KeywordConstant, nil},
 30			{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},
 31			{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},
 32			{`\d+i`, LiteralNumber, nil},
 33			{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
 34			{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
 35			{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
 36			{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
 37			{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
 38			{`0[0-7]+`, LiteralNumberOct, nil},
 39			{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
 40			{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
 41			{`'(\\['"\\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},
 42			{"(`)([^`]*)(`)", ByGroups(LiteralString, Using(TypeRemappingLexer(GoTextTemplate, TypeMapping{{Other, LiteralString, nil}})), LiteralString), nil},
 43			{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
 44			{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
 45			{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
 46			{`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
 47			{`[^\W\d]\w*`, NameOther, nil},
 48		},
 49	},
 50).SetAnalyser(func(text string) float32 {
 51	if strings.Contains(text, "fmt.") && strings.Contains(text, "package ") {
 52		return 0.5
 53	}
 54	if strings.Contains(text, "package ") {
 55		return 0.1
 56	}
 57	return 0.0
 58}))
 59
 60var goTemplateRules = Rules{
 61	"root": {
 62		{`{{[-]?`, CommentPreproc, Push("template")},
 63		{`[^{]+`, Other, nil},
 64		{`{`, Other, nil},
 65	},
 66	"template": {
 67		{`[-]?}}`, CommentPreproc, Pop(1)},
 68		{`/\*.*?\*/`, Comment, nil},
 69		{`(?=}})`, CommentPreproc, Pop(1)}, // Terminate the pipeline
 70		{`\(`, Operator, Push("subexpression")},
 71		{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
 72		Include("expression"),
 73	},
 74	"subexpression": {
 75		{`\)`, Operator, Pop(1)},
 76		Include("expression"),
 77	},
 78	"expression": {
 79		{`\s+`, Whitespace, nil},
 80		{`\(`, Operator, Push("subexpression")},
 81		{`(range|if|else|while|with|template|end|true|false|nil|and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b`, Keyword, nil},
 82		{`\||:=`, Operator, nil},
 83		{`[$]?[^\W\d]\w*`, NameOther, nil},
 84		{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
 85		{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
 86		{`\d+i`, LiteralNumber, nil},
 87		{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
 88		{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
 89		{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
 90		{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
 91		{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
 92		{`0[0-7]+`, LiteralNumberOct, nil},
 93		{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
 94		{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
 95		{`'(\\['"\\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},
 96		{"`[^`]*`", LiteralString, nil},
 97	},
 98}
 99
100var GoHTMLTemplate = internal.Register(DelegatingLexer(h.HTML, MustNewLexer(
101	&Config{
102		Name:    "Go HTML Template",
103		Aliases: []string{"go-html-template"},
104	},
105	goTemplateRules,
106)))
107
108var GoTextTemplate = internal.Register(MustNewLexer(
109	&Config{
110		Name:    "Go Text Template",
111		Aliases: []string{"go-text-template"},
112	},
113	goTemplateRules,
114))