codekingpro/portable-devtools
114k
1"""2 pygments.lexers.csound3 ~~~~~~~~~~~~~~~~~~~~~~4 5 Lexers for Csound languages.6 7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11import re12 13from pygments.lexer import RegexLexer, bygroups, default, include, using, words14from pygments.token import Comment, Error, Keyword, Name, Number, Operator, Punctuation, \15 String, Text, Whitespace16from pygments.lexers._csound_builtins import OPCODES, DEPRECATED_OPCODES, REMOVED_OPCODES17from pygments.lexers.html import HtmlLexer18from pygments.lexers.python import PythonLexer19from pygments.lexers.scripting import LuaLexer20 21__all__ = ['CsoundScoreLexer', 'CsoundOrchestraLexer', 'CsoundDocumentLexer']22 23newline = (r'((?:(?:;|//).*)*)(\n)', bygroups(Comment.Single, Text))24 25 26class CsoundLexer(RegexLexer):27 url = 'https://csound.com/'28 29 tokens = {30 'whitespace': [31 (r'[ \t]+', Whitespace),32 (r'/[*](?:.|\n)*?[*]/', Comment.Multiline),33 (r'(?:;|//).*$', Comment.Single),34 (r'(\\)(\n)', bygroups(Text, Whitespace))35 ],36 37 'preprocessor directives': [38 (r'#(?:e(?:nd(?:if)?|lse)\b|##)|@@?[ \t]*\d+', Comment.Preproc),39 (r'#includestr', Comment.Preproc, 'includestr directive'),40 (r'#include', Comment.Preproc, 'include directive'),41 (r'#[ \t]*define', Comment.Preproc, 'define directive'),42 (r'#(?:ifn?def|undef)\b', Comment.Preproc, 'macro directive')43 ],44 45 'include directive': [46 include('whitespace'),47 (r'([^ \t]).*?\1', String, '#pop')48 ],49 'includestr directive': [50 include('whitespace'),51 (r'"', String, ('#pop', 'quoted string'))52 ],53 54 'define directive': [55 (r'\n', Whitespace),56 include('whitespace'),57 (r'([A-Z_a-z]\w*)(\()', bygroups(Comment.Preproc, Punctuation),58 ('#pop', 'macro parameter name list')),59 (r'[A-Z_a-z]\w*', Comment.Preproc, ('#pop', 'before macro body'))60 ],61 'macro parameter name list': [62 include('whitespace'),63 (r'[A-Z_a-z]\w*', Comment.Preproc),64 (r"['#]", Punctuation),65 (r'\)', Punctuation, ('#pop', 'before macro body'))66 ],67 'before macro body': [68 (r'\n', Whitespace),69 include('whitespace'),70 (r'#', Punctuation, ('#pop', 'macro body'))71 ],72 'macro body': [73 (r'(?:\\(?!#)|[^#\\]|\n)+', Comment.Preproc),74 (r'\\#', Comment.Preproc),75 (r'(?<!\\)#', Punctuation, '#pop')76 ],77 78 'macro directive': [79 include('whitespace'),80 (r'[A-Z_a-z]\w*', Comment.Preproc, '#pop')81 ],82 83 'macro uses': [84 (r'(\$[A-Z_a-z]\w*\.?)(\()', bygroups(Comment.Preproc, Punctuation),85 'macro parameter value list'),86 (r'\$[A-Z_a-z]\w*(?:\.|\b)', Comment.Preproc)87 ],88 'macro parameter value list': [89 (r'(?:[^\'#"{()]|\{(?!\{))+', Comment.Preproc),90 (r"['#]", Punctuation),91 (r'"', String, 'macro parameter value quoted string'),92 (r'\{\{', String, 'macro parameter value braced string'),93 (r'\(', Comment.Preproc, 'macro parameter value parenthetical'),94 (r'\)', Punctuation, '#pop')95 ],96 'macro parameter value quoted string': [97 (r"\\[#'()]", Comment.Preproc),98 (r"[#'()]", Error),99 include('quoted string')100 ],101 'macro parameter value braced string': [102 (r"\\[#'()]", Comment.Preproc),103 (r"[#'()]", Error),104 include('braced string')105 ],106 'macro parameter value parenthetical': [107 (r'(?:[^\\()]|\\\))+', Comment.Preproc),108 (r'\(', Comment.Preproc, '#push'),109 (r'\)', Comment.Preproc, '#pop')110 ],111 112 'whitespace and macro uses': [113 include('whitespace'),114 include('macro uses')115 ],116 117 'numbers': [118 (r'\d+[Ee][+-]?\d+|(\d+\.\d*|\d*\.\d+)([Ee][+-]?\d+)?', Number.Float),119 (r'(0[Xx])([0-9A-Fa-f]+)', bygroups(Keyword.Type, Number.Hex)),120 (r'\d+', Number.Integer)121 ],122 123 'quoted string': [124 (r'"', String, '#pop'),125 (r'[^"$]+', String),126 include('macro uses'),127 (r'[$]', String)128 ],129 130 'braced string': [131 # Do nothing. This must be defined in subclasses.132 ]133 }134 135 136class CsoundScoreLexer(CsoundLexer):137 """138 For `Csound <https://csound.com>`_ scores.139 """140 141 name = 'Csound Score'142 aliases = ['csound-score', 'csound-sco']143 filenames = ['*.sco']144 version_added = '2.1'145 146 tokens = {147 'root': [148 (r'\n', Whitespace),149 include('whitespace and macro uses'),150 include('preprocessor directives'),151 152 (r'[aBbCdefiqstvxy]', Keyword),153 # There is also a w statement that is generated internally and should not be154 # used; see https://github.com/csound/csound/issues/750.155 156 (r'z', Keyword.Constant),157 # z is a constant equal to 800,000,000,000. 800 billion seconds is about158 # 25,367.8 years. See also159 # https://csound.com/docs/manual/ScoreTop.html and160 # https://github.com/csound/csound/search?q=stof+path%3AEngine+filename%3Asread.c.161 162 (r'([nNpP][pP])(\d+)', bygroups(Keyword, Number.Integer)),163 164 (r'[mn]', Keyword, 'mark statement'),165 166 include('numbers'),167 (r'[!+\-*/^%&|<>#~.]', Operator),168 (r'[()\[\]]', Punctuation),169 (r'"', String, 'quoted string'),170 (r'\{', Comment.Preproc, 'loop after left brace'),171 ],172 173 'mark statement': [174 include('whitespace and macro uses'),175 (r'[A-Z_a-z]\w*', Name.Label),176 (r'\n', Whitespace, '#pop')177 ],178 179 'loop after left brace': [180 include('whitespace and macro uses'),181 (r'\d+', Number.Integer, ('#pop', 'loop after repeat count')),182 ],183 'loop after repeat count': [184 include('whitespace and macro uses'),185 (r'[A-Z_a-z]\w*', Comment.Preproc, ('#pop', 'loop'))186 ],187 'loop': [188 (r'\}', Comment.Preproc, '#pop'),189 include('root')190 ],191 192 # Braced strings are not allowed in Csound scores, but this is needed because the193 # superclass includes it.194 'braced string': [195 (r'\}\}', String, '#pop'),196 (r'[^}]|\}(?!\})', String)197 ]198 }199 200 201class CsoundOrchestraLexer(CsoundLexer):202 """203 For `Csound <https://csound.com>`_ orchestras.204 """205 206 name = 'Csound Orchestra'207 aliases = ['csound', 'csound-orc']208 filenames = ['*.orc', '*.udo']209 version_added = '2.1'210 211 user_defined_opcodes = set()212 213 def opcode_name_callback(lexer, match):214 opcode = match.group(0)215 lexer.user_defined_opcodes.add(opcode)216 yield match.start(), Name.Function, opcode217 218 def name_callback(lexer, match):219 type_annotation_token = Keyword.Type220 221 name = match.group(1)222 if name in OPCODES or name in DEPRECATED_OPCODES or name in REMOVED_OPCODES:223 yield match.start(), Name.Builtin, name224 elif name in lexer.user_defined_opcodes:225 yield match.start(), Name.Function, name226 else:227 type_annotation_token = Name228 name_match = re.search(r'^(g?[afikSw])(\w+)', name)229 if name_match:230 yield name_match.start(1), Keyword.Type, name_match.group(1)231 yield name_match.start(2), Name, name_match.group(2)232 else:233 yield match.start(), Name, name234 235 if match.group(2):236 yield match.start(2), Punctuation, match.group(2)237 yield match.start(3), type_annotation_token, match.group(3)238 239 tokens = {240 'root': [241 (r'\n', Whitespace),242 243 (r'^([ \t]*)(\w+)(:)([ \t]+|$)', bygroups(Whitespace, Name.Label, Punctuation, Whitespace)),244 245 include('whitespace and macro uses'),246 include('preprocessor directives'),247 248 (r'\binstr\b', Keyword.Declaration, 'instrument numbers and identifiers'),249 (r'\bopcode\b', Keyword.Declaration, 'after opcode keyword'),250 (r'\b(?:end(?:in|op))\b', Keyword.Declaration),251 252 include('partial statements')253 ],254 255 'partial statements': [256 (r'\b(?:0dbfs|A4|k(?:r|smps)|nchnls(?:_i)?|sr)\b', Name.Variable.Global),257 258 include('numbers'),259 260 (r'\+=|-=|\*=|/=|<<|>>|<=|>=|==|!=|&&|\|\||[~¬]|[=!+\-*/^%&|<>#?:]', Operator),261 (r'[(),\[\]]', Punctuation),262 263 (r'"', String, 'quoted string'),264 (r'\{\{', String, 'braced string'),265 266 (words((267 'do', 'else', 'elseif', 'endif', 'enduntil', 'fi', 'if', 'ithen', 'kthen',268 'od', 'then', 'until', 'while',269 ), prefix=r'\b', suffix=r'\b'), Keyword),270 (words(('return', 'rireturn'), prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),271 272 (r'\b[ik]?goto\b', Keyword, 'goto label'),273 (r'\b(r(?:einit|igoto)|tigoto)(\(|\b)', bygroups(Keyword.Pseudo, Punctuation),274 'goto label'),275 (r'\b(c(?:g|in?|k|nk?)goto)(\(|\b)', bygroups(Keyword.Pseudo, Punctuation),276 ('goto label', 'goto argument')),277 (r'\b(timout)(\(|\b)', bygroups(Keyword.Pseudo, Punctuation),278 ('goto label', 'goto argument', 'goto argument')),279 (r'\b(loop_[gl][et])(\(|\b)', bygroups(Keyword.Pseudo, Punctuation),280 ('goto label', 'goto argument', 'goto argument', 'goto argument')),281 282 (r'\bprintk?s\b', Name.Builtin, 'prints opcode'),283 (r'\b(?:readscore|scoreline(?:_i)?)\b', Name.Builtin, 'Csound score opcode'),284 (r'\bpyl?run[it]?\b', Name.Builtin, 'Python opcode'),285 (r'\blua_(?:exec|opdef)\b', Name.Builtin, 'Lua opcode'),286 (r'\bp\d+\b', Name.Variable.Instance),287 (r'\b([A-Z_a-z]\w*)(?:(:)([A-Za-z]))?\b', name_callback)288 ],289 290 'instrument numbers and identifiers': [291 include('whitespace and macro uses'),292 (r'\d+|[A-Z_a-z]\w*', Name.Function),293 (r'[+,]', Punctuation),294 (r'\n', Whitespace, '#pop')295 ],296 297 'after opcode keyword': [298 include('whitespace and macro uses'),299 (r'[A-Z_a-z]\w*', opcode_name_callback, ('#pop', 'opcode type signatures')),300 (r'\n', Whitespace, '#pop')301 ],302 'opcode type signatures': [303 include('whitespace and macro uses'),304 305 # https://github.com/csound/csound/search?q=XIDENT+path%3AEngine+filename%3Acsound_orc.lex306 (r'0|[afijkKoOpPStV\[\]]+', Keyword.Type),307 308 (r',', Punctuation),309 (r'\n', Whitespace, '#pop')310 ],311 312 'quoted string': [313 (r'"', String, '#pop'),314 (r'[^\\"$%)]+', String),315 include('macro uses'),316 include('escape sequences'),317 include('format specifiers'),318 (r'[\\$%)]', String)319 ],320 'braced string': [321 (r'\}\}', String, '#pop'),322 (r'(?:[^\\%)}]|\}(?!\}))+', String),323 include('escape sequences'),324 include('format specifiers'),325 (r'[\\%)]', String)326 ],327 'escape sequences': [328 # https://github.com/csound/csound/search?q=unquote_string+path%3AEngine+filename%3Acsound_orc_compile.c329 (r'\\(?:[\\abnrt"]|[0-7]{1,3})', String.Escape)330 ],331 # Format specifiers are highlighted in all strings, even though only332 # fprintks https://csound.com/docs/manual/fprintks.html333 # fprints https://csound.com/docs/manual/fprints.html334 # printf/printf_i https://csound.com/docs/manual/printf.html335 # printks https://csound.com/docs/manual/printks.html336 # prints https://csound.com/docs/manual/prints.html337 # sprintf https://csound.com/docs/manual/sprintf.html338 # sprintfk https://csound.com/docs/manual/sprintfk.html339 # work with strings that contain format specifiers. In addition, these opcodes’340 # handling of format specifiers is inconsistent:341 # - fprintks and fprints accept %a and %A specifiers, and accept %s specifiers342 # starting in Csound 6.15.0.343 # - printks and prints accept %a and %A specifiers, but don’t accept %s344 # specifiers.345 # - printf, printf_i, sprintf, and sprintfk don’t accept %a and %A specifiers,346 # but accept %s specifiers.347 # See https://github.com/csound/csound/issues/747 for more information.348 'format specifiers': [349 (r'%[#0\- +]*\d*(?:\.\d+)?[AE-GXac-giosux]', String.Interpol),350 (r'%%', String.Escape)351 ],352 353 'goto argument': [354 include('whitespace and macro uses'),355 (r',', Punctuation, '#pop'),356 include('partial statements')357 ],358 'goto label': [359 include('whitespace and macro uses'),360 (r'\w+', Name.Label, '#pop'),361 default('#pop')362 ],363 364 'prints opcode': [365 include('whitespace and macro uses'),366 (r'"', String, 'prints quoted string'),367 default('#pop')368 ],369 'prints quoted string': [370 (r'\\\\[aAbBnNrRtT]', String.Escape),371 (r'%[!nNrRtT]|[~^]{1,2}', String.Escape),372 include('quoted string')373 ],374 375 'Csound score opcode': [376 include('whitespace and macro uses'),377 (r'"', String, 'quoted string'),378 (r'\{\{', String, 'Csound score'),379 (r'\n', Whitespace, '#pop')380 ],381 'Csound score': [382 (r'\}\}', String, '#pop'),383 (r'([^}]+)|\}(?!\})', using(CsoundScoreLexer))384 ],385 386 'Python opcode': [387 include('whitespace and macro uses'),388 (r'"', String, 'quoted string'),389 (r'\{\{', String, 'Python'),390 (r'\n', Whitespace, '#pop')391 ],392 'Python': [393 (r'\}\}', String, '#pop'),394 (r'([^}]+)|\}(?!\})', using(PythonLexer))395 ],396 397 'Lua opcode': [398 include('whitespace and macro uses'),399 (r'"', String, 'quoted string'),400 (r'\{\{', String, 'Lua'),401 (r'\n', Whitespace, '#pop')402 ],403 'Lua': [404 (r'\}\}', String, '#pop'),405 (r'([^}]+)|\}(?!\})', using(LuaLexer))406 ]407 }408 409 410class CsoundDocumentLexer(RegexLexer):411 """412 For Csound documents.413 """414 415 name = 'Csound Document'416 aliases = ['csound-document', 'csound-csd']417 filenames = ['*.csd']418 url = 'https://csound.com'419 version_added = '2.1'420 421 # These tokens are based on those in XmlLexer in pygments/lexers/html.py. Making422 # CsoundDocumentLexer a subclass of XmlLexer rather than RegexLexer may seem like a423 # better idea, since Csound Document files look like XML files. However, Csound424 # Documents can contain Csound comments (preceded by //, for example) before and425 # after the root element, unescaped bitwise AND & and less than < operators, etc. In426 # other words, while Csound Document files look like XML files, they may not actually427 # be XML files.428 tokens = {429 'root': [430 (r'/[*](.|\n)*?[*]/', Comment.Multiline),431 (r'(?:;|//).*$', Comment.Single),432 (r'[^/;<]+|/(?!/)', Text),433 434 (r'<\s*CsInstruments', Name.Tag, ('orchestra', 'tag')),435 (r'<\s*CsScore', Name.Tag, ('score', 'tag')),436 (r'<\s*[Hh][Tt][Mm][Ll]', Name.Tag, ('HTML', 'tag')),437 438 (r'<\s*[\w:.-]+', Name.Tag, 'tag'),439 (r'<\s*/\s*[\w:.-]+\s*>', Name.Tag)440 ],441 442 'orchestra': [443 (r'<\s*/\s*CsInstruments\s*>', Name.Tag, '#pop'),444 (r'(.|\n)+?(?=<\s*/\s*CsInstruments\s*>)', using(CsoundOrchestraLexer))445 ],446 'score': [447 (r'<\s*/\s*CsScore\s*>', Name.Tag, '#pop'),448 (r'(.|\n)+?(?=<\s*/\s*CsScore\s*>)', using(CsoundScoreLexer))449 ],450 'HTML': [451 (r'<\s*/\s*[Hh][Tt][Mm][Ll]\s*>', Name.Tag, '#pop'),452 (r'(.|\n)+?(?=<\s*/\s*[Hh][Tt][Mm][Ll]\s*>)', using(HtmlLexer))453 ],454 455 'tag': [456 (r'\s+', Whitespace),457 (r'[\w.:-]+\s*=', Name.Attribute, 'attr'),458 (r'/?\s*>', Name.Tag, '#pop')459 ],460 'attr': [461 (r'\s+', Whitespace),462 (r'".*?"', String, '#pop'),463 (r"'.*?'", String, '#pop'),464 (r'[^\s>]+', String, '#pop')465 ]466 }467 