3 pygments.lexers.esoteric |
3 pygments.lexers.esoteric |
4 ~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for esoteric languages. |
6 Lexers for esoteric languages. |
7 |
7 |
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 from pygments.lexer import RegexLexer, include, words |
12 from pygments.lexer import RegexLexer, include, words |
13 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
13 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
14 Number, Punctuation, Error |
14 Number, Punctuation, Error |
|
15 |
|
16 import re |
15 |
17 |
16 __all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer', 'CAmkESLexer', |
18 __all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer', 'CAmkESLexer', |
17 'CapDLLexer', 'AheuiLexer'] |
19 'CapDLLexer', 'AheuiLexer'] |
18 |
20 |
19 |
21 |
45 (r'\[', Keyword, '#push'), |
47 (r'\[', Keyword, '#push'), |
46 (r'\]', Keyword, '#pop'), |
48 (r'\]', Keyword, '#pop'), |
47 include('common'), |
49 include('common'), |
48 ] |
50 ] |
49 } |
51 } |
|
52 |
|
53 def analyse_text(text): |
|
54 """It's safe to assume that a program which mostly consists of + - |
|
55 and < > is brainfuck.""" |
|
56 plus_minus_count = 0 |
|
57 greater_less_count = 0 |
|
58 |
|
59 range_to_check = max(256, len(text)) |
|
60 |
|
61 for c in text[:range_to_check]: |
|
62 if c == '+' or c == '-': |
|
63 plus_minus_count += 1 |
|
64 if c == '<' or c == '>': |
|
65 greater_less_count += 1 |
|
66 |
|
67 if plus_minus_count > (0.25 * range_to_check): |
|
68 return 1.0 |
|
69 if greater_less_count > (0.25 * range_to_check): |
|
70 return 1.0 |
|
71 |
|
72 result = 0 |
|
73 if '[-]' in text: |
|
74 result += 0.5 |
|
75 |
|
76 return result |
50 |
77 |
51 |
78 |
52 class BefungeLexer(RegexLexer): |
79 class BefungeLexer(RegexLexer): |
53 """ |
80 """ |
54 Lexer for the esoteric `Befunge <http://en.wikipedia.org/wiki/Befunge>`_ |
81 Lexer for the esoteric `Befunge <http://en.wikipedia.org/wiki/Befunge>`_ |