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

Refactor(kclvm-tools): move 'printer' from 'kclvm-tools' to 'kclvm-ast'. #228

Merged
merged 5 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 22 additions & 12 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ lto = true
members = [
"capi",
"ast",
"ast_pretty",
"compiler",
"config",
"error",
Expand Down
14 changes: 14 additions & 0 deletions kclvm/ast_pretty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "kclvm-ast-pretty"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kclvm-parser = {path = "../parser", version = "0.1.0"}
kclvm-error = {path = "../error", version = "0.1.0"}
kclvm-ast = {path = "../ast", version = "0.1.0"}
indexmap = "1.0"
fancy-regex = "0.7.1"
pretty_assertions = "1.3.0"
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use indexmap::IndexMap;
use kclvm_ast::{
ast::{self, Module},
token::TokenKind,
walker::MutSelfTypedResultWalker,
};
use std::collections::VecDeque;

use kclvm_ast::{ast, token::TokenKind, walker::MutSelfTypedResultWalker};

mod node;

#[cfg(test)]
Expand Down Expand Up @@ -244,7 +247,7 @@ impl<'p> Printer<'p> {
}

/// Print AST to string
pub fn print_ast_module(module: &ast::Module) -> String {
pub fn print_ast_module(module: &Module) -> String {
let mut printer = Printer::default();
printer.write_module(module);
printer.out
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::collections::HashSet;

use kclvm_ast::ast::{self, CallExpr};
use kclvm_ast::token::{DelimToken, TokenKind};
use kclvm_ast::walker::MutSelfTypedResultWalker;
use kclvm_ast::{
ast::{self, CallExpr},
token::{DelimToken, TokenKind},
walker::MutSelfTypedResultWalker,
};
use kclvm_error::bug;

use super::{Indentation, Printer};

Expand Down
23 changes: 13 additions & 10 deletions kclvm/tools/src/printer/tests.rs → kclvm/ast_pretty/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::*;
use std::path::PathBuf;

use super::print_ast_module;
use kclvm_parser::parse_file;
use pretty_assertions::assert_eq;

Expand All @@ -23,18 +25,19 @@ const TEST_CASES: &[&'static str; 15] = &[
];

fn read_data(data_name: &str) -> (String, String) {
let module = parse_file(
&format!("./src/printer/test_data/{}{}", data_name, FILE_INPUT_SUFFIX),
None,
);
let mut filename = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
filename.push(&format!("src/test_data/{}{}", data_name, FILE_INPUT_SUFFIX));

let module = parse_file(filename.to_str().unwrap(), None);

let mut filename_expect = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
filename_expect.push(&format!(
"src/test_data/{}{}",
data_name, FILE_OUTPUT_SUFFIX
));
(
print_ast_module(&module.unwrap()),
std::fs::read_to_string(&format!(
"./src/printer/test_data/{}{}",
data_name, FILE_OUTPUT_SUFFIX
))
.unwrap(),
std::fs::read_to_string(filename_expect.to_str().unwrap()).unwrap(),
)
}

Expand Down
2 changes: 2 additions & 0 deletions kclvm/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ kclvm-error = {path = "../error", version = "0.1.0"}
kclvm-parser = {path = "../parser", version = "0.1.0"}
kclvm-sema = {path = "../sema", version = "0.1.0"}
kclvm-config = {path = "../config", version = "0.1.0"}
kclvm-ast-pretty = {path = "../ast_pretty", version = "0.1.0"}

serde_json = "1.0.85"
serde_yaml = "0.9.13"

Expand Down
3 changes: 2 additions & 1 deletion kclvm/tools/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//! AST Module, and then use the AST printer [kclvm_tools::printer::print_ast_module]
//! to print it as source code string.
use anyhow::{anyhow, Result};
use kclvm_ast_pretty::print_ast_module;
use std::path::Path;

use crate::{printer::print_ast_module, util::get_kcl_files};
use crate::util::get_kcl_files;
use kclvm_parser::parse_file;

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion kclvm/tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod format;
pub mod lint;
pub mod printer;
pub mod query;
mod util;
pub mod vet;
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ mod util;

use anyhow::{anyhow, Result};
use kclvm_ast::ast;
use kclvm_ast_pretty::print_ast_module;
use kclvm_parser::parse_file;

pub use r#override::{apply_override_on_module, apply_overrides};

use self::r#override::parse_override_spec;
use crate::printer::print_ast_module;

/// Override and rewrite a file with override specifications. Please note that this is an external user API,
/// and it can directly modify the KCL file in place.
Expand Down
3 changes: 1 addition & 2 deletions kclvm/tools/src/query/override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use kclvm_ast::config::try_get_config_expr_mut;
use kclvm_ast::path::{get_attr_paths_from_config_expr, get_key_path};
use kclvm_ast::walker::MutSelfMutWalker;
use kclvm_ast::{ast, walk_if_mut};
use kclvm_ast_pretty::print_ast_module;
use kclvm_parser::parse_expr;
use kclvm_sema::pre_process::{fix_config_expr_nest_attr, transform_multi_assign};

use crate::printer::print_ast_module;

use super::util::{invalid_spec_error, split_field_path};

/// Import statement column offset always start with 1.
Expand Down
1 change: 0 additions & 1 deletion kclvm/tools/src/query/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{r#override::apply_override_on_module, *};
use crate::printer::print_ast_module;
use kclvm_ast::ast;
use kclvm_parser::parse_file;
use pretty_assertions::assert_eq;
Expand Down