Skip to content

Commit

Permalink
fix: fix parse_body_item loop (kcl-lang#1817)
Browse files Browse the repository at this point in the history
* fix: fix parse_body_item loop

Signed-off-by: he1pa <18012015693@163.com>

* fmt code

Signed-off-by: he1pa <18012015693@163.com>

---------

Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa authored Jan 13, 2025
1 parent 2562e63 commit a93ccad
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 26 deletions.
56 changes: 30 additions & 26 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,36 @@ impl<'a> Parser<'a> {
}
};

if !need_skip_newlines {
// Only parse one inline key-value pair.
parse_body_item(self, &mut body, need_skip_newlines);
} else {
while parse_body_item(self, &mut body, need_skip_newlines) {
if self.token.kind == TokenKind::Eof {
self.bump();
break;
}
// bump optional comma at the endline.
if let TokenKind::Comma = self.token.kind {
self.bump();
}
self.skip_newlines();
}
}

return if need_skip_newlines {
self.skip_newlines();
Box::new(Node::node(
body,
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
Box::new(Node::node(
body,
self.sess.struct_token_loc(token, self.prev_token),
))
};

fn parse_body_item(
this: &mut Parser,
body: &mut ConfigIfEntryExpr,
Expand Down Expand Up @@ -1844,32 +1874,6 @@ impl<'a> Parser<'a> {
this.drop(marker);
true
}

if !need_skip_newlines {
// Only parse one inline key-value pair.
parse_body_item(self, &mut body, need_skip_newlines);
} else {
while parse_body_item(self, &mut body, need_skip_newlines) {
// bump optional comma at the endline.
if let TokenKind::Comma = self.token.kind {
self.bump();
}
self.skip_newlines();
}
}

if need_skip_newlines {
self.skip_newlines();
Box::new(Node::node(
body,
self.sess.struct_token_loc(token, self.prev_token),
))
} else {
Box::new(Node::node(
body,
self.sess.struct_token_loc(token, self.prev_token),
))
}
}

/// Syntax:
Expand Down
5 changes: 5 additions & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,8 @@ 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"#}

parse_module_snapshot! { parse_body_item, r#"
x = {
if True
}"#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
source: parser/src/tests/error_recovery.rs
expression: "$crate :: tests :: parsing_module_string(r#\"\nx = {\n if True\n}\"#)"
---
Module {
filename: "",
doc: None,
body: [
Node {
node: Assign(
AssignStmt {
targets: [
Node {
node: Target {
name: Node {
node: "x",
filename: "",
line: 2,
column: 0,
end_line: 2,
end_column: 1,
},
paths: [],
pkgpath: "",
},
filename: "",
line: 2,
column: 0,
end_line: 2,
end_column: 1,
},
],
value: Node {
node: Config(
ConfigExpr {
items: [
Node {
node: ConfigEntry {
key: None,
value: Node {
node: ConfigIfEntry(
ConfigIfEntryExpr {
if_cond: Node {
node: NameConstantLit(
NameConstantLit {
value: True,
},
),
filename: "",
line: 3,
column: 7,
end_line: 3,
end_column: 11,
},
items: [],
orelse: None,
},
),
filename: "",
line: 3,
column: 4,
end_line: 4,
end_column: 0,
},
operation: Union,
},
filename: "",
line: 3,
column: 4,
end_line: 4,
end_column: 0,
},
],
},
),
filename: "",
line: 2,
column: 4,
end_line: 4,
end_column: 1,
},
ty: None,
},
),
filename: "",
line: 2,
column: 0,
end_line: 4,
end_column: 1,
},
],
comments: [],
}

0 comments on commit a93ccad

Please sign in to comment.