Skip to content

Commit

Permalink
fix: list and config if expr error recovery in the parser
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed May 31, 2024
1 parent 87c53c5 commit 58243d8
Show file tree
Hide file tree
Showing 8 changed files with 705 additions and 0 deletions.
6 changes: 6 additions & 0 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ impl<'a> Parser<'a> {
// elif ...
let mut elif_list = Vec::new();
loop {
let marker = self.mark();
if !self.token.is_keyword(kw::Elif) {
break;
}
Expand All @@ -1188,6 +1189,7 @@ impl<'a> Parser<'a> {
x,
self.sess.struct_token_loc(token, self.prev_token),
)));
self.drop(marker);
}

if let TokenKind::Newline = self.token.kind {
Expand Down Expand Up @@ -1259,6 +1261,7 @@ impl<'a> Parser<'a> {
let mut body = Vec::new();

loop {
let marker = self.mark();
self.validate_dedent();
if matches!(self.token.kind, TokenKind::Dedent(VALID_SPACES_LENGTH)) {
break;
Expand All @@ -1270,6 +1273,7 @@ impl<'a> Parser<'a> {
self.bump();
}
self.skip_newlines();
self.drop(marker);
}
self.validate_dedent();
self.bump_token(TokenKind::Dedent(VALID_SPACES_LENGTH));
Expand Down Expand Up @@ -1581,6 +1585,7 @@ impl<'a> Parser<'a> {
let mut elif_list = Vec::new();
let mut last_token = self.token;
loop {
let marker = self.mark();
if !self.token.is_keyword(kw::Elif) {
break;
}
Expand All @@ -1607,6 +1612,7 @@ impl<'a> Parser<'a> {
self.sess.struct_token_loc(token, self.prev_token),
)));
last_token = self.prev_token;
self.drop(marker);
}

if let TokenKind::Newline = self.token.kind {
Expand Down
30 changes: 30 additions & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ parse_expr_snapshot! { list_recovery_9, "[*a, **b]" }
parse_expr_snapshot! { list_recovery_10, "[**a, *b" }
parse_expr_snapshot! { list_recovery_11, "[if True: a, b]" }
parse_expr_snapshot! { list_recovery_12, "[if True: **a, b]" }
parse_expr_snapshot! { list_recovery_13, r#"[
if True:
b = []
]
"# }
parse_expr_snapshot! { list_recovery_14, r#"[
if True:
b =
]
"# }
parse_expr_snapshot! { list_recovery_15, r#"[
if True:
b -
]
"# }
parse_expr_snapshot! { config_recovery_0, "{" }
parse_expr_snapshot! { config_recovery_1, "{a = 1" }
parse_expr_snapshot! { config_recovery_2, "{a = 1, b = 2" }
Expand All @@ -71,6 +86,21 @@ parse_expr_snapshot! { config_recovery_12, "{if True: *a, b = 2}" }
parse_expr_snapshot! { config_recovery_13, "{if True: key: {}}" }
parse_expr_snapshot! { config_recovery_14, "{if True: key: []}" }
parse_expr_snapshot! { config_recovery_15, "{你好" }
parse_expr_snapshot! { list_recovery_16, r#"{
if True:
b = [] = []
}
"# }
parse_expr_snapshot! { list_recovery_17, r#"{
if True:
b = [] =
}
"# }
parse_expr_snapshot! { list_recovery_18, r#"{
if True:
b = [] -
}
"# }
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]]" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_expr_string(r#\"[\n if True:\n b = []\n]\n\"#)"
---
Node {
node: List(
ListExpr {
elts: [
Node {
node: ListIfItem(
ListIfItemExpr {
if_cond: Node {
node: NameConstantLit(
NameConstantLit {
value: True,
},
),
filename: "",
line: 2,
column: 7,
end_line: 2,
end_column: 11,
},
exprs: [
Node {
node: Identifier(
Identifier {
names: [
Node {
node: "b",
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 3,
column: 10,
end_line: 3,
end_column: 11,
},
Node {
node: List(
ListExpr {
elts: [],
ctx: Load,
},
),
filename: "",
line: 3,
column: 12,
end_line: 3,
end_column: 14,
},
],
orelse: None,
},
),
filename: "",
line: 2,
column: 4,
end_line: 4,
end_column: 0,
},
],
ctx: Load,
},
),
filename: "",
line: 1,
column: 0,
end_line: 4,
end_column: 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_expr_string(r#\"[\n if True:\n b = \n]\n\"#)"
---
Node {
node: List(
ListExpr {
elts: [
Node {
node: ListIfItem(
ListIfItemExpr {
if_cond: Node {
node: NameConstantLit(
NameConstantLit {
value: True,
},
),
filename: "",
line: 2,
column: 7,
end_line: 2,
end_column: 11,
},
exprs: [
Node {
node: Identifier(
Identifier {
names: [
Node {
node: "b",
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 3,
column: 10,
end_line: 3,
end_column: 11,
},
Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 3,
column: 12,
end_line: 4,
end_column: 0,
},
],
orelse: None,
},
),
filename: "",
line: 2,
column: 4,
end_line: 4,
end_column: 0,
},
],
ctx: Load,
},
),
filename: "",
line: 1,
column: 0,
end_line: 4,
end_column: 1,
}
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_expr_string(r#\"[\n if True:\n b -\n]\n\"#)"
---
Node {
node: List(
ListExpr {
elts: [
Node {
node: ListIfItem(
ListIfItemExpr {
if_cond: Node {
node: NameConstantLit(
NameConstantLit {
value: True,
},
),
filename: "",
line: 2,
column: 7,
end_line: 2,
end_column: 11,
},
exprs: [
Node {
node: Binary(
BinaryExpr {
left: Node {
node: Identifier(
Identifier {
names: [
Node {
node: "b",
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 9,
},
op: Sub,
right: Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 3,
column: 11,
end_line: 4,
end_column: 0,
},
},
),
filename: "",
line: 3,
column: 8,
end_line: 3,
end_column: 11,
},
],
orelse: None,
},
),
filename: "",
line: 2,
column: 4,
end_line: 4,
end_column: 0,
},
],
ctx: Load,
},
),
filename: "",
line: 1,
column: 0,
end_line: 4,
end_column: 1,
}
Loading

0 comments on commit 58243d8

Please sign in to comment.