teraterm.py 9.5 KB

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