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