bibtex.go

 1package b
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Bibtex lexer.
 9var Bibtex = internal.Register(MustNewLexer(
10	&Config{
11		Name:            "BibTeX",
12		Aliases:         []string{"bib", "bibtex"},
13		Filenames:       []string{"*.bib"},
14		MimeTypes:       []string{"text/x-bibtex"},
15		NotMultiline:    true,
16		CaseInsensitive: true,
17	},
18	Rules{
19		"root": {
20			Include("whitespace"),
21			{`@comment`, Comment, nil},
22			{`@preamble`, NameClass, Push("closing-brace", "value", "opening-brace")},
23			{`@string`, NameClass, Push("closing-brace", "field", "opening-brace")},
24			{"@[a-z_@!$&*+\\-./:;<>?\\[\\\\\\]^`|~][\\w@!$&*+\\-./:;<>?\\[\\\\\\]^`|~]*", NameClass, Push("closing-brace", "command-body", "opening-brace")},
25			{`.+`, Comment, nil},
26		},
27		"opening-brace": {
28			Include("whitespace"),
29			{`[{(]`, Punctuation, Pop(1)},
30		},
31		"closing-brace": {
32			Include("whitespace"),
33			{`[})]`, Punctuation, Pop(1)},
34		},
35		"command-body": {
36			Include("whitespace"),
37			{`[^\s\,\}]+`, NameLabel, Push("#pop", "fields")},
38		},
39		"fields": {
40			Include("whitespace"),
41			{`,`, Punctuation, Push("field")},
42			Default(Pop(1)),
43		},
44		"field": {
45			Include("whitespace"),
46			{"[a-z_@!$&*+\\-./:;<>?\\[\\\\\\]^`|~][\\w@!$&*+\\-./:;<>?\\[\\\\\\]^`|~]*", NameAttribute, Push("value", "=")},
47			Default(Pop(1)),
48		},
49		"=": {
50			Include("whitespace"),
51			{`=`, Punctuation, Pop(1)},
52		},
53		"value": {
54			Include("whitespace"),
55			{"[a-z_@!$&*+\\-./:;<>?\\[\\\\\\]^`|~][\\w@!$&*+\\-./:;<>?\\[\\\\\\]^`|~]*", NameVariable, nil},
56			{`"`, LiteralString, Push("quoted-string")},
57			{`\{`, LiteralString, Push("braced-string")},
58			{`[\d]+`, LiteralNumber, nil},
59			{`#`, Punctuation, nil},
60			Default(Pop(1)),
61		},
62		"quoted-string": {
63			{`\{`, LiteralString, Push("braced-string")},
64			{`"`, LiteralString, Pop(1)},
65			{`[^\{\"]+`, LiteralString, nil},
66		},
67		"braced-string": {
68			{`\{`, LiteralString, Push()},
69			{`\}`, LiteralString, Pop(1)},
70			{`[^\{\}]+`, LiteralString, nil},
71		},
72		"whitespace": {
73			{`\s+`, Text, nil},
74		},
75	},
76))