1package r
2
3import (
4	"strings"
5
6	. "github.com/alecthomas/chroma" // nolint
7	"github.com/alecthomas/chroma/lexers/internal"
8)
9
10// Restructuredtext lexer.
11var Restructuredtext = internal.Register(MustNewLexer(
12	&Config{
13		Name:      "reStructuredText",
14		Aliases:   []string{"rst", "rest", "restructuredtext"},
15		Filenames: []string{"*.rst", "*.rest"},
16		MimeTypes: []string{"text/x-rst", "text/prs.fallenstein.rst"},
17	},
18	Rules{
19		"root": {
20			{"^(=+|-+|`+|:+|\\.+|\\'+|\"+|~+|\\^+|_+|\\*+|\\++|#+)([ \\t]*\\n)(.+)(\\n)(\\1)(\\n)", ByGroups(GenericHeading, Text, GenericHeading, Text, GenericHeading, Text), nil},
21			{"^(\\S.*)(\\n)(={3,}|-{3,}|`{3,}|:{3,}|\\.{3,}|\\'{3,}|\"{3,}|~{3,}|\\^{3,}|_{3,}|\\*{3,}|\\+{3,}|#{3,})(\\n)", ByGroups(GenericHeading, Text, GenericHeading, Text), nil},
22			{`^(\s*)([-*+])( .+\n(?:\1  .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
23			{`^(\s*)([0-9#ivxlcmIVXLCM]+\.)( .+\n(?:\1  .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
24			{`^(\s*)(\(?[0-9#ivxlcmIVXLCM]+\))( .+\n(?:\1  .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
25			{`^(\s*)([A-Z]+\.)( .+\n(?:\1  .+\n)+)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
26			{`^(\s*)(\(?[A-Za-z]+\))( .+\n(?:\1  .+\n)+)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
27			{`^(\s*)(\|)( .+\n(?:\|  .+\n)*)`, ByGroups(Text, Operator, UsingSelf("inline")), nil},
28			{`^( *\.\.)(\s*)((?:source)?code(?:-block)?)(::)([ \t]*)([^\n]+)(\n[ \t]*\n)([ \t]+)(.*)(\n)((?:(?:\8.*|)\n)+)`, EmitterFunc(rstCodeBlock), nil},
29			{`^( *\.\.)(\s*)([\w:-]+?)(::)(?:([ \t]*)(.*))`, ByGroups(Punctuation, Text, OperatorWord, Punctuation, Text, UsingSelf("inline")), nil},
30			{`^( *\.\.)(\s*)(_(?:[^:\\]|\\.)+:)(.*?)$`, ByGroups(Punctuation, Text, NameTag, UsingSelf("inline")), nil},
31			{`^( *\.\.)(\s*)(\[.+\])(.*?)$`, ByGroups(Punctuation, Text, NameTag, UsingSelf("inline")), nil},
32			{`^( *\.\.)(\s*)(\|.+\|)(\s*)([\w:-]+?)(::)(?:([ \t]*)(.*))`, ByGroups(Punctuation, Text, NameTag, Text, OperatorWord, Punctuation, Text, UsingSelf("inline")), nil},
33			{`^ *\.\..*(\n( +.*\n|\n)+)?`, CommentPreproc, nil},
34			{`^( *)(:[a-zA-Z-]+:)(\s*)$`, ByGroups(Text, NameClass, Text), nil},
35			{`^( *)(:.*?:)([ \t]+)(.*?)$`, ByGroups(Text, NameClass, Text, NameFunction), nil},
36			{`^(\S.*(?<!::)\n)((?:(?: +.*)\n)+)`, ByGroups(UsingSelf("inline"), UsingSelf("inline")), nil},
37			{`(::)(\n[ \t]*\n)([ \t]+)(.*)(\n)((?:(?:\3.*|)\n)+)`, ByGroups(LiteralStringEscape, Text, LiteralString, LiteralString, Text, LiteralString), nil},
38			Include("inline"),
39		},
40		"inline": {
41			{`\\.`, Text, nil},
42			{"``", LiteralString, Push("literal")},
43			{"(`.+?)(<.+?>)(`__?)", ByGroups(LiteralString, LiteralStringInterpol, LiteralString), nil},
44			{"`.+?`__?", LiteralString, nil},
45			{"(`.+?`)(:[a-zA-Z0-9:-]+?:)?", ByGroups(NameVariable, NameAttribute), nil},
46			{"(:[a-zA-Z0-9:-]+?:)(`.+?`)", ByGroups(NameAttribute, NameVariable), nil},
47			{`\*\*.+?\*\*`, GenericStrong, nil},
48			{`\*.+?\*`, GenericEmph, nil},
49			{`\[.*?\]_`, LiteralString, nil},
50			{`<.+?>`, NameTag, nil},
51			{"[^\\\\\\n\\[*`:]+", Text, nil},
52			{`.`, Text, nil},
53		},
54		"literal": {
55			{"[^`]+", LiteralString, nil},
56			{"``((?=$)|(?=[-/:.,; \\n\\x00\\\u2010\\\u2011\\\u2012\\\u2013\\\u2014\\\u00a0\\'\\\"\\)\\]\\}\\>\\\u2019\\\u201d\\\u00bb\\!\\?]))", LiteralString, Pop(1)},
57			{"`", LiteralString, nil},
58		},
59	},
60))
61
62func rstCodeBlock(groups []string, lexer Lexer) Iterator {
63	iterators := []Iterator{}
64	tokens := []Token{
65		{Punctuation, groups[1]},
66		{Text, groups[2]},
67		{OperatorWord, groups[3]},
68		{Punctuation, groups[4]},
69		{Text, groups[5]},
70		{Keyword, groups[6]},
71		{Text, groups[7]},
72	}
73	code := strings.Join(groups[8:], "")
74	lexer = internal.Get(groups[6])
75	if lexer == nil {
76		tokens = append(tokens, Token{String, code})
77		iterators = append(iterators, Literator(tokens...))
78	} else {
79		sub, err := lexer.Tokenise(nil, code)
80		if err != nil {
81			panic(err)
82		}
83		iterators = append(iterators, Literator(tokens...), sub)
84	}
85	return Concaterator(iterators...)
86}
87