1package b
2
3import (
4 "regexp"
5
6 . "github.com/alecthomas/chroma" // nolint
7 "github.com/alecthomas/chroma/lexers/internal"
8)
9
10var bashAnalyserRe = regexp.MustCompile(`(?m)^#!.*/bin/(?:env |)(?:bash|zsh|sh|ksh)`)
11
12// Bash lexer.
13var Bash = internal.Register(MustNewLexer(
14 &Config{
15 Name: "Bash",
16 Aliases: []string{"bash", "sh", "ksh", "zsh", "shell"},
17 Filenames: []string{"*.sh", "*.ksh", "*.bash", "*.ebuild", "*.eclass", "*.exheres-0", "*.exlib", "*.zsh", "*.zshrc", ".bashrc", "bashrc", ".bash_*", "bash_*", "zshrc", ".zshrc", "PKGBUILD"},
18 MimeTypes: []string{"application/x-sh", "application/x-shellscript"},
19 },
20 Rules{
21 "root": {
22 Include("basic"),
23 {"`", LiteralStringBacktick, Push("backticks")},
24 Include("data"),
25 Include("interp"),
26 },
27 "interp": {
28 {`\$\(\(`, Keyword, Push("math")},
29 {`\$\(`, Keyword, Push("paren")},
30 {`\$\{#?`, LiteralStringInterpol, Push("curly")},
31 {`\$[a-zA-Z_]\w*`, NameVariable, nil},
32 {`\$(?:\d+|[#$?!_*@-])`, NameVariable, nil},
33 {`\$`, Text, nil},
34 },
35 "basic": {
36 {`\b(if|fi|else|while|do|done|for|then|return|function|case|select|continue|until|esac|elif)(\s*)\b`, ByGroups(Keyword, Text), nil},
37 {"\\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|complete|declare|dirs|disown|echo|enable|eval|exec|exit|export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|shopt|source|suspend|test|time|times|trap|true|type|typeset|ulimit|umask|unalias|unset|wait)(?=[\\s)`])", NameBuiltin, nil},
38 {`\A#!.+\n`, CommentPreproc, nil},
39 {`#.*\S`, CommentSingle, nil},
40 {`\\[\w\W]`, LiteralStringEscape, nil},
41 {`(\b\w+)(\s*)(\+?=)`, ByGroups(NameVariable, Text, Operator), nil},
42 {`[\[\]{}()=]`, Operator, nil},
43 {`<<<`, Operator, nil},
44 {`<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2`, LiteralString, nil},
45 {`&&|\|\|`, Operator, nil},
46 },
47 "data": {
48 {`(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"`, LiteralStringDouble, nil},
49 {`"`, LiteralStringDouble, Push("string")},
50 {`(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'`, LiteralStringSingle, nil},
51 {`(?s)'.*?'`, LiteralStringSingle, nil},
52 {`;`, Punctuation, nil},
53 {`&`, Punctuation, nil},
54 {`\|`, Punctuation, nil},
55 {`\s+`, Text, nil},
56 {`\d+(?= |$)`, LiteralNumber, nil},
57 {"[^=\\s\\[\\]{}()$\"\\'`\\\\<&|;]+", Text, nil},
58 {`<`, Text, nil},
59 },
60 "string": {
61 {`"`, LiteralStringDouble, Pop(1)},
62 {`(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+`, LiteralStringDouble, nil},
63 Include("interp"),
64 },
65 "curly": {
66 {`\}`, LiteralStringInterpol, Pop(1)},
67 {`:-`, Keyword, nil},
68 {`\w+`, NameVariable, nil},
69 {"[^}:\"\\'`$\\\\]+", Punctuation, nil},
70 {`:`, Punctuation, nil},
71 Include("root"),
72 },
73 "paren": {
74 {`\)`, Keyword, Pop(1)},
75 Include("root"),
76 },
77 "math": {
78 {`\)\)`, Keyword, Pop(1)},
79 {`[-+*/%^|&]|\*\*|\|\|`, Operator, nil},
80 {`\d+#\d+`, LiteralNumber, nil},
81 {`\d+#(?! )`, LiteralNumber, nil},
82 {`\d+`, LiteralNumber, nil},
83 Include("root"),
84 },
85 "backticks": {
86 {"`", LiteralStringBacktick, Pop(1)},
87 Include("root"),
88 },
89 },
90).SetAnalyser(func(text string) float32 {
91 if bashAnalyserRe.FindString(text) != "" {
92 return 1.0
93 }
94 return 0.0
95}))