Skip to content

Commit

Permalink
mv query to ast_pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Oct 8, 2022
1 parent 55680e6 commit b15d822
Show file tree
Hide file tree
Showing 42 changed files with 54 additions and 21 deletions.
12 changes: 12 additions & 0 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
1 change: 0 additions & 1 deletion kclvm/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::ast::*;
pub mod ast;
pub mod config;
pub mod path;
pub mod printer;
pub mod token;
pub mod token_stream;
pub mod walker;
Expand Down
13 changes: 13 additions & 0 deletions kclvm/ast_pretty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[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"
9 changes: 6 additions & 3 deletions kclvm/ast/src/printer/mod.rs → kclvm/ast_pretty/src/lib.rs
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 crate::{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,12 +1,11 @@
use std::collections::HashSet;

use kclvm_error::bug;

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

use super::{Indentation, Printer};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 14 additions & 11 deletions kclvm/ast/src/printer/tests.rs → kclvm/ast_pretty/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
use std::path::PathBuf;

use kclvm_parser::parse_file;
use pretty_assertions::assert_eq;

use super::print_ast_module;

const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".output";
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
1 change: 1 addition & 0 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn parse_file(filename: &str, code: Option<String>) -> Result<ast::Module, S
let src = if let Some(s) = code {
s
} else {
println!("{:?}", filename);
std::fs::read_to_string(filename).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
2 changes: 1 addition & 1 deletion kclvm/tools/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +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::util::get_kcl_files;
use kclvm_ast::printer::print_ast_module;
use kclvm_parser::parse_file;

#[cfg(test)]
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 kclvm_ast::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
2 changes: 1 addition & 1 deletion kclvm/tools/src/query/override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use anyhow::{anyhow, Result};

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::printer::print_ast_module;
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};

Expand Down

0 comments on commit b15d822

Please sign in to comment.