Skip to content

Commit

Permalink
Add support for scala (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
ketkarameya authored Aug 7, 2023
1 parent afcf8e2 commit 24be1f1
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tree-sitter-go = { git = "https://github.com/uber/tree-sitter-go.git", rev = "8f
tree-sitter-thrift = "0.5.0"
tree-sitter-strings = { git = "https://github.com/uber/tree-sitter-strings.git" }
tree-sitter-query = "0.1.0"
tree-sitter-scala = "0.20.1"
derive_builder = "0.12.0"
getset = "0.1.2"
pyo3 = "0.19.0"
Expand Down
1 change: 1 addition & 0 deletions site/docs/reference/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ title: Languages supported
| Python | :heavy_check_mark: | :calendar: | :calendar: |
| TypeScript | :heavy_check_mark: | :calendar: | :calendar: |
| TypeScript+React | :heavy_check_mark: | :calendar: | :calendar: |
| Scala | :heavy_check_mark: | :calendar: | :calendar: |
| C# | :calendar: | :calendar: | :calendar: |
| JavaScript | :calendar: | :calendar: | :calendar: |
1 change: 1 addition & 0 deletions src/models/default_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub const TSX: &str = "tsx";
pub const THRIFT: &str = "thrift";
pub const STRINGS: &str = "strings";
pub const TS_SCHEME: &str = "scm"; // We support scheme files that contain tree-sitter query
pub const SCALA: &str = "scala";

pub const REGEX_QUERY_PREFIX: &str = "rgx ";
pub const CONCRETE_SYNTAX_QUERY_PREFIX: &str = "cs ";
Expand Down
14 changes: 12 additions & 2 deletions src/models/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::utilities::parse_toml;

use super::{
default_configs::{
default_language, GO, JAVA, JAVA_CS, KOTLIN, PYTHON, STRINGS, SWIFT, THRIFT, TSX, TS_SCHEME,
TYPESCRIPT,
default_language, GO, JAVA, JAVA_CS, KOTLIN, PYTHON, SCALA, STRINGS, SWIFT, THRIFT, TSX,
TS_SCHEME, TYPESCRIPT,
},
outgoing_edges::Edges,
rule::Rules,
Expand Down Expand Up @@ -67,6 +67,7 @@ pub enum SupportedLanguage {
Thrift,
Strings,
TsScheme,
Scala,
}

impl PiranhaLanguage {
Expand Down Expand Up @@ -260,6 +261,15 @@ impl std::str::FromStr for PiranhaLanguage {
scopes: vec![],
comment_nodes: vec![],
}),
SCALA => Ok(PiranhaLanguage {
extension: language.to_string(),
supported_language: SupportedLanguage::Scala,
language: tree_sitter_scala::language(),
rules: None,
edges: None,
scopes: vec![],
comment_nodes: vec![],
}),
_ => Err("Language not supported"),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod test_piranha_go;
mod test_piranha_ts;
mod test_piranha_tsx;

mod test_piranha_scala;

mod test_piranha_thrift;

mod test_piranha_scm;
Expand Down
21 changes: 21 additions & 0 deletions src/tests/test_piranha_scala.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright (c) 2023 Uber Technologies, Inc.
<p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
<p>http://www.apache.org/licenses/LICENSE-2.0
<p>Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/

use super::create_rewrite_tests;
use crate::models::default_configs::SCALA;

create_rewrite_tests! {
SCALA,
test_simple_match_replace: "simple_match_replace", 1;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[rules]]
name = "Replace + operator"
query = "cs :[x] + :[y]"
replace_node = "*"
replace = "add(@x,@y)"
16 changes: 16 additions & 0 deletions test-resources/scala/simple_match_replace/expected/Sample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object AddTwoNumbers {
def main(args: Array[String]): Unit = {
// Read input from the user
print("Enter first number: ")
val num1 = scala.io.StdIn.readDouble()

print("Enter second number: ")
val num2 = scala.io.StdIn.readDouble()

// Calculate the sum
val sum = add(num1, num2)

// Display the result
println(s"The sum of $num1 and $num2 is: $sum")
}
}
16 changes: 16 additions & 0 deletions test-resources/scala/simple_match_replace/input/Sample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object AddTwoNumbers {
def main(args: Array[String]): Unit = {
// Read input from the user
print("Enter first number: ")
val num1 = scala.io.StdIn.readDouble()

print("Enter second number: ")
val num2 = scala.io.StdIn.readDouble()

// Calculate the sum
val sum = num1 + num2

// Display the result
println(s"The sum of $num1 and $num2 is: $sum")
}
}

0 comments on commit 24be1f1

Please sign in to comment.