dalvik.py 4.5 KB

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