dalvik.py 4.3 KB

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