No Description

xtend.lua 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. -- Copyright (c)2014 Piotr Orzechowski [drzewo.org]. See LICENSE.
  2. -- Xtend LPeg lexer.
  3. local l = require('lexer')
  4. local token, word_match = l.token, l.word_match
  5. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  6. local M = {_NAME = 'xtend'}
  7. -- Whitespace.
  8. local ws = token(l.WHITESPACE, l.space^1)
  9. -- Comments.
  10. local line_comment = '//' * l.nonnewline_esc^0
  11. local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
  12. local comment = token(l.COMMENT, line_comment + block_comment)
  13. -- Strings.
  14. local sq_str = l.delimited_range("'", true)
  15. local dq_str = l.delimited_range('"', true)
  16. local string = token(l.STRING, sq_str + dq_str)
  17. -- Templates.
  18. local templ_str = "'''" * (l.any - P("'''"))^0 * P("'''")^-1
  19. local template = token('template', templ_str, true)
  20. -- Numbers.
  21. local small_suff = S('lL')
  22. local med_suff = P(S('bB') * S('iI'))
  23. local large_suff = S('dD') + S('fF') + P(S('bB') * S('dD'))
  24. local exp = S('eE') * l.digit^1
  25. local dec_inf = ('_' * l.digit^1)^0
  26. local hex_inf = ('_' * l.xdigit^1)^0
  27. local float_pref = l.digit^1 * '.' * l.digit^1
  28. local float_suff = exp^-1 * med_suff^-1 * large_suff^-1
  29. local dec = l.digit * dec_inf * (small_suff^-1 + float_suff)
  30. local hex = l.hex_num * hex_inf * P('#' * (small_suff + med_suff))^-1
  31. local float = float_pref * dec_inf * float_suff
  32. local number = token(l.NUMBER, float + hex + dec)
  33. -- Keywords.
  34. local keyword = token(l.KEYWORD, word_match{
  35. -- General.
  36. 'abstract', 'annotation', 'as', 'case', 'catch', 'class', 'create', 'def',
  37. 'default', 'dispatch', 'do', 'else', 'enum', 'extends', 'extension', 'final',
  38. 'finally', 'for', 'if', 'implements', 'import', 'interface', 'instanceof',
  39. 'it', 'new', 'override', 'package', 'private', 'protected', 'public',
  40. 'return', 'self', 'static', 'super', 'switch', 'synchronized', 'this',
  41. 'throw', 'throws', 'try', 'typeof', 'val', 'var', 'while',
  42. -- Templates.
  43. -- 'AFTER', 'BEFORE', 'ENDFOR', 'ENDIF', 'FOR', 'IF', 'SEPARATOR',
  44. -- Literals.
  45. 'true', 'false', 'null'
  46. })
  47. -- Types.
  48. local type = token(l.TYPE, word_match{
  49. 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void',
  50. 'Boolean', 'Byte', 'Character', 'Double', 'Float', 'Integer', 'Long', 'Short',
  51. 'String'
  52. })
  53. -- Identifiers.
  54. local identifier = token(l.IDENTIFIER, l.word)
  55. -- Operators.
  56. local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#'))
  57. -- Annotations.
  58. local annotation = token('annotation', '@' * l.word)
  59. -- Functions.
  60. local func = token(l.FUNCTION, l.word) * #P('(')
  61. -- Classes.
  62. local class = token(l.KEYWORD, P('class')) * ws^1 * token(l.CLASS, l.word)
  63. -- Rules.
  64. M._rules = {
  65. {'whitespace', ws},
  66. {'class', class},
  67. {'keyword', keyword},
  68. {'type', type},
  69. {'function', func},
  70. {'identifier', identifier},
  71. {'template', template},
  72. {'string', string},
  73. {'comment', comment},
  74. {'number', number},
  75. {'annotation', annotation},
  76. {'operator', operator},
  77. {'error', token(l.ERROR, l.any)},
  78. }
  79. -- Token styles.
  80. M._tokenstyles = {
  81. annotation = l.STYLE_PREPROCESSOR,
  82. template = l.STYLE_EMBEDDED
  83. }
  84. -- Folding.
  85. M._foldsymbols = {
  86. _patterns = {'[{}]', '/%*', '%*/', '//', 'import'},
  87. [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
  88. [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')},
  89. [l.KEYWORD] = {['import'] = l.fold_line_comments('import')}
  90. }
  91. return M