...

Source file src/github.com/alecthomas/chroma/lexers/r/r.go

Documentation: github.com/alecthomas/chroma/lexers/r

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

View as plain text