tcl.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """
  2. pygments.lexers.tcl
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for Tcl and related languages.
  5. :copyright: Copyright 2006-present 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. """
  17. keyword_cmds_re = words((
  18. 'after', 'apply', 'array', 'break', 'catch', 'continue', 'elseif',
  19. 'else', 'error', 'eval', 'expr', 'for', 'foreach', 'global', 'if',
  20. 'namespace', 'proc', 'rename', 'return', 'set', 'switch', 'then',
  21. 'trace', 'unset', 'update', 'uplevel', 'upvar', 'variable', 'vwait',
  22. 'while'), prefix=r'\b', suffix=r'\b')
  23. builtin_cmds_re = words((
  24. 'append', 'bgerror', 'binary', 'cd', 'chan', 'clock', 'close',
  25. 'concat', 'dde', 'dict', 'encoding', 'eof', 'exec', 'exit', 'fblocked',
  26. 'fconfigure', 'fcopy', 'file', 'fileevent', 'flush', 'format', 'gets',
  27. 'glob', 'history', 'http', 'incr', 'info', 'interp', 'join', 'lappend',
  28. 'lassign', 'lindex', 'linsert', 'list', 'llength', 'load', 'loadTk',
  29. 'lrange', 'lrepeat', 'lreplace', 'lreverse', 'lsearch', 'lset', 'lsort',
  30. 'mathfunc', 'mathop', 'memory', 'msgcat', 'open', 'package', 'pid',
  31. 'pkg::create', 'pkg_mkIndex', 'platform', 'platform::shell', 'puts',
  32. 'pwd', 're_syntax', 'read', 'refchan', 'regexp', 'registry', 'regsub',
  33. 'scan', 'seek', 'socket', 'source', 'split', 'string', 'subst', 'tell',
  34. 'time', 'tm', 'unknown', 'unload'), prefix=r'\b', suffix=r'\b')
  35. name = 'Tcl'
  36. url = 'https://www.tcl.tk/about/language.html'
  37. aliases = ['tcl']
  38. filenames = ['*.tcl', '*.rvt']
  39. mimetypes = ['text/x-tcl', 'text/x-script.tcl', 'application/x-tcl']
  40. version_added = '0.10'
  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. # `$name` notation stops at a dash (e.g. `$foo-bar` reads `$foo`),
  80. # whereas the braced `${name}` form may contain dashes.
  81. (r'\$[\w.:]+', Name.Variable),
  82. (r'\$\{[\w.:-]+\}', Name.Variable),
  83. (r'[\w.,@:-]+', Text),
  84. ],
  85. 'params': [
  86. (r';', Keyword, '#pop'),
  87. (r'\n', Text, '#pop'),
  88. (r'(else|elseif|then)\b', Keyword),
  89. include('basic'),
  90. include('data'),
  91. ],
  92. 'params-in-brace': [
  93. (r'\}', Keyword, ('#pop', '#pop')),
  94. include('params')
  95. ],
  96. 'params-in-paren': [
  97. (r'\)', Keyword, ('#pop', '#pop')),
  98. include('params')
  99. ],
  100. 'params-in-bracket': [
  101. (r'\]', Keyword, ('#pop', '#pop')),
  102. include('params')
  103. ],
  104. 'string': [
  105. (r'\[', String.Double, 'string-square'),
  106. (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\])', String.Double),
  107. (r'"', String.Double, '#pop')
  108. ],
  109. 'string-square': [
  110. (r'\[', String.Double, 'string-square'),
  111. (r'(?s)(\\\\|\\[0-7]+|\\.|\\\n|[^\]\\])', String.Double),
  112. (r'\]', String.Double, '#pop')
  113. ],
  114. 'brace': [
  115. (r'\}', Keyword, '#pop'),
  116. include('command-in-brace'),
  117. include('basic'),
  118. include('data'),
  119. ],
  120. 'paren': [
  121. (r'\)', Keyword, '#pop'),
  122. include('command-in-paren'),
  123. include('basic'),
  124. include('data'),
  125. ],
  126. 'bracket': [
  127. (r'\]', Keyword, '#pop'),
  128. include('command-in-bracket'),
  129. include('basic'),
  130. include('data'),
  131. ],
  132. 'comment': [
  133. (r'.*[^\\]\n', Comment, '#pop'),
  134. (r'.*\\\n', Comment),
  135. ],
  136. }
  137. def analyse_text(text):
  138. return shebang_matches(text, r'(tcl)')