esoteric.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. """
  2. pygments.lexers.esoteric
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for esoteric languages.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, words, bygroups
  9. from pygments.token import Comment, Operator, Keyword, Name, String, Number, \
  10. Punctuation, Error, Whitespace
  11. __all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer', 'CAmkESLexer',
  12. 'CapDLLexer', 'AheuiLexer']
  13. class BrainfuckLexer(RegexLexer):
  14. """
  15. Lexer for the esoteric BrainFuck language.
  16. """
  17. name = 'Brainfuck'
  18. url = 'http://www.muppetlabs.com/~breadbox/bf/'
  19. aliases = ['brainfuck', 'bf']
  20. filenames = ['*.bf', '*.b']
  21. mimetypes = ['application/x-brainfuck']
  22. tokens = {
  23. 'common': [
  24. # use different colors for different instruction types
  25. (r'[.,]+', Name.Tag),
  26. (r'[+-]+', Name.Builtin),
  27. (r'[<>]+', Name.Variable),
  28. (r'[^.,+\-<>\[\]]+', Comment),
  29. ],
  30. 'root': [
  31. (r'\[', Keyword, 'loop'),
  32. (r'\]', Error),
  33. include('common'),
  34. ],
  35. 'loop': [
  36. (r'\[', Keyword, '#push'),
  37. (r'\]', Keyword, '#pop'),
  38. include('common'),
  39. ]
  40. }
  41. def analyse_text(text):
  42. """It's safe to assume that a program which mostly consists of + -
  43. and < > is brainfuck."""
  44. plus_minus_count = 0
  45. greater_less_count = 0
  46. range_to_check = max(256, len(text))
  47. for c in text[:range_to_check]:
  48. if c == '+' or c == '-':
  49. plus_minus_count += 1
  50. if c == '<' or c == '>':
  51. greater_less_count += 1
  52. if plus_minus_count > (0.25 * range_to_check):
  53. return 1.0
  54. if greater_less_count > (0.25 * range_to_check):
  55. return 1.0
  56. result = 0
  57. if '[-]' in text:
  58. result += 0.5
  59. return result
  60. class BefungeLexer(RegexLexer):
  61. """
  62. Lexer for the esoteric Befunge language.
  63. .. versionadded:: 0.7
  64. """
  65. name = 'Befunge'
  66. url = 'http://en.wikipedia.org/wiki/Befunge'
  67. aliases = ['befunge']
  68. filenames = ['*.befunge']
  69. mimetypes = ['application/x-befunge']
  70. tokens = {
  71. 'root': [
  72. (r'[0-9a-f]', Number),
  73. (r'[+*/%!`-]', Operator), # Traditional math
  74. (r'[<>^v?\[\]rxjk]', Name.Variable), # Move, imperatives
  75. (r'[:\\$.,n]', Name.Builtin), # Stack ops, imperatives
  76. (r'[|_mw]', Keyword),
  77. (r'[{}]', Name.Tag), # Befunge-98 stack ops
  78. (r'".*?"', String.Double), # Strings don't appear to allow escapes
  79. (r'\'.', String.Single), # Single character
  80. (r'[#;]', Comment), # Trampoline... depends on direction hit
  81. (r'[pg&~=@iotsy]', Keyword), # Misc
  82. (r'[()A-Z]', Comment), # Fingerprints
  83. (r'\s+', Whitespace), # Whitespace doesn't matter
  84. ],
  85. }
  86. class CAmkESLexer(RegexLexer):
  87. """
  88. Basic lexer for the input language for the CAmkES component platform.
  89. .. versionadded:: 2.1
  90. """
  91. name = 'CAmkES'
  92. url = 'https://sel4.systems/CAmkES/'
  93. aliases = ['camkes', 'idl4']
  94. filenames = ['*.camkes', '*.idl4']
  95. tokens = {
  96. 'root': [
  97. # C pre-processor directive
  98. (r'^(\s*)(#.*)(\n)', bygroups(Whitespace, Comment.Preproc,
  99. Whitespace)),
  100. # Whitespace, comments
  101. (r'\s+', Whitespace),
  102. (r'/\*(.|\n)*?\*/', Comment),
  103. (r'//.*$', Comment),
  104. (r'[\[(){},.;\]]', Punctuation),
  105. (r'[~!%^&*+=|?:<>/-]', Operator),
  106. (words(('assembly', 'attribute', 'component', 'composition',
  107. 'configuration', 'connection', 'connector', 'consumes',
  108. 'control', 'dataport', 'Dataport', 'Dataports', 'emits',
  109. 'event', 'Event', 'Events', 'export', 'from', 'group',
  110. 'hardware', 'has', 'interface', 'Interface', 'maybe',
  111. 'procedure', 'Procedure', 'Procedures', 'provides',
  112. 'template', 'thread', 'threads', 'to', 'uses', 'with'),
  113. suffix=r'\b'), Keyword),
  114. (words(('bool', 'boolean', 'Buf', 'char', 'character', 'double',
  115. 'float', 'in', 'inout', 'int', 'int16_6', 'int32_t',
  116. 'int64_t', 'int8_t', 'integer', 'mutex', 'out', 'real',
  117. 'refin', 'semaphore', 'signed', 'string', 'struct',
  118. 'uint16_t', 'uint32_t', 'uint64_t', 'uint8_t', 'uintptr_t',
  119. 'unsigned', 'void'),
  120. suffix=r'\b'), Keyword.Type),
  121. # Recognised attributes
  122. (r'[a-zA-Z_]\w*_(priority|domain|buffer)', Keyword.Reserved),
  123. (words(('dma_pool', 'from_access', 'to_access'), suffix=r'\b'),
  124. Keyword.Reserved),
  125. # CAmkES-level include
  126. (r'(import)(\s+)((?:<[^>]*>|"[^"]*");)',
  127. bygroups(Comment.Preproc, Whitespace, Comment.Preproc)),
  128. # C-level include
  129. (r'(include)(\s+)((?:<[^>]*>|"[^"]*");)',
  130. bygroups(Comment.Preproc, Whitespace, Comment.Preproc)),
  131. # Literals
  132. (r'0[xX][\da-fA-F]+', Number.Hex),
  133. (r'-?[\d]+', Number),
  134. (r'-?[\d]+\.[\d]+', Number.Float),
  135. (r'"[^"]*"', String),
  136. (r'[Tt]rue|[Ff]alse', Name.Builtin),
  137. # Identifiers
  138. (r'[a-zA-Z_]\w*', Name),
  139. ],
  140. }
  141. class CapDLLexer(RegexLexer):
  142. """
  143. Basic lexer for CapDL.
  144. The source of the primary tool that reads such specifications is available
  145. at https://github.com/seL4/capdl/tree/master/capDL-tool. Note that this
  146. lexer only supports a subset of the grammar. For example, identifiers can
  147. shadow type names, but these instances are currently incorrectly
  148. highlighted as types. Supporting this would need a stateful lexer that is
  149. considered unnecessarily complex for now.
  150. .. versionadded:: 2.2
  151. """
  152. name = 'CapDL'
  153. url = 'https://ssrg.nicta.com.au/publications/nictaabstracts/Kuz_KLW_10.abstract.pml'
  154. aliases = ['capdl']
  155. filenames = ['*.cdl']
  156. tokens = {
  157. 'root': [
  158. # C pre-processor directive
  159. (r'^(\s*)(#.*)(\n)',
  160. bygroups(Whitespace, Comment.Preproc, Whitespace)),
  161. # Whitespace, comments
  162. (r'\s+', Whitespace),
  163. (r'/\*(.|\n)*?\*/', Comment),
  164. (r'(//|--).*$', Comment),
  165. (r'[<>\[(){},:;=\]]', Punctuation),
  166. (r'\.\.', Punctuation),
  167. (words(('arch', 'arm11', 'caps', 'child_of', 'ia32', 'irq', 'maps',
  168. 'objects'), suffix=r'\b'), Keyword),
  169. (words(('aep', 'asid_pool', 'cnode', 'ep', 'frame', 'io_device',
  170. 'io_ports', 'io_pt', 'notification', 'pd', 'pt', 'tcb',
  171. 'ut', 'vcpu'), suffix=r'\b'), Keyword.Type),
  172. # Properties
  173. (words(('asid', 'addr', 'badge', 'cached', 'dom', 'domainID', 'elf',
  174. 'fault_ep', 'G', 'guard', 'guard_size', 'init', 'ip',
  175. 'prio', 'sp', 'R', 'RG', 'RX', 'RW', 'RWG', 'RWX', 'W',
  176. 'WG', 'WX', 'level', 'masked', 'master_reply', 'paddr',
  177. 'ports', 'reply', 'uncached'), suffix=r'\b'),
  178. Keyword.Reserved),
  179. # Literals
  180. (r'0[xX][\da-fA-F]+', Number.Hex),
  181. (r'\d+(\.\d+)?(k|M)?', Number),
  182. (words(('bits',), suffix=r'\b'), Number),
  183. (words(('cspace', 'vspace', 'reply_slot', 'caller_slot',
  184. 'ipc_buffer_slot'), suffix=r'\b'), Number),
  185. # Identifiers
  186. (r'[a-zA-Z_][-@\.\w]*', Name),
  187. ],
  188. }
  189. class RedcodeLexer(RegexLexer):
  190. """
  191. A simple Redcode lexer based on ICWS'94.
  192. Contributed by Adam Blinkinsop <blinks@acm.org>.
  193. .. versionadded:: 0.8
  194. """
  195. name = 'Redcode'
  196. aliases = ['redcode']
  197. filenames = ['*.cw']
  198. opcodes = ('DAT', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'MOD',
  199. 'JMP', 'JMZ', 'JMN', 'DJN', 'CMP', 'SLT', 'SPL',
  200. 'ORG', 'EQU', 'END')
  201. modifiers = ('A', 'B', 'AB', 'BA', 'F', 'X', 'I')
  202. tokens = {
  203. 'root': [
  204. # Whitespace:
  205. (r'\s+', Whitespace),
  206. (r';.*$', Comment.Single),
  207. # Lexemes:
  208. # Identifiers
  209. (r'\b(%s)\b' % '|'.join(opcodes), Name.Function),
  210. (r'\b(%s)\b' % '|'.join(modifiers), Name.Decorator),
  211. (r'[A-Za-z_]\w+', Name),
  212. # Operators
  213. (r'[-+*/%]', Operator),
  214. (r'[#$@<>]', Operator), # mode
  215. (r'[.,]', Punctuation), # mode
  216. # Numbers
  217. (r'[-+]?\d+', Number.Integer),
  218. ],
  219. }
  220. class AheuiLexer(RegexLexer):
  221. """
  222. Aheui is esoteric language based on Korean alphabets.
  223. """
  224. name = 'Aheui'
  225. url = 'http://aheui.github.io/'
  226. aliases = ['aheui']
  227. filenames = ['*.aheui']
  228. tokens = {
  229. 'root': [
  230. ('['
  231. '나-낳냐-냫너-넣녀-녛노-놓뇨-눟뉴-닇'
  232. '다-닿댜-댷더-덯뎌-뎧도-돟됴-둫듀-딓'
  233. '따-땋땨-떃떠-떻뗘-뗳또-똫뚀-뚷뜌-띟'
  234. '라-랗랴-럏러-렇려-렿로-롷료-뤃류-릫'
  235. '마-맣먀-먛머-멓며-몋모-뫃묘-뭏뮤-믷'
  236. '바-밯뱌-뱧버-벟벼-볗보-봏뵤-붛뷰-빃'
  237. '빠-빻뺘-뺳뻐-뻫뼈-뼣뽀-뽛뾰-뿧쀼-삏'
  238. '사-샇샤-샿서-섷셔-셯소-솧쇼-숳슈-싛'
  239. '싸-쌓쌰-썋써-쎃쎠-쎻쏘-쏳쑈-쑿쓔-씧'
  240. '자-잫쟈-쟣저-젛져-졓조-좋죠-줗쥬-즿'
  241. '차-챃챠-챻처-첳쳐-쳫초-촣쵸-춯츄-칗'
  242. '카-캏캬-컇커-컿켜-켷코-콯쿄-쿻큐-킣'
  243. '타-탛탸-턓터-텋텨-톃토-톻툐-퉇튜-틯'
  244. '파-팧퍄-퍟퍼-펗펴-폏포-퐇표-풓퓨-픻'
  245. '하-핳햐-햫허-헣혀-혛호-홓효-훟휴-힇'
  246. ']', Operator),
  247. ('.', Comment),
  248. ],
  249. }