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

crystal.lua (3932B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Copyright 2017 Michel Martens.
  3. -- Crystal LPeg lexer (based on Ruby).
  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('crystal')
  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. 'alias', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else', 'elsif', 'end',
  13. 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', 'redo', 'rescue', 'retry',
  14. 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield',
  15. '__FILE__', '__LINE__'
  16. }))
  17. -- Functions.
  18. lex:add_rule('function', token(lexer.FUNCTION, word_match{
  19. 'abort', 'at_exit', 'caller', 'delay', 'exit', 'fork', 'future', 'get_stack_top', 'gets', 'lazy',
  20. 'loop', 'main', 'p', 'print', 'printf', 'puts', 'raise', 'rand', 'read_line', 'require', 'sleep',
  21. 'spawn', 'sprintf', 'system', 'with_color',
  22. -- Macros.
  23. 'assert_responds_to', 'debugger', 'parallel', 'pp', 'record', 'redefine_main'
  24. }) * -S('.:|'))
  25. -- Identifiers.
  26. local word_char = lexer.alnum + S('_!?')
  27. local word = (lexer.alpha + '_') * word_char^0
  28. lex:add_rule('identifier', token(lexer.IDENTIFIER, word))
  29. -- Comments.
  30. lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
  31. -- Strings.
  32. local cmd_str = lexer.range('`')
  33. local sq_str = lexer.range("'")
  34. local dq_str = lexer.range('"')
  35. local heredoc = '<<' * P(function(input, index)
  36. local _, e, indented, _, delimiter = input:find('^(%-?)(["`]?)([%a_][%w_]*)%2[\n\r\f;]+', index)
  37. if not delimiter then return end
  38. local end_heredoc = (#indented > 0 and '[\n\r\f]+ *' or '[\n\r\f]+')
  39. _, e = input:find(end_heredoc .. delimiter, e)
  40. return e and e + 1 or #input + 1
  41. end)
  42. local string = token(lexer.STRING, (sq_str + dq_str + heredoc + cmd_str) * S('f')^-1)
  43. -- TODO: regex_str fails with `obj.method /patt/` syntax.
  44. local regex_str = lexer.after_set('!%^&*([{-=+|:;,?<>~', lexer.range('/', true) * S('iomx')^0)
  45. local regex = token(lexer.REGEX, regex_str)
  46. lex:add_rule('string', string + regex)
  47. -- Numbers.
  48. local numeric_literal = '?' * (lexer.any - lexer.space) * -word_char -- TODO: meta, control, etc.
  49. lex:add_rule('number', token(lexer.NUMBER, lexer.number_('_') * S('ri')^-1 + numeric_literal))
  50. -- Variables.
  51. local global_var = '$' *
  52. (word + S('!@L+`\'=~/\\,.;<>_*"$?:') + lexer.digit + '-' * S('0FadiIKlpvw'))
  53. local class_var = '@@' * word
  54. local inst_var = '@' * word
  55. lex:add_rule('variable', token(lexer.VARIABLE, global_var + class_var + inst_var))
  56. -- Symbols.
  57. lex:add_rule('symbol', token('symbol', ':' * P(function(input, index)
  58. if input:sub(index - 2, index - 2) ~= ':' then return true end
  59. end) * (word_char^1 + sq_str + dq_str)))
  60. lex:add_style('symbol', lexer.styles.constant)
  61. -- Operators.
  62. lex:add_rule('operator', token(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~')))
  63. -- Fold points.
  64. local function disambiguate(text, pos, line, s)
  65. return line:sub(1, s - 1):match('^%s*$') and not text:sub(1, pos - 1):match('\\[ \t]*\r?\n$') and
  66. 1 or 0
  67. end
  68. lex:add_fold_point(lexer.KEYWORD, 'begin', 'end')
  69. lex:add_fold_point(lexer.KEYWORD, 'case', 'end')
  70. lex:add_fold_point(lexer.KEYWORD, 'class', 'end')
  71. lex:add_fold_point(lexer.KEYWORD, 'def', 'end')
  72. lex:add_fold_point(lexer.KEYWORD, 'do', 'end')
  73. lex:add_fold_point(lexer.KEYWORD, 'for', 'end')
  74. lex:add_fold_point(lexer.KEYWORD, 'module', 'end')
  75. lex:add_fold_point(lexer.KEYWORD, 'if', disambiguate)
  76. lex:add_fold_point(lexer.KEYWORD, 'while', disambiguate)
  77. lex:add_fold_point(lexer.KEYWORD, 'unless', disambiguate)
  78. lex:add_fold_point(lexer.KEYWORD, 'until', disambiguate)
  79. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  80. lex:add_fold_point(lexer.OPERATOR, '[', ']')
  81. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  82. lexer.property['scintillua.comment'] = '#'
  83. return lex