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

xtend.lua (2934B)


  1. -- Copyright (c) 2014-2024 Piotr Orzechowski [drzewo.org]. See LICENSE.
  2. -- Xtend 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('xtend')
  7. -- Whitespace.
  8. local ws = token(lexer.WHITESPACE, lexer.space^1)
  9. lex:add_rule('whitespace', ws)
  10. -- Classes.
  11. lex:add_rule('class', token(lexer.KEYWORD, 'class') * ws^1 * token(lexer.CLASS, lexer.word))
  12. -- Keywords.
  13. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  14. -- General.
  15. 'abstract', 'annotation', 'as', 'case', 'catch', 'class', 'create', 'def', 'default', 'dispatch',
  16. 'do', 'else', 'enum', 'extends', 'extension', 'final', 'finally', 'for', 'if', 'implements',
  17. 'import', 'interface', 'instanceof', 'it', 'new', 'override', 'package', 'private', 'protected',
  18. 'public', 'return', 'self', 'static', 'super', 'switch', 'synchronized', 'this', 'throw',
  19. 'throws', 'try', 'typeof', 'val', 'var', 'while',
  20. -- Templates.
  21. 'AFTER', 'BEFORE', 'ENDFOR', 'ENDIF', 'FOR', 'IF', 'SEPARATOR',
  22. -- Literals.
  23. 'true', 'false', 'null'
  24. }))
  25. -- Types.
  26. lex:add_rule('type', token(lexer.TYPE, word_match{
  27. 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void', 'Boolean', 'Byte',
  28. 'Character', 'Double', 'Float', 'Integer', 'Long', 'Short', 'String'
  29. }))
  30. -- Functions.
  31. lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
  32. -- Identifiers.
  33. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  34. -- Templates.
  35. lex:add_rule('template', token(lexer.EMBEDDED, lexer.range("'''")))
  36. -- Strings.
  37. local sq_str = lexer.range("'", true)
  38. local dq_str = lexer.range('"', true)
  39. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
  40. -- Comments.
  41. local line_comment = lexer.to_eol('//', true)
  42. local block_comment = lexer.range('/*', '*/')
  43. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  44. -- Numbers.
  45. local small_suff = S('lL')
  46. local med_suff = S('bB') * S('iI')
  47. local large_suff = S('dD') + S('fF') + S('bB') * S('dD')
  48. local exp = S('eE') * lexer.digit^1
  49. local dec_inf = ('_' * lexer.digit^1)^0
  50. local hex_inf = ('_' * lexer.xdigit^1)^0
  51. local float_pref = lexer.digit^1 * '.' * lexer.digit^1
  52. local float_suff = exp^-1 * med_suff^-1 * large_suff^-1
  53. local dec = lexer.digit * dec_inf * (small_suff^-1 + float_suff)
  54. local hex = lexer.hex_num * hex_inf * P('#' * (small_suff + med_suff))^-1
  55. local float = float_pref * dec_inf * float_suff
  56. lex:add_rule('number', token(lexer.NUMBER, float + hex + dec))
  57. -- Annotations.
  58. lex:add_rule('annotation', token(lexer.ANNOTATION, '@' * lexer.word))
  59. -- Operators.
  60. lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#')))
  61. -- Error.
  62. lex:add_rule('error', token(lexer.ERROR, lexer.any))
  63. -- Fold points.
  64. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  65. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  66. lexer.property['scintillua.comment'] = '//'
  67. return lex