Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: invalid comp clause parse and sematic error recovery. #497

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:
contents: write
jobs:
test:
name: Unit tests with coverage
name: Build and release
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand Down
90 changes: 69 additions & 21 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ impl<'a> Parser<'a> {
false
};

let item_start_token = self.token;
let items = self.parse_list_items(has_newline);
let generators = self.parse_comp_clauses();

Expand Down Expand Up @@ -919,17 +920,41 @@ impl<'a> Parser<'a> {

if !generators.is_empty() {
if items.len() > 1 {
self.sess
.struct_span_error("list multiple items found", self.token.span)
self.sess.struct_span_error(
&format!(
"multiple list comp clause expression found: expected 1, got {}",
items.len()
),
item_start_token.span,
);
Box::new(Node::node(
Expr::ListComp(ListComp {
elt: items[0].clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else if items.len() == 1 {
Box::new(Node::node(
Expr::ListComp(ListComp {
elt: items[0].clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
self.sess.struct_span_error(
"missing list comp clause expression",
item_start_token.span,
);
Box::new(Node::node(
Expr::List(ListExpr {
elts: items,
ctx: ExprContext::Load,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
}

Box::new(Node::node(
Expr::ListComp(ListComp {
elt: items[0].clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
Box::new(Node::node(
Expr::List(ListExpr {
Expand Down Expand Up @@ -1208,6 +1233,7 @@ impl<'a> Parser<'a> {
false
};

let item_start_token = self.token;
let items = self.parse_config_entries(has_newline);
let generators = self.parse_comp_clauses();

Expand Down Expand Up @@ -1235,17 +1261,38 @@ impl<'a> Parser<'a> {

if !generators.is_empty() {
if items.len() > 1 {
self.sess
.struct_span_error("config multiple entries found", self.token.span)
self.sess.struct_span_error(
&format!(
"multiple config comp clause expression found: expected 1, got {}",
items.len()
),
item_start_token.span,
);
Box::new(Node::node(
Expr::DictComp(DictComp {
entry: items[0].node.clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else if items.len() == 1 {
Box::new(Node::node(
Expr::DictComp(DictComp {
entry: items[0].node.clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
self.sess.struct_span_error(
"missing config comp clause expression",
item_start_token.span,
);
Box::new(Node::node(
Expr::Config(ConfigExpr { items }),
self.sess.struct_token_loc(token, self.prev_token),
))
}

Box::new(Node::node(
Expr::DictComp(DictComp {
entry: items[0].node.clone(),
generators,
}),
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
Box::new(Node::node(
Expr::Config(ConfigExpr { items }),
Expand Down Expand Up @@ -1384,6 +1431,7 @@ impl<'a> Parser<'a> {
/// loop_variables: identifier (COMMA identifier)*
fn parse_comp_clause(&mut self) -> NodeRef<CompClause> {
let token = self.token;
// bump the `for` keyword.
self.bump();

let mut targets = vec![self.parse_identifier()];
Expand Down Expand Up @@ -1848,7 +1896,7 @@ impl<'a> Parser<'a> {
loop {
if matches!(
self.token.kind,
TokenKind::CloseDelim(DelimToken::Brace) | TokenKind::Dedent
TokenKind::CloseDelim(DelimToken::Brace) | TokenKind::Dedent | TokenKind::Eof
) {
break;
}
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 @@ -68,6 +68,13 @@ parse_expr_snapshot! { config_recovery_9, "{*a, **b}" }
parse_expr_snapshot! { config_recovery_10, "{**a, *b}" }
parse_expr_snapshot! { config_recovery_11, "{if True: a = , b = 2}" }
parse_expr_snapshot! { config_recovery_12, "{if True: *a, b = 2}" }
parse_expr_snapshot! { comp_clause_recovery_0, "[i for i in [1,2,3]]" }
parse_expr_snapshot! { comp_clause_recovery_1, "[i, j for i in [1,2,3]]" }
parse_expr_snapshot! { comp_clause_recovery_2, "[for i in [1,2,3]]" }
parse_expr_snapshot! { comp_clause_recovery_3, "{i for i in [1,2,3]}" }
parse_expr_snapshot! { comp_clause_recovery_4, "{i: for i in [1,2,3]}" }
parse_expr_snapshot! { comp_clause_recovery_5, "{i: 1, j for i in [1,2,3]}" }
parse_expr_snapshot! { comp_clause_recovery_6, "{for i in [1,2,3]}" }
parse_expr_snapshot! { unary_recovery_0, r#"!a"# }
parse_expr_snapshot! { unary_recovery_1, r#"!!a"# }
parse_expr_snapshot! { unary_recovery_2, r#"not (!a)"# }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 71
expression: "crate::tests::parsing_expr_string(\"[i for i in [1,2,3]]\")"
---
Node {
node: ListComp(
ListComp {
elt: Node {
node: Identifier(
Identifier {
names: [
"i",
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 2,
},
generators: [
Node {
node: CompClause {
targets: [
Node {
node: Identifier {
names: [
"i",
],
pkgpath: "",
ctx: Load,
},
filename: "",
line: 1,
column: 7,
end_line: 1,
end_column: 8,
},
],
iter: Node {
node: List(
ListExpr {
elts: [
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
1,
),
},
),
filename: "",
line: 1,
column: 13,
end_line: 1,
end_column: 14,
},
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
2,
),
},
),
filename: "",
line: 1,
column: 15,
end_line: 1,
end_column: 16,
},
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
3,
),
},
),
filename: "",
line: 1,
column: 17,
end_line: 1,
end_column: 18,
},
],
ctx: Load,
},
),
filename: "",
line: 1,
column: 12,
end_line: 1,
end_column: 19,
},
ifs: [],
},
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 19,
},
],
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 20,
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 72
expression: "crate::tests::parsing_expr_string(\"[i, j for i in [1,2,3]]\")"
---
Node {
node: ListComp(
ListComp {
elt: Node {
node: Identifier(
Identifier {
names: [
"i",
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 2,
},
generators: [
Node {
node: CompClause {
targets: [
Node {
node: Identifier {
names: [
"i",
],
pkgpath: "",
ctx: Load,
},
filename: "",
line: 1,
column: 10,
end_line: 1,
end_column: 11,
},
],
iter: Node {
node: List(
ListExpr {
elts: [
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
1,
),
},
),
filename: "",
line: 1,
column: 16,
end_line: 1,
end_column: 17,
},
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
2,
),
},
),
filename: "",
line: 1,
column: 18,
end_line: 1,
end_column: 19,
},
Node {
node: NumberLit(
NumberLit {
binary_suffix: None,
value: Int(
3,
),
},
),
filename: "",
line: 1,
column: 20,
end_line: 1,
end_column: 21,
},
],
ctx: Load,
},
),
filename: "",
line: 1,
column: 15,
end_line: 1,
end_column: 22,
},
ifs: [],
},
filename: "",
line: 1,
column: 6,
end_line: 1,
end_column: 22,
},
],
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 23,
}

Loading