supercollider.py 3.6 KB

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