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