supercollider.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. pygments.lexers.supercollider
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for SuperCollider
  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 RegexLexer, include, words, default
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['SuperColliderLexer']
  13. class SuperColliderLexer(RegexLexer):
  14. """
  15. For SuperCollider source code.
  16. """
  17. name = 'SuperCollider'
  18. url = 'http://supercollider.github.io/'
  19. aliases = ['supercollider', 'sc']
  20. filenames = ['*.sc', '*.scd']
  21. mimetypes = ['application/supercollider', 'text/supercollider']
  22. version_added = '2.1'
  23. flags = re.DOTALL | re.MULTILINE
  24. tokens = {
  25. 'commentsandwhitespace': [
  26. (r'\s+', Text),
  27. (r'<!--', Comment),
  28. (r'//.*?\n', Comment.Single),
  29. (r'/\*.*?\*/', Comment.Multiline)
  30. ],
  31. 'slashstartsregex': [
  32. include('commentsandwhitespace'),
  33. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  34. r'([gim]+\b|\B)', String.Regex, '#pop'),
  35. (r'(?=/)', Text, ('#pop', 'badregex')),
  36. default('#pop'),
  37. ],
  38. 'badregex': [
  39. (r'\n', Text, '#pop')
  40. ],
  41. 'root': [
  42. (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
  43. include('commentsandwhitespace'),
  44. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  45. r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'),
  46. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  47. (r'[})\].]', Punctuation),
  48. (words((
  49. 'for', 'in', 'while', 'do', 'break', 'return', 'continue',
  50. 'switch', 'case', 'default', 'if', 'else', 'throw', 'try',
  51. 'catch', 'finally', 'new', 'delete', 'typeof', 'instanceof',
  52. 'void'), suffix=r'\b'),
  53. Keyword, 'slashstartsregex'),
  54. (words(('var', 'let', 'with', 'function', 'arg'), suffix=r'\b'),
  55. Keyword.Declaration, 'slashstartsregex'),
  56. (words((
  57. '(abstract', 'boolean', 'byte', 'char', 'class', 'const',
  58. 'debugger', 'double', 'enum', 'export', 'extends', 'final',
  59. 'float', 'goto', 'implements', 'import', 'int', 'interface',
  60. 'long', 'native', 'package', 'private', 'protected', 'public',
  61. 'short', 'static', 'super', 'synchronized', 'throws',
  62. 'transient', 'volatile'), suffix=r'\b'),
  63. Keyword.Reserved),
  64. (words(('true', 'false', 'nil', 'inf'), suffix=r'\b'), Keyword.Constant),
  65. (words((
  66. 'Array', 'Boolean', 'Date', 'Error', 'Function', 'Number',
  67. 'Object', 'Packages', 'RegExp', 'String',
  68. 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'super',
  69. 'thisFunctionDef', 'thisFunction', 'thisMethod', 'thisProcess',
  70. 'thisThread', 'this'), suffix=r'\b'),
  71. Name.Builtin),
  72. (r'[$a-zA-Z_]\w*', Name.Other),
  73. (r'\\?[$a-zA-Z_]\w*', String.Symbol),
  74. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  75. (r'0x[0-9a-fA-F]+', Number.Hex),
  76. (r'[0-9]+', Number.Integer),
  77. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  78. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  79. ]
  80. }
  81. def analyse_text(text):
  82. """We're searching for a common function and a unique keyword here."""
  83. if 'SinOsc' in text or 'thisFunctionDef' in text:
  84. return 0.1