...

Source file src/github.com/alecthomas/chroma/lexers/b/bash.go

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

     1  package b
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "github.com/alecthomas/chroma" // nolint
     7  	"github.com/alecthomas/chroma/lexers/internal"
     8  )
     9  
    10  // TODO(moorereason): can this be factored away?
    11  var bashAnalyserRe = regexp.MustCompile(`(?m)^#!.*/bin/(?:env |)(?:bash|zsh|sh|ksh)`)
    12  
    13  // Bash lexer.
    14  var Bash = internal.Register(MustNewLazyLexer(
    15  	&Config{
    16  		Name:      "Bash",
    17  		Aliases:   []string{"bash", "sh", "ksh", "zsh", "shell"},
    18  		Filenames: []string{"*.sh", "*.ksh", "*.bash", "*.ebuild", "*.eclass", ".env", "*.env", "*.exheres-0", "*.exlib", "*.zsh", "*.zshrc", ".bashrc", "bashrc", ".bash_*", "bash_*", "zshrc", ".zshrc", "PKGBUILD"},
    19  		MimeTypes: []string{"application/x-sh", "application/x-shellscript"},
    20  	},
    21  	bashRules,
    22  ).SetAnalyser(func(text string) float32 {
    23  	if bashAnalyserRe.FindString(text) != "" {
    24  		return 1.0
    25  	}
    26  	return 0.0
    27  }))
    28  
    29  func bashRules() Rules {
    30  	return Rules{
    31  		"root": {
    32  			Include("basic"),
    33  			{"`", LiteralStringBacktick, Push("backticks")},
    34  			Include("data"),
    35  			Include("interp"),
    36  		},
    37  		"interp": {
    38  			{`\$\(\(`, Keyword, Push("math")},
    39  			{`\$\(`, Keyword, Push("paren")},
    40  			{`\$\{#?`, LiteralStringInterpol, Push("curly")},
    41  			{`\$[a-zA-Z_]\w*`, NameVariable, nil},
    42  			{`\$(?:\d+|[#$?!_*@-])`, NameVariable, nil},
    43  			{`\$`, Text, nil},
    44  		},
    45  		"basic": {
    46  			{`\b(if|fi|else|while|do|done|for|then|return|function|case|select|continue|until|esac|elif)(\s*)\b`, ByGroups(Keyword, Text), nil},
    47  			{"\\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},
    48  			{`\A#!.+\n`, CommentPreproc, nil},
    49  			{`#.*(\S|$)`, CommentSingle, nil},
    50  			{`\\[\w\W]`, LiteralStringEscape, nil},
    51  			{`(\b\w+)(\s*)(\+?=)`, ByGroups(NameVariable, Text, Operator), nil},
    52  			{`[\[\]{}()=]`, Operator, nil},
    53  			{`<<<`, Operator, nil},
    54  			{`<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2`, LiteralString, nil},
    55  			{`&&|\|\|`, Operator, nil},
    56  		},
    57  		"data": {
    58  			{`(?s)\$?"(\\\\|\\[0-7]+|\\.|[^"\\$])*"`, LiteralStringDouble, nil},
    59  			{`"`, LiteralStringDouble, Push("string")},
    60  			{`(?s)\$'(\\\\|\\[0-7]+|\\.|[^'\\])*'`, LiteralStringSingle, nil},
    61  			{`(?s)'.*?'`, LiteralStringSingle, nil},
    62  			{`;`, Punctuation, nil},
    63  			{`&`, Punctuation, nil},
    64  			{`\|`, Punctuation, nil},
    65  			{`\s+`, Text, nil},
    66  			{`\d+(?= |$)`, LiteralNumber, nil},
    67  			{"[^=\\s\\[\\]{}()$\"\\'`\\\\<&|;]+", Text, nil},
    68  			{`<`, Text, nil},
    69  		},
    70  		"string": {
    71  			{`"`, LiteralStringDouble, Pop(1)},
    72  			{`(?s)(\\\\|\\[0-7]+|\\.|[^"\\$])+`, LiteralStringDouble, nil},
    73  			Include("interp"),
    74  		},
    75  		"curly": {
    76  			{`\}`, LiteralStringInterpol, Pop(1)},
    77  			{`:-`, Keyword, nil},
    78  			{`\w+`, NameVariable, nil},
    79  			{"[^}:\"\\'`$\\\\]+", Punctuation, nil},
    80  			{`:`, Punctuation, nil},
    81  			Include("root"),
    82  		},
    83  		"paren": {
    84  			{`\)`, Keyword, Pop(1)},
    85  			Include("root"),
    86  		},
    87  		"math": {
    88  			{`\)\)`, Keyword, Pop(1)},
    89  			{`[-+*/%^|&]|\*\*|\|\|`, Operator, nil},
    90  			{`\d+#\d+`, LiteralNumber, nil},
    91  			{`\d+#(?! )`, LiteralNumber, nil},
    92  			{`\d+`, LiteralNumber, nil},
    93  			Include("root"),
    94  		},
    95  		"backticks": {
    96  			{"`", LiteralStringBacktick, Pop(1)},
    97  			Include("root"),
    98  		},
    99  	}
   100  }
   101  

View as plain text