logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

bash.lua (5831B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Shell LPeg lexer.
  3. local lexer = lexer
  4. local P, S, B = lpeg.P, lpeg.S, lpeg.B
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Builtins.
  9. lex:add_rule('builtin',
  10. lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN)) * -P('='))
  11. -- Variable assignment.
  12. local assign = lex:tag(lexer.VARIABLE, lexer.word) * lex:tag(lexer.OPERATOR, '=')
  13. lex:add_rule('assign', lexer.starts_line(assign, true))
  14. -- Identifiers.
  15. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
  16. -- Strings.
  17. local sq_str = -B('\\') * lexer.range("'", false, false)
  18. local dq_str = -B('\\') * lexer.range('"')
  19. local heredoc = '<<' * P(function(input, index)
  20. local _, e, minus, _, delimiter = input:find('^(%-?)%s*(["\']?)([%w_]+)%2[^\n]*[\n\r\f;]+', index)
  21. if not delimiter then return nil end
  22. -- If the starting delimiter of a here-doc begins with "-", then spaces are allowed to come
  23. -- before the closing delimiter.
  24. _, e =
  25. input:find((minus == '' and '[\n\r\f]+' or '[\n\r\f]+[ \t]*') .. delimiter .. '%f[^%w_]', e)
  26. return e and e + 1 or #input + 1
  27. end)
  28. local ex_str = -B('\\') * '`'
  29. lex:add_rule('string',
  30. lex:tag(lexer.STRING, sq_str + dq_str + heredoc) + lex:tag(lexer.EMBEDDED, ex_str))
  31. -- Comments.
  32. lex:add_rule('comment', lex:tag(lexer.COMMENT, -B('\\') * lexer.to_eol('#')))
  33. -- Numbers.
  34. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
  35. -- Variables.
  36. local builtin_var = lex:tag(lexer.OPERATOR, '$' * P('{')^-1) * lex:tag(lexer.VARIABLE_BUILTIN,
  37. lex:word_match(lexer.VARIABLE_BUILTIN) + S('!#?*@$-') * -lexer.alnum + lexer.digit^1)
  38. local var_ref = lex:tag(lexer.OPERATOR, '$' * ('{' * S('!#')^-1)^-1) *
  39. lex:tag(lexer.VARIABLE, lexer.word)
  40. local patt_expansion = lex:tag(lexer.DEFAULT, '/#' + '#' * P('#')^-1)
  41. lex:add_rule('variable', builtin_var + var_ref * patt_expansion^-1)
  42. -- Operators.
  43. local op = S('!<>&|;$()[]{}') + lpeg.B(lexer.space) * S('.:') * #lexer.space
  44. local function in_expr(constructs)
  45. return P(function(input, index)
  46. local line = input:sub(1, index):match('[^\r\n]*$')
  47. for k, v in pairs(constructs) do
  48. local s = line:find(k, 1, true)
  49. if not s then goto continue end
  50. local e = line:find(v, 1, true)
  51. if not e or e < s then return true end
  52. ::continue::
  53. end
  54. return nil
  55. end)
  56. end
  57. local file_op = '-' * (S('abcdefghkprstuwxGLNOS') + 'ef' + 'nt' + 'ot')
  58. local shell_op = '-o'
  59. local var_op = '-' * S('vR')
  60. local string_op = '-' * S('zn') + S('!=')^-1 * '=' + S('<>')
  61. local num_op = '-' * lexer.word_match('eq ne lt le gt ge')
  62. local in_cond_expr = in_expr{['[[ '] = ' ]]', ['[ '] = ' ]'}
  63. local conditional_op = (num_op + file_op + shell_op + var_op + string_op) * #lexer.space *
  64. in_cond_expr
  65. local in_arith_expr = in_expr{['(('] = '))'}
  66. local arith_op = (S('+!~*/%<>=&^|?:,') + '--' + '-' * #S(' \t')) * in_arith_expr
  67. -- TODO: performance is terrible on large files.
  68. -- lex:add_rule('operator', lex:tag(lexer.OPERATOR, op + conditional_op + arith_op))
  69. lex:add_rule('operator', lex:tag(lexer.OPERATOR, op))
  70. -- Flags/options.
  71. lex:add_rule('flag', lex:tag(lexer.DEFAULT, '-' * P('-')^-1 * lexer.word * ('-' * lexer.word)^0))
  72. -- Fold points.
  73. lex:add_fold_point(lexer.KEYWORD, 'if', 'fi')
  74. lex:add_fold_point(lexer.KEYWORD, 'case', 'esac')
  75. lex:add_fold_point(lexer.KEYWORD, 'do', 'done')
  76. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  77. -- Word lists.
  78. lex:set_word_list(lexer.KEYWORD, {
  79. 'if', 'then', 'elif', 'else', 'fi', 'time', 'for', 'in', 'until', 'while', 'do', 'done', 'case',
  80. 'esac', 'coproc', 'select', 'function'
  81. })
  82. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  83. -- Shell built-ins.
  84. 'break', 'cd', 'continue', 'eval', 'exec', 'exit', 'export', 'getopts', 'hash', 'pwd', 'readonly',
  85. 'return', 'shift', 'test', 'times', 'trap', 'umask', 'unset',
  86. -- Bash built-ins.
  87. 'alias', 'bind', 'builtin', 'caller', 'command', 'declare', 'echo', 'enable', 'help', 'let',
  88. 'local', 'logout', 'mapfile', 'printf', 'read', 'readarray', 'source', 'type', 'typeset',
  89. 'ulimit', 'unalias', --
  90. 'set', 'shopt', -- shell behavior
  91. 'dirs', 'popd', 'pushd', -- directory stack
  92. 'bg', 'fg', 'jobs', 'kill', 'wait', 'disown', 'suspend', -- job control
  93. 'fc', 'history' -- history
  94. })
  95. lex:set_word_list(lexer.VARIABLE_BUILTIN, {
  96. -- Shell built-ins.
  97. 'CDPATH', 'HOME', 'IFS', 'MAIL', 'MAILPATH', 'OPTARG', 'OPTIND', 'PATH', 'PS1', 'PS2',
  98. -- Bash built-ins.
  99. 'BASH', 'BASHOPTS', 'BASHPID', 'BASH_ALIASES', 'BASH_ARGC', 'BASH_ARGV', 'BASH_ARGV0',
  100. 'BASH_CMDS', 'BASH_COMMAND', 'BASH_COMPAT', 'BASH_ENV', 'BASH_EXECUTION_STRING', 'BASH_LINENO',
  101. 'BASH_LOADABLES_PATH', 'BASH_REMATCH', 'BASH_SOURCE', 'BASH_SUBSHELL', 'BASH_VERSINFO',
  102. 'BASH_VERSION', 'BASH_XTRACEFD', 'CHILD_MAX', 'COLUMNS', 'COMP_CWORD', 'COMP_LINE', 'COMP_POINT',
  103. 'COMP_TYPE', 'COMP_KEY', 'COMP_WORDBREAKS', 'COMP_WORDS', 'COMP_REPLY', 'COPROC', 'DIRSTACK',
  104. 'EMACS', 'ENV', 'EPOCHREALTIME', 'EPOCHSECONDS', 'EUID', 'EXECIGNORE', 'FCEDIT', 'FIGNORE',
  105. 'FUNCNAME', 'FUNCNEST', 'GLOBIGNORE', 'GROUPS', 'histchars', 'HISTCMD', 'HISTCONTROL', 'HISTFILE',
  106. 'HISTFILESIZE', 'HISTIGNORE', 'HISTSIZE', 'HISTTIMEFORMAT', 'HOSTFILE', 'HOSTNAME', 'HOSTTYPE',
  107. 'IGNOREEOF', 'INPUTRC', 'INSIDE_EMACS', 'LANG', 'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES',
  108. 'LC_NUMERIC', 'LC_TIME', 'LINENO', 'LINES', 'MACHTYPE', 'MAILCHECK', 'MAPFILE', 'OLDPWD',
  109. 'OPTERR', 'OSTYPE', 'PIPESTATUS', 'POSIXLY_CORRECT', 'PPID', 'PROMPT_COMMAND', 'PROMPT_DIRTRIM',
  110. 'PSO', 'PS3', 'PS4', 'PWD', 'RANDOM', 'READLINE_LINE', 'READLINE_MARK', 'READLINE_POINT', 'REPLY',
  111. 'SECONDS', 'SHELL', 'SHELLOPTS', 'SHLVL', 'SRANDOM', 'TIMEFORMAT', 'TMOUT', 'TMPDIR', 'UID',
  112. -- Job control.
  113. 'auto_resume'
  114. })
  115. lexer.property['scintillua.comment'] = '#'
  116. return lex