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

xs.lua (2004B)


  1. -- Copyright 2017-2024 David B. Lamkins. See LICENSE.
  2. -- xs LPeg lexer.
  3. -- Adapted from rc lexer by Michael Forney.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local P, S = lpeg.P, lpeg.S
  7. local lex = lexer.new('xs')
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Keywords.
  11. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  12. 'access', 'alias', 'catch', 'cd', 'dirs', 'echo', 'else', 'escape', 'eval', 'exec', 'exit',
  13. 'false', 'fn-', 'fn', 'for', 'forever', 'fork', 'history', 'if', 'jobs', 'let', 'limit', 'local',
  14. 'map', 'omap', 'popd', 'printf', 'pushd', 'read', 'result', 'set-', 'switch', 'throw', 'time',
  15. 'true', 'umask', 'until', 'unwind-protect', 'var', 'vars', 'wait', 'whats', 'while', ':lt', ':le',
  16. ':gt', ':ge', ':eq', ':ne', '~', '~~', '...', '.'
  17. }))
  18. -- Identifiers.
  19. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  20. -- Strings.
  21. local str = lexer.range("'", false, true)
  22. local herestr = '<<<' * str
  23. local heredoc = '<<' * P(function(input, index)
  24. local s, e, _, delimiter = input:find('[ \t]*(["\']?)([%w!"%%+,-./:?@_~]+)%1', index)
  25. if s == index and delimiter then
  26. delimiter = delimiter:gsub('[%%+-.?]', '%%%1')
  27. e = select(2, input:find('[\n\r]' .. delimiter .. '[\n\r]', e))
  28. return e and e + 1 or #input + 1
  29. end
  30. end)
  31. lex:add_rule('string', token(lexer.STRING, str + herestr + heredoc))
  32. -- Comments.
  33. lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
  34. -- Numbers.
  35. -- lex:add_rule('number', token(lexer.NUMBER, lexer.number))
  36. -- Constants.
  37. lex:add_rule('constant', token(lexer.CONSTANT, '$&' * lexer.word))
  38. -- Variables.
  39. lex:add_rule('variable',
  40. token(lexer.VARIABLE, '$' * S('"#')^-1 * ('*' + lexer.digit^1 + lexer.word)))
  41. -- Operators.
  42. lex:add_rule('operator', token(lexer.OPERATOR, S('@`=!<>*&^|;?()[]{}') + '\\\n'))
  43. -- Fold points.
  44. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  45. lexer.property['scintillua.comment'] = '#'
  46. return lex