Skip to content

Commit

Permalink
Added support for Smali (#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jun 27, 2020
1 parent a13ee8d commit 22eb5ca
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,10 @@
"require": "bash",
"owner": "RunDevelopment"
},
"smali": {
"title": "Smali",
"owner": "RunDevelopment"
},
"smalltalk": {
"title": "Smalltalk",
"owner": "Golmote"
Expand Down
86 changes: 86 additions & 0 deletions components/prism-smali.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Test files for the parser itself:
// https://github.com/JesusFreke/smali/tree/master/smali/src/test/resources/LexerTest

Prism.languages.smali = {
'comment': /#.*/,
'string': {
pattern: /"(?:[^\r\n\\"]|\\.)*"|'(?:[^\r\n\\']|\\(?:.|u[\da-fA-F]{4}))'/,
greedy: true
},

'class-name': {
pattern: /L(?:(?:\w+|`[^`\r\n]*`)\/)*(?:[\w$]+|`[^`\r\n]*`)(?=\s*;)/,
inside: {
'class-name': {
pattern: /(^L|\/)(?:[\w$]+|`[^`\r\n]*`)$/,
lookbehind: true
},
'namespace': {
pattern: /^(L)(?:(?:\w+|`[^`\r\n]*`)\/)+/,
lookbehind: true,
inside: {
'punctuation': /\//
}
},
'builtin': /^L/
}
},
'builtin': [
{
// Reference: https://github.com/JesusFreke/smali/wiki/TypesMethodsAndFields#types
pattern: /([();\[])[BCDFIJSVZ]+/,
lookbehind: true
},
{
// e.g. .field mWifiOnUid:I
pattern: /([\w$>]:)[BCDFIJSVZ]/,
lookbehind: true
}
],
'keyword': [
{
pattern: /(\.end\s+)[\w-]+/,
lookbehind: true
},
{
pattern: /(^|[^\w.-])\.(?!\d)[\w-]+/,
lookbehind: true
},
{
pattern: /(^|[^\w.-])(?:abstract|annotation|bridge|constructor|enum|final|interface|private|protected|public|runtime|static|synthetic|system|transient)(?![\w.-])/,
lookbehind: true
}
],
'function': {
pattern: /(^|[^\w.-])(?:\w+|<[\w$-]+>)(?=\()/,
lookbehind: true
},

'field': {
pattern: /[\w$]+(?=:)/,
alias: 'variable'
},
'register': {
pattern: /(^|[^\w.-])[vp]\d(?![\w.-])/,
lookbehind: true,
alias: 'variable'
},

'boolean': {
pattern: /(^|[^\w.-])(?:true|false)(?![\w.-])/,
lookbehind: true
},
'number': {
pattern: /(^|[^/\w.-])-?(?:NAN|INFINITY|0x(?:[\dA-F]+(?:\.[\dA-F]*)?|\.[\dA-F]+)(?:p[+-]?[\dA-F]+)?|(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)[dflst]?(?![\w.-])/i,
lookbehind: true
},

'label': {
pattern: /(:)\w+/,
lookbehind: true,
alias: 'property'
},

'operator': /->|\.\.|[\[=]/,
'punctuation': /[{}(),;:]/
};
1 change: 1 addition & 0 deletions components/prism-smali.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions examples/prism-smali.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h2>Full example</h2>
<pre><code># Source: https://github.com/JesusFreke/smali/blob/master/examples/HelloWorld/HelloWorld.smali

.class public LHelloWorld;

#Ye olde hello world application
#To assemble and run this on a phone or emulator:
#
#java -jar smali.jar -o classes.dex HelloWorld.smali
#zip HelloWorld.zip classes.dex
#adb push HelloWorld.zip /data/local
#adb shell dalvikvm -cp /data/local/HelloWorld.zip HelloWorld
#
#if you get out of memory type errors when running smali.jar, try
#java -Xmx512m -jar smali.jar HelloWorld.smali
#instead

.super Ljava/lang/Object;

.method public static main([Ljava/lang/String;)V
.registers 2

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

const-string v1, "Hello World!"

invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V

return-void
.end method</code></pre>
13 changes: 13 additions & 0 deletions tests/languages/smali/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
true
false

----------------------------------------------------

[
["boolean", "true"],
["boolean", "false"]
]

----------------------------------------------------

Checks for booleans.
125 changes: 125 additions & 0 deletions tests/languages/smali/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
LMain;
Ljava/lang/String;
Lfoo/bar/Foo$Bar;
LFoo$Bar;

Ljava/lang/String;
LI;
LV;
LI/I/I;
L`single`;
L`java`/lang/String;
L`java`/`lang`/`String`;
Lspace/test/`20 a0 1680 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 200a 202f 205f 3000 `;


----------------------------------------------------

[
["class-name", [
["builtin", "L"],
["class-name", "Main"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"foo",
["punctuation", "/"],
"bar",
["punctuation", "/"]
]],
["class-name", "Foo$Bar"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["class-name", "Foo$Bar"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["class-name", "I"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["class-name", "V"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"I",
["punctuation", "/"],
"I",
["punctuation", "/"]
]],
["class-name", "I"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["class-name", "`single`"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"`java`",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"`java`",
["punctuation", "/"],
"`lang`",
["punctuation", "/"]
]],
["class-name", "`String`"]
]],
["punctuation", ";"],
["class-name", [
["builtin", "L"],
["namespace", [
"space",
["punctuation", "/"],
"test",
["punctuation", "/"]
]],
["class-name", "`20 a0 1680 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 200a 202f 205f 3000 `"]
]],
["punctuation", ";"]
]

----------------------------------------------------

Checks for class names.
11 changes: 11 additions & 0 deletions tests/languages/smali/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# comment

----------------------------------------------------

[
["comment", "# comment"]
]

----------------------------------------------------

Checks for comments.
18 changes: 18 additions & 0 deletions tests/languages/smali/field_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
foo:
$VALUES:
12:

----------------------------------------------------

[
["field", "foo"],
["punctuation", ":"],
["field", "$VALUES"],
["punctuation", ":"],
["field", "12"],
["punctuation", ":"]
]

----------------------------------------------------

Checks for fields.
85 changes: 85 additions & 0 deletions tests/languages/smali/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
foo()V
<init>(Ljava/lang/String;I)V
<clinit>()V

# a complex method
method(I[[IILjava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;

----------------------------------------------------

[
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],
["builtin", "V"],

["function", "<init>"],
["punctuation", "("],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"],
["builtin", "I"],
["punctuation", ")"],
["builtin", "V"],

["function", "<clinit>"],
["punctuation", "("],
["punctuation", ")"],
["builtin", "V"],

["comment", "# a complex method"],

["function", "method"],
["punctuation", "("],
["builtin", "I"],
["operator", "["],
["operator", "["],
["builtin", "II"],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"],
["operator", "["],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "Object"]
]],
["punctuation", ";"],
["punctuation", ")"],
["class-name", [
["builtin", "L"],
["namespace", [
"java",
["punctuation", "/"],
"lang",
["punctuation", "/"]
]],
["class-name", "String"]
]],
["punctuation", ";"]
]

----------------------------------------------------

Checks for functions/methods.
Loading

0 comments on commit 22eb5ca

Please sign in to comment.