berry.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. pygments.lexers.berry
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Berry.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words, include, bygroups
  9. from pygments.token import Comment, Whitespace, Operator, Keyword, Name, \
  10. String, Number, Punctuation
  11. __all__ = ['BerryLexer']
  12. class BerryLexer(RegexLexer):
  13. """
  14. For `berry <http://github.com/berry-lang/berry>`_ source code.
  15. .. versionadded:: 2.12.0
  16. """
  17. name = 'Berry'
  18. aliases = ['berry', 'be']
  19. filenames = ['*.be']
  20. mimetypes = ['text/x-berry', 'application/x-berry']
  21. _name = r'\b[^\W\d]\w*'
  22. tokens = {
  23. 'root': [
  24. include('whitespace'),
  25. include('numbers'),
  26. include('keywords'),
  27. (rf'(def)(\s+)({_name})',
  28. bygroups(Keyword.Declaration, Whitespace, Name.Function)),
  29. (rf'\b(class)(\s+)({_name})',
  30. bygroups(Keyword.Declaration, Whitespace, Name.Class)),
  31. (rf'\b(import)(\s+)({_name})',
  32. bygroups(Keyword.Namespace, Whitespace, Name.Namespace)),
  33. include('expr')
  34. ],
  35. 'expr': [
  36. (r'[^\S\n]+', Whitespace),
  37. (r'\.\.|[~!%^&*+=|?:<>/-]', Operator),
  38. (r'[(){}\[\],.;]', Punctuation),
  39. include('controls'),
  40. include('builtins'),
  41. include('funccall'),
  42. include('member'),
  43. include('name'),
  44. include('strings')
  45. ],
  46. 'whitespace': [
  47. (r'\s+', Whitespace),
  48. (r'#-(.|\n)*?-#', Comment.Multiline),
  49. (r'#.*?$', Comment.Single)
  50. ],
  51. 'keywords': [
  52. (words((
  53. 'as', 'break', 'continue', 'import', 'static', 'self', 'super'),
  54. suffix=r'\b'), Keyword.Reserved),
  55. (r'(true|false|nil)\b', Keyword.Constant),
  56. (r'(var|def)\b', Keyword.Declaration)
  57. ],
  58. 'controls': [
  59. (words((
  60. 'if', 'elif', 'else', 'for', 'while', 'do', 'end', 'break',
  61. 'continue', 'return', 'try', 'except', 'raise'),
  62. suffix=r'\b'), Keyword)
  63. ],
  64. 'builtins': [
  65. (words((
  66. 'assert', 'bool', 'input', 'classname', 'classof', 'number', 'real',
  67. 'bytes', 'compile', 'map', 'list', 'int', 'isinstance', 'print',
  68. 'range', 'str', 'super', 'module', 'size', 'issubclass', 'open',
  69. 'file', 'type', 'call'),
  70. suffix=r'\b'), Name.Builtin)
  71. ],
  72. 'numbers': [
  73. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  74. (r'-?\d+', Number.Integer),
  75. (r'(-?\d+\.?|\.\d)\d*([eE][+-]?\d+)?', Number.Float)
  76. ],
  77. 'name': [
  78. (_name, Name)
  79. ],
  80. 'funccall': [
  81. (rf'{_name}(?=\s*\()', Name.Function, '#pop')
  82. ],
  83. 'member': [
  84. (rf'(?<=\.){_name}\b(?!\()', Name.Attribute, '#pop')
  85. ],
  86. 'strings': [
  87. (r'"([^\\]|\\.)*?"', String.Double, '#pop'),
  88. (r'\'([^\\]|\\.)*?\'', String.Single, '#pop')
  89. ]
  90. }