From 24be1f165ba0ab37beaa615a9563829e4d06fb4e Mon Sep 17 00:00:00 2001 From: Ameya Ketkar <94497232+ketkarameya@users.noreply.github.com> Date: Mon, 7 Aug 2023 10:32:49 -0700 Subject: [PATCH] Add support for scala (#569) --- Cargo.toml | 1 + site/docs/reference/languages.md | 1 + src/models/default_configs.rs | 1 + src/models/language.rs | 14 +++++++++++-- src/tests/mod.rs | 2 ++ src/tests/test_piranha_scala.rs | 21 +++++++++++++++++++ .../configurations/rules.toml | 5 +++++ .../expected/Sample.scala | 16 ++++++++++++++ .../simple_match_replace/input/Sample.scala | 16 ++++++++++++++ 9 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_piranha_scala.rs create mode 100644 test-resources/scala/simple_match_replace/configurations/rules.toml create mode 100644 test-resources/scala/simple_match_replace/expected/Sample.scala create mode 100644 test-resources/scala/simple_match_replace/input/Sample.scala diff --git a/Cargo.toml b/Cargo.toml index 303d0b485..1d99a640e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/site/docs/reference/languages.md b/site/docs/reference/languages.md index 00b08e114..a2712fba1 100644 --- a/site/docs/reference/languages.md +++ b/site/docs/reference/languages.md @@ -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: | diff --git a/src/models/default_configs.rs b/src/models/default_configs.rs index 0224a0305..c4d14e376 100644 --- a/src/models/default_configs.rs +++ b/src/models/default_configs.rs @@ -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 "; diff --git a/src/models/language.rs b/src/models/language.rs index e857b8b21..dc16d3566 100644 --- a/src/models/language.rs +++ b/src/models/language.rs @@ -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, @@ -67,6 +67,7 @@ pub enum SupportedLanguage { Thrift, Strings, TsScheme, + Scala, } impl PiranhaLanguage { @@ -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"), } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index e5bc6929a..4a57fdd19 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -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; diff --git a/src/tests/test_piranha_scala.rs b/src/tests/test_piranha_scala.rs new file mode 100644 index 000000000..29cb56b31 --- /dev/null +++ b/src/tests/test_piranha_scala.rs @@ -0,0 +1,21 @@ +/* +Copyright (c) 2023 Uber Technologies, Inc. + +

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 +

http://www.apache.org/licenses/LICENSE-2.0 + +

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; + +} diff --git a/test-resources/scala/simple_match_replace/configurations/rules.toml b/test-resources/scala/simple_match_replace/configurations/rules.toml new file mode 100644 index 000000000..2abd3cac5 --- /dev/null +++ b/test-resources/scala/simple_match_replace/configurations/rules.toml @@ -0,0 +1,5 @@ +[[rules]] +name = "Replace + operator" +query = "cs :[x] + :[y]" +replace_node = "*" +replace = "add(@x,@y)" diff --git a/test-resources/scala/simple_match_replace/expected/Sample.scala b/test-resources/scala/simple_match_replace/expected/Sample.scala new file mode 100644 index 000000000..429325fd4 --- /dev/null +++ b/test-resources/scala/simple_match_replace/expected/Sample.scala @@ -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") + } +} diff --git a/test-resources/scala/simple_match_replace/input/Sample.scala b/test-resources/scala/simple_match_replace/input/Sample.scala new file mode 100644 index 000000000..f89d228d1 --- /dev/null +++ b/test-resources/scala/simple_match_replace/input/Sample.scala @@ -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") + } +}