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