|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.esoteric |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for esoteric languages. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, include |
|
13 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
14 Number, Punctuation, Error |
|
15 |
|
16 __all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer'] |
|
17 |
|
18 |
|
19 class BrainfuckLexer(RegexLexer): |
|
20 """ |
|
21 Lexer for the esoteric `BrainFuck <http://www.muppetlabs.com/~breadbox/bf/>`_ |
|
22 language. |
|
23 """ |
|
24 |
|
25 name = 'Brainfuck' |
|
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 |
|
51 class BefungeLexer(RegexLexer): |
|
52 """ |
|
53 Lexer for the esoteric `Befunge <http://en.wikipedia.org/wiki/Befunge>`_ |
|
54 language. |
|
55 |
|
56 .. versionadded:: 0.7 |
|
57 """ |
|
58 name = 'Befunge' |
|
59 aliases = ['befunge'] |
|
60 filenames = ['*.befunge'] |
|
61 mimetypes = ['application/x-befunge'] |
|
62 |
|
63 tokens = { |
|
64 'root': [ |
|
65 (r'[0-9a-f]', Number), |
|
66 (r'[+*/%!`-]', Operator), # Traditional math |
|
67 (r'[<>^v?\[\]rxjk]', Name.Variable), # Move, imperatives |
|
68 (r'[:\\$.,n]', Name.Builtin), # Stack ops, imperatives |
|
69 (r'[|_mw]', Keyword), |
|
70 (r'[{}]', Name.Tag), # Befunge-98 stack ops |
|
71 (r'".*?"', String.Double), # Strings don't appear to allow escapes |
|
72 (r'\'.', String.Single), # Single character |
|
73 (r'[#;]', Comment), # Trampoline... depends on direction hit |
|
74 (r'[pg&~=@iotsy]', Keyword), # Misc |
|
75 (r'[()A-Z]', Comment), # Fingerprints |
|
76 (r'\s+', Text), # Whitespace doesn't matter |
|
77 ], |
|
78 } |
|
79 |
|
80 |
|
81 class RedcodeLexer(RegexLexer): |
|
82 """ |
|
83 A simple Redcode lexer based on ICWS'94. |
|
84 Contributed by Adam Blinkinsop <blinks@acm.org>. |
|
85 |
|
86 .. versionadded:: 0.8 |
|
87 """ |
|
88 name = 'Redcode' |
|
89 aliases = ['redcode'] |
|
90 filenames = ['*.cw'] |
|
91 |
|
92 opcodes = ('DAT', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'MOD', |
|
93 'JMP', 'JMZ', 'JMN', 'DJN', 'CMP', 'SLT', 'SPL', |
|
94 'ORG', 'EQU', 'END') |
|
95 modifiers = ('A', 'B', 'AB', 'BA', 'F', 'X', 'I') |
|
96 |
|
97 tokens = { |
|
98 'root': [ |
|
99 # Whitespace: |
|
100 (r'\s+', Text), |
|
101 (r';.*$', Comment.Single), |
|
102 # Lexemes: |
|
103 # Identifiers |
|
104 (r'\b(%s)\b' % '|'.join(opcodes), Name.Function), |
|
105 (r'\b(%s)\b' % '|'.join(modifiers), Name.Decorator), |
|
106 (r'[A-Za-z_]\w+', Name), |
|
107 # Operators |
|
108 (r'[-+*/%]', Operator), |
|
109 (r'[#$@<>]', Operator), # mode |
|
110 (r'[.,]', Punctuation), # mode |
|
111 # Numbers |
|
112 (r'[-+]?\d+', Number.Integer), |
|
113 ], |
|
114 } |