1"""
2 pygments.lexers.teraterm
3 ~~~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for Tera Term macro files.
6
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13from pygments.lexer import RegexLexer, include, bygroups
14from pygments.token import Text, Comment, Operator, Name, String, \
15 Number, Keyword, Error
16
17__all__ = ['TeraTermLexer']
18
19
20class TeraTermLexer(RegexLexer):
21 """
22 For Tera Term macro source code.
23 """
24 name = 'Tera Term macro'
25 url = 'https://ttssh2.osdn.jp/'
26 aliases = ['teratermmacro', 'teraterm', 'ttl']
27 filenames = ['*.ttl']
28 mimetypes = ['text/x-teratermmacro']
29 version_added = '2.4'
30
31 tokens = {
32 'root': [
33 include('comments'),
34 include('labels'),
35 include('commands'),
36 include('builtin-variables'),
37 include('user-variables'),
38 include('operators'),
39 include('numeric-literals'),
40 include('string-literals'),
41 include('all-whitespace'),
42 (r'\S', Text),
43 ],
44 'comments': [
45 (r';[^\r\n]*', Comment.Single),
46 (r'/\*', Comment.Multiline, 'in-comment'),
47 ],
48 'in-comment': [
49 (r'\*/', Comment.Multiline, '#pop'),
50 (r'[^*/]+', Comment.Multiline),
51 (r'[*/]', Comment.Multiline)
52 ],
53 'labels': [
54 (r'(?i)^(\s*)(:[a-z0-9_]+)', bygroups(Text.Whitespace, Name.Label)),
55 ],
56 'commands': [
57 (
58 r'(?i)\b('
59 r'basename|'
60 r'beep|'
61 r'bplusrecv|'
62 r'bplussend|'
63 r'break|'
64 r'bringupbox|'
65 # 'call' is handled separately.
66 r'callmenu|'
67 r'changedir|'
68 r'checksum16|'
69 r'checksum16file|'
70 r'checksum32|'
71 r'checksum32file|'
72 r'checksum8|'
73 r'checksum8file|'
74 r'clearscreen|'
75 r'clipb2var|'
76 r'closesbox|'
77 r'closett|'
78 r'code2str|'
79 r'connect|'
80 r'continue|'
81 r'crc16|'
82 r'crc16file|'
83 r'crc32|'
84 r'crc32file|'
85 r'cygconnect|'
86 r'delpassword|'
87 r'delpassword2|'
88 r'dirname|'
89 r'dirnamebox|'
90 r'disconnect|'
91 r'dispstr|'
92 r'do|'
93 r'else|'
94 r'elseif|'
95 r'enablekeyb|'
96 r'end|'
97 r'endif|'
98 r'enduntil|'
99 r'endwhile|'
100 r'exec|'
101 r'execcmnd|'
102 r'exit|'
103 r'expandenv|'
104 r'fileclose|'
105 r'fileconcat|'
106 r'filecopy|'
107 r'filecreate|'
108 r'filedelete|'
109 r'filelock|'
110 r'filemarkptr|'
111 r'filenamebox|'
112 r'fileopen|'
113 r'fileread|'
114 r'filereadln|'
115 r'filerename|'
116 r'filesearch|'
117 r'fileseek|'
118 r'fileseekback|'
119 r'filestat|'
120 r'filestrseek|'
121 r'filestrseek2|'
122 r'filetruncate|'
123 r'fileunlock|'
124 r'filewrite|'
125 r'filewriteln|'
126 r'findclose|'
127 r'findfirst|'
128 r'findnext|'
129 r'flushrecv|'
130 r'foldercreate|'
131 r'folderdelete|'
132 r'foldersearch|'
133 r'for|'
134 r'getdate|'
135 r'getdir|'
136 r'getenv|'
137 r'getfileattr|'
138 r'gethostname|'
139 r'getipv4addr|'
140 r'getipv6addr|'
141 r'getmodemstatus|'
142 r'getpassword|'
143 r'getpassword2|'
144 r'getspecialfolder|'
145 r'gettime|'
146 r'gettitle|'
147 r'getttdir|'
148 r'getttpos|'
149 r'getver|'
150 # 'goto' is handled separately.
151 r'if|'
152 r'ifdefined|'
153 r'include|'
154 r'inputbox|'
155 r'int2str|'
156 r'intdim|'
157 r'ispassword|'
158 r'ispassword2|'
159 r'kmtfinish|'
160 r'kmtget|'
161 r'kmtrecv|'
162 r'kmtsend|'
163 r'listbox|'
164 r'loadkeymap|'
165 r'logautoclosemode|'
166 r'logclose|'
167 r'loginfo|'
168 r'logopen|'
169 r'logpause|'
170 r'logrotate|'
171 r'logstart|'
172 r'logwrite|'
173 r'loop|'
174 r'makepath|'
175 r'messagebox|'
176 r'mpause|'
177 r'next|'
178 r'passwordbox|'
179 r'pause|'
180 r'quickvanrecv|'
181 r'quickvansend|'
182 r'random|'
183 r'recvfile'
184 r'recvln|'
185 r'regexoption|'
186 r'restoresetup|'
187 r'return|'
188 r'rotateleft|'
189 r'rotateright|'
190 r'scprecv|'
191 r'scpsend|'
192 r'send|'
193 r'sendbinary|'
194 r'sendbreak|'
195 r'sendbroadcast|'
196 r'sendfile|'
197 r'sendkcode|'
198 r'sendln|'
199 r'sendlnbroadcast|'
200 r'sendlnmulticast|'
201 r'sendmulticast|'
202 r'sendtext|'
203 r'setbaud|'
204 r'setdate|'
205 r'setdebug|'
206 r'setdir|'
207 r'setdlgpos|'
208 r'setdtr|'
209 r'setecho|'
210 r'setenv|'
211 r'setexitcode|'
212 r'setfileattr|'
213 r'setflowctrl|'
214 r'setmulticastname|'
215 r'setpassword|'
216 r'setpassword2|'
217 r'setrts|'
218 r'setserialdelaychar'
219 r'setserialdelayline'
220 r'setspeed|'
221 r'setsync|'
222 r'settime|'
223 r'settitle|'
224 r'show|'
225 r'showtt|'
226 r'sprintf|'
227 r'sprintf2|'
228 r'statusbox|'
229 r'str2code|'
230 r'str2int|'
231 r'strcompare|'
232 r'strconcat|'
233 r'strcopy|'
234 r'strdim|'
235 r'strinsert|'
236 r'strjoin|'
237 r'strlen|'
238 r'strmatch|'
239 r'strremove|'
240 r'strreplace|'
241 r'strscan|'
242 r'strspecial|'
243 r'strsplit|'
244 r'strtrim|'
245 r'testlink|'
246 r'then|'
247 r'tolower|'
248 r'toupper|'
249 r'unlink|'
250 r'until|'
251 r'uptime|'
252 r'var2clipb|'
253 r'wait|'
254 r'wait4all|'
255 r'waitevent|'
256 r'waitln|'
257 r'waitn|'
258 r'waitrecv|'
259 r'waitregex|'
260 r'while|'
261 r'xmodemrecv|'
262 r'xmodemsend|'
263 r'yesnobox|'
264 r'ymodemrecv|'
265 r'ymodemsend|'
266 r'zmodemrecv|'
267 r'zmodemsend'
268 r')\b',
269 Keyword,
270 ),
271 (r'(?i)(call|goto)([ \t]+)([a-z0-9_]+)',
272 bygroups(Keyword, Text.Whitespace, Name.Label)),
273 ],
274 'builtin-variables': [
275 (
276 r'(?i)('
277 r'groupmatchstr1|'
278 r'groupmatchstr2|'
279 r'groupmatchstr3|'
280 r'groupmatchstr4|'
281 r'groupmatchstr5|'
282 r'groupmatchstr6|'
283 r'groupmatchstr7|'
284 r'groupmatchstr8|'
285 r'groupmatchstr9|'
286 r'inputstr|'
287 r'matchstr|'
288 r'mtimeout|'
289 r'param1|'
290 r'param2|'
291 r'param3|'
292 r'param4|'
293 r'param5|'
294 r'param6|'
295 r'param7|'
296 r'param8|'
297 r'param9|'
298 r'paramcnt|'
299 r'params|'
300 r'result|'
301 r'timeout'
302 r')\b',
303 Name.Builtin
304 ),
305 ],
306 'user-variables': [
307 (r'(?i)[a-z_][a-z0-9_]*', Name.Variable),
308 ],
309 'numeric-literals': [
310 (r'(-?)([0-9]+)', bygroups(Operator, Number.Integer)),
311 (r'(?i)\$[0-9a-f]+', Number.Hex),
312 ],
313 'string-literals': [
314 (r'(?i)#(?:[0-9]+|\$[0-9a-f]+)', String.Char),
315 (r"'[^'\n]*'", String.Single),
316 (r'"[^"\n]*"', String.Double),
317 # Opening quotes without a closing quote on the same line are errors.
318 (r"('[^']*)(\n)", bygroups(Error, Text.Whitespace)),
319 (r'("[^"]*)(\n)', bygroups(Error, Text.Whitespace)),
320 ],
321 'operators': [
322 (r'and|not|or|xor', Operator.Word),
323 (r'[!%&*+<=>^~\|\/-]+', Operator),
324 (r'[()]', String.Symbol),
325 ],
326 'all-whitespace': [
327 (r'\s+', Text.Whitespace),
328 ],
329 }
330
331 # Turtle and Tera Term macro files share the same file extension
332 # but each has a recognizable and distinct syntax.
333 def analyse_text(text):
334 if re.search(TeraTermLexer.tokens['commands'][0][0], text):
335 return 0.01