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

elixir.lua (4408B)


  1. -- Copyright 2015-2024 Mitchell. See LICENSE.
  2. -- Contributed by Richard Philips.
  3. -- Elixir LPeg lexer.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local B, P, S = lpeg.B, lpeg.P, lpeg.S
  7. local lex = lexer.new('elixir', {fold_by_indentation = true})
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Sigils.
  11. local sigil11 = '~' * S('CRSW') * lexer.range('<', '>')
  12. local sigil12 = '~' * S('CRSW') * lexer.range('{', '}')
  13. local sigil13 = '~' * S('CRSW') * lexer.range('[', ']')
  14. local sigil14 = '~' * S('CRSW') * lexer.range('(', ')')
  15. local sigil15 = '~' * S('CRSW') * lexer.range('|', false, false)
  16. local sigil16 = '~' * S('CRSW') * lexer.range('/', false, false)
  17. local sigil17 = '~' * S('CRSW') * lexer.range('"', false, false)
  18. local sigil18 = '~' * S('CRSW') * lexer.range("'", false, false)
  19. local sigil19 = '~' * S('CRSW') * lexer.range('"""')
  20. local sigil10 = '~' * S('CRSW') * lexer.range("'''")
  21. local sigil21 = '~' * S('crsw') * lexer.range('<', '>')
  22. local sigil22 = '~' * S('crsw') * lexer.range('{', '}')
  23. local sigil23 = '~' * S('crsw') * lexer.range('[', ']')
  24. local sigil24 = '~' * S('crsw') * lexer.range('(', ')')
  25. local sigil25 = '~' * S('crsw') * lexer.range('|')
  26. local sigil26 = '~' * S('crsw') * lexer.range('/')
  27. local sigil27 = '~' * S('crsw') * lexer.range('"')
  28. local sigil28 = '~' * S('crsw') * lexer.range("'")
  29. local sigil29 = '~' * S('crsw') * lexer.range('"""')
  30. local sigil20 = '~' * S('crsw') * lexer.range("'''")
  31. local sigil_token = token(lexer.REGEX,
  32. sigil10 + sigil19 + sigil11 + sigil12 + sigil13 + sigil14 + sigil15 + sigil16 + sigil17 + sigil18 +
  33. sigil20 + sigil29 + sigil21 + sigil22 + sigil23 + sigil24 + sigil25 + sigil26 + sigil27 +
  34. sigil28)
  35. local sigiladdon_token = token(lexer.EMBEDDED, lexer.alpha^0)
  36. lex:add_rule('sigil', sigil_token * sigiladdon_token)
  37. -- Atoms.
  38. local atom1 = B(1 - P(':')) * ':' * lexer.range('"')
  39. local atom2 = B(1 - P(':')) * ':' * lexer.alpha * (lexer.alnum + S('_@'))^0 * S('?!')^-1
  40. local atom3 = B(1 - (lexer.alnum + S('_:'))) * lexer.upper * (lexer.alnum + S('_@'))^0 * S('?!')^-1
  41. lex:add_rule('atom', token(lexer.CONSTANT, atom1 + atom2 + atom3))
  42. -- Strings.
  43. local dq_str = lexer.range('"')
  44. local triple_dq_str = lexer.range('"""')
  45. lex:add_rule('string', token(lexer.STRING, triple_dq_str + dq_str))
  46. -- Comments.
  47. lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
  48. -- Attributes.
  49. lex:add_rule('attribute', token(lexer.LABEL, B(1 - (lexer.alnum + '_')) * '@' * lexer.alpha *
  50. (lexer.alnum + '_')^0))
  51. -- Booleans.
  52. lex:add_rule('boolean', token(lexer.NUMBER, P(':')^-1 * word_match('true false nil')))
  53. -- Functions.
  54. lex:add_rule('function', token(lexer.FUNCTION, word_match{
  55. 'defstruct', 'defrecordp', 'defrecord', 'defprotocol', 'defp', 'defoverridable', 'defmodule',
  56. 'defmacrop', 'defmacro', 'defimpl', 'defexception', 'defdelegate', 'defcallback', 'def'
  57. }))
  58. -- Keywords.
  59. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  60. 'is_atom', 'is_binary', 'is_bitstring', 'is_boolean', 'is_float', 'is_function', 'is_integer',
  61. 'is_list', 'is_map', 'is_number', 'is_pid', 'is_port', 'is_record', 'is_reference', 'is_tuple',
  62. 'is_exception', 'case', 'when', 'cond', 'for', 'if', 'unless', 'try', 'receive', 'send', 'exit',
  63. 'raise', 'throw', 'after', 'rescue', 'catch', 'else', 'do', 'end', 'quote', 'unquote', 'super',
  64. 'import', 'require', 'alias', 'use', 'self', 'with', 'fn'
  65. }))
  66. -- Operators
  67. local operator1 = word_match('and or not when xor in')
  68. local operator2 = P('!==') + '!=' + '!' + '=~' + '===' + '==' + '=' + '<<<' + '<<' + '<=' + '<-' +
  69. '<' + '>>>' + '>>' + '>=' + '>' + '->' + '--' + '-' + '++' + '+' + '&&&' + '&&' + '&' + '|||' +
  70. '||' + '|>' + '|' + '..' + '.' + '^^^' + '^' + '\\\\' + '::' + '*' + '/' + '~~~' + '@'
  71. lex:add_rule('operator', token(lexer.OPERATOR, operator1 + operator2))
  72. -- Identifiers
  73. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word * S('?!')^-1))
  74. -- Numbers
  75. local dec = lexer.digit * (lexer.digit + '_')^0
  76. local bin = '0b' * S('01')^1
  77. local oct = '0o' * lpeg.R('07')^1
  78. local integer = bin + lexer.hex_num + oct + dec
  79. local float = lexer.digit^1 * '.' * lexer.digit^1 * S('eE') * (S('+-')^-1 * lexer.digit^1)^-1
  80. lex:add_rule('number',
  81. B(1 - (lexer.alpha + '_')) * S('+-')^-1 * token(lexer.NUMBER, float + integer))
  82. lexer.property['scintillua.comment'] = '#'
  83. return lex