csound.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.csound
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for CSound languages.
  6. :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, bygroups, default, include, using, words
  11. from pygments.token import Comment, Keyword, Name, Number, Operator, Punctuation, \
  12. String, Text
  13. from pygments.lexers._csound_builtins import OPCODES
  14. from pygments.lexers.html import HtmlLexer
  15. from pygments.lexers.python import PythonLexer
  16. from pygments.lexers.scripting import LuaLexer
  17. __all__ = ['CsoundScoreLexer', 'CsoundOrchestraLexer', 'CsoundDocumentLexer']
  18. newline = (r'((?:(?:;|//).*)*)(\n)', bygroups(Comment.Single, Text))
  19. class CsoundLexer(RegexLexer):
  20. # Subclasses must define a 'single-line string' state.
  21. tokens = {
  22. 'whitespace': [
  23. (r'[ \t]+', Text),
  24. (r'\\\n', Text),
  25. (r'/[*](.|\n)*?[*]/', Comment.Multiline)
  26. ],
  27. 'macro call': [
  28. (r'(\$\w+\.?)(\()', bygroups(Comment.Preproc, Punctuation),
  29. 'function macro call'),
  30. (r'\$\w+(\.|\b)', Comment.Preproc)
  31. ],
  32. 'function macro call': [
  33. (r"((?:\\['\)]|[^'\)])+)(')", bygroups(Comment.Preproc, Punctuation)),
  34. (r"([^'\)]+)(\))", bygroups(Comment.Preproc, Punctuation), '#pop')
  35. ],
  36. 'whitespace or macro call': [
  37. include('whitespace'),
  38. include('macro call')
  39. ],
  40. 'preprocessor directives': [
  41. (r'#(e(nd(if)?|lse)|ifn?def|undef)\b|##', Comment.Preproc),
  42. (r'#include\b', Comment.Preproc, 'include'),
  43. (r'#[ \t]*define\b', Comment.Preproc, 'macro name'),
  44. (r'@+[ \t]*\d*', Comment.Preproc)
  45. ],
  46. 'include': [
  47. include('whitespace'),
  48. (r'"', String, 'single-line string')
  49. ],
  50. 'macro name': [
  51. include('whitespace'),
  52. (r'(\w+)(\()', bygroups(Comment.Preproc, Text),
  53. 'function macro argument list'),
  54. (r'\w+', Comment.Preproc, 'object macro definition after name')
  55. ],
  56. 'object macro definition after name': [
  57. include('whitespace'),
  58. (r'#', Punctuation, 'object macro replacement text')
  59. ],
  60. 'object macro replacement text': [
  61. (r'(\\#|[^#])+', Comment.Preproc),
  62. (r'#', Punctuation, '#pop:3')
  63. ],
  64. 'function macro argument list': [
  65. (r"(\w+)(['#])", bygroups(Comment.Preproc, Punctuation)),
  66. (r'(\w+)(\))', bygroups(Comment.Preproc, Punctuation),
  67. 'function macro definition after name')
  68. ],
  69. 'function macro definition after name': [
  70. (r'[ \t]+', Text),
  71. (r'#', Punctuation, 'function macro replacement text')
  72. ],
  73. 'function macro replacement text': [
  74. (r'(\\#|[^#])+', Comment.Preproc),
  75. (r'#', Punctuation, '#pop:4')
  76. ]
  77. }
  78. class CsoundScoreLexer(CsoundLexer):
  79. """
  80. For `Csound <http://csound.github.io>`_ scores.
  81. .. versionadded:: 2.1
  82. """
  83. name = 'Csound Score'
  84. aliases = ['csound-score', 'csound-sco']
  85. filenames = ['*.sco']
  86. tokens = {
  87. 'partial statement': [
  88. include('preprocessor directives'),
  89. (r'\d+e[+-]?\d+|(\d+\.\d*|\d*\.\d+)(e[+-]?\d+)?', Number.Float),
  90. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  91. (r'\d+', Number.Integer),
  92. (r'"', String, 'single-line string'),
  93. (r'[+\-*/%^!=<>|&#~.]', Operator),
  94. (r'[]()[]', Punctuation),
  95. (r'\w+', Comment.Preproc)
  96. ],
  97. 'statement': [
  98. include('whitespace or macro call'),
  99. newline + ('#pop',),
  100. include('partial statement')
  101. ],
  102. 'root': [
  103. newline,
  104. include('whitespace or macro call'),
  105. (r'[{}]', Punctuation, 'statement'),
  106. (r'[abefimq-tv-z]|[nN][pP]?', Keyword, 'statement')
  107. ],
  108. 'single-line string': [
  109. (r'"', String, '#pop'),
  110. (r'[^\\"]+', String)
  111. ]
  112. }
  113. class CsoundOrchestraLexer(CsoundLexer):
  114. """
  115. For `Csound <http://csound.github.io>`_ orchestras.
  116. .. versionadded:: 2.1
  117. """
  118. name = 'Csound Orchestra'
  119. aliases = ['csound', 'csound-orc']
  120. filenames = ['*.orc']
  121. user_defined_opcodes = set()
  122. def opcode_name_callback(lexer, match):
  123. opcode = match.group(0)
  124. lexer.user_defined_opcodes.add(opcode)
  125. yield match.start(), Name.Function, opcode
  126. def name_callback(lexer, match):
  127. name = match.group(0)
  128. if re.match('p\d+$', name) or name in OPCODES:
  129. yield match.start(), Name.Builtin, name
  130. elif name in lexer.user_defined_opcodes:
  131. yield match.start(), Name.Function, name
  132. else:
  133. nameMatch = re.search(r'^(g?[aikSw])(\w+)', name)
  134. if nameMatch:
  135. yield nameMatch.start(1), Keyword.Type, nameMatch.group(1)
  136. yield nameMatch.start(2), Name, nameMatch.group(2)
  137. else:
  138. yield match.start(), Name, name
  139. tokens = {
  140. 'label': [
  141. (r'\b(\w+)(:)', bygroups(Name.Label, Punctuation))
  142. ],
  143. 'partial expression': [
  144. include('preprocessor directives'),
  145. (r'\b(0dbfs|k(r|smps)|nchnls(_i)?|sr)\b', Name.Variable.Global),
  146. (r'\d+e[+-]?\d+|(\d+\.\d*|\d*\.\d+)(e[+-]?\d+)?', Number.Float),
  147. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  148. (r'\d+', Number.Integer),
  149. (r'"', String, 'single-line string'),
  150. (r'\{\{', String, 'multi-line string'),
  151. (r'[+\-*/%^!=&|<>#~¬]', Operator),
  152. (r'[](),?:[]', Punctuation),
  153. (words((
  154. # Keywords
  155. 'do', 'else', 'elseif', 'endif', 'enduntil', 'fi', 'if', 'ithen', 'kthen',
  156. 'od', 'then', 'until', 'while',
  157. # Opcodes that act as control structures
  158. 'return', 'timout'
  159. ), prefix=r'\b', suffix=r'\b'), Keyword),
  160. (words(('goto', 'igoto', 'kgoto', 'rigoto', 'tigoto'),
  161. prefix=r'\b', suffix=r'\b'), Keyword, 'goto label'),
  162. (words(('cggoto', 'cigoto', 'cingoto', 'ckgoto', 'cngoto'),
  163. prefix=r'\b', suffix=r'\b'), Keyword,
  164. ('goto label', 'goto expression')),
  165. (words(('loop_ge', 'loop_gt', 'loop_le', 'loop_lt'),
  166. prefix=r'\b', suffix=r'\b'), Keyword,
  167. ('goto label', 'goto expression', 'goto expression', 'goto expression')),
  168. (r'\bscoreline(_i)?\b', Name.Builtin, 'scoreline opcode'),
  169. (r'\bpyl?run[it]?\b', Name.Builtin, 'python opcode'),
  170. (r'\blua_(exec|opdef)\b', Name.Builtin, 'lua opcode'),
  171. (r'\b[a-zA-Z_]\w*\b', name_callback)
  172. ],
  173. 'expression': [
  174. include('whitespace or macro call'),
  175. newline + ('#pop',),
  176. include('partial expression')
  177. ],
  178. 'root': [
  179. newline,
  180. include('whitespace or macro call'),
  181. (r'\binstr\b', Keyword, ('instrument block', 'instrument name list')),
  182. (r'\bopcode\b', Keyword, ('opcode block', 'opcode parameter list',
  183. 'opcode types', 'opcode types', 'opcode name')),
  184. include('label'),
  185. default('expression')
  186. ],
  187. 'instrument name list': [
  188. include('whitespace or macro call'),
  189. (r'\d+|\+?[a-zA-Z_]\w*', Name.Function),
  190. (r',', Punctuation),
  191. newline + ('#pop',)
  192. ],
  193. 'instrument block': [
  194. newline,
  195. include('whitespace or macro call'),
  196. (r'\bendin\b', Keyword, '#pop'),
  197. include('label'),
  198. default('expression')
  199. ],
  200. 'opcode name': [
  201. include('whitespace or macro call'),
  202. (r'[a-zA-Z_]\w*', opcode_name_callback, '#pop')
  203. ],
  204. 'opcode types': [
  205. include('whitespace or macro call'),
  206. (r'0|[]afijkKoOpPStV[]+', Keyword.Type, '#pop'),
  207. (r',', Punctuation)
  208. ],
  209. 'opcode parameter list': [
  210. include('whitespace or macro call'),
  211. newline + ('#pop',)
  212. ],
  213. 'opcode block': [
  214. newline,
  215. include('whitespace or macro call'),
  216. (r'\bendop\b', Keyword, '#pop'),
  217. include('label'),
  218. default('expression')
  219. ],
  220. 'goto label': [
  221. include('whitespace or macro call'),
  222. (r'\w+', Name.Label, '#pop'),
  223. default('#pop')
  224. ],
  225. 'goto expression': [
  226. include('whitespace or macro call'),
  227. (r',', Punctuation, '#pop'),
  228. include('partial expression')
  229. ],
  230. 'single-line string': [
  231. include('macro call'),
  232. (r'"', String, '#pop'),
  233. # From https://github.com/csound/csound/blob/develop/Opcodes/fout.c#L1405
  234. (r'%\d*(\.\d+)?[cdhilouxX]', String.Interpol),
  235. (r'%[!%nNrRtT]|[~^]|\\([\\aAbBnNrRtT"]|[0-7]{1,3})', String.Escape),
  236. (r'[^\\"~$%\^\n]+', String),
  237. (r'[\\"~$%\^\n]', String)
  238. ],
  239. 'multi-line string': [
  240. (r'\}\}', String, '#pop'),
  241. (r'[^}]+|\}(?!\})', String)
  242. ],
  243. 'scoreline opcode': [
  244. include('whitespace or macro call'),
  245. (r'\{\{', String, 'scoreline'),
  246. default('#pop')
  247. ],
  248. 'scoreline': [
  249. (r'\}\}', String, '#pop'),
  250. (r'([^}]+)|\}(?!\})', using(CsoundScoreLexer))
  251. ],
  252. 'python opcode': [
  253. include('whitespace or macro call'),
  254. (r'\{\{', String, 'python'),
  255. default('#pop')
  256. ],
  257. 'python': [
  258. (r'\}\}', String, '#pop'),
  259. (r'([^}]+)|\}(?!\})', using(PythonLexer))
  260. ],
  261. 'lua opcode': [
  262. include('whitespace or macro call'),
  263. (r'"', String, 'single-line string'),
  264. (r'\{\{', String, 'lua'),
  265. (r',', Punctuation),
  266. default('#pop')
  267. ],
  268. 'lua': [
  269. (r'\}\}', String, '#pop'),
  270. (r'([^}]+)|\}(?!\})', using(LuaLexer))
  271. ]
  272. }
  273. class CsoundDocumentLexer(RegexLexer):
  274. """
  275. For `Csound <http://csound.github.io>`_ documents.
  276. .. versionadded:: 2.1
  277. """
  278. name = 'Csound Document'
  279. aliases = ['csound-document', 'csound-csd']
  280. filenames = ['*.csd']
  281. # These tokens are based on those in XmlLexer in pygments/lexers/html.py. Making
  282. # CsoundDocumentLexer a subclass of XmlLexer rather than RegexLexer may seem like a
  283. # better idea, since Csound Document files look like XML files. However, Csound
  284. # Documents can contain Csound comments (preceded by //, for example) before and
  285. # after the root element, unescaped bitwise AND & and less than < operators, etc. In
  286. # other words, while Csound Document files look like XML files, they may not actually
  287. # be XML files.
  288. tokens = {
  289. 'root': [
  290. newline,
  291. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  292. (r'[^<&;/]+', Text),
  293. (r'<\s*CsInstruments', Name.Tag, ('orchestra', 'tag')),
  294. (r'<\s*CsScore', Name.Tag, ('score', 'tag')),
  295. (r'<\s*[hH][tT][mM][lL]', Name.Tag, ('HTML', 'tag')),
  296. (r'<\s*[\w:.-]+', Name.Tag, 'tag'),
  297. (r'<\s*/\s*[\w:.-]+\s*>', Name.Tag)
  298. ],
  299. 'orchestra': [
  300. (r'<\s*/\s*CsInstruments\s*>', Name.Tag, '#pop'),
  301. (r'(.|\n)+?(?=<\s*/\s*CsInstruments\s*>)', using(CsoundOrchestraLexer))
  302. ],
  303. 'score': [
  304. (r'<\s*/\s*CsScore\s*>', Name.Tag, '#pop'),
  305. (r'(.|\n)+?(?=<\s*/\s*CsScore\s*>)', using(CsoundScoreLexer))
  306. ],
  307. 'HTML': [
  308. (r'<\s*/\s*[hH][tT][mM][lL]\s*>', Name.Tag, '#pop'),
  309. (r'(.|\n)+?(?=<\s*/\s*[hH][tT][mM][lL]\s*>)', using(HtmlLexer))
  310. ],
  311. 'tag': [
  312. (r'\s+', Text),
  313. (r'[\w.:-]+\s*=', Name.Attribute, 'attr'),
  314. (r'/?\s*>', Name.Tag, '#pop')
  315. ],
  316. 'attr': [
  317. (r'\s+', Text),
  318. (r'".*?"', String, '#pop'),
  319. (r"'.*?'", String, '#pop'),
  320. (r'[^\s>]+', String, '#pop')
  321. ]
  322. }