summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2012-10-30 09:38:41 -0700
committerTim Hatch <tim@timhatch.com>2012-10-30 09:38:41 -0700
commit603f0ff49bd1137a8428e18f111facd3c31adc86 (patch)
tree60a76dacdab82547fe2f09d8adee5396e397b246
parent02528e23b813468ed1b2489475a706a3ae828d81 (diff)
downloadpygments-603f0ff49bd1137a8428e18f111facd3c31adc86.tar.gz
Upstream lexer for Smali.
-rw-r--r--AUTHORS1
-rw-r--r--CHANGES1
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/dalvik.py104
-rw-r--r--tests/examplefiles/hello.smali40
5 files changed, 147 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index 6db0490c..f04c56b9 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -63,6 +63,7 @@ Other contributors, listed alphabetically, are:
* Marek Kubica -- Scheme lexer
* Jochen Kupperschmidt -- Markdown processor
* Gerd Kurzbach -- Modelica lexer
+* Jon Larimer, Google Inc. -- Smali lexer
* Olov Lassus -- Dart lexer
* Sylvestre Ledru -- Scilab lexer
* Mark Lee -- Vala lexer
diff --git a/CHANGES b/CHANGES
index 31322f30..230afc60 100644
--- a/CHANGES
+++ b/CHANGES
@@ -26,6 +26,7 @@ Version 1.6
* VGL (PR#12)
* SourcePawn (PR#39)
* Ceylon (PR#86)
+ * Smali (Dalvik assembly)
- Fix Template Haskell highlighting (PR#63)
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 0eca2682..d2cb3135 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -237,6 +237,7 @@ LEXERS = {
'SchemeLexer': ('pygments.lexers.functional', 'Scheme', ('scheme', 'scm'), ('*.scm', '*.ss'), ('text/x-scheme', 'application/x-scheme')),
'ScilabLexer': ('pygments.lexers.math', 'Scilab', ('scilab',), ('*.sci', '*.sce', '*.tst'), ('text/scilab',)),
'ScssLexer': ('pygments.lexers.web', 'SCSS', ('scss',), ('*.scss',), ('text/x-scss',)),
+ 'SmaliLexer': ('pygments.lexers.dalvik', 'Smali', ('smali',), ('*.smali',), ('text/smali',)),
'SmalltalkLexer': ('pygments.lexers.other', 'Smalltalk', ('smalltalk', 'squeak'), ('*.st',), ('text/x-smalltalk',)),
'SmartyLexer': ('pygments.lexers.templates', 'Smarty', ('smarty',), ('*.tpl',), ('application/x-smarty',)),
'SnobolLexer': ('pygments.lexers.other', 'Snobol', ('snobol',), ('*.snobol',), ('text/x-snobol',)),
diff --git a/pygments/lexers/dalvik.py b/pygments/lexers/dalvik.py
new file mode 100644
index 00000000..7da0953a
--- /dev/null
+++ b/pygments/lexers/dalvik.py
@@ -0,0 +1,104 @@
+# -*- coding: utf-8 -*-
+"""
+ pygments.lexers.dalvik
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Pygments lexers for Dalvik VM-related languages.
+
+ :copyright: Copyright 2011-2012 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+from pygments.lexer import RegexLexer, include, bygroups, using
+from pygments.token import Keyword, Text, Comment, Name, String, Number, \
+ Punctuation
+
+__all__ = ['SmaliLexer']
+
+
+class SmaliLexer(RegexLexer):
+ """
+ For `Smali <http://code.google.com/p/smali/>`_ (Android/Dalvik) assembly
+ code.
+
+ *New in Pygments 1.6.*
+ """
+ name = 'Smali'
+ aliases = ['smali']
+ filenames = ['*.smali']
+ mimetypes = ['text/smali']
+
+ tokens = {
+ 'root': [
+ include('comment'),
+ include('label'),
+ include('field'),
+ include('method'),
+ include('class'),
+ include('directive'),
+ include('access-modifier'),
+ include('instruction'),
+ include('literal'),
+ include('punctuation'),
+ include('type'),
+ include('whitespace')
+ ],
+ 'directive': [
+ (r'^[ \t]*\.(class|super|implements|field|subannotation|annotation|'
+ r'enum|method|registers|locals|array-data|packed-switch|'
+ r'sparse-switch|catchall|catch|line|parameter|local|prologue|'
+ r'epilogue|source)', Keyword),
+ (r'^[ \t]*\.end (field|subannotation|annotation|method|array-data|'
+ 'packed-switch|sparse-switch|parameter|local)', Keyword),
+ (r'^[ \t]*\.restart local', Keyword),
+ ],
+ 'access-modifier': [
+ (r'(public|private|protected|static|final|synchronized|bridge|'
+ r'varargs|native|abstract|strictfp|synthetic|constructor|'
+ r'declared-synchronized|interface|enum|annotation|volatile|'
+ r'transient)', Keyword),
+ ],
+ 'whitespace': [
+ (r'\n', Text),
+ (r'\s+', Text),
+ ],
+ 'instruction': [
+ (r'\b[vp]\d+\b', Name.Builtin), # registers
+ (r'\b[a-z][A-Za-z0-9/-]+\s+', Text), # instructions
+ ],
+ 'literal': [
+ (r'".*"', String),
+ (r'0x[0-9A-Fa-f]+t?', Number.Hex),
+ (r'[0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
+ (r'[0-9]+L?', Number.Integer),
+ ],
+ 'field': [
+ (r'\$?\b([A-Za-z0-9_$]*)(:)', bygroups(Name.Variable,
+ Punctuation)),
+ ],
+ 'method': [
+ (r'<(?:cl)?init>', Name.Function), # constructor
+ (r'\$?\b([A-Za-z0-9_$]*)(\()', bygroups(Name.Function,
+ Punctuation)),
+ ],
+ 'label': [
+ (r':[A-Za-z0-9_]+', Name.Label),
+ ],
+ 'class': [
+ # class names in the form Lcom/namespace/ClassName;
+ # I only want to color the ClassName part, so the namespace part is
+ # treated as 'Text'
+ (r'(L)((?:[A-Za-z0-9_$]+/)*)([A-Za-z0-9_$]+)(;)',
+ bygroups(Keyword.Type, Text, Name.Class, Text)),
+ ],
+ 'punctuation': [
+ (r'->', Punctuation),
+ (r'[{},\(\):=\.-]', Punctuation),
+ ],
+ 'type': [
+ (r'[ZBSCIJFDV\[]+', Keyword.Type),
+ ],
+ 'comment': [
+ (r'#.*?\n', Comment),
+ ],
+ }
diff --git a/tests/examplefiles/hello.smali b/tests/examplefiles/hello.smali
new file mode 100644
index 00000000..e539f00e
--- /dev/null
+++ b/tests/examplefiles/hello.smali
@@ -0,0 +1,40 @@
+# To Recreate:
+#
+# echo -e 'class hello {\n public static void main(String[] args) {\n
+# System.out.println("hi");\n }\n}\n' > hello.java
+# javac -target 1.4 -source 1.4 hello.java
+# dx --dex --output=hello.dex hello.class
+# baksmali hello.dex
+# cat out/hello.smali
+
+.class Lhello;
+.super Ljava/lang/Object;
+.source "hello.java"
+
+
+# direct methods
+.method constructor <init>()V
+ .registers 1
+
+ .prologue
+ .line 1
+ invoke-direct {p0}, Ljava/lang/Object;-><init>()V
+
+ return-void
+.end method
+
+.method public static main([Ljava/lang/String;)V
+ .registers 3
+ .parameter
+
+ .prologue
+ .line 3
+ sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
+
+ const-string v1, "hi"
+
+ invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
+
+ .line 4
+ return-void
+.end method