r.go

 1package r
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// R/S lexer.
 9var R = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "R",
12		Aliases:   []string{"splus", "s", "r"},
13		Filenames: []string{"*.S", "*.R", "*.r", ".Rhistory", ".Rprofile", ".Renviron"},
14		MimeTypes: []string{"text/S-plus", "text/S", "text/x-r-source", "text/x-r", "text/x-R", "text/x-r-history", "text/x-r-profile"},
15	},
16	Rules{
17		"comments": {
18			{`#.*$`, CommentSingle, nil},
19		},
20		"valid_name": {
21			{"(?:`[^`\\\\]*(?:\\\\.[^`\\\\]*)*`)|(?:(?:[a-zA-z]|[_.][^0-9])[\\w_.]*)", Name, nil},
22		},
23		"punctuation": {
24			{`\[{1,2}|\]{1,2}|\(|\)|;|,`, Punctuation, nil},
25		},
26		"keywords": {
27			{`(if|else|for|while|repeat|in|next|break|return|switch|function)(?![\w.])`, KeywordReserved, nil},
28		},
29		"operators": {
30			{`<<?-|->>?|-|==|<=|>=|<|>|&&?|!=|\|\|?|\?`, Operator, nil},
31			{`\*|\+|\^|/|!|%[^%]*%|=|~|\$|@|:{1,3}`, Operator, nil},
32		},
33		"builtin_symbols": {
34			{`(NULL|NA(_(integer|real|complex|character)_)?|letters|LETTERS|Inf|TRUE|FALSE|NaN|pi|\.\.(\.|[0-9]+))(?![\w.])`, KeywordConstant, nil},
35			{`(T|F)\b`, NameBuiltinPseudo, nil},
36		},
37		"numbers": {
38			{`0[xX][a-fA-F0-9]+([pP][0-9]+)?[Li]?`, LiteralNumberHex, nil},
39			{`[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)([eE][+-]?[0-9]+)?[Li]?`, LiteralNumber, nil},
40		},
41		"statements": {
42			Include("comments"),
43			{`\s+`, Text, nil},
44			{`\'`, LiteralString, Push("string_squote")},
45			{`\"`, LiteralString, Push("string_dquote")},
46			Include("builtin_symbols"),
47			Include("valid_name"),
48			Include("numbers"),
49			Include("keywords"),
50			Include("punctuation"),
51			Include("operators"),
52		},
53		"root": {
54			{"((?:`[^`\\\\]*(?:\\\\.[^`\\\\]*)*`)|(?:(?:[a-zA-z]|[_.][^0-9])[\\w_.]*))\\s*(?=\\()", NameFunction, nil},
55			Include("statements"),
56			{`\{|\}`, Punctuation, nil},
57			{`.`, Text, nil},
58		},
59		"string_squote": {
60			{`([^\'\\]|\\.)*\'`, LiteralString, Pop(1)},
61		},
62		"string_dquote": {
63			{`([^"\\]|\\.)*"`, LiteralString, Pop(1)},
64		},
65	},
66))