...

Source file src/github.com/alecthomas/chroma/lexers/s/scala.go

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

     1  package s
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/alecthomas/chroma" // nolint
     7  	"github.com/alecthomas/chroma/lexers/internal"
     8  )
     9  
    10  // Scala lexer.
    11  var Scala = internal.Register(MustNewLazyLexer(
    12  	&Config{
    13  		Name:      "Scala",
    14  		Aliases:   []string{"scala"},
    15  		Filenames: []string{"*.scala"},
    16  		MimeTypes: []string{"text/x-scala"},
    17  		DotAll:    true,
    18  	},
    19  	scalaRules,
    20  ))
    21  
    22  func scalaRules() Rules {
    23  	var (
    24  		scalaOp     = "[-~\\^\\*!%&\\\\<>\\|+=:/?@\xa6-\xa7\xa9\xac\xae\xb0-\xb1\xb6\xd7\xf7\u03f6\u0482\u0606-\u0608\u060e-\u060f\u06e9\u06fd-\u06fe\u07f6\u09fa\u0b70\u0bf3-\u0bf8\u0bfa\u0c7f\u0cf1-\u0cf2\u0d79\u0f01-\u0f03\u0f13-\u0f17\u0f1a-\u0f1f\u0f34\u0f36\u0f38\u0fbe-\u0fc5\u0fc7-\u0fcf\u109e-\u109f\u1360\u1390-\u1399\u1940\u19e0-\u19ff\u1b61-\u1b6a\u1b74-\u1b7c\u2044\u2052\u207a-\u207c\u208a-\u208c\u2100-\u2101\u2103-\u2106\u2108-\u2109\u2114\u2116-\u2118\u211e-\u2123\u2125\u2127\u2129\u212e\u213a-\u213b\u2140-\u2144\u214a-\u214d\u214f\u2190-\u2328\u232b-\u244a\u249c-\u24e9\u2500-\u2767\u2794-\u27c4\u27c7-\u27e5\u27f0-\u2982\u2999-\u29d7\u29dc-\u29fb\u29fe-\u2b54\u2ce5-\u2cea\u2e80-\u2ffb\u3004\u3012-\u3013\u3020\u3036-\u3037\u303e-\u303f\u3190-\u3191\u3196-\u319f\u31c0-\u31e3\u3200-\u321e\u322a-\u3250\u3260-\u327f\u328a-\u32b0\u32c0-\u33ff\u4dc0-\u4dff\ua490-\ua4c6\ua828-\ua82b\ufb29\ufdfd\ufe62\ufe64-\ufe66\uff0b\uff1c-\uff1e\uff5c\uff5e\uffe2\uffe4\uffe8-\uffee\ufffc-\ufffd]+"
    25  		scalaUpper  = `[\\$_\p{Lu}]`
    26  		scalaLetter = `[\\$_\p{L}]`
    27  		scalaIDRest = fmt.Sprintf(`%s(?:%s|[0-9])*(?:(?<=_)%s)?`, scalaLetter, scalaLetter, scalaOp)
    28  	)
    29  
    30  	return Rules{
    31  		"root": {
    32  			{`(class|trait|object)(\s+)`, ByGroups(Keyword, Text), Push("class")},
    33  			{`[^\S\n]+`, Text, nil},
    34  			{`//.*?\n`, CommentSingle, nil},
    35  			{`/\*`, CommentMultiline, Push("comment")},
    36  			{`@` + scalaIDRest, NameDecorator, nil},
    37  			{`(abstract|ca(?:se|tch)|d(?:ef|o)|e(?:lse|xtends)|f(?:inal(?:ly)?|or(?:Some)?)|i(?:f|mplicit)|lazy|match|new|override|pr(?:ivate|otected)|re(?:quires|turn)|s(?:ealed|uper)|t(?:h(?:is|row)|ry)|va[lr]|w(?:hile|ith)|yield)\b|(<[%:-]|=>|>:|[#=@_⇒←])(\b|(?=\s)|$)`, Keyword, nil},
    38  			{`:(?!` + scalaOp + `%s)`, Keyword, Push("type")},
    39  			{fmt.Sprintf("%s%s\\b", scalaUpper, scalaIDRest), NameClass, nil},
    40  			{`(true|false|null)\b`, KeywordConstant, nil},
    41  			{`(import|package)(\s+)`, ByGroups(Keyword, Text), Push("import")},
    42  			{`(type)(\s+)`, ByGroups(Keyword, Text), Push("type")},
    43  			{`""".*?"""(?!")`, LiteralString, nil},
    44  			{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
    45  			{`'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'`, LiteralStringChar, nil},
    46  			{"'" + scalaIDRest, TextSymbol, nil},
    47  			{`[fs]"""`, LiteralString, Push("interptriplestring")},
    48  			{`[fs]"`, LiteralString, Push("interpstring")},
    49  			{`raw"(\\\\|\\"|[^"])*"`, LiteralString, nil},
    50  			{scalaIDRest, Name, nil},
    51  			{"`[^`]+`", Name, nil},
    52  			{`\[`, Operator, Push("typeparam")},
    53  			{`[(){};,.#]`, Operator, nil},
    54  			{scalaOp, Operator, nil},
    55  			{`([0-9][0-9]*\.[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?[fFdD]?`, LiteralNumberFloat, nil},
    56  			{`0x[0-9a-fA-F]+`, LiteralNumberHex, nil},
    57  			{`[0-9]+L?`, LiteralNumberInteger, nil},
    58  			{`\n`, Text, nil},
    59  		},
    60  		"class": {
    61  			{fmt.Sprintf("(%s|%s|`[^`]+`)(\\s*)(\\[)", scalaIDRest, scalaOp), ByGroups(NameClass, Text, Operator), Push("typeparam")},
    62  			{`\s+`, Text, nil},
    63  			{`\{`, Operator, Pop(1)},
    64  			{`\(`, Operator, Pop(1)},
    65  			{`//.*?\n`, CommentSingle, Pop(1)},
    66  			{fmt.Sprintf("%s|%s|`[^`]+`", scalaIDRest, scalaOp), NameClass, Pop(1)},
    67  		},
    68  		"type": {
    69  			{`\s+`, Text, nil},
    70  			{`<[%:]|>:|[#_]|forSome|type`, Keyword, nil},
    71  			{`([,);}]|=>|=|⇒)(\s*)`, ByGroups(Operator, Text), Pop(1)},
    72  			{`[({]`, Operator, Push()},
    73  			{fmt.Sprintf("((?:%s|%s|`[^`]+`)(?:\\.(?:%s|%s|`[^`]+`))*)(\\s*)(\\[)", scalaIDRest, scalaOp, scalaIDRest, scalaOp), ByGroups(KeywordType, Text, Operator), Push("#pop", "typeparam")},
    74  			{fmt.Sprintf("((?:%s|%s|`[^`]+`)(?:\\.(?:%s|%s|`[^`]+`))*)(\\s*)$", scalaIDRest, scalaOp, scalaIDRest, scalaOp), ByGroups(KeywordType, Text), Pop(1)},
    75  			{`//.*?\n`, CommentSingle, Pop(1)},
    76  			{fmt.Sprintf("\\.|%s|%s|`[^`]+`", scalaIDRest, scalaOp), KeywordType, nil},
    77  		},
    78  		"typeparam": {
    79  			{`[\s,]+`, Text, nil},
    80  			{`<[%:]|=>|>:|[#_⇒]|forSome|type`, Keyword, nil},
    81  			{`([\])}])`, Operator, Pop(1)},
    82  			{`[(\[{]`, Operator, Push()},
    83  			{fmt.Sprintf("\\.|%s|%s|`[^`]+`", scalaIDRest, scalaOp), KeywordType, nil},
    84  		},
    85  		"comment": {
    86  			{`[^/*]+`, CommentMultiline, nil},
    87  			{`/\*`, CommentMultiline, Push()},
    88  			{`\*/`, CommentMultiline, Pop(1)},
    89  			{`[*/]`, CommentMultiline, nil},
    90  		},
    91  		"import": {
    92  			{fmt.Sprintf("(%s|\\.)+", scalaIDRest), NameNamespace, Pop(1)},
    93  		},
    94  		"interpstringcommon": {
    95  			{`[^"$\\]+`, LiteralString, nil},
    96  			{`\$\$`, LiteralString, nil},
    97  			{`\$` + scalaLetter + `(?:` + scalaLetter + `|\d)*`, LiteralStringInterpol, nil},
    98  			{`\$\{`, LiteralStringInterpol, Push("interpbrace")},
    99  			{`\\.`, LiteralString, nil},
   100  		},
   101  		"interptriplestring": {
   102  			{`"""(?!")`, LiteralString, Pop(1)},
   103  			{`"`, LiteralString, nil},
   104  			Include("interpstringcommon"),
   105  		},
   106  		"interpstring": {
   107  			{`"`, LiteralString, Pop(1)},
   108  			Include("interpstringcommon"),
   109  		},
   110  		"interpbrace": {
   111  			{`\}`, LiteralStringInterpol, Pop(1)},
   112  			{`\{`, LiteralStringInterpol, Push()},
   113  			Include("root"),
   114  		},
   115  	}
   116  }
   117  

View as plain text