teraterm.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.teraterm
  4. ~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for Tera Term macro files.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups
  11. from pygments.token import Text, Comment, Operator, Name, String, \
  12. Number, Keyword
  13. __all__ = ['TeraTermLexer']
  14. class TeraTermLexer(RegexLexer):
  15. """
  16. For `Tera Term <https://ttssh2.osdn.jp/>`_ macro source code.
  17. .. versionadded:: 2.4
  18. """
  19. name = 'Tera Term macro'
  20. aliases = ['ttl', 'teraterm', 'teratermmacro']
  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*)(:[0-9a-z_]+)', bygroups(Text, Name.Label)),
  47. ],
  48. 'commands': [
  49. (
  50. r'(?i)\b('
  51. r'basename|beep|bplusrecv|bplussend|break|bringupbox|'
  52. r'callmenu|changedir|checksum16|checksum16file|'
  53. r'checksum32|checksum32file|checksum8|checksum8file|'
  54. r'clearscreen|clipb2var|closesbox|closett|code2str|'
  55. r'connect|continue|crc16|crc16file|crc32|crc32file|'
  56. r'cygconnect|delpassword|dirname|dirnamebox|disconnect|'
  57. r'dispstr|do|else|elseif|enablekeyb|end|endif|enduntil|'
  58. r'endwhile|exec|execcmnd|exit|expandenv|fileclose|'
  59. r'fileconcat|filecopy|filecreate|filedelete|filelock|'
  60. r'filemarkptr|filenamebox|fileopen|fileread|filereadln|'
  61. r'filerename|filesearch|fileseek|fileseekback|filestat|'
  62. r'filestrseek|filestrseek2|filetruncate|fileunlock|'
  63. r'filewrite|filewriteln|findclose|findfirst|findnext|'
  64. r'flushrecv|foldercreate|folderdelete|foldersearch|for|'
  65. r'getdate|getdir|getenv|getfileattr|gethostname|'
  66. r'getipv4addr|getipv6addr|getmodemstatus|getpassword|'
  67. r'getspecialfolder|gettime|gettitle|getttdir|getver|'
  68. r'if|ifdefined|include|inputbox|int2str|intdim|'
  69. r'ispassword|kmtfinish|kmtget|kmtrecv|kmtsend|listbox|'
  70. r'loadkeymap|logautoclosemode|logclose|loginfo|logopen|'
  71. r'logpause|logrotate|logstart|logwrite|loop|makepath|'
  72. r'messagebox|mpause|next|passwordbox|pause|quickvanrecv|'
  73. r'quickvansend|random|recvln|regexoption|restoresetup|'
  74. r'return|rotateleft|rotateright|scprecv|scpsend|send|'
  75. r'sendbreak|sendbroadcast|sendfile|sendkcode|sendln|'
  76. r'sendlnbroadcast|sendlnmulticast|sendmulticast|setbaud|'
  77. r'setdate|setdebug|setdir|setdlgpos|setdtr|setecho|'
  78. r'setenv|setexitcode|setfileattr|setflowctrl|'
  79. r'setmulticastname|setpassword|setrts|setsync|settime|'
  80. r'settitle|show|showtt|sprintf|sprintf2|statusbox|'
  81. r'str2code|str2int|strcompare|strconcat|strcopy|strdim|'
  82. r'strinsert|strjoin|strlen|strmatch|strremove|'
  83. r'strreplace|strscan|strspecial|strsplit|strtrim|'
  84. r'testlink|then|tolower|toupper|unlink|until|uptime|'
  85. r'var2clipb|wait|wait4all|waitevent|waitln|waitn|'
  86. r'waitrecv|waitregex|while|xmodemrecv|xmodemsend|'
  87. r'yesnobox|ymodemrecv|ymodemsend|zmodemrecv|zmodemsend'
  88. r')\b',
  89. Keyword,
  90. ),
  91. (
  92. r'(?i)(call|goto)([ \t]+)([0-9a-z_]+)',
  93. bygroups(Keyword, Text, Name.Label),
  94. )
  95. ],
  96. 'builtin-variables': [
  97. (
  98. r'(?i)('
  99. r'groupmatchstr1|groupmatchstr2|groupmatchstr3|'
  100. r'groupmatchstr4|groupmatchstr5|groupmatchstr6|'
  101. r'groupmatchstr7|groupmatchstr8|groupmatchstr9|'
  102. r'param1|param2|param3|param4|param5|param6|'
  103. r'param7|param8|param9|paramcnt|params|'
  104. r'inputstr|matchstr|mtimeout|result|timeout'
  105. r')\b',
  106. Name.Builtin
  107. ),
  108. ],
  109. 'user-variables': [
  110. (r'(?i)[A-Z_][A-Z0-9_]*', Name.Variable),
  111. ],
  112. 'numeric-literals': [
  113. (r'(-?)([0-9]+)', bygroups(Operator, Number.Integer)),
  114. (r'(?i)\$[0-9a-f]+', Number.Hex),
  115. ],
  116. 'string-literals': [
  117. (r'(?i)#(?:[0-9]+|\$[0-9a-f]+)', String.Char),
  118. (r"'", String.Single, 'in-single-string'),
  119. (r'"', String.Double, 'in-double-string'),
  120. ],
  121. 'in-general-string': [
  122. (r'[\\][\\nt]', String.Escape), # Only three escapes are supported.
  123. (r'.', String),
  124. ],
  125. 'in-single-string': [
  126. (r"'", String.Single, '#pop'),
  127. include('in-general-string'),
  128. ],
  129. 'in-double-string': [
  130. (r'"', String.Double, '#pop'),
  131. include('in-general-string'),
  132. ],
  133. 'operators': [
  134. (r'and|not|or|xor', Operator.Word),
  135. (r'[!%&*+<=>^~\|\/-]+', Operator),
  136. (r'[()]', String.Symbol),
  137. ],
  138. 'all-whitespace': [
  139. (r'[\s]+', Text),
  140. ],
  141. }
  142. # Turtle and Tera Term macro files share the same file extension
  143. # but each has a recognizable and distinct syntax.
  144. def analyse_text(text):
  145. result = 0.0
  146. if re.search(TeraTermLexer.tokens['commands'][0][0], text):
  147. result += 0.01
  148. return result