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

antlr.lua (1919B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- ANTLR 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('antlr')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Keywords.
  10. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  11. 'abstract', 'break', 'case', 'catch', 'continue', 'default', 'do', 'else', 'extends', 'final',
  12. 'finally', 'for', 'if', 'implements', 'instanceof', 'native', 'new', 'private', 'protected',
  13. 'public', 'return', 'static', 'switch', 'synchronized', 'throw', 'throws', 'transient', 'try',
  14. 'volatile', 'while', 'package', 'import', 'header', 'options', 'tokens', 'strictfp', 'false',
  15. 'null', 'super', 'this', 'true'
  16. }))
  17. -- Types.
  18. lex:add_rule('type', token(lexer.TYPE, word_match(
  19. 'boolean byte char class double float int interface long short void')))
  20. -- Functions.
  21. lex:add_rule('func', token(lexer.FUNCTION, 'assert'))
  22. -- Identifiers.
  23. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  24. -- Comments.
  25. local line_comment = lexer.to_eol('//')
  26. local block_comment = lexer.range('/*', '*/')
  27. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  28. -- Actions.
  29. local open_brace = token(lexer.OPERATOR, '{')
  30. local close_brace = token(lexer.OPERATOR, '}')
  31. lex:add_rule('action', open_brace * token('action', (1 - P('}'))^0) * close_brace^-1)
  32. lex:add_style('action', lexer.styles.nothing)
  33. -- Strings.
  34. lex:add_rule('string', token(lexer.STRING, lexer.range("'", true)))
  35. -- Operators.
  36. lex:add_rule('operator', token(lexer.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}')))
  37. -- Fold points.
  38. lex:add_fold_point(lexer.OPERATOR, ':', ';')
  39. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  40. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  41. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  42. lexer.property['scintillua.comment'] = '//'
  43. return lex