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

rc.lua (1568B)


  1. -- Copyright 2017-2024 Michael Forney. See LICENSE.
  2. -- rc LPeg lexer.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, S = lpeg.P, lpeg.S
  6. local lex = lexer.new('rc')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Keywords.
  10. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  11. 'for', 'in', 'while', 'if', 'not', 'switch', 'case', 'fn', 'builtin', 'cd', 'eval', 'exec',
  12. 'exit', 'flag', 'rfork', 'shift', 'ulimit', 'umask', 'wait', 'whatis', '.', '~'
  13. }))
  14. -- Identifiers.
  15. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  16. -- Strings.
  17. local str = lexer.range("'", false, false)
  18. local heredoc = '<<' * P(function(input, index)
  19. local s, e, _, delimiter = input:find('[ \t]*(["\']?)([%w!"%%+,-./:?@_~]+)%1', index)
  20. if s == index and delimiter then
  21. delimiter = delimiter:gsub('[%%+-.?]', '%%%1')
  22. e = select(2, input:find('[\n\r]' .. delimiter .. '[\n\r]', e))
  23. return e and e + 1 or #input + 1
  24. end
  25. end)
  26. lex:add_rule('string', token(lexer.STRING, str + heredoc))
  27. -- Comments.
  28. lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
  29. -- Numbers.
  30. lex:add_rule('number', token(lexer.NUMBER, lexer.number))
  31. -- Variables.
  32. lex:add_rule('variable',
  33. token(lexer.VARIABLE, '$' * S('"#')^-1 * ('*' + lexer.digit^1 + lexer.word)))
  34. -- Operators.
  35. lex:add_rule('operator', token(lexer.OPERATOR, S('@`=!<>*&^|;?()[]{}') + '\\\n'))
  36. -- Fold points.
  37. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  38. lexer.property['scintillua.comment'] = '#'
  39. return lex