Skip to content

Commit

Permalink
feat: add some position info about import stmt node (#974)
Browse files Browse the repository at this point in the history
* feat: add some position info about import stmt node

Signed-off-by: he1pa <[email protected]>

* add invalid import asname test case

Signed-off-by: he1pa <[email protected]>

---------

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Jan 9, 2024
1 parent 161b376 commit 9a3e0ec
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 66 deletions.
4 changes: 2 additions & 2 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ pub struct IfStmt {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ImportStmt {
/// `path` is the import path, if 'import a.b.c' in kcl, `path` is a.b.c
pub path: String,
pub path: Node<String>,
pub rawpath: String,
pub name: String,
pub asname: Option<String>,
pub asname: Option<Node<String>>,
/// `pkg_name` means the name of the package that the current import statement indexs to.
///
/// 1. If the current import statement indexs to the kcl plugins, kcl builtin methods or the internal kcl packages,
Expand Down
4 changes: 2 additions & 2 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {

fn walk_import_stmt(&mut self, import_stmt: &'ctx ast::ImportStmt) -> Self::Result {
self.write("import ");
self.write(&import_stmt.path);
self.write(&import_stmt.path.node);
if let Some(as_name) = &import_stmt.asname {
self.write(" as ");
self.write(as_name);
self.write(&as_name.node);
}
self.write_newline_without_fill();
}
Expand Down
12 changes: 6 additions & 6 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
}
fn walk_import_stmt(&self, import_stmt: &'ctx ast::ImportStmt) -> Self::Result {
check_backtrack_stop!(self);
let pkgpath = import_stmt.path.as_str();
let pkgpath = import_stmt.path.node.as_str();
{
let imported = self.imported.borrow_mut();
if imported.contains(pkgpath) {
Expand All @@ -302,10 +302,10 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
// Nothing to do on the builtin system module import because the check has been done.
return self.ok_result();
} else {
let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path);
let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path.node);
self.pkgpath_stack.borrow_mut().push(pkgpath);
let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path);
let has_pkgpath = self.program.pkgs.contains_key(&import_stmt.path);
let pkgpath = format!("{}{}", PKG_PATH_PREFIX, import_stmt.path.node);
let has_pkgpath = self.program.pkgs.contains_key(&import_stmt.path.node);
let func_before_block = if self.no_link {
if has_pkgpath {
let func_before_block = self.append_block("");
Expand Down Expand Up @@ -346,7 +346,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
for ast_module in self
.program
.pkgs
.get(&import_stmt.path)
.get(&import_stmt.path.node)
.expect(kcl_error::INTERNAL_ERROR_MSG)
{
{
Expand All @@ -362,7 +362,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
for ast_module in self
.program
.pkgs
.get(&import_stmt.path)
.get(&import_stmt.path.node)
.expect(kcl_error::INTERNAL_ERROR_MSG)
{
{
Expand Down
12 changes: 6 additions & 6 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,22 @@ impl Loader {
for stmt in &mut m.body {
let pos = stmt.pos().clone();
if let ast::Stmt::Import(ref mut import_spec) = &mut stmt.node {
import_spec.path = kclvm_config::vfs::fix_import_path(
import_spec.path.node = kclvm_config::vfs::fix_import_path(
pkgroot,
&m.filename,
import_spec.path.as_str(),
import_spec.path.node.as_str(),
);
import_spec.pkg_name = pkg_name.to_string();
// Load the import package source code and compile.
if let Some(pkg_info) = self.load_package(
&pkgroot,
pkg_name.to_string(),
import_spec.path.to_string(),
import_spec.path.node.to_string(),
pos.into(),
pkgs,
)? {
// Add the external package name as prefix of the [`kclvm_ast::ImportStmt`]'s member [`path`].
import_spec.path = pkg_info.pkg_path.to_string();
import_spec.path.node = pkg_info.pkg_path.to_string();
import_spec.pkg_name = pkg_info.pkg_name
}
}
Expand All @@ -505,10 +505,10 @@ impl Loader {
fn fix_rel_import_path(&mut self, pkgroot: &str, m: &mut ast::Module) {
for stmt in &mut m.body {
if let ast::Stmt::Import(ref mut import_spec) = &mut stmt.node {
import_spec.path = kclvm_config::vfs::fix_import_path(
import_spec.path.node = kclvm_config::vfs::fix_import_path(
pkgroot,
&m.filename,
import_spec.path.as_str(),
import_spec.path.node.as_str(),
);
}
}
Expand Down
20 changes: 17 additions & 3 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl<'a> Parser<'a> {
fn parse_import_stmt(&mut self) -> NodeRef<Stmt> {
let token = self.token;
self.bump_keyword(kw::Import);
let dot_name_token = self.token;

let mut leading_dot = Vec::new();
while let TokenKind::DotDotDot = self.token.kind {
Expand All @@ -432,10 +433,19 @@ impl<'a> Parser<'a> {
self.bump_token(TokenKind::Dot);
}
let dot_name = self.parse_identifier().node;
let dot_name_end_token = self.prev_token;

let asname = if self.token.is_keyword(kw::As) {
self.bump_keyword(kw::As);
let ident = self.parse_identifier().node;
Some(ident.get_names().join("."))
match ident.names.len() {
1 => Some(ident.names[0].clone()),
_ => {
self.sess
.struct_span_error("Invalid import asname", self.token.span);
None
}
}
} else {
None
};
Expand All @@ -444,16 +454,20 @@ impl<'a> Parser<'a> {
path.push_str(dot_name.get_names().join(".").as_str());

let rawpath = path.clone();
let path_node = Node::node_with_pos(
path,
self.token_span_pos(dot_name_token, dot_name_end_token),
);

let name = if let Some(as_name_value) = asname.clone() {
as_name_value
as_name_value.node
} else {
dot_name.get_names().last().unwrap().clone()
};

let t = node_ref!(
Stmt::Import(ImportStmt {
path,
path: path_node,
rawpath,
name,
asname,
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,4 @@ parse_module_snapshot! { fn_ty_annotation_recovery_20, r#"a:(str|int) -> i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_21, r#"a:(str|int, int) -> i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_22, r#"a:(str|int, int|"#}
parse_module_snapshot! { fn_ty_annotation_recovery_23, r#"a:(str|int, int|) ->"#}
parse_module_snapshot! { import_recovery_0, r#"import json as j.a"#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"import json as j.a\"#)"
---
Module {
filename: "",
pkg: "",
doc: None,
name: "",
body: [
Node {
node: Import(
ImportStmt {
path: Node {
node: "json",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 11,
},
rawpath: "json",
name: "json",
asname: None,
pkg_name: "",
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 18,
},
],
comments: [],
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Module {
Node {
node: Import(
ImportStmt {
path: "",
path: Node {
node: "",
filename: "",
line: 1,
column: 6,
end_line: 1,
end_column: 6,
},
rawpath: "",
name: "",
asname: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Module {
Node {
node: Import(
ImportStmt {
path: "",
path: Node {
node: "",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 6,
},
rawpath: "",
name: "",
asname: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Module {
Node {
node: Import(
ImportStmt {
path: "pkg_path.",
path: Node {
node: "pkg_path.",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 16,
},
rawpath: "pkg_path.",
name: "",
asname: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Module {
Node {
node: Import(
ImportStmt {
path: "pkg_path",
path: Node {
node: "pkg_path",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 15,
},
rawpath: "pkg_path",
name: "pkg_path",
asname: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Module {
Node {
node: Import(
ImportStmt {
path: ".pkg_path.",
path: Node {
node: ".pkg_path.",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 17,
},
rawpath: ".pkg_path.",
name: "",
asname: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ Module {
Node {
node: Import(
ImportStmt {
path: "pkg_path",
path: Node {
node: "pkg_path",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 15,
},
rawpath: "pkg_path",
name: "",
asname: Some(
"",
Node {
node: "",
filename: "",
line: 1,
column: 19,
end_line: 1,
end_column: 19,
},
),
pkg_name: "",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ Module {
Node {
node: Import(
ImportStmt {
path: "pkg_path",
path: Node {
node: "pkg_path",
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 15,
},
rawpath: "pkg_path",
name: "",
asname: Some(
"",
Node {
node: "",
filename: "",
line: 1,
column: 19,
end_line: 1,
end_column: 19,
},
),
pkg_name: "",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: parser/src/tests/file.rs
expression: "crate::tests::parsing_file_string(\"testdata/import-01.k\")"
---
{"filename":"import-01.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Import":{"path":"a1","rawpath":"a1","name":"a1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":1,"column":0,"end_line":1,"end_column":9},{"node":{"Import":{"path":"a2","rawpath":"a2","name":"a2_pkg","asname":"a2_pkg","pkg_name":""}},"filename":"import-01.k","line":3,"column":0,"end_line":3,"end_column":19},{"node":{"Import":{"path":"subpkg.b1.c1","rawpath":"subpkg.b1.c1","name":"c1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":5,"column":0,"end_line":5,"end_column":19},{"node":{"Import":{"path":".a3","rawpath":".a3","name":"a3","asname":null,"pkg_name":""}},"filename":"import-01.k","line":7,"column":0,"end_line":7,"end_column":10}],"comments":[]}
{"filename":"import-01.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Import":{"path":{"node":"a1","filename":"import-01.k","line":1,"column":7,"end_line":1,"end_column":9},"rawpath":"a1","name":"a1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":1,"column":0,"end_line":1,"end_column":9},{"node":{"Import":{"path":{"node":"a2","filename":"import-01.k","line":3,"column":7,"end_line":3,"end_column":9},"rawpath":"a2","name":"a2_pkg","asname":{"node":"a2_pkg","filename":"import-01.k","line":3,"column":13,"end_line":3,"end_column":19},"pkg_name":""}},"filename":"import-01.k","line":3,"column":0,"end_line":3,"end_column":19},{"node":{"Import":{"path":{"node":"subpkg.b1.c1","filename":"import-01.k","line":5,"column":7,"end_line":5,"end_column":19},"rawpath":"subpkg.b1.c1","name":"c1","asname":null,"pkg_name":""}},"filename":"import-01.k","line":5,"column":0,"end_line":5,"end_column":19},{"node":{"Import":{"path":{"node":".a3","filename":"import-01.k","line":7,"column":7,"end_line":7,"end_column":10},"rawpath":".a3","name":"a3","asname":null,"pkg_name":""}},"filename":"import-01.k","line":7,"column":0,"end_line":7,"end_column":10}],"comments":[]}
6 changes: 3 additions & 3 deletions kclvm/query/src/override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ fn apply_import_paths_on_module(m: &mut ast::Module, import_paths: &[String]) ->
for stmt in &m.body {
if let ast::Stmt::Import(import_stmt) = &stmt.node {
if let Some(asname) = &import_stmt.asname {
exist_import_set.insert(format!("{} as {}", import_stmt.path, asname));
exist_import_set.insert(format!("{} as {}", import_stmt.path.node, asname.node));
} else {
exist_import_set.insert(import_stmt.path.to_string());
exist_import_set.insert(import_stmt.path.node.to_string());
}
}
}
Expand All @@ -206,7 +206,7 @@ fn apply_import_paths_on_module(m: &mut ast::Module, import_paths: &[String]) ->
.last()
.ok_or_else(|| anyhow!("Invalid import path {}", path))?;
let import_node = ast::ImportStmt {
path: path.to_string(),
path: ast::Node::dummy_node(path.to_string()),
rawpath: "".to_string(),
name: name.to_string(),
asname: None,
Expand Down
4 changes: 2 additions & 2 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
let ast_id = self.ctx.cur_node.clone();
let (start_pos, end_pos) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone());
let mut unresolved =
UnresolvedSymbol::new(import_stmt.path.clone(), start_pos, end_pos, None);
UnresolvedSymbol::new(import_stmt.path.node.clone(), start_pos, end_pos, None);
let package_symbol = self
.gs
.get_symbols()
.get_symbol_by_fully_qualified_name(&import_stmt.path)?;
.get_symbol_by_fully_qualified_name(&import_stmt.path.node)?;
unresolved.def = Some(package_symbol);
let unresolved_ref = self
.gs
Expand Down
4 changes: 2 additions & 2 deletions kclvm/sema/src/lint/lints_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl LintPass for ReImport {
let mut import_names = IndexSet::<String>::new();
for stmt in &module.body {
if let ast::Stmt::Import(import_stmt) = &stmt.node {
if import_names.contains(&import_stmt.path) {
if import_names.contains(&import_stmt.path.node) {
handler.add_warning(
WarningKind::ReimportWarning,
&[Message {
Expand All @@ -169,7 +169,7 @@ impl LintPass for ReImport {
}],
);
} else {
import_names.insert(import_stmt.path.clone());
import_names.insert(import_stmt.path.node.clone());
}
}
}
Expand Down
Loading

0 comments on commit 9a3e0ec

Please sign in to comment.