Skip to content

Commit

Permalink
fix: unification stmt schema arguments parse
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 23, 2024
1 parent 8f0671a commit 07aadc8
Show file tree
Hide file tree
Showing 12 changed files with 669 additions and 6 deletions.
8 changes: 6 additions & 2 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ impl<'a> Parser<'a> {
))
}

fn parse_call(&mut self, func: NodeRef<Expr>) -> CallExpr {
pub(crate) fn parse_call(&mut self, func: NodeRef<Expr>) -> CallExpr {
// LEFT_PARENTHESES
match self.token.kind {
TokenKind::OpenDelim(DelimToken::Paren) => self.bump(),
Expand Down Expand Up @@ -1906,7 +1906,11 @@ impl<'a> Parser<'a> {

/// Syntax:
/// schema_expr: identifier LEFT_PARENTHESES [arguments] RIGHT_PARENTHESES config_expr
fn parse_schema_expr_with_args(&mut self, call: CallExpr, lo: token::Token) -> NodeRef<Expr> {
pub(crate) fn parse_schema_expr_with_args(
&mut self,
call: CallExpr,
lo: token::Token,
) -> NodeRef<Expr> {
let result = call.func.as_ref().clone().try_into();

let name = match result {
Expand Down
17 changes: 13 additions & 4 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,21 @@ impl<'a> Parser<'a> {
let typ = self.parse_type_annotation();

type_annotation = Some(node_ref!(typ.node.to_string(), typ.pos()));
// Unification statement
if let TokenKind::OpenDelim(DelimToken::Brace) = self.token.kind {
// schema expression without args
// Maybe the unification statement with optional schema arguments
// `s: Schema {` or `s: Schema(`
if matches!(
self.token.kind,
TokenKind::OpenDelim(DelimToken::Brace) | TokenKind::OpenDelim(DelimToken::Paren)
) {
if let Type::Named(ref identifier) = typ.node {
let identifier = node_ref!(Expr::Identifier(identifier.clone()), typ.pos());
let schema_expr = self.parse_schema_expr(*identifier, token);
let schema_expr =
if matches!(self.token.kind, TokenKind::OpenDelim(DelimToken::Paren)) {
let call = self.parse_call(identifier);
self.parse_schema_expr_with_args(call, token)
} else {
self.parse_schema_expr(*identifier, token)
};
let mut ident = self.expr_as_identifier(targets[0].clone(), token);
ident.ctx = ExprContext::Store;
let unification_stmt = UnificationStmt {
Expand Down
7 changes: 7 additions & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ parse_module_snapshot! { assert_stmt_recovery_0, r#"assert"#}
parse_module_snapshot! { assert_stmt_recovery_1, r#"assert a."#}
parse_module_snapshot! { assert_stmt_recovery_2, r#"assert True,,, 'msg'"#}
parse_module_snapshot! { assert_stmt_recovery_3, r#"assert True if data else 'msg'"#}
parse_module_snapshot! { unification_stmt_recovery_0, r#"s: Server {"#}
parse_module_snapshot! { unification_stmt_recovery_1, r#"s: Server {}"#}
parse_module_snapshot! { unification_stmt_recovery_2, r#"s: Server ("#}
parse_module_snapshot! { unification_stmt_recovery_3, r#"s: Server ()"#}
parse_module_snapshot! { unification_stmt_recovery_4, r#"s: Server () {"#}
parse_module_snapshot! { unification_stmt_recovery_5, r#"s: Server ( {"#}
parse_module_snapshot! { unification_stmt_recovery_6, r#"s: Server ( }"#}
parse_module_snapshot! { import_stmt_recovery_0, r#"import"#}
parse_module_snapshot! { import_stmt_recovery_1, r#"import 'pkg_path'"#}
parse_module_snapshot! { import_stmt_recovery_2, r#"import pkg_path."#}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"s: Server {\"#)"
---
Module {
filename: "",
pkg: "",
doc: None,
name: "",
body: [
Node {
node: Unification(
UnificationStmt {
target: Node {
node: Identifier {
names: [
Node {
node: "s",
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
pkgpath: "",
ctx: Store,
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
value: Node {
node: SchemaExpr {
name: Node {
node: Identifier {
names: [
Node {
node: "Server",
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
args: [],
kwargs: [],
config: Node {
node: Config(
ConfigExpr {
items: [],
},
),
filename: "",
line: 1,
column: 10,
end_line: 1,
end_column: 11,
},
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 11,
},
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 11,
},
],
comments: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"s: Server {}\"#)"
---
Module {
filename: "",
pkg: "",
doc: None,
name: "",
body: [
Node {
node: Unification(
UnificationStmt {
target: Node {
node: Identifier {
names: [
Node {
node: "s",
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
pkgpath: "",
ctx: Store,
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
value: Node {
node: SchemaExpr {
name: Node {
node: Identifier {
names: [
Node {
node: "Server",
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
args: [],
kwargs: [],
config: Node {
node: Config(
ConfigExpr {
items: [],
},
),
filename: "",
line: 1,
column: 10,
end_line: 1,
end_column: 12,
},
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 12,
},
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 12,
},
],
comments: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"s: Server (\"#)"
---
Module {
filename: "",
pkg: "",
doc: None,
name: "",
body: [
Node {
node: Unification(
UnificationStmt {
target: Node {
node: Identifier {
names: [
Node {
node: "s",
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
pkgpath: "",
ctx: Store,
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
value: Node {
node: SchemaExpr {
name: Node {
node: Identifier {
names: [
Node {
node: "Server",
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 9,
},
args: [],
kwargs: [],
config: Node {
node: Config(
ConfigExpr {
items: [],
},
),
filename: "",
line: 1,
column: 11,
end_line: 1,
end_column: 11,
},
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 11,
},
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 11,
},
],
comments: [],
}
Loading

0 comments on commit 07aadc8

Please sign in to comment.