mirror of
https://github.com/alecthomas/chroma.git
synced 2025-02-21 19:06:18 +02:00
This was done to speed up incremental compilation when working on lexers. That is, modifying a single lexer will no longer require recompiling all lexers. This is a (slightly) backwards breaking change in that lexers are no longer exported directly in the lexers package. The registry API is "aliased" at the old location.
123 lines
3.5 KiB
Go
123 lines
3.5 KiB
Go
package n
|
|
|
|
import (
|
|
"strings"
|
|
|
|
. "github.com/alecthomas/chroma" // nolint
|
|
"github.com/alecthomas/chroma/lexers/internal"
|
|
)
|
|
|
|
// nixb matches right boundary of a nix word. Use it instead of \b.
|
|
const nixb = `(?![a-zA-Z0-9_'-])`
|
|
|
|
// Nix lexer.
|
|
var Nix = internal.Register(MustNewLexer(
|
|
&Config{
|
|
Name: "Nix",
|
|
Aliases: []string{"nixos", "nix"},
|
|
Filenames: []string{"*.nix"},
|
|
MimeTypes: []string{"text/x-nix"},
|
|
},
|
|
Rules{
|
|
"root": {
|
|
Include("keywords"),
|
|
Include("builtins"),
|
|
// "./path" and ".float" literals have to be above "." operator
|
|
Include("literals"),
|
|
Include("operators"),
|
|
{`#.*$`, CommentSingle, nil},
|
|
{`/\*`, CommentMultiline, Push("comment")},
|
|
{`\(`, Punctuation, Push("paren")},
|
|
{`\[`, Punctuation, Push("list")},
|
|
{`"`, StringDouble, Push("qstring")},
|
|
{`''`, StringSingle, Push("istring")},
|
|
{`{`, Punctuation, Push("scope")},
|
|
{`let` + nixb, Keyword, Push("scope")},
|
|
Include("id"),
|
|
Include("space"),
|
|
},
|
|
"keywords": {
|
|
{`import` + nixb, KeywordNamespace, nil},
|
|
{Words(``, nixb, strings.Fields("rec inherit with if then else assert")...), Keyword, nil},
|
|
},
|
|
"builtins": {
|
|
{`throw` + nixb, NameException, nil},
|
|
{Words(``, nixb, strings.Fields("abort baseNameOf builtins currentTime dependencyClosure derivation dirOf fetchTarball filterSource getAttr getEnv hasAttr isNull map removeAttrs toString toXML")...), NameBuiltin, nil},
|
|
},
|
|
"literals": {
|
|
{Words(``, nixb, strings.Fields("true false null")...), NameConstant, nil},
|
|
Include("uri"),
|
|
Include("path"),
|
|
Include("int"),
|
|
Include("float"),
|
|
},
|
|
"operators": {
|
|
{` [/-] `, Operator, nil},
|
|
{`(\.)(\${)`, ByGroups(Operator, StringInterpol), Push("interpol")},
|
|
{`(\?)(\s*)(\${)`, ByGroups(Operator, Text, StringInterpol), Push("interpol")},
|
|
{Words(``, ``, strings.Fields("@ . ? ++ + != ! // == && || -> <= < >= > *")...), Operator, nil},
|
|
{`[;:]`, Punctuation, nil},
|
|
},
|
|
"comment": {
|
|
{`\*/`, CommentMultiline, Pop(1)},
|
|
{`.|\n`, CommentMultiline, nil},
|
|
},
|
|
"paren": {
|
|
{`\)`, Punctuation, Pop(1)},
|
|
Include("root"),
|
|
},
|
|
"list": {
|
|
{`\]`, Punctuation, Pop(1)},
|
|
Include("root"),
|
|
},
|
|
"qstring": {
|
|
{`"`, StringDouble, Pop(1)},
|
|
{`\${`, StringInterpol, Push("interpol")},
|
|
{`\\.`, StringEscape, nil},
|
|
{`.|\n`, StringDouble, nil},
|
|
},
|
|
"istring": {
|
|
{`''\$`, StringEscape, nil}, // "$"
|
|
{`'''`, StringEscape, nil}, // "''"
|
|
{`''\\.`, StringEscape, nil}, // "\."
|
|
{`''`, StringSingle, Pop(1)},
|
|
{`\${`, StringInterpol, Push("interpol")},
|
|
// The next rule is important: "$" escapes any symbol except "{"!
|
|
{`\$.`, StringSingle, nil}, // "$."
|
|
{`.|\n`, StringSingle, nil},
|
|
},
|
|
"scope": {
|
|
{`}:`, Punctuation, Pop(1)},
|
|
{`}`, Punctuation, Pop(1)},
|
|
{`in` + nixb, Keyword, Pop(1)},
|
|
{`\${`, StringInterpol, Push("interpol")},
|
|
Include("root"), // "==" has to be above "="
|
|
{Words(``, ``, strings.Fields("= ? ,")...), Operator, nil},
|
|
},
|
|
"interpol": {
|
|
{`}`, StringInterpol, Pop(1)},
|
|
Include("root"),
|
|
},
|
|
"id": {
|
|
{`[a-zA-Z_][a-zA-Z0-9_'-]*`, Name, nil},
|
|
},
|
|
"uri": {
|
|
{`[a-zA-Z][a-zA-Z0-9+.-]*:[a-zA-Z0-9%/?:@&=+$,_.!~*'-]+`, StringDoc, nil},
|
|
},
|
|
"path": {
|
|
{`[a-zA-Z0-9._+-]*(/[a-zA-Z0-9._+-]+)+`, StringRegex, nil},
|
|
{`~(/[a-zA-Z0-9._+-]+)+/?`, StringRegex, nil},
|
|
{`<[a-zA-Z0-9._+-]+(/[a-zA-Z0-9._+-]+)*>`, StringRegex, nil},
|
|
},
|
|
"int": {
|
|
{`-?[0-9]+` + nixb, NumberInteger, nil},
|
|
},
|
|
"float": {
|
|
{`-?(([1-9][0-9]*\.[0-9]*)|(0?\.[0-9]+))([Ee][+-]?[0-9]+)?` + nixb, NumberFloat, nil},
|
|
},
|
|
"space": {
|
|
{`[ \t\r\n]+`, Text, nil},
|
|
},
|
|
},
|
|
))
|