Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/tcl.py: 95%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2 pygments.lexers.tcl
3 ~~~~~~~~~~~~~~~~~~~
5 Lexers for Tcl and related languages.
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
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
16__all__ = ['TclLexer']
19class TclLexer(RegexLexer):
20 """
21 For Tcl source code.
22 """
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')
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')
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'
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 ]
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 # `$name` notation stops at a dash (e.g. `$foo-bar` reads `$foo`),
91 # whereas the braced `${name}` form may contain dashes.
92 (r'\$[\w.:]+', Name.Variable),
93 (r'\$\{[\w.:-]+\}', Name.Variable),
94 (r'[\w.,@:-]+', Text),
95 ],
96 'params': [
97 (r';', Keyword, '#pop'),
98 (r'\n', Text, '#pop'),
99 (r'(else|elseif|then)\b', Keyword),
100 include('basic'),
101 include('data'),
102 ],
103 'params-in-brace': [
104 (r'\}', Keyword, ('#pop', '#pop')),
105 include('params')
106 ],
107 'params-in-paren': [
108 (r'\)', Keyword, ('#pop', '#pop')),
109 include('params')
110 ],
111 'params-in-bracket': [
112 (r'\]', Keyword, ('#pop', '#pop')),
113 include('params')
114 ],
115 'string': [
116 (r'\[', String.Double, 'string-square'),
117 (r'(?s)(\\\\|\\[0-7]+|\\.|[^"\\])', String.Double),
118 (r'"', String.Double, '#pop')
119 ],
120 'string-square': [
121 (r'\[', String.Double, 'string-square'),
122 (r'(?s)(\\\\|\\[0-7]+|\\.|\\\n|[^\]\\])', String.Double),
123 (r'\]', String.Double, '#pop')
124 ],
125 'brace': [
126 (r'\}', Keyword, '#pop'),
127 include('command-in-brace'),
128 include('basic'),
129 include('data'),
130 ],
131 'paren': [
132 (r'\)', Keyword, '#pop'),
133 include('command-in-paren'),
134 include('basic'),
135 include('data'),
136 ],
137 'bracket': [
138 (r'\]', Keyword, '#pop'),
139 include('command-in-bracket'),
140 include('basic'),
141 include('data'),
142 ],
143 'comment': [
144 (r'.*[^\\]\n', Comment, '#pop'),
145 (r'.*\\\n', Comment),
146 ],
147 }
149 def analyse_text(text):
150 return shebang_matches(text, r'(tcl)')