Skip to content

Commit

Permalink
fix: raw identifier in the schema string attribute
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Feb 18, 2024
1 parent a735a37 commit c5dd159
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion kclvm/sema/src/pre_process/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ impl<'ctx> MutSelfMutWalker<'ctx> for RawIdentifierTransformer {
.collect::<Vec<Node<String>>>();
}
fn walk_schema_attr(&mut self, schema_attr: &'ctx mut ast::SchemaAttr) {
schema_attr.name.node = remove_raw_ident_prefix(&schema_attr.name.node);
// If the attribute is an identifier and then fix it.
// Note that we do not fix a string-like attribute e.g., `"$name"`
if schema_attr.name.end_column - schema_attr.name.column
<= schema_attr.name.node.chars().count() as u64
{
schema_attr.name.node = remove_raw_ident_prefix(&schema_attr.name.node);
}
walk_list_mut!(self, walk_call_expr, schema_attr.decorators);
walk_if_mut!(self, walk_expr, schema_attr.value);
}
Expand Down
8 changes: 8 additions & 0 deletions kclvm/sema/src/pre_process/test_data/raw_identifier.k
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
$schema = 1

schema Name:
$name?: str
"$name"?: str

n = Name {
"$name": "1"
}
14 changes: 14 additions & 0 deletions kclvm/sema/src/pre_process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ fn test_fix_raw_identifier_prefix() {
} else {
panic!("invalid assign statement")
}
if let ast::Stmt::Schema(schema_stmt) = &module.body[1].node {
if let ast::Stmt::SchemaAttr(attr) = &schema_stmt.body[0].node {
assert_eq!(attr.name.node, "name");
} else {
panic!("invalid schema attr")
}
if let ast::Stmt::SchemaAttr(attr) = &schema_stmt.body[1].node {
assert_eq!(attr.name.node, "$name");
} else {
panic!("invalid schema attr")
}
} else {
panic!("invalid schema statement")
}
}

#[test]
Expand Down

0 comments on commit c5dd159

Please sign in to comment.