powershell.go

 1package p
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Powershell lexer.
 9var Powershell = internal.Register(MustNewLexer(
10	&Config{
11		Name:            "PowerShell",
12		Aliases:         []string{"powershell", "posh", "ps1", "psm1"},
13		Filenames:       []string{"*.ps1", "*.psm1"},
14		MimeTypes:       []string{"text/x-powershell"},
15		DotAll:          true,
16		CaseInsensitive: true,
17	},
18	Rules{
19		"root": {
20			{`\(`, Punctuation, Push("child")},
21			{`\s+`, Text, nil},
22			{`^(\s*#[#\s]*)(\.(?:component|description|example|externalhelp|forwardhelpcategory|forwardhelptargetname|functionality|inputs|link|notes|outputs|parameter|remotehelprunspace|role|synopsis))([^\n]*$)`, ByGroups(Comment, LiteralStringDoc, Comment), nil},
23			{`#[^\n]*?$`, Comment, nil},
24			{`(&lt;|<)#`, CommentMultiline, Push("multline")},
25			{`@"\n`, LiteralStringHeredoc, Push("heredoc-double")},
26			{`@'\n.*?\n'@`, LiteralStringHeredoc, nil},
27			{"`[\\'\"$@-]", Punctuation, nil},
28			{`"`, LiteralStringDouble, Push("string")},
29			{`'([^']|'')*'`, LiteralStringSingle, nil},
30			{`(\$|@@|@)((global|script|private|env):)?\w+`, NameVariable, nil},
31			{`(while|validateset|validaterange|validatepattern|validatelength|validatecount|until|trap|switch|return|ref|process|param|parameter|in|if|global:|function|foreach|for|finally|filter|end|elseif|else|dynamicparam|do|default|continue|cmdletbinding|break|begin|alias|\?|%|#script|#private|#local|#global|mandatory|parametersetname|position|valuefrompipeline|valuefrompipelinebypropertyname|valuefromremainingarguments|helpmessage|try|catch|throw)\b`, Keyword, nil},
32			{`-(and|as|band|bnot|bor|bxor|casesensitive|ccontains|ceq|cge|cgt|cle|clike|clt|cmatch|cne|cnotcontains|cnotlike|cnotmatch|contains|creplace|eq|exact|f|file|ge|gt|icontains|ieq|ige|igt|ile|ilike|ilt|imatch|ine|inotcontains|inotlike|inotmatch|ireplace|is|isnot|le|like|lt|match|ne|not|notcontains|notlike|notmatch|or|regex|replace|wildcard)\b`, Operator, nil},
33			{`(write|where|wait|use|update|unregister|undo|trace|test|tee|take|suspend|stop|start|split|sort|skip|show|set|send|select|scroll|resume|restore|restart|resolve|resize|reset|rename|remove|register|receive|read|push|pop|ping|out|new|move|measure|limit|join|invoke|import|group|get|format|foreach|export|expand|exit|enter|enable|disconnect|disable|debug|cxnew|copy|convertto|convertfrom|convert|connect|complete|compare|clear|checkpoint|aggregate|add)-[a-z_]\w*\b`, NameBuiltin, nil},
34			{"\\[[a-z_\\[][\\w. `,\\[\\]]*\\]", NameConstant, nil},
35			{`-[a-z_]\w*`, Name, nil},
36			{`\w+`, Name, nil},
37			{"[.,;@{}\\[\\]$()=+*/\\\\&%!~?^`|<>-]|::", Punctuation, nil},
38		},
39		"child": {
40			{`\)`, Punctuation, Pop(1)},
41			Include("root"),
42		},
43		"multline": {
44			{`[^#&.]+`, CommentMultiline, nil},
45			{`#(>|&gt;)`, CommentMultiline, Pop(1)},
46			{`\.(component|description|example|externalhelp|forwardhelpcategory|forwardhelptargetname|functionality|inputs|link|notes|outputs|parameter|remotehelprunspace|role|synopsis)`, LiteralStringDoc, nil},
47			{`[#&.]`, CommentMultiline, nil},
48		},
49		"string": {
50			{"`[0abfnrtv'\\\"$`]", LiteralStringEscape, nil},
51			{"[^$`\"]+", LiteralStringDouble, nil},
52			{`\$\(`, Punctuation, Push("child")},
53			{`""`, LiteralStringDouble, nil},
54			{"[`$]", LiteralStringDouble, nil},
55			{`"`, LiteralStringDouble, Pop(1)},
56		},
57		"heredoc-double": {
58			{`\n"@`, LiteralStringHeredoc, Pop(1)},
59			{`\$\(`, Punctuation, Push("child")},
60			{`[^@\n]+"]`, LiteralStringHeredoc, nil},
61			{`.`, LiteralStringHeredoc, nil},
62		},
63	},
64))