dalvik.py 4.3 KB

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