tcl.py 5.3 KB

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