...

Source file src/github.com/alecthomas/chroma/lexers/n/nix.go

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

     1  package n
     2  
     3  import (
     4  	"strings"
     5  
     6  	. "github.com/alecthomas/chroma" // nolint
     7  	"github.com/alecthomas/chroma/lexers/internal"
     8  )
     9  
    10  // Nix lexer.
    11  var Nix = internal.Register(MustNewLazyLexer(
    12  	&Config{
    13  		Name:      "Nix",
    14  		Aliases:   []string{"nixos", "nix"},
    15  		Filenames: []string{"*.nix"},
    16  		MimeTypes: []string{"text/x-nix"},
    17  	},
    18  	nixRules,
    19  ))
    20  
    21  func nixRules() Rules {
    22  	// nixb matches right boundary of a nix word. Use it instead of \b.
    23  	const nixb = `(?![a-zA-Z0-9_'-])`
    24  
    25  	return Rules{
    26  		"root": {
    27  			Include("keywords"),
    28  			Include("builtins"),
    29  			// "./path" and ".float" literals have to be above "." operator
    30  			Include("literals"),
    31  			Include("operators"),
    32  			{`#.*$`, CommentSingle, nil},
    33  			{`/\*`, CommentMultiline, Push("comment")},
    34  			{`\(`, Punctuation, Push("paren")},
    35  			{`\[`, Punctuation, Push("list")},
    36  			{`"`, StringDouble, Push("qstring")},
    37  			{`''`, StringSingle, Push("istring")},
    38  			{`{`, Punctuation, Push("scope")},
    39  			{`let` + nixb, Keyword, Push("scope")},
    40  			Include("id"),
    41  			Include("space"),
    42  		},
    43  		"keywords": {
    44  			{`import` + nixb, KeywordNamespace, nil},
    45  			{Words(``, nixb, strings.Fields("rec inherit with if then else assert")...), Keyword, nil},
    46  		},
    47  		"builtins": {
    48  			{`throw` + nixb, NameException, nil},
    49  			{Words(``, nixb, strings.Fields("abort baseNameOf builtins currentTime dependencyClosure derivation dirOf fetchTarball filterSource getAttr getEnv hasAttr isNull map removeAttrs toString toXML")...), NameBuiltin, nil},
    50  		},
    51  		"literals": {
    52  			{Words(``, nixb, strings.Fields("true false null")...), NameConstant, nil},
    53  			Include("uri"),
    54  			Include("path"),
    55  			Include("int"),
    56  			Include("float"),
    57  		},
    58  		"operators": {
    59  			{` [/-] `, Operator, nil},
    60  			{`(\.)(\${)`, ByGroups(Operator, StringInterpol), Push("interpol")},
    61  			{`(\?)(\s*)(\${)`, ByGroups(Operator, Text, StringInterpol), Push("interpol")},
    62  			{Words(``, ``, strings.Fields("@ . ? ++ + != ! // == && || -> <= < >= > *")...), Operator, nil},
    63  			{`[;:]`, Punctuation, nil},
    64  		},
    65  		"comment": {
    66  			{`\*/`, CommentMultiline, Pop(1)},
    67  			{`.|\n`, CommentMultiline, nil},
    68  		},
    69  		"paren": {
    70  			{`\)`, Punctuation, Pop(1)},
    71  			Include("root"),
    72  		},
    73  		"list": {
    74  			{`\]`, Punctuation, Pop(1)},
    75  			Include("root"),
    76  		},
    77  		"qstring": {
    78  			{`"`, StringDouble, Pop(1)},
    79  			{`\${`, StringInterpol, Push("interpol")},
    80  			{`\\.`, StringEscape, nil},
    81  			{`.|\n`, StringDouble, nil},
    82  		},
    83  		"istring": {
    84  			{`''\$`, StringEscape, nil},  // "$"
    85  			{`'''`, StringEscape, nil},   // "''"
    86  			{`''\\.`, StringEscape, nil}, // "\."
    87  			{`''`, StringSingle, Pop(1)},
    88  			{`\${`, StringInterpol, Push("interpol")},
    89  			// The next rule is important: "$" escapes any symbol except "{"!
    90  			{`\$.`, StringSingle, nil}, // "$."
    91  			{`.|\n`, StringSingle, nil},
    92  		},
    93  		"scope": {
    94  			{`}:`, Punctuation, Pop(1)},
    95  			{`}`, Punctuation, Pop(1)},
    96  			{`in` + nixb, Keyword, Pop(1)},
    97  			{`\${`, StringInterpol, Push("interpol")},
    98  			Include("root"), // "==" has to be above "="
    99  			{Words(``, ``, strings.Fields("= ? ,")...), Operator, nil},
   100  		},
   101  		"interpol": {
   102  			{`}`, StringInterpol, Pop(1)},
   103  			Include("root"),
   104  		},
   105  		"id": {
   106  			{`[a-zA-Z_][a-zA-Z0-9_'-]*`, Name, nil},
   107  		},
   108  		"uri": {
   109  			{`[a-zA-Z][a-zA-Z0-9+.-]*:[a-zA-Z0-9%/?:@&=+$,_.!~*'-]+`, StringDoc, nil},
   110  		},
   111  		"path": {
   112  			{`[a-zA-Z0-9._+-]*(/[a-zA-Z0-9._+-]+)+`, StringRegex, nil},
   113  			{`~(/[a-zA-Z0-9._+-]+)+/?`, StringRegex, nil},
   114  			{`<[a-zA-Z0-9._+-]+(/[a-zA-Z0-9._+-]+)*>`, StringRegex, nil},
   115  		},
   116  		"int": {
   117  			{`-?[0-9]+` + nixb, NumberInteger, nil},
   118  		},
   119  		"float": {
   120  			{`-?(([1-9][0-9]*\.[0-9]*)|(0?\.[0-9]+))([Ee][+-]?[0-9]+)?` + nixb, NumberFloat, nil},
   121  		},
   122  		"space": {
   123  			{`[ \t\r\n]+`, Text, nil},
   124  		},
   125  	}
   126  }
   127  

View as plain text