diff --git a/kclvm/Cargo.lock b/kclvm/Cargo.lock index 48cdc702b..a5c3a9886 100644 --- a/kclvm/Cargo.lock +++ b/kclvm/Cargo.lock @@ -624,6 +624,17 @@ dependencies = [ "serde_json", ] +[[package]] +name = "kclvm-ast-pretty" +version = "0.1.0" +dependencies = [ + "fancy-regex", + "indexmap", + "kclvm-ast", + "kclvm-error", + "kclvm-parser", +] + [[package]] name = "kclvm-capi" version = "0.1.0" @@ -836,6 +847,7 @@ dependencies = [ "fancy-regex", "indexmap", "kclvm-ast", + "kclvm-ast-pretty", "kclvm-config", "kclvm-error", "kclvm-parser", diff --git a/kclvm/Cargo.toml b/kclvm/Cargo.toml index f63512519..34f09e3b3 100644 --- a/kclvm/Cargo.toml +++ b/kclvm/Cargo.toml @@ -48,6 +48,7 @@ lto = true members = [ "capi", "ast", + "ast_pretty", "compiler", "config", "error", diff --git a/kclvm/ast/src/lib.rs b/kclvm/ast/src/lib.rs index 0f2cb628b..edaf80490 100644 --- a/kclvm/ast/src/lib.rs +++ b/kclvm/ast/src/lib.rs @@ -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; diff --git a/kclvm/ast_pretty/Cargo.toml b/kclvm/ast_pretty/Cargo.toml new file mode 100644 index 000000000..5d6b22e26 --- /dev/null +++ b/kclvm/ast_pretty/Cargo.toml @@ -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" \ No newline at end of file diff --git a/kclvm/ast/src/printer/mod.rs b/kclvm/ast_pretty/src/lib.rs similarity index 97% rename from kclvm/ast/src/printer/mod.rs rename to kclvm/ast_pretty/src/lib.rs index 02449d8e5..1c7f2f870 100644 --- a/kclvm/ast/src/printer/mod.rs +++ b/kclvm/ast_pretty/src/lib.rs @@ -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)] @@ -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 diff --git a/kclvm/ast/src/printer/node.rs b/kclvm/ast_pretty/src/node.rs similarity index 99% rename from kclvm/ast/src/printer/node.rs rename to kclvm/ast_pretty/src/node.rs index 50a7756a2..c9db75b3f 100644 --- a/kclvm/ast/src/printer/node.rs +++ b/kclvm/ast_pretty/src/node.rs @@ -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}; diff --git a/kclvm/ast/src/printer/test_data/arguments.input b/kclvm/ast_pretty/src/test_data/arguments.input similarity index 100% rename from kclvm/ast/src/printer/test_data/arguments.input rename to kclvm/ast_pretty/src/test_data/arguments.input diff --git a/kclvm/ast/src/printer/test_data/arguments.output b/kclvm/ast_pretty/src/test_data/arguments.output similarity index 100% rename from kclvm/ast/src/printer/test_data/arguments.output rename to kclvm/ast_pretty/src/test_data/arguments.output diff --git a/kclvm/ast/src/printer/test_data/codelayout.input b/kclvm/ast_pretty/src/test_data/codelayout.input similarity index 100% rename from kclvm/ast/src/printer/test_data/codelayout.input rename to kclvm/ast_pretty/src/test_data/codelayout.input diff --git a/kclvm/ast/src/printer/test_data/codelayout.output b/kclvm/ast_pretty/src/test_data/codelayout.output similarity index 100% rename from kclvm/ast/src/printer/test_data/codelayout.output rename to kclvm/ast_pretty/src/test_data/codelayout.output diff --git a/kclvm/ast/src/printer/test_data/collection_if.input b/kclvm/ast_pretty/src/test_data/collection_if.input similarity index 100% rename from kclvm/ast/src/printer/test_data/collection_if.input rename to kclvm/ast_pretty/src/test_data/collection_if.input diff --git a/kclvm/ast/src/printer/test_data/collection_if.output b/kclvm/ast_pretty/src/test_data/collection_if.output similarity index 100% rename from kclvm/ast/src/printer/test_data/collection_if.output rename to kclvm/ast_pretty/src/test_data/collection_if.output diff --git a/kclvm/ast/src/printer/test_data/comment.input b/kclvm/ast_pretty/src/test_data/comment.input similarity index 100% rename from kclvm/ast/src/printer/test_data/comment.input rename to kclvm/ast_pretty/src/test_data/comment.input diff --git a/kclvm/ast/src/printer/test_data/comment.output b/kclvm/ast_pretty/src/test_data/comment.output similarity index 100% rename from kclvm/ast/src/printer/test_data/comment.output rename to kclvm/ast_pretty/src/test_data/comment.output diff --git a/kclvm/ast/src/printer/test_data/empty.input b/kclvm/ast_pretty/src/test_data/empty.input similarity index 100% rename from kclvm/ast/src/printer/test_data/empty.input rename to kclvm/ast_pretty/src/test_data/empty.input diff --git a/kclvm/ast/src/printer/test_data/empty.output b/kclvm/ast_pretty/src/test_data/empty.output similarity index 100% rename from kclvm/ast/src/printer/test_data/empty.output rename to kclvm/ast_pretty/src/test_data/empty.output diff --git a/kclvm/ast/src/printer/test_data/if_stmt.input b/kclvm/ast_pretty/src/test_data/if_stmt.input similarity index 100% rename from kclvm/ast/src/printer/test_data/if_stmt.input rename to kclvm/ast_pretty/src/test_data/if_stmt.input diff --git a/kclvm/ast/src/printer/test_data/if_stmt.output b/kclvm/ast_pretty/src/test_data/if_stmt.output similarity index 100% rename from kclvm/ast/src/printer/test_data/if_stmt.output rename to kclvm/ast_pretty/src/test_data/if_stmt.output diff --git a/kclvm/ast/src/printer/test_data/import.input b/kclvm/ast_pretty/src/test_data/import.input similarity index 100% rename from kclvm/ast/src/printer/test_data/import.input rename to kclvm/ast_pretty/src/test_data/import.input diff --git a/kclvm/ast/src/printer/test_data/import.output b/kclvm/ast_pretty/src/test_data/import.output similarity index 100% rename from kclvm/ast/src/printer/test_data/import.output rename to kclvm/ast_pretty/src/test_data/import.output diff --git a/kclvm/ast/src/printer/test_data/index_sign.input b/kclvm/ast_pretty/src/test_data/index_sign.input similarity index 100% rename from kclvm/ast/src/printer/test_data/index_sign.input rename to kclvm/ast_pretty/src/test_data/index_sign.input diff --git a/kclvm/ast/src/printer/test_data/index_sign.output b/kclvm/ast_pretty/src/test_data/index_sign.output similarity index 100% rename from kclvm/ast/src/printer/test_data/index_sign.output rename to kclvm/ast_pretty/src/test_data/index_sign.output diff --git a/kclvm/ast/src/printer/test_data/joined_str.input b/kclvm/ast_pretty/src/test_data/joined_str.input similarity index 100% rename from kclvm/ast/src/printer/test_data/joined_str.input rename to kclvm/ast_pretty/src/test_data/joined_str.input diff --git a/kclvm/ast/src/printer/test_data/joined_str.output b/kclvm/ast_pretty/src/test_data/joined_str.output similarity index 100% rename from kclvm/ast/src/printer/test_data/joined_str.output rename to kclvm/ast_pretty/src/test_data/joined_str.output diff --git a/kclvm/ast/src/printer/test_data/lambda.input b/kclvm/ast_pretty/src/test_data/lambda.input similarity index 100% rename from kclvm/ast/src/printer/test_data/lambda.input rename to kclvm/ast_pretty/src/test_data/lambda.input diff --git a/kclvm/ast/src/printer/test_data/lambda.output b/kclvm/ast_pretty/src/test_data/lambda.output similarity index 100% rename from kclvm/ast/src/printer/test_data/lambda.output rename to kclvm/ast_pretty/src/test_data/lambda.output diff --git a/kclvm/ast/src/printer/test_data/quant.input b/kclvm/ast_pretty/src/test_data/quant.input similarity index 100% rename from kclvm/ast/src/printer/test_data/quant.input rename to kclvm/ast_pretty/src/test_data/quant.input diff --git a/kclvm/ast/src/printer/test_data/quant.output b/kclvm/ast_pretty/src/test_data/quant.output similarity index 100% rename from kclvm/ast/src/printer/test_data/quant.output rename to kclvm/ast_pretty/src/test_data/quant.output diff --git a/kclvm/ast/src/printer/test_data/rule.input b/kclvm/ast_pretty/src/test_data/rule.input similarity index 100% rename from kclvm/ast/src/printer/test_data/rule.input rename to kclvm/ast_pretty/src/test_data/rule.input diff --git a/kclvm/ast/src/printer/test_data/rule.output b/kclvm/ast_pretty/src/test_data/rule.output similarity index 100% rename from kclvm/ast/src/printer/test_data/rule.output rename to kclvm/ast_pretty/src/test_data/rule.output diff --git a/kclvm/ast/src/printer/test_data/type_alias.input b/kclvm/ast_pretty/src/test_data/type_alias.input similarity index 100% rename from kclvm/ast/src/printer/test_data/type_alias.input rename to kclvm/ast_pretty/src/test_data/type_alias.input diff --git a/kclvm/ast/src/printer/test_data/type_alias.output b/kclvm/ast_pretty/src/test_data/type_alias.output similarity index 100% rename from kclvm/ast/src/printer/test_data/type_alias.output rename to kclvm/ast_pretty/src/test_data/type_alias.output diff --git a/kclvm/ast/src/printer/test_data/unary.input b/kclvm/ast_pretty/src/test_data/unary.input similarity index 100% rename from kclvm/ast/src/printer/test_data/unary.input rename to kclvm/ast_pretty/src/test_data/unary.input diff --git a/kclvm/ast/src/printer/test_data/unary.output b/kclvm/ast_pretty/src/test_data/unary.output similarity index 100% rename from kclvm/ast/src/printer/test_data/unary.output rename to kclvm/ast_pretty/src/test_data/unary.output diff --git a/kclvm/ast/src/printer/test_data/unification.input b/kclvm/ast_pretty/src/test_data/unification.input similarity index 100% rename from kclvm/ast/src/printer/test_data/unification.input rename to kclvm/ast_pretty/src/test_data/unification.input diff --git a/kclvm/ast/src/printer/test_data/unification.output b/kclvm/ast_pretty/src/test_data/unification.output similarity index 100% rename from kclvm/ast/src/printer/test_data/unification.output rename to kclvm/ast_pretty/src/test_data/unification.output diff --git a/kclvm/ast/src/printer/tests.rs b/kclvm/ast_pretty/src/tests.rs similarity index 57% rename from kclvm/ast/src/printer/tests.rs rename to kclvm/ast_pretty/src/tests.rs index 76fcec852..c007b1a59 100644 --- a/kclvm/ast/src/printer/tests.rs +++ b/kclvm/ast_pretty/src/tests.rs @@ -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"; @@ -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(), ) } diff --git a/kclvm/parser/src/lib.rs b/kclvm/parser/src/lib.rs index ecfc34a32..be02086d2 100644 --- a/kclvm/parser/src/lib.rs +++ b/kclvm/parser/src/lib.rs @@ -70,6 +70,7 @@ pub fn parse_file(filename: &str, code: Option) -> Result