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

scala.lua (2026B)


  1. -- Copyright 2006-2024 JMS. See LICENSE.
  2. -- Scala 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('scala')
  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. 'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false', 'final', 'finally',
  15. 'for', 'forSome', 'if', 'implicit', 'import', 'lazy', 'match', 'new', 'null', 'object',
  16. 'override', 'package', 'private', 'protected', 'return', 'sealed', 'super', 'this', 'throw',
  17. 'trait', 'try', 'true', 'type', 'val', 'var', 'while', 'with', 'yield'
  18. }))
  19. -- Types.
  20. lex:add_rule('type', token(lexer.TYPE, word_match{
  21. 'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float', 'Int', 'Iterator',
  22. 'LinkedList', 'List', 'Long', 'Map', 'None', 'Option', 'Set', 'Short', 'SortedMap', 'SortedSet',
  23. 'String', 'TreeMap', 'TreeSet'
  24. }))
  25. -- Functions.
  26. lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
  27. -- Identifiers.
  28. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  29. -- Strings.
  30. local symbol = "'" * lexer.word
  31. local dq_str = lexer.range('"', true)
  32. local tq_str = lexer.range('"""')
  33. lex:add_rule('string', token(lexer.STRING, tq_str + symbol + dq_str))
  34. -- Comments.
  35. local line_comment = lexer.to_eol('//', true)
  36. local block_comment = lexer.range('/*', '*/')
  37. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  38. -- Numbers.
  39. lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlFfDd')^-1))
  40. -- Operators.
  41. lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')))
  42. -- Fold points.
  43. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  44. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  45. lexer.property['scintillua.comment'] = '//'
  46. return lex