Skip to content

Commit

Permalink
Adding support for YAML (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvmarcilio authored Jul 18, 2024
1 parent c47edc3 commit c650f56
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ 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"
# newer versions of "tree-sitter-yaml" require that we bump "tree-sitter" version
tree-sitter-yaml = "0.0.1"
derive_builder = "0.12.0"
getset = "0.1.2"
pyo3 = "0.20.0"
Expand Down
2 changes: 1 addition & 1 deletion polyglot_piranha.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from __future__ import annotations
from typing import List, Optional, Literal

# Languages that Piranha supports (see ./src/models/language.rs)
PiranhaLanguage = Literal["java", "kt", "kotlin", "go", "py", "swift", "ts", "tsx", "thrift", "strings", "scm", "scala", "rb"]
PiranhaLanguage = Literal["java", "kt", "kotlin", "go", "py", "swift", "ts", "tsx", "thrift", "strings", "scm", "scala", "rb", "yaml", "yml"]


def execute_piranha(piranha_argument: PiranhaArguments) -> list[PiranhaOutputSummary]:
Expand Down
2 changes: 2 additions & 0 deletions src/models/default_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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 RUBY: &str = "rb";
pub const YAML: &str = "yaml";
pub const YAML_ALIAS: &str = "yml";

pub const REGEX_QUERY_PREFIX: &str = "rgx ";
pub const CONCRETE_SYNTAX_QUERY_PREFIX: &str = "cs ";
Expand Down
12 changes: 11 additions & 1 deletion src/models/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::utilities::parse_toml;
use super::{
default_configs::{
default_language, GO, JAVA, JAVA_CS, KOTLIN, KOTLIN_ALIAS, PYTHON, RUBY, SCALA, STRINGS, SWIFT,
THRIFT, TSX, TS_SCHEME, TYPESCRIPT,
THRIFT, TSX, TS_SCHEME, TYPESCRIPT, YAML, YAML_ALIAS,
},
outgoing_edges::Edges,
rule::Rules,
Expand Down Expand Up @@ -69,6 +69,7 @@ pub enum SupportedLanguage {
TsScheme,
Scala,
Ruby,
Yaml,
}

impl PiranhaLanguage {
Expand Down Expand Up @@ -290,6 +291,15 @@ impl std::str::FromStr for PiranhaLanguage {
edges: Some(ruby_edges),
})
}
YAML | YAML_ALIAS => Ok(PiranhaLanguage {
extension: language.to_string(),
supported_language: SupportedLanguage::Yaml,
language: tree_sitter_yaml::language(),
rules: None,
edges: None,
scopes: vec![],
comment_nodes: vec![],
}),
_ => Err("Language not supported"),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod test_piranha_thrift;
mod test_piranha_ruby;
mod test_piranha_scm;
mod test_piranha_strings;
mod test_piranha_yaml;

use std::sync::Once;

Expand Down Expand Up @@ -165,7 +166,7 @@ fn execute_piranha_and_check_result(
/// "java",
/// test_a1: "relative/path_1", HashMap::from([("match_class", 2));
/// test_a2: "relative/path_2", HashMap::from([("match_class", 2), ("match_class_1", 1)])
///
///
/// ;
/// }
/// ```
Expand Down
24 changes: 24 additions & 0 deletions src/tests/test_piranha_yaml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
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 crate::models::default_configs::YAML;

use super::{create_rewrite_tests, substitutions};

create_rewrite_tests! {
YAML,
test_yaml_find_replace_with_holes: "find_replace/",1,
substitutions= substitutions! {
"value_to_replace" => "localhost"
};
}
32 changes: 32 additions & 0 deletions test-resources/yaml/find_replace/configurations/rules.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2024 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.

[[rules]]
name = "replace_hostname_value"
query = """(
(block_mapping_pair
key: (flow_node) @config_key
value: (block_node
(block_mapping
(block_mapping_pair
key: (flow_node) @hostname_key
value: (flow_node) @hostname_value
) @inner_mapping
) @block_mapping
) @block_node
(#eq? @config_key "config")
(#eq? @hostname_key "hostname")
(#eq? @hostname_value "\\\"@value_to_replace\\\"")
) @outer_mapping
)"""
replace_node = "hostname_value"
replace = "\"127.0.0.1\""
holes = ["value_to_replace"]
16 changes: 16 additions & 0 deletions test-resources/yaml/find_replace/expected/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 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.

config:
hostname: "127.0.0.1"
port: 8080
username: "admin"
password: "admin"
16 changes: 16 additions & 0 deletions test-resources/yaml/find_replace/input/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 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.

config:
hostname: "localhost"
port: 8080
username: "admin"
password: "admin"

0 comments on commit c650f56

Please sign in to comment.