From d106c979ee97f3bfdc6d2e2ec20762e270267437 Mon Sep 17 00:00:00 2001 From: Evan Richter Date: Thu, 1 Feb 2024 15:41:14 -0700 Subject: [PATCH] add smali language support --- build.rs | 5 +++++ manual/src/languages_supported.md | 1 + sample_files/compare.expected | 3 +++ sample_files/compare_all.sh | 2 +- sample_files/hello_world_after.smali | 29 ++++++++++++++++++++++++++ sample_files/hello_world_before.smali | 27 ++++++++++++++++++++++++ src/parse/guess_language.rs | 3 +++ src/parse/tree_sitter_parser.rs | 15 +++++++++++++ vendored_parsers/highlights/smali.scm | 1 + vendored_parsers/tree-sitter-smali-src | 1 + 10 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 sample_files/hello_world_after.smali create mode 100644 sample_files/hello_world_before.smali create mode 120000 vendored_parsers/highlights/smali.scm create mode 120000 vendored_parsers/tree-sitter-smali-src diff --git a/build.rs b/build.rs index 96e1a9cf4..222abf7cd 100644 --- a/build.rs +++ b/build.rs @@ -311,6 +311,11 @@ fn main() { src_dir: "vendored_parsers/tree-sitter-scss-src", extra_files: vec!["scanner.c"], }, + TreeSitterParser { + name: "tree-sitter-smali", + src_dir: "vendored_parsers/tree-sitter-smali-src", + extra_files: vec!["scanner.c"], + }, TreeSitterParser { name: "tree-sitter-sql", src_dir: "vendored_parsers/tree-sitter-sql-src", diff --git a/manual/src/languages_supported.md b/manual/src/languages_supported.md index 7f319969a..96a48ecae 100644 --- a/manual/src/languages_supported.md +++ b/manual/src/languages_supported.md @@ -47,6 +47,7 @@ with `difft --list-languages`. | Ruby | [tree-sitter/tree-sitter-ruby](https://github.com/tree-sitter/tree-sitter-ruby) | | Rust | [tree-sitter/tree-sitter-rust](https://github.com/tree-sitter/tree-sitter-rust) | | Scala | [tree-sitter/tree-sitter-scala](https://github.com/tree-sitter/tree-sitter-scala) | +| Smali | [amaanq/tree-sitter-smali](https://github.com/amaanq/tree-sitter-smali) | | Solidity | [JoranHonig/tree-sitter-solidity](https://github.com/JoranHonig/tree-sitter-solidity) | | SQL | [m-novikov/tree-sitter-sql](https://github.com/m-novikov/tree-sitter-sql) | | Swift | [alex-pinkus/tree-sitter-swift](https://github.com/alex-pinkus/tree-sitter-swift) | diff --git a/sample_files/compare.expected b/sample_files/compare.expected index bbe7516d7..c5f6913d0 100644 --- a/sample_files/compare.expected +++ b/sample_files/compare.expected @@ -76,6 +76,9 @@ sample_files/haskell_before.hs sample_files/haskell_after.hs sample_files/hcl_before.hcl sample_files/hcl_after.hcl 52e271ea8ffbe500d6bc3c47d35df13a - +sample_files/hello_world_before.smali sample_files/hello_world_after.smali +c2a686a9d70b8e54d49ef7fdf2f87fb7 - + sample_files/helpful-unit-test-before.el sample_files/helpful-unit-test-after.el f85460a4e60d77b4cae1a987a5af4c49 - diff --git a/sample_files/compare_all.sh b/sample_files/compare_all.sh index 8478c588e..91cfdc0dd 100755 --- a/sample_files/compare_all.sh +++ b/sample_files/compare_all.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Run difftastic on all the sample files, so we can see if any output # has been changed. diff --git a/sample_files/hello_world_after.smali b/sample_files/hello_world_after.smali new file mode 100644 index 000000000..56529003b --- /dev/null +++ b/sample_files/hello_world_after.smali @@ -0,0 +1,29 @@ +.class public LHelloPerson; + +#Ye olde hello world application +#To assemble and run this on a phone or emulator: +# +#java -jar smali.jar a -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 3 + + sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream; + + const-string v1, "Hello %s!" + + const-string v2, "Evan" + + invoke-virtual {v0, v1, v2}, Ljava/io/PrintStream;->printf(Ljava/lang/String;Ljava/lang/String;)V + + return-void +.end method diff --git a/sample_files/hello_world_before.smali b/sample_files/hello_world_before.smali new file mode 100644 index 000000000..2fd2c3246 --- /dev/null +++ b/sample_files/hello_world_before.smali @@ -0,0 +1,27 @@ +.class public LHelloWorld; + +#Ye olde hello world application +#To assemble and run this on a phone or emulator: +# +#java -jar smali.jar a -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 diff --git a/src/parse/guess_language.rs b/src/parse/guess_language.rs index c5dbd9bf3..732901669 100644 --- a/src/parse/guess_language.rs +++ b/src/parse/guess_language.rs @@ -67,6 +67,7 @@ pub(crate) enum Language { Rust, Scala, Scss, + Smali, Solidity, Sql, Swift, @@ -155,6 +156,7 @@ pub(crate) fn language_name(language: Language) -> &'static str { Ruby => "Ruby", Rust => "Rust", Scala => "Scala", + Smali => "Smali", Scss => "SCSS", Solidity => "Solidity", Sql => "SQL", @@ -341,6 +343,7 @@ pub(crate) fn language_globs(language: Language) -> Vec { ], Rust => &["*.rs"], Scala => &["*.scala", "*.sbt", "*.sc"], + Smali => &["*.smali"], Scss => &["*.scss"], Solidity => &["*.sol"], Sql => &["*.sql", "*.pgsql"], diff --git a/src/parse/tree_sitter_parser.rs b/src/parse/tree_sitter_parser.rs index e59aa1be1..845581bef 100644 --- a/src/parse/tree_sitter_parser.rs +++ b/src/parse/tree_sitter_parser.rs @@ -107,6 +107,7 @@ extern "C" { fn tree_sitter_ruby() -> ts::Language; fn tree_sitter_rust() -> ts::Language; fn tree_sitter_scala() -> ts::Language; + fn tree_sitter_smali() -> ts::Language; fn tree_sitter_scss() -> ts::Language; fn tree_sitter_solidity() -> ts::Language; fn tree_sitter_sql() -> ts::Language; @@ -978,6 +979,20 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig { sub_languages: vec![], } } + Smali => { + let language = unsafe { tree_sitter_smali() }; + TreeSitterConfig { + language, + atom_nodes: HashSet::from(["string"]), + delimiter_tokens: Vec::new(), + highlight_query: ts::Query::new( + language, + include_str!("../../vendored_parsers/highlights/smali.scm"), + ) + .unwrap(), + sub_languages: Vec::new(), + } + } Solidity => { let language = unsafe { tree_sitter_solidity() }; TreeSitterConfig { diff --git a/vendored_parsers/highlights/smali.scm b/vendored_parsers/highlights/smali.scm new file mode 120000 index 000000000..8296e2f69 --- /dev/null +++ b/vendored_parsers/highlights/smali.scm @@ -0,0 +1 @@ +../tree-sitter-smali/queries/highlights.scm \ No newline at end of file diff --git a/vendored_parsers/tree-sitter-smali-src b/vendored_parsers/tree-sitter-smali-src new file mode 120000 index 000000000..ab67f8267 --- /dev/null +++ b/vendored_parsers/tree-sitter-smali-src @@ -0,0 +1 @@ +tree-sitter-smali/src/ \ No newline at end of file