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

fantom.lua (2814B)


  1. -- Copyright 2018-2024 Simeon Maryasin (MarSoft). See LICENSE.
  2. -- Fantom LPeg lexer.
  3. -- Based on Java LPeg lexer by Mitchell and Vim's Fantom syntax.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local P, S = lpeg.P, lpeg.S
  7. local lex = lexer.new('fantom')
  8. -- Whitespace.
  9. local ws = token(lexer.WHITESPACE, lexer.space^1)
  10. lex:add_rule('whitespace', ws)
  11. -- Classes.
  12. local type = token(lexer.TYPE, lexer.word)
  13. lex:add_rule('class_sequence',
  14. token(lexer.KEYWORD, 'class') * ws * type * ( -- at most one inheritance spec
  15. ws * token(lexer.OPERATOR, ':') * ws * type *
  16. ( -- at least 0 (i.e. any number) of additional classes
  17. ws^-1 * token(lexer.OPERATOR, ',') * ws^-1 * type)^0)^-1)
  18. -- Keywords.
  19. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  20. 'using', 'native', -- external
  21. 'goto', 'void', 'serializable', 'volatile', -- error
  22. 'if', 'else', 'switch', -- conditional
  23. 'do', 'while', 'for', 'foreach', 'each', -- repeat
  24. 'true', 'false', -- boolean
  25. 'null', -- constant
  26. 'this', 'super', -- typedef
  27. 'new', 'is', 'isnot', 'as', -- operator
  28. 'plus', 'minus', 'mult', 'div', 'mod', 'get', 'set', 'slice', 'lshift', 'rshift', 'and', 'or',
  29. 'xor', 'inverse', 'negate', --
  30. 'increment', 'decrement', 'equals', 'compare', -- long operator
  31. 'return', -- stmt
  32. 'static', 'const', 'final', -- storage class
  33. 'virtual', 'override', 'once', -- slot
  34. 'readonly', -- field
  35. 'throw', 'try', 'catch', 'finally', -- exceptions
  36. 'assert', -- assert
  37. 'class', 'enum', 'mixin', -- typedef
  38. 'break', 'continue', -- branch
  39. 'default', 'case', -- labels
  40. 'public', 'internal', 'protected', 'private', 'abstract' -- scope decl
  41. }))
  42. -- Types.
  43. lex:add_rule('type', token(lexer.TYPE, word_match(
  44. 'Void Bool Int Float Decimal Str Duration Uri Type Range List Map Obj Err Env')))
  45. -- Functions.
  46. -- lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
  47. -- Identifiers.
  48. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  49. -- Strings.
  50. local sq_str = lexer.range("'", true)
  51. local dq_str = lexer.range('"', true)
  52. local bq_str = lexer.range('`', true)
  53. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + bq_str))
  54. -- Comments.
  55. local line_comment = lexer.to_eol('//', true)
  56. local block_comment = lexer.range('/*', '*/')
  57. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  58. -- Numbers.
  59. lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlFfDd')^-1))
  60. -- Operators.
  61. lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#')))
  62. -- Annotations.
  63. lex:add_rule('facet', token(lexer.ANNOTATION, '@' * lexer.word))
  64. -- Fold points.
  65. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  66. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  67. lexer.property['scintillua.comment'] = '//'
  68. return lex