tcl.py 5.2 KB

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