Skip to content
Permalink
Browse files

#170 Add lexer for systemd config files (#171)

  • Loading branch information...
Serranya authored and alecthomas committed Sep 18, 2018
1 parent e36baa2 commit c4123fc163de72aea0a1006c52b74d11799cc1cd
Showing with 66 additions and 0 deletions.
  1. +29 −0 lexers/s/systemd.go
  2. +11 −0 lexers/testdata/systemd.actual
  3. +26 −0 lexers/testdata/systemd.expected
@@ -0,0 +1,29 @@
package s

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)

var SYSTEMD = internal.Register(MustNewLexer(
&Config{
Name: "SYSTEMD",
Aliases: []string{"systemd"},
Filenames: []string{"*.service"},
MimeTypes: []string{"text/plain"},
},
Rules {
"root": {
{`\s+`, Text, nil},
{`[;#].*`, Comment, nil},
{`\[.*?\]$`, Keyword, nil},
{`(.*?)(=)(.*)(\\\n)`, ByGroups(NameAttribute, Operator, LiteralString, Text), Push("continuation")},
{`(.*?)(=)(.*)`, ByGroups(NameAttribute, Operator, LiteralString), nil},
},
"continuation": {
{`(.*?)(\\\n)`, ByGroups(LiteralString, Text), nil},
{`(.*)`, LiteralString, Pop(1)},
},
},
))

@@ -0,0 +1,11 @@
[Section A]
KeyOne=value 1
KeyTwo=value 2

# a comment

[Section B]
Setting="something" "some thing" "…"
KeyTwo=value 2 \
value 2 continued

@@ -0,0 +1,26 @@
[
{"type":"Keyword","value":"[Section A]"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyOne"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 1"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyTwo"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 2"},
{"type":"Text","value":"\n\n"},
{"type":"Comment","value":"# a comment"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"[Section B]"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"Setting"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"something\" \"some thing\" \"…\""},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyTwo"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 2 "},
{"type":"Text","value":"\\\n"},
{"type":"LiteralString","value":" value 2 continued"},
{"type":"Text","value":"\n\n"}
]

0 comments on commit c4123fc

Please sign in to comment.
You can’t perform that action at this time.