tcl.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. pygments.lexers.tcl
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for Tcl and related languages.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Whitespace
  11. from pygments.util import shebang_matches
  12. __all__ = ['TclLexer']
  13. class TclLexer(RegexLexer):
  14. """
  15. For Tcl source code.
  16. .. versionadded:: 0.10
  17. """
  18. keyword_cmds_re = words((
  19. 'after', 'apply', 'array', 'break', 'catch', 'continue', 'elseif',
  20. 'else', 'error', 'eval', 'expr', 'for', 'foreach', 'global', 'if',
  21. 'namespace', 'proc', 'rename', 'return', 'set', 'switch', 'then',
  22. 'trace', 'unset', 'update', 'uplevel', 'upvar', 'variable', 'vwait',
  23. 'while'), prefix=r'\b', suffix=r'\b')
  24. builtin_cmds_re = words((
  25. 'append', 'bgerror', 'binary', 'cd', 'chan', 'clock', 'close',
  26. 'concat', 'dde', 'dict', 'encoding', 'eof', 'exec', 'exit', 'fblocked',
  27. 'fconfigure', 'fcopy', 'file', 'fileevent', 'flush', 'format', 'gets',
  28. 'glob', 'history', 'http', 'incr', 'info', 'interp', 'join', 'lappend',
  29. 'lassign', 'lindex', 'linsert', 'list', 'llength', 'load', 'loadTk',
  30. 'lrange', 'lrepeat', 'lreplace', 'lreverse', 'lsearch', 'lset', 'lsort',
  31. 'mathfunc', 'mathop', 'memory', 'msgcat', 'open', 'package', 'pid',
  32. 'pkg::create', 'pkg_mkIndex', 'platform', 'platform::shell', 'puts',
  33. 'pwd', 're_syntax', 'read', 'refchan', 'regexp', 'registry', 'regsub',
  34. 'scan', 'seek', 'socket', 'source', 'split', 'string', 'subst', 'tell',
  35. 'time', 'tm', 'unknown', 'unload'), prefix=r'\b', suffix=r'\b')
  36. name = 'Tcl'
  37. url = 'https://www.tcl.tk/about/language.html'
  38. aliases = ['tcl']
  39. filenames = ['*.tcl', '*.rvt']
  40. mimetypes = ['text/x-tcl', 'text/x-script.tcl', 'application/x-tcl']
  41. def _gen_command_rules(keyword_cmds_re, builtin_cmds_re, context=""):
  42. return [
  43. (keyword_cmds_re, Keyword, 'params' + context),
  44. (builtin_cmds_re, Name.Builtin, 'params' + context),
  45. (r'([\w.-]+)', Name.Variable, 'params' + context),
  46. (r'#', Comment, 'comment'),
  47. ]
  48. tokens = {
  49. 'root': [
  50. include('command'),
  51. include('basic'),
  52. include('data'),
  53. (r'\}', Keyword), # HACK: somehow we miscounted our braces
  54. ],
  55. 'command': _gen_command_rules(keyword_cmds_re, builtin_cmds_re),
  56. 'command-in-brace': _gen_command_rules(keyword_cmds_re,
  57. builtin_cmds_re,
  58. "-in-brace"),
  59. 'command-in-bracket': _gen_command_rules(keyword_cmds_re,
  60. builtin_cmds_re,
  61. "-in-bracket"),
  62. 'command-in-paren': _gen_command_rules(keyword_cmds_re,
  63. builtin_cmds_re,
  64. "-in-paren"),
  65. 'basic': [
  66. (r'\(', Keyword, 'paren'),
  67. (r'\[', Keyword, 'bracket'),
  68. (r'\{', Keyword, 'brace'),
  69. (r'"', String.Double, 'string'),
  70. (r'(eq|ne|in|ni)\b', Operator.Word),
  71. (r'!=|==|<<|>>|<=|>=|&&|\|\||\*\*|[-+~!*/%<>&^|?:]', Operator),
  72. ],
  73. 'data': [
  74. (r'\s+', Whitespace),
  75. (r'0x[a-fA-F0-9]+', Number.Hex),
  76. (r'0[0-7]+', Number.Oct),
  77. (r'\d+\.\d+', Number.Float),
  78. (r'\d+', Number.Integer),
  79. (r'\$[\w.:-]+', Name.Variable),
  80. (r'\$\{[\w.:-]+\}', Name.Variable),
  81. (r'[\w.,@:-]+', Text),
  82. ],
  83. 'params': [
  84. (r';', Keyword, '#pop'),
  85. (r'\n', Text, '#pop'),
  86. (r'(else|elseif|then)\b', Keyword),
  87. include('basic'),
  88. include('data'),
  89. ],
  90. 'params-in-brace': [
  91. (r'\}', Keyword, ('#pop', '#pop')),
  92. include('params')
  93. ],
  94. 'params-in-paren': [
  95. (r'\)', Keyword, ('#pop', '#pop')),
  96. include('params')
  97. ],
  98. 'params-in-bracket': [
  99. (r'\]', Keyword, ('#pop', '#pop')),
  100. include('params')
  101. ],
  102. 'string': [
  103. (r'\[', String.Double, 'string-square'),
  104. (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\])', String.Double),
  105. (r'"', String.Double, '#pop')
  106. ],
  107. 'string-square': [
  108. (r'\[', String.Double, 'string-square'),
  109. (r'(?s)(\\\\|\\[0-7]+|\\.|\\\n|[^\]\\])', String.Double),
  110. (r'\]', String.Double, '#pop')
  111. ],
  112. 'brace': [
  113. (r'\}', Keyword, '#pop'),
  114. include('command-in-brace'),
  115. include('basic'),
  116. include('data'),
  117. ],
  118. 'paren': [
  119. (r'\)', Keyword, '#pop'),
  120. include('command-in-paren'),
  121. include('basic'),
  122. include('data'),
  123. ],
  124. 'bracket': [
  125. (r'\]', Keyword, '#pop'),
  126. include('command-in-bracket'),
  127. include('basic'),
  128. include('data'),
  129. ],
  130. 'comment': [
  131. (r'.*[^\\]\n', Comment, '#pop'),
  132. (r'.*\\\n', Comment),
  133. ],
  134. }
  135. def analyse_text(text):
  136. return shebang_matches(text, r'(tcl)')