wren.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. pygments.lexers.wren
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Wren.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import include, RegexLexer, words
  10. from pygments.token import Whitespace, Punctuation, Keyword, Name, Comment, \
  11. Operator, Number, String
  12. __all__ = ['WrenLexer']
  13. class WrenLexer(RegexLexer):
  14. """
  15. For Wren source code, version 0.4.0.
  16. """
  17. name = 'Wren'
  18. url = 'https://wren.io'
  19. aliases = ['wren']
  20. filenames = ['*.wren']
  21. version_added = '2.14'
  22. flags = re.MULTILINE | re.DOTALL
  23. tokens = {
  24. 'root': [
  25. # Whitespace.
  26. (r'\s+', Whitespace),
  27. (r'[,\\\[\]{}]', Punctuation),
  28. # Really 'root', not '#push': in 'interpolation',
  29. # parentheses inside the interpolation expression are
  30. # Punctuation, not String.Interpol.
  31. (r'\(', Punctuation, 'root'),
  32. (r'\)', Punctuation, '#pop'),
  33. # Keywords.
  34. (words((
  35. 'as', 'break', 'class', 'construct', 'continue', 'else',
  36. 'for', 'foreign', 'if', 'import', 'return', 'static', 'super',
  37. 'this', 'var', 'while'), prefix = r'(?<!\.)',
  38. suffix = r'\b'), Keyword),
  39. (words((
  40. 'true', 'false', 'null'), prefix = r'(?<!\.)',
  41. suffix = r'\b'), Keyword.Constant),
  42. (words((
  43. 'in', 'is'), prefix = r'(?<!\.)',
  44. suffix = r'\b'), Operator.Word),
  45. # Comments.
  46. (r'/\*', Comment.Multiline, 'comment'), # Multiline, can nest.
  47. (r'//.*?$', Comment.Single), # Single line.
  48. (r'#.*?(\(.*?\))?$', Comment.Special), # Attribute or shebang.
  49. # Names and operators.
  50. (r'[!%&*+\-./:<=>?\\^|~]+', Operator),
  51. (r'[a-z][a-zA-Z_0-9]*', Name),
  52. (r'[A-Z][a-zA-Z_0-9]*', Name.Class),
  53. (r'__[a-zA-Z_0-9]*', Name.Variable.Class),
  54. (r'_[a-zA-Z_0-9]*', Name.Variable.Instance),
  55. # Numbers.
  56. (r'0x[0-9a-fA-F]+', Number.Hex),
  57. (r'\d+(\.\d+)?([eE][-+]?\d+)?', Number.Float),
  58. # Strings.
  59. (r'""".*?"""', String), # Raw string
  60. (r'"', String, 'string'), # Other string
  61. ],
  62. 'comment': [
  63. (r'/\*', Comment.Multiline, '#push'),
  64. (r'\*/', Comment.Multiline, '#pop'),
  65. (r'([^*/]|\*(?!/)|/(?!\*))+', Comment.Multiline),
  66. ],
  67. 'string': [
  68. (r'"', String, '#pop'),
  69. (r'\\[\\%"0abefnrtv]', String.Escape), # Escape.
  70. (r'\\x[a-fA-F0-9]{2}', String.Escape), # Byte escape.
  71. (r'\\u[a-fA-F0-9]{4}', String.Escape), # Unicode escape.
  72. (r'\\U[a-fA-F0-9]{8}', String.Escape), # Long Unicode escape.
  73. (r'%\(', String.Interpol, 'interpolation'),
  74. (r'[^\\"%]+', String), # All remaining characters.
  75. ],
  76. 'interpolation': [
  77. # redefine closing paren to be String.Interpol
  78. (r'\)', String.Interpol, '#pop'),
  79. include('root'),
  80. ],
  81. }