Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(kcl-vet): add ast builder for kcl-vet. #224

Merged
merged 10 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kclvm/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod lint;
pub mod printer;
pub mod query;
mod util;
pub mod vet;

#[macro_use]
extern crate kclvm_error;
8 changes: 7 additions & 1 deletion kclvm/tools/src/util/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use anyhow::{bail, Context, Result};
pub(crate) trait Loader<T> {
fn load(&self) -> Result<T>;
}
pub(crate) enum LoaderKind {

#[derive(Clone, Copy)]
pub enum LoaderKind {
YAML,
JSON,
}
Expand Down Expand Up @@ -41,6 +43,10 @@ impl DataLoader {
pub(crate) fn get_data(&self) -> &str {
&self.content
}

pub(crate) fn get_kind(&self) -> &LoaderKind {
&self.kind
}
}

impl Loader<serde_json::Value> for DataLoader {
Expand Down
5 changes: 1 addition & 4 deletions kclvm/tools/src/util/test_datas/test_invalid.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
{
"name": "John Doe",
"city": "London"
"name": "John Doe",
invalid

2 changes: 1 addition & 1 deletion kclvm/tools/src/util/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ websites:
panic!("unreachable")
}
Err(err) => {
assert_eq!(format!("{:?}", err), "Failed to String '{\n \"name\": \"John Doe\",\n \"city\": \"London\"\ninvalid\n\n' to Yaml\n\nCaused by:\n did not find expected ',' or '}' at line 4 column 1, while parsing a flow mapping");
assert_eq!(format!("{:?}", err), "Failed to String '\"name\": \"John Doe\",\ninvalid\n' to Yaml\n\nCaused by:\n did not find expected key at line 1 column 19, while parsing a block mapping");
}
}
}
Expand Down
242 changes: 242 additions & 0 deletions kclvm/tools/src/vet/expr_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
use kclvm_ast::{
ast::{
ConfigEntry, ConfigEntryOperation, ConfigExpr, Expr, ExprContext, Identifier, ListExpr,
NameConstant, NameConstantLit, Node, NodeRef, NumberLit, NumberLitValue, SchemaExpr,
StringLit,
},
node_ref,
};

use crate::util::loader::{DataLoader, Loader, LoaderKind};
use anyhow::{Context, Result};

trait ExprGenerator<T> {
fn generate(&self, value: &T) -> NodeRef<Expr>;
}

/// `ExprBuilder` will generate ast expr from Json/Yaml.
/// `Object` in Json and `Mapping` in Yaml is mapped to `Schema Expr`.
/// You should set `schema_name` for `Schema Expr` before using `ExprBuilder`.
pub(crate) struct ExprBuilder {
schema_name: Option<String>,
loader: DataLoader,
}

impl ExprBuilder {
pub(crate) fn new_with_file_path(
schema_name: Option<String>,
kind: LoaderKind,
file_path: String,
) -> Result<Self> {
let loader = DataLoader::new_with_file_path(kind, &file_path)
.with_context(|| format!("Failed to Load '{}'", file_path))?;

Ok(Self {
schema_name,
loader,
})
}

pub(crate) fn new_with_str(
schema_name: Option<String>,
kind: LoaderKind,
content: String,
) -> Result<Self> {
let loader = DataLoader::new_with_str(kind, &content)
.with_context(|| format!("Failed to Parse String '{}'", content))?;

Ok(Self {
schema_name,
loader,
})
}

/// Generate ast expr from Json/Yaml depends on `LoaderKind`.
pub(crate) fn build(&self) -> Result<NodeRef<Expr>> {
match self.loader.get_kind() {
LoaderKind::JSON => {
let value = <DataLoader as Loader<serde_json::Value>>::load(&self.loader)
.with_context(|| format!("Failed to Load JSON"))?;
Ok(self.generate(&value))
}
LoaderKind::YAML => {
let value = <DataLoader as Loader<serde_yaml::Value>>::load(&self.loader)
.with_context(|| format!("Failed to Load YAML"))?;
Ok(self.generate(&value))
}
}
}
}

impl ExprGenerator<serde_yaml::Value> for ExprBuilder {
fn generate(&self, value: &serde_yaml::Value) -> NodeRef<Expr> {
match value {
serde_yaml::Value::Null => {
node_ref!(Expr::NameConstantLit(NameConstantLit {
value: NameConstant::None,
}))
}
serde_yaml::Value::Bool(j_bool) => {
node_ref!(Expr::NameConstantLit(NameConstantLit {
value: NameConstant::try_from(*j_bool).unwrap()
}))
}
serde_yaml::Value::Number(j_num) => {
if j_num.is_f64() {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Float(j_num.as_f64().unwrap())
}))
} else if j_num.is_i64() {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Int(j_num.as_i64().unwrap())
}))
} else {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Int(j_num.as_u64().unwrap().try_into().unwrap())
}))
}
}
serde_yaml::Value::String(j_string) => {
node_ref!(Expr::StringLit(
StringLit::try_from(j_string.to_string()).unwrap()
))
}
serde_yaml::Value::Sequence(j_arr) => {
let mut j_arr_ast_nodes: Vec<NodeRef<Expr>> = Vec::new();
for j_arr_item in j_arr {
j_arr_ast_nodes.push(self.generate(j_arr_item));
}
node_ref!(Expr::List(ListExpr {
ctx: ExprContext::Load,
elts: j_arr_ast_nodes
}))
}
serde_yaml::Value::Mapping(j_map) => {
let mut config_entries: Vec<NodeRef<ConfigEntry>> = Vec::new();

for (k, v) in j_map.iter() {
let config_entry = node_ref!(ConfigEntry {
key: Some(self.generate(k)),
value: self.generate(v),
operation: ConfigEntryOperation::Union,
insert_index: -1
});
config_entries.push(config_entry);
}

let config_expr = node_ref!(Expr::Config(ConfigExpr {
items: config_entries
}));

match &self.schema_name {
Some(s_name) => {
let iden = node_ref!(Identifier {
names: vec![s_name.to_string()],
pkgpath: String::new(),
ctx: ExprContext::Load
});
node_ref!(Expr::Schema(SchemaExpr {
name: iden,
config: config_expr,
args: vec![],
kwargs: vec![]
}))
}
None => config_expr,
}
}
serde_yaml::Value::Tagged(_) => {
bug!("Yaml Tagged is not supported in KCL_Vet, this is an internal bug.")
}
}
}
}

impl ExprGenerator<serde_json::Value> for ExprBuilder {
fn generate(&self, value: &serde_json::Value) -> NodeRef<Expr> {
match value {
serde_json::Value::Null => {
node_ref!(Expr::NameConstantLit(NameConstantLit {
value: NameConstant::None,
}))
}
serde_json::Value::Bool(j_bool) => {
node_ref!(Expr::NameConstantLit(NameConstantLit {
value: NameConstant::try_from(*j_bool).unwrap()
}))
}
serde_json::Value::Number(j_num) => {
if j_num.is_f64() {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Float(j_num.as_f64().unwrap())
}))
} else if j_num.is_i64() {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Int(j_num.as_i64().unwrap())
}))
} else {
node_ref!(Expr::NumberLit(NumberLit {
binary_suffix: None,
value: NumberLitValue::Int(j_num.as_u64().unwrap().try_into().unwrap())
}))
}
}
serde_json::Value::String(j_string) => {
node_ref!(Expr::StringLit(
StringLit::try_from(j_string.to_string()).unwrap()
))
}
serde_json::Value::Array(j_arr) => {
let mut j_arr_ast_nodes: Vec<NodeRef<Expr>> = Vec::new();
for j_arr_item in j_arr {
j_arr_ast_nodes.push(self.generate(j_arr_item));
}
node_ref!(Expr::List(ListExpr {
ctx: ExprContext::Load,
elts: j_arr_ast_nodes
}))
}
serde_json::Value::Object(j_map) => {
let mut config_entries: Vec<NodeRef<ConfigEntry>> = Vec::new();

for (k, v) in j_map.iter() {
let config_entry = node_ref!(ConfigEntry {
key: Some(node_ref!(Expr::StringLit(
StringLit::try_from(k.to_string()).unwrap()
))),
value: self.generate(v),
operation: ConfigEntryOperation::Union,
insert_index: -1
});
config_entries.push(config_entry);
}

let config_expr = node_ref!(Expr::Config(ConfigExpr {
items: config_entries
}));

match &self.schema_name {
Some(s_name) => {
let iden = node_ref!(Identifier {
names: vec![s_name.to_string()],
pkgpath: String::new(),
ctx: ExprContext::Load
});
node_ref!(Expr::Schema(SchemaExpr {
name: iden,
config: config_expr,
args: vec![],
kwargs: vec![]
}))
}
None => config_expr,
}
}
}
}
}
4 changes: 4 additions & 0 deletions kclvm/tools/src/vet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod expr_builder;

#[cfg(test)]
mod tests;
2 changes: 2 additions & 0 deletions kclvm/tools/src/vet/test_datas/invalid/test_invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
languages:
- Ruby
5 changes: 5 additions & 0 deletions kclvm/tools/src/vet/test_datas/invalid/test_invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "John Doe",
"city": "London"
invalid

Loading