teraterm.py 9.7 KB

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