You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bash.go 3.1KB

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