|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.dalvik |
|
4 ~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Pygments lexers for Dalvik VM-related languages. |
|
7 |
|
8 :copyright: Copyright 2006-2017 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 |
|
15 from pygments.token import Keyword, Text, Comment, Name, String, Number, \ |
|
16 Punctuation |
|
17 |
|
18 __all__ = ['SmaliLexer'] |
|
19 |
|
20 |
|
21 class SmaliLexer(RegexLexer): |
|
22 """ |
|
23 For `Smali <http://code.google.com/p/smali/>`_ (Android/Dalvik) assembly |
|
24 code. |
|
25 |
|
26 .. versionadded:: 1.6 |
|
27 """ |
|
28 name = 'Smali' |
|
29 aliases = ['smali'] |
|
30 filenames = ['*.smali'] |
|
31 mimetypes = ['text/smali'] |
|
32 |
|
33 tokens = { |
|
34 'root': [ |
|
35 include('comment'), |
|
36 include('label'), |
|
37 include('field'), |
|
38 include('method'), |
|
39 include('class'), |
|
40 include('directive'), |
|
41 include('access-modifier'), |
|
42 include('instruction'), |
|
43 include('literal'), |
|
44 include('punctuation'), |
|
45 include('type'), |
|
46 include('whitespace') |
|
47 ], |
|
48 'directive': [ |
|
49 (r'^[ \t]*\.(class|super|implements|field|subannotation|annotation|' |
|
50 r'enum|method|registers|locals|array-data|packed-switch|' |
|
51 r'sparse-switch|catchall|catch|line|parameter|local|prologue|' |
|
52 r'epilogue|source)', Keyword), |
|
53 (r'^[ \t]*\.end (field|subannotation|annotation|method|array-data|' |
|
54 'packed-switch|sparse-switch|parameter|local)', Keyword), |
|
55 (r'^[ \t]*\.restart local', Keyword), |
|
56 ], |
|
57 'access-modifier': [ |
|
58 (r'(public|private|protected|static|final|synchronized|bridge|' |
|
59 r'varargs|native|abstract|strictfp|synthetic|constructor|' |
|
60 r'declared-synchronized|interface|enum|annotation|volatile|' |
|
61 r'transient)', Keyword), |
|
62 ], |
|
63 'whitespace': [ |
|
64 (r'\n', Text), |
|
65 (r'\s+', Text), |
|
66 ], |
|
67 'instruction': [ |
|
68 (r'\b[vp]\d+\b', Name.Builtin), # registers |
|
69 (r'\b[a-z][A-Za-z0-9/-]+\s+', Text), # instructions |
|
70 ], |
|
71 'literal': [ |
|
72 (r'".*"', String), |
|
73 (r'0x[0-9A-Fa-f]+t?', Number.Hex), |
|
74 (r'[0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
|
75 (r'[0-9]+L?', Number.Integer), |
|
76 ], |
|
77 'field': [ |
|
78 (r'(\$?\b)([\w$]*)(:)', |
|
79 bygroups(Punctuation, Name.Variable, Punctuation)), |
|
80 ], |
|
81 'method': [ |
|
82 (r'<(?:cl)?init>', Name.Function), # constructor |
|
83 (r'(\$?\b)([\w$]*)(\()', |
|
84 bygroups(Punctuation, Name.Function, Punctuation)), |
|
85 ], |
|
86 'label': [ |
|
87 (r':\w+', Name.Label), |
|
88 ], |
|
89 'class': [ |
|
90 # class names in the form Lcom/namespace/ClassName; |
|
91 # I only want to color the ClassName part, so the namespace part is |
|
92 # treated as 'Text' |
|
93 (r'(L)((?:[\w$]+/)*)([\w$]+)(;)', |
|
94 bygroups(Keyword.Type, Text, Name.Class, Text)), |
|
95 ], |
|
96 'punctuation': [ |
|
97 (r'->', Punctuation), |
|
98 (r'[{},():=.-]', Punctuation), |
|
99 ], |
|
100 'type': [ |
|
101 (r'[ZBSCIJFDV\[]+', Keyword.Type), |
|
102 ], |
|
103 'comment': [ |
|
104 (r'#.*?\n', Comment), |
|
105 ], |
|
106 } |
|
107 |
|
108 def analyse_text(text): |
|
109 score = 0 |
|
110 if re.search(r'^\s*\.class\s', text, re.MULTILINE): |
|
111 score += 0.5 |
|
112 if re.search(r'\b((check-cast|instance-of|throw-verification-error' |
|
113 r')\b|(-to|add|[ais]get|[ais]put|and|cmpl|const|div|' |
|
114 r'if|invoke|move|mul|neg|not|or|rem|return|rsub|shl|' |
|
115 r'shr|sub|ushr)[-/])|{|}', text, re.MULTILINE): |
|
116 score += 0.3 |
|
117 if re.search(r'(\.(catchall|epilogue|restart local|prologue)|' |
|
118 r'\b(array-data|class-change-error|declared-synchronized|' |
|
119 r'(field|inline|vtable)@0x[0-9a-fA-F]|generic-error|' |
|
120 r'illegal-class-access|illegal-field-access|' |
|
121 r'illegal-method-access|instantiation-error|no-error|' |
|
122 r'no-such-class|no-such-field|no-such-method|' |
|
123 r'packed-switch|sparse-switch))\b', text, re.MULTILINE): |
|
124 score += 0.6 |
|
125 return score |