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