~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/pygments/lexers/asm.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
"""
3
 
    pygments.lexers.asm
4
 
    ~~~~~~~~~~~~~~~~~~~
5
 
 
6
 
    Lexers for assembly languages.
7
 
 
8
 
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
 
    :license: BSD, see LICENSE for details.
10
 
"""
11
 
 
12
 
import re
13
 
 
14
 
from pygments.lexer import RegexLexer, include, bygroups, using, DelegatingLexer
15
 
from pygments.lexers.compiled import DLexer, CppLexer, CLexer
16
 
from pygments.token import *
17
 
 
18
 
__all__ = ['GasLexer', 'ObjdumpLexer','DObjdumpLexer', 'CppObjdumpLexer',
19
 
           'CObjdumpLexer', 'LlvmLexer', 'NasmLexer']
20
 
 
21
 
 
22
 
class GasLexer(RegexLexer):
23
 
    """
24
 
    For Gas (AT&T) assembly code.
25
 
    """
26
 
    name = 'GAS'
27
 
    aliases = ['gas']
28
 
    filenames = ['*.s', '*.S']
29
 
    mimetypes = ['text/x-gas']
30
 
 
31
 
    #: optional Comment or Whitespace
32
 
    string = r'"(\\"|[^"])*"'
33
 
    char = r'[a-zA-Z$._0-9@]'
34
 
    identifier = r'(?:[a-zA-Z$_]' + char + '*|\.' + char + '+)'
35
 
    number = r'(?:0[xX][a-zA-Z0-9]+|\d+)'
36
 
 
37
 
    tokens = {
38
 
        'root': [
39
 
            include('whitespace'),
40
 
            (identifier + ':', Name.Label),
41
 
            (r'\.' + identifier, Name.Attribute, 'directive-args'),
42
 
            (r'lock|rep(n?z)?|data\d+', Name.Attribute),
43
 
            (identifier, Name.Function, 'instruction-args'),
44
 
            (r'[\r\n]+', Text)
45
 
        ],
46
 
        'directive-args': [
47
 
            (identifier, Name.Constant),
48
 
            (string, String),
49
 
            ('@' + identifier, Name.Attribute),
50
 
            (number, Number.Integer),
51
 
            (r'[\r\n]+', Text, '#pop'),
52
 
 
53
 
            (r'#.*?$', Comment, '#pop'),
54
 
 
55
 
            include('punctuation'),
56
 
            include('whitespace')
57
 
        ],
58
 
        'instruction-args': [
59
 
            # For objdump-disassembled code, shouldn't occur in
60
 
            # actual assembler input
61
 
            ('([a-z0-9]+)( )(<)('+identifier+')(>)',
62
 
                bygroups(Number.Hex, Text, Punctuation, Name.Constant,
63
 
                         Punctuation)),
64
 
            ('([a-z0-9]+)( )(<)('+identifier+')([-+])('+number+')(>)',
65
 
                bygroups(Number.Hex, Text, Punctuation, Name.Constant,
66
 
                         Punctuation, Number.Integer, Punctuation)),
67
 
 
68
 
            # Address constants
69
 
            (identifier, Name.Constant),
70
 
            (number, Number.Integer),
71
 
            # Registers
72
 
            ('%' + identifier, Name.Variable),
73
 
            # Numeric constants
74
 
            ('$'+number, Number.Integer),
75
 
            (r'[\r\n]+', Text, '#pop'),
76
 
            (r'#.*?$', Comment, '#pop'),
77
 
            include('punctuation'),
78
 
            include('whitespace')
79
 
        ],
80
 
        'whitespace': [
81
 
            (r'\n', Text),
82
 
            (r'\s+', Text),
83
 
            (r'#.*?\n', Comment)
84
 
        ],
85
 
        'punctuation': [
86
 
            (r'[-*,.():]+', Punctuation)
87
 
        ]
88
 
    }
89
 
 
90
 
    def analyse_text(text):
91
 
        return re.match(r'^\.\w+', text, re.M)
92
 
 
93
 
class ObjdumpLexer(RegexLexer):
94
 
    """
95
 
    For the output of 'objdump -dr'
96
 
    """
97
 
    name = 'objdump'
98
 
    aliases = ['objdump']
99
 
    filenames = ['*.objdump']
100
 
    mimetypes = ['text/x-objdump']
101
 
 
102
 
    hex = r'[0-9A-Za-z]'
103
 
 
104
 
    tokens = {
105
 
        'root': [
106
 
            # File name & format:
107
 
            ('(.*?)(:)( +file format )(.*?)$',
108
 
                bygroups(Name.Label, Punctuation, Text, String)),
109
 
            # Section header
110
 
            ('(Disassembly of section )(.*?)(:)$',
111
 
                bygroups(Text, Name.Label, Punctuation)),
112
 
            # Function labels
113
 
            # (With offset)
114
 
            ('('+hex+'+)( )(<)(.*?)([-+])(0[xX][A-Za-z0-9]+)(>:)$',
115
 
                bygroups(Number.Hex, Text, Punctuation, Name.Function,
116
 
                         Punctuation, Number.Hex, Punctuation)),
117
 
            # (Without offset)
118
 
            ('('+hex+'+)( )(<)(.*?)(>:)$',
119
 
                bygroups(Number.Hex, Text, Punctuation, Name.Function,
120
 
                         Punctuation)),
121
 
            # Code line with disassembled instructions
122
 
            ('( *)('+hex+r'+:)(\t)((?:'+hex+hex+' )+)( *\t)([a-zA-Z].*?)$',
123
 
                bygroups(Text, Name.Label, Text, Number.Hex, Text,
124
 
                         using(GasLexer))),
125
 
            # Code line with ascii
126
 
            ('( *)('+hex+r'+:)(\t)((?:'+hex+hex+' )+)( *)(.*?)$',
127
 
                bygroups(Text, Name.Label, Text, Number.Hex, Text, String)),
128
 
            # Continued code line, only raw opcodes without disassembled
129
 
            # instruction
130
 
            ('( *)('+hex+r'+:)(\t)((?:'+hex+hex+' )+)$',
131
 
                bygroups(Text, Name.Label, Text, Number.Hex)),
132
 
            # Skipped a few bytes
133
 
            ('\t\.\.\.$', Text),
134
 
            # Relocation line
135
 
            # (With offset)
136
 
            ('(\t\t\t)('+hex+'+:)( )([^\t]+)(\t)(.*?)([-+])(0x' + hex + '+)$',
137
 
                bygroups(Text, Name.Label, Text, Name.Property, Text,
138
 
                         Name.Constant, Punctuation, Number.Hex)),
139
 
            # (Without offset)
140
 
            ('(\t\t\t)('+hex+'+:)( )([^\t]+)(\t)(.*?)$',
141
 
                bygroups(Text, Name.Label, Text, Name.Property, Text,
142
 
                         Name.Constant)),
143
 
            ('[^\n]+\n', Other)
144
 
        ]
145
 
    }
146
 
 
147
 
 
148
 
class DObjdumpLexer(DelegatingLexer):
149
 
    """
150
 
    For the output of 'objdump -Sr on compiled D files'
151
 
    """
152
 
    name = 'd-objdump'
153
 
    aliases = ['d-objdump']
154
 
    filenames = ['*.d-objdump']
155
 
    mimetypes = ['text/x-d-objdump']
156
 
 
157
 
    def __init__(self, **options):
158
 
        super(DObjdumpLexer, self).__init__(DLexer, ObjdumpLexer, **options)
159
 
 
160
 
 
161
 
class CppObjdumpLexer(DelegatingLexer):
162
 
    """
163
 
    For the output of 'objdump -Sr on compiled C++ files'
164
 
    """
165
 
    name = 'cpp-objdump'
166
 
    aliases = ['cpp-objdump', 'c++-objdumb', 'cxx-objdump']
167
 
    filenames = ['*.cpp-objdump', '*.c++-objdump', '*.cxx-objdump']
168
 
    mimetypes = ['text/x-cpp-objdump']
169
 
 
170
 
    def __init__(self, **options):
171
 
        super(CppObjdumpLexer, self).__init__(CppLexer, ObjdumpLexer, **options)
172
 
 
173
 
 
174
 
class CObjdumpLexer(DelegatingLexer):
175
 
    """
176
 
    For the output of 'objdump -Sr on compiled C files'
177
 
    """
178
 
    name = 'c-objdump'
179
 
    aliases = ['c-objdump']
180
 
    filenames = ['*.c-objdump']
181
 
    mimetypes = ['text/x-c-objdump']
182
 
 
183
 
    def __init__(self, **options):
184
 
        super(CObjdumpLexer, self).__init__(CLexer, ObjdumpLexer, **options)
185
 
 
186
 
 
187
 
class LlvmLexer(RegexLexer):
188
 
    """
189
 
    For LLVM assembly code.
190
 
    """
191
 
    name = 'LLVM'
192
 
    aliases = ['llvm']
193
 
    filenames = ['*.ll']
194
 
    mimetypes = ['text/x-llvm']
195
 
 
196
 
    #: optional Comment or Whitespace
197
 
    string = r'"[^"]*?"'
198
 
    identifier = r'([-a-zA-Z$._][-a-zA-Z$._0-9]*|' + string + ')'
199
 
 
200
 
    tokens = {
201
 
        'root': [
202
 
            include('whitespace'),
203
 
 
204
 
            # Before keywords, because keywords are valid label names :(...
205
 
            (r'^\s*' + identifier + '\s*:', Name.Label),
206
 
 
207
 
            include('keyword'),
208
 
 
209
 
            (r'%' + identifier, Name.Variable),#Name.Identifier.Local),
210
 
            (r'@' + identifier, Name.Variable.Global),#Name.Identifier.Global),
211
 
            (r'%\d+', Name.Variable.Anonymous),#Name.Identifier.Anonymous),
212
 
            (r'@\d+', Name.Variable.Global),#Name.Identifier.Anonymous),
213
 
            (r'!' + identifier, Name.Variable),
214
 
            (r'!\d+', Name.Variable.Anonymous),
215
 
            (r'c?' + string, String),
216
 
 
217
 
            (r'0[xX][a-fA-F0-9]+', Number),
218
 
            (r'-?\d+(?:[.]\d+)?(?:[eE][-+]?\d+(?:[.]\d+)?)?', Number),
219
 
 
220
 
            (r'[=<>{}\[\]()*.,!]|x\b', Punctuation)
221
 
        ],
222
 
        'whitespace': [
223
 
            (r'(\n|\s)+', Text),
224
 
            (r';.*?\n', Comment)
225
 
        ],
226
 
        'keyword': [
227
 
            # Regular keywords
228
 
            (r'(begin|end'
229
 
             r'|true|false'
230
 
             r'|declare|define'
231
 
             r'|global|constant'
232
 
 
233
 
             r'|private|linker_private|internal|available_externally|linkonce'
234
 
             r'|linkonce_odr|weak|weak_odr|appending|dllimport|dllexport'
235
 
             r'|common|default|hidden|protected|extern_weak|external'
236
 
             r'|thread_local|zeroinitializer|undef|null|to|tail|target|triple'
237
 
             r'|deplibs|datalayout|volatile|nuw|nsw|exact|inbounds|align'
238
 
             r'|addrspace|section|alias|module|asm|sideeffect|gc|dbg'
239
 
 
240
 
             r'|ccc|fastcc|coldcc|x86_stdcallcc|x86_fastcallcc|arm_apcscc'
241
 
             r'|arm_aapcscc|arm_aapcs_vfpcc'
242
 
 
243
 
             r'|cc|c'
244
 
 
245
 
             r'|signext|zeroext|inreg|sret|nounwind|noreturn|noalias|nocapture'
246
 
             r'|byval|nest|readnone|readonly'
247
 
 
248
 
             r'|inlinehint|noinline|alwaysinline|optsize|ssp|sspreq|noredzone'
249
 
             r'|noimplicitfloat|naked'
250
 
 
251
 
             r'|type|opaque'
252
 
 
253
 
             r'|eq|ne|slt|sgt|sle'
254
 
             r'|sge|ult|ugt|ule|uge'
255
 
             r'|oeq|one|olt|ogt|ole'
256
 
             r'|oge|ord|uno|ueq|une'
257
 
             r'|x'
258
 
 
259
 
             # instructions
260
 
             r'|add|fadd|sub|fsub|mul|fmul|udiv|sdiv|fdiv|urem|srem|frem|shl'
261
 
             r'|lshr|ashr|and|or|xor|icmp|fcmp'
262
 
 
263
 
             r'|phi|call|trunc|zext|sext|fptrunc|fpext|uitofp|sitofp|fptoui'
264
 
             r'fptosi|inttoptr|ptrtoint|bitcast|select|va_arg|ret|br|switch'
265
 
             r'|invoke|unwind|unreachable'
266
 
 
267
 
             r'|malloc|alloca|free|load|store|getelementptr'
268
 
 
269
 
             r'|extractelement|insertelement|shufflevector|getresult'
270
 
             r'|extractvalue|insertvalue'
271
 
 
272
 
             r')\b', Keyword),
273
 
 
274
 
            # Types
275
 
            (r'void|float|double|x86_fp80|fp128|ppc_fp128|label|metadata',
276
 
             Keyword.Type),
277
 
 
278
 
            # Integer types
279
 
            (r'i[1-9]\d*', Keyword)
280
 
        ]
281
 
    }
282
 
 
283
 
 
284
 
class NasmLexer(RegexLexer):
285
 
    """
286
 
    For Nasm (Intel) assembly code.
287
 
    """
288
 
    name = 'NASM'
289
 
    aliases = ['nasm']
290
 
    filenames = ['*.asm', '*.ASM']
291
 
    mimetypes = ['text/x-nasm']
292
 
 
293
 
    identifier = r'[a-zA-Z$._?][a-zA-Z0-9$._?#@~]*'
294
 
    hexn = r'(?:0[xX][0-9a-fA-F]+|$0[0-9a-fA-F]*|[0-9]+[0-9a-fA-F]*h)'
295
 
    octn = r'[0-7]+q'
296
 
    binn = r'[01]+b'
297
 
    decn = r'[0-9]+'
298
 
    floatn = decn + r'\.e?' + decn
299
 
    string = r'"(\\"|[^"])*"|' + r"'(\\'|[^'])*'"
300
 
    declkw = r'(?:res|d)[bwdqt]|times'
301
 
    register = (r'[a-d][lh]|e?[a-d]x|e?[sb]p|e?[sd]i|[c-gs]s|st[0-7]|'
302
 
                r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]')
303
 
    wordop = r'seg|wrt|strict'
304
 
    type = r'byte|[dq]?word'
305
 
    directives = (r'BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|'
306
 
                  r'ORG|ALIGN|STRUC|ENDSTRUC|COMMON|CPU|GROUP|UPPERCASE|IMPORT|'
307
 
                  r'EXPORT|LIBRARY|MODULE')
308
 
 
309
 
    flags = re.IGNORECASE | re.MULTILINE
310
 
    tokens = {
311
 
        'root': [
312
 
            include('whitespace'),
313
 
            (r'^\s*%', Comment.Preproc, 'preproc'),
314
 
            (identifier + ':', Name.Label),
315
 
            (r'(%s)(\s+)(equ)' % identifier,
316
 
                bygroups(Name.Constant, Keyword.Declaration, Keyword.Declaration),
317
 
                'instruction-args'),
318
 
            (directives, Keyword, 'instruction-args'),
319
 
            (declkw, Keyword.Declaration, 'instruction-args'),
320
 
            (identifier, Name.Function, 'instruction-args'),
321
 
            (r'[\r\n]+', Text)
322
 
        ],
323
 
        'instruction-args': [
324
 
            (string, String),
325
 
            (hexn, Number.Hex),
326
 
            (octn, Number.Oct),
327
 
            (binn, Number),
328
 
            (floatn, Number.Float),
329
 
            (decn, Number.Integer),
330
 
            include('punctuation'),
331
 
            (register, Name.Builtin),
332
 
            (identifier, Name.Variable),
333
 
            (r'[\r\n]+', Text, '#pop'),
334
 
            include('whitespace')
335
 
        ],
336
 
        'preproc': [
337
 
            (r'[^;\n]+', Comment.Preproc),
338
 
            (r';.*?\n', Comment.Single, '#pop'),
339
 
            (r'\n', Comment.Preproc, '#pop'),
340
 
        ],
341
 
        'whitespace': [
342
 
            (r'\n', Text),
343
 
            (r'[ \t]+', Text),
344
 
            (r';.*', Comment.Single)
345
 
        ],
346
 
        'punctuation': [
347
 
            (r'[,():\[\]]+', Punctuation),
348
 
            (r'[&|^<>+*/%~-]+', Operator),
349
 
            (r'[$]+', Keyword.Constant),
350
 
            (wordop, Operator.Word),
351
 
            (type, Keyword.Type)
352
 
        ],
353
 
    }