Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/esoteric.py: 73%

59 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.esoteric 

3 ~~~~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for esoteric languages. 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, include, words, bygroups 

12from pygments.token import Comment, Operator, Keyword, Name, String, Number, \ 

13 Punctuation, Error, Whitespace 

14 

15__all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer', 'CAmkESLexer', 

16 'CapDLLexer', 'AheuiLexer'] 

17 

18 

19class BrainfuckLexer(RegexLexer): 

20 """ 

21 Lexer for the esoteric BrainFuck language. 

22 """ 

23 

24 name = 'Brainfuck' 

25 url = 'http://www.muppetlabs.com/~breadbox/bf/' 

26 aliases = ['brainfuck', 'bf'] 

27 filenames = ['*.bf', '*.b'] 

28 mimetypes = ['application/x-brainfuck'] 

29 

30 tokens = { 

31 'common': [ 

32 # use different colors for different instruction types 

33 (r'[.,]+', Name.Tag), 

34 (r'[+-]+', Name.Builtin), 

35 (r'[<>]+', Name.Variable), 

36 (r'[^.,+\-<>\[\]]+', Comment), 

37 ], 

38 'root': [ 

39 (r'\[', Keyword, 'loop'), 

40 (r'\]', Error), 

41 include('common'), 

42 ], 

43 'loop': [ 

44 (r'\[', Keyword, '#push'), 

45 (r'\]', Keyword, '#pop'), 

46 include('common'), 

47 ] 

48 } 

49 

50 def analyse_text(text): 

51 """It's safe to assume that a program which mostly consists of + - 

52 and < > is brainfuck.""" 

53 plus_minus_count = 0 

54 greater_less_count = 0 

55 

56 range_to_check = max(256, len(text)) 

57 

58 for c in text[:range_to_check]: 

59 if c == '+' or c == '-': 

60 plus_minus_count += 1 

61 if c == '<' or c == '>': 

62 greater_less_count += 1 

63 

64 if plus_minus_count > (0.25 * range_to_check): 

65 return 1.0 

66 if greater_less_count > (0.25 * range_to_check): 

67 return 1.0 

68 

69 result = 0 

70 if '[-]' in text: 

71 result += 0.5 

72 

73 return result 

74 

75 

76class BefungeLexer(RegexLexer): 

77 """ 

78 Lexer for the esoteric Befunge language. 

79 

80 .. versionadded:: 0.7 

81 """ 

82 name = 'Befunge' 

83 url = 'http://en.wikipedia.org/wiki/Befunge' 

84 aliases = ['befunge'] 

85 filenames = ['*.befunge'] 

86 mimetypes = ['application/x-befunge'] 

87 

88 tokens = { 

89 'root': [ 

90 (r'[0-9a-f]', Number), 

91 (r'[+*/%!`-]', Operator), # Traditional math 

92 (r'[<>^v?\[\]rxjk]', Name.Variable), # Move, imperatives 

93 (r'[:\\$.,n]', Name.Builtin), # Stack ops, imperatives 

94 (r'[|_mw]', Keyword), 

95 (r'[{}]', Name.Tag), # Befunge-98 stack ops 

96 (r'".*?"', String.Double), # Strings don't appear to allow escapes 

97 (r'\'.', String.Single), # Single character 

98 (r'[#;]', Comment), # Trampoline... depends on direction hit 

99 (r'[pg&~=@iotsy]', Keyword), # Misc 

100 (r'[()A-Z]', Comment), # Fingerprints 

101 (r'\s+', Whitespace), # Whitespace doesn't matter 

102 ], 

103 } 

104 

105 

106class CAmkESLexer(RegexLexer): 

107 """ 

108 Basic lexer for the input language for the CAmkES component platform. 

109 

110 .. versionadded:: 2.1 

111 """ 

112 name = 'CAmkES' 

113 url = 'https://sel4.systems/CAmkES/' 

114 aliases = ['camkes', 'idl4'] 

115 filenames = ['*.camkes', '*.idl4'] 

116 

117 tokens = { 

118 'root': [ 

119 # C pre-processor directive 

120 (r'^(\s*)(#.*)(\n)', bygroups(Whitespace, Comment.Preproc, 

121 Whitespace)), 

122 

123 # Whitespace, comments 

124 (r'\s+', Whitespace), 

125 (r'/\*(.|\n)*?\*/', Comment), 

126 (r'//.*$', Comment), 

127 

128 (r'[\[(){},.;\]]', Punctuation), 

129 (r'[~!%^&*+=|?:<>/-]', Operator), 

130 

131 (words(('assembly', 'attribute', 'component', 'composition', 

132 'configuration', 'connection', 'connector', 'consumes', 

133 'control', 'dataport', 'Dataport', 'Dataports', 'emits', 

134 'event', 'Event', 'Events', 'export', 'from', 'group', 

135 'hardware', 'has', 'interface', 'Interface', 'maybe', 

136 'procedure', 'Procedure', 'Procedures', 'provides', 

137 'template', 'thread', 'threads', 'to', 'uses', 'with'), 

138 suffix=r'\b'), Keyword), 

139 

140 (words(('bool', 'boolean', 'Buf', 'char', 'character', 'double', 

141 'float', 'in', 'inout', 'int', 'int16_6', 'int32_t', 

142 'int64_t', 'int8_t', 'integer', 'mutex', 'out', 'real', 

143 'refin', 'semaphore', 'signed', 'string', 'struct', 

144 'uint16_t', 'uint32_t', 'uint64_t', 'uint8_t', 'uintptr_t', 

145 'unsigned', 'void'), 

146 suffix=r'\b'), Keyword.Type), 

147 

148 # Recognised attributes 

149 (r'[a-zA-Z_]\w*_(priority|domain|buffer)', Keyword.Reserved), 

150 (words(('dma_pool', 'from_access', 'to_access'), suffix=r'\b'), 

151 Keyword.Reserved), 

152 

153 # CAmkES-level include 

154 (r'(import)(\s+)((?:<[^>]*>|"[^"]*");)', 

155 bygroups(Comment.Preproc, Whitespace, Comment.Preproc)), 

156 

157 # C-level include 

158 (r'(include)(\s+)((?:<[^>]*>|"[^"]*");)', 

159 bygroups(Comment.Preproc, Whitespace, Comment.Preproc)), 

160 

161 # Literals 

162 (r'0[xX][\da-fA-F]+', Number.Hex), 

163 (r'-?[\d]+', Number), 

164 (r'-?[\d]+\.[\d]+', Number.Float), 

165 (r'"[^"]*"', String), 

166 (r'[Tt]rue|[Ff]alse', Name.Builtin), 

167 

168 # Identifiers 

169 (r'[a-zA-Z_]\w*', Name), 

170 ], 

171 } 

172 

173 

174class CapDLLexer(RegexLexer): 

175 """ 

176 Basic lexer for CapDL. 

177 

178 The source of the primary tool that reads such specifications is available 

179 at https://github.com/seL4/capdl/tree/master/capDL-tool. Note that this 

180 lexer only supports a subset of the grammar. For example, identifiers can 

181 shadow type names, but these instances are currently incorrectly 

182 highlighted as types. Supporting this would need a stateful lexer that is 

183 considered unnecessarily complex for now. 

184 

185 .. versionadded:: 2.2 

186 """ 

187 name = 'CapDL' 

188 url = 'https://ssrg.nicta.com.au/publications/nictaabstracts/Kuz_KLW_10.abstract.pml' 

189 aliases = ['capdl'] 

190 filenames = ['*.cdl'] 

191 

192 tokens = { 

193 'root': [ 

194 # C pre-processor directive 

195 (r'^(\s*)(#.*)(\n)', 

196 bygroups(Whitespace, Comment.Preproc, Whitespace)), 

197 

198 # Whitespace, comments 

199 (r'\s+', Whitespace), 

200 (r'/\*(.|\n)*?\*/', Comment), 

201 (r'(//|--).*$', Comment), 

202 

203 (r'[<>\[(){},:;=\]]', Punctuation), 

204 (r'\.\.', Punctuation), 

205 

206 (words(('arch', 'arm11', 'caps', 'child_of', 'ia32', 'irq', 'maps', 

207 'objects'), suffix=r'\b'), Keyword), 

208 

209 (words(('aep', 'asid_pool', 'cnode', 'ep', 'frame', 'io_device', 

210 'io_ports', 'io_pt', 'notification', 'pd', 'pt', 'tcb', 

211 'ut', 'vcpu'), suffix=r'\b'), Keyword.Type), 

212 

213 # Properties 

214 (words(('asid', 'addr', 'badge', 'cached', 'dom', 'domainID', 'elf', 

215 'fault_ep', 'G', 'guard', 'guard_size', 'init', 'ip', 

216 'prio', 'sp', 'R', 'RG', 'RX', 'RW', 'RWG', 'RWX', 'W', 

217 'WG', 'WX', 'level', 'masked', 'master_reply', 'paddr', 

218 'ports', 'reply', 'uncached'), suffix=r'\b'), 

219 Keyword.Reserved), 

220 

221 # Literals 

222 (r'0[xX][\da-fA-F]+', Number.Hex), 

223 (r'\d+(\.\d+)?(k|M)?', Number), 

224 (words(('bits',), suffix=r'\b'), Number), 

225 (words(('cspace', 'vspace', 'reply_slot', 'caller_slot', 

226 'ipc_buffer_slot'), suffix=r'\b'), Number), 

227 

228 # Identifiers 

229 (r'[a-zA-Z_][-@\.\w]*', Name), 

230 ], 

231 } 

232 

233 

234class RedcodeLexer(RegexLexer): 

235 """ 

236 A simple Redcode lexer based on ICWS'94. 

237 Contributed by Adam Blinkinsop <blinks@acm.org>. 

238 

239 .. versionadded:: 0.8 

240 """ 

241 name = 'Redcode' 

242 aliases = ['redcode'] 

243 filenames = ['*.cw'] 

244 

245 opcodes = ('DAT', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'MOD', 

246 'JMP', 'JMZ', 'JMN', 'DJN', 'CMP', 'SLT', 'SPL', 

247 'ORG', 'EQU', 'END') 

248 modifiers = ('A', 'B', 'AB', 'BA', 'F', 'X', 'I') 

249 

250 tokens = { 

251 'root': [ 

252 # Whitespace: 

253 (r'\s+', Whitespace), 

254 (r';.*$', Comment.Single), 

255 # Lexemes: 

256 # Identifiers 

257 (r'\b(%s)\b' % '|'.join(opcodes), Name.Function), 

258 (r'\b(%s)\b' % '|'.join(modifiers), Name.Decorator), 

259 (r'[A-Za-z_]\w+', Name), 

260 # Operators 

261 (r'[-+*/%]', Operator), 

262 (r'[#$@<>]', Operator), # mode 

263 (r'[.,]', Punctuation), # mode 

264 # Numbers 

265 (r'[-+]?\d+', Number.Integer), 

266 ], 

267 } 

268 

269 

270class AheuiLexer(RegexLexer): 

271 """ 

272 Aheui is esoteric language based on Korean alphabets. 

273 """ 

274 

275 name = 'Aheui' 

276 url = 'http://aheui.github.io/' 

277 aliases = ['aheui'] 

278 filenames = ['*.aheui'] 

279 

280 tokens = { 

281 'root': [ 

282 ('[' 

283 '나-낳냐-냫너-넣녀-녛노-놓뇨-눟뉴-닇' 

284 '다-닿댜-댷더-덯뎌-뎧도-돟됴-둫듀-딓' 

285 '따-땋땨-떃떠-떻뗘-뗳또-똫뚀-뚷뜌-띟' 

286 '라-랗랴-럏러-렇려-렿로-롷료-뤃류-릫' 

287 '마-맣먀-먛머-멓며-몋모-뫃묘-뭏뮤-믷' 

288 '바-밯뱌-뱧버-벟벼-볗보-봏뵤-붛뷰-빃' 

289 '빠-빻뺘-뺳뻐-뻫뼈-뼣뽀-뽛뾰-뿧쀼-삏' 

290 '사-샇샤-샿서-섷셔-셯소-솧쇼-숳슈-싛' 

291 '싸-쌓쌰-썋써-쎃쎠-쎻쏘-쏳쑈-쑿쓔-씧' 

292 '자-잫쟈-쟣저-젛져-졓조-좋죠-줗쥬-즿' 

293 '차-챃챠-챻처-첳쳐-쳫초-촣쵸-춯츄-칗' 

294 '카-캏캬-컇커-컿켜-켷코-콯쿄-쿻큐-킣' 

295 '타-탛탸-턓터-텋텨-톃토-톻툐-퉇튜-틯' 

296 '파-팧퍄-퍟퍼-펗펴-폏포-퐇표-풓퓨-픻' 

297 '하-핳햐-햫허-헣혀-혛호-홓효-훟휴-힇' 

298 ']', Operator), 

299 ('.', Comment), 

300 ], 

301 }