From cd7778f89f02c0ca76004c9d1d7f9d0a33d54d6e Mon Sep 17 00:00:00 2001 From: Peefy Date: Wed, 12 Apr 2023 11:59:31 +0800 Subject: [PATCH] fix: inline config if entry parse. (#509) --- kclvm/parser/src/parser/expr.rs | 17 +-- kclvm/parser/src/parser/tests.rs | 2 +- kclvm/parser/src/tests/error_recovery.rs | 2 + ...s__error_recovery__config_recovery_11.snap | 126 +++++++----------- ...s__error_recovery__config_recovery_12.snap | 126 +++++++----------- ...s__error_recovery__config_recovery_13.snap | 98 ++++++++++++++ ...s__error_recovery__config_recovery_14.snap | 99 ++++++++++++++ test/grammar/datatype/dict/if_item_10/main.k | 6 + .../datatype/dict/if_item_10/stdout.golden | 4 + test/grammar/datatype/dict/if_item_11/main.k | 1 + .../datatype/dict/if_item_11/stdout.golden | 2 + 11 files changed, 316 insertions(+), 167 deletions(-) create mode 100644 kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap create mode 100644 kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap create mode 100644 test/grammar/datatype/dict/if_item_10/main.k create mode 100644 test/grammar/datatype/dict/if_item_10/stdout.golden create mode 100644 test/grammar/datatype/dict/if_item_11/main.k create mode 100644 test/grammar/datatype/dict/if_item_11/stdout.golden diff --git a/kclvm/parser/src/parser/expr.rs b/kclvm/parser/src/parser/expr.rs index ec5292f9a..3ff1c66cc 100644 --- a/kclvm/parser/src/parser/expr.rs +++ b/kclvm/parser/src/parser/expr.rs @@ -1757,17 +1757,18 @@ impl<'a> Parser<'a> { true } - while parse_body_item(self, &mut body, need_skip_newlines) { - if let TokenKind::Comma = self.token.kind { - self.bump(); - } - if need_skip_newlines { + 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 let TokenKind::Newline = self.token.kind { - self.bump(); - } if need_skip_newlines { self.skip_newlines(); diff --git a/kclvm/parser/src/parser/tests.rs b/kclvm/parser/src/parser/tests.rs index 162b9aa64..62e78e12c 100644 --- a/kclvm/parser/src/parser/tests.rs +++ b/kclvm/parser/src/parser/tests.rs @@ -1179,7 +1179,7 @@ data2 = { } "####, expect![[r#" - {"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":["data2"],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":5}],"value":{"node":{"Config":{"items":[{"node":{"key":null,"value":{"node":{"Config":{"items":[{"node":{"key":{"node":{"Identifier":{"names":["key"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":10},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value1\"","value":"value1"}},"filename":"hello.k","line":3,"column":13,"end_line":3,"end_column":21},"operation":"Override","insert_index":-1},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":21}]}},"filename":"hello.k","line":3,"column":6,"end_line":3,"end_column":22},"operation":"Union","insert_index":-1},"filename":"hello.k","line":3,"column":4,"end_line":3,"end_column":22},{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":["a"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":8},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":4,"column":12,"end_line":4,"end_column":15}]}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":15},"items":[{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":["b"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":21},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":456}}},"filename":"hello.k","line":4,"column":25,"end_line":4,"end_column":28}]}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":28},"items":[{"node":{"key":{"node":{"Identifier":{"names":["key"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":33},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value2\"","value":"value2"}},"filename":"hello.k","line":4,"column":36,"end_line":4,"end_column":44},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":36,"end_line":4,"end_column":44}],"orelse":null}},"filename":"hello.k","line":4,"column":30,"end_line":5,"end_column":0},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":30,"end_line":5,"end_column":0}],"orelse":null}},"filename":"hello.k","line":4,"column":17,"end_line":5,"end_column":0},"operation":"Union","insert_index":-1},"filename":"hello.k","line":4,"column":4,"end_line":5,"end_column":0}]}},"filename":"hello.k","line":2,"column":8,"end_line":5,"end_column":1},"type_annotation":null,"ty":null}},"filename":"hello.k","line":2,"column":0,"end_line":5,"end_column":1}],"comments":[]} + {"filename":"hello.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":["data2"],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":2,"column":0,"end_line":2,"end_column":5}],"value":{"node":{"Config":{"items":[{"node":{"key":null,"value":{"node":{"Config":{"items":[{"node":{"key":{"node":{"Identifier":{"names":["key"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":10},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value1\"","value":"value1"}},"filename":"hello.k","line":3,"column":13,"end_line":3,"end_column":21},"operation":"Override","insert_index":-1},"filename":"hello.k","line":3,"column":7,"end_line":3,"end_column":21}]}},"filename":"hello.k","line":3,"column":6,"end_line":3,"end_column":22},"operation":"Union","insert_index":-1},"filename":"hello.k","line":3,"column":4,"end_line":3,"end_column":22},{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":["a"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":8},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":4,"column":12,"end_line":4,"end_column":15}]}},"filename":"hello.k","line":4,"column":7,"end_line":4,"end_column":15},"items":[{"node":{"key":null,"value":{"node":{"ConfigIfEntry":{"if_cond":{"node":{"Compare":{"left":{"node":{"Identifier":{"names":["b"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":21},"ops":["Eq"],"comparators":[{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":456}}},"filename":"hello.k","line":4,"column":25,"end_line":4,"end_column":28}]}},"filename":"hello.k","line":4,"column":20,"end_line":4,"end_column":28},"items":[{"node":{"key":{"node":{"Identifier":{"names":["key"],"pkgpath":"","ctx":"Load"}},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":33},"value":{"node":{"StringLit":{"is_long_string":false,"raw_value":"\"value2\"","value":"value2"}},"filename":"hello.k","line":4,"column":36,"end_line":4,"end_column":44},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":36,"end_line":4,"end_column":44}],"orelse":null}},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":44},"operation":"Override","insert_index":-1},"filename":"hello.k","line":4,"column":30,"end_line":4,"end_column":44}],"orelse":null}},"filename":"hello.k","line":4,"column":17,"end_line":5,"end_column":0},"operation":"Union","insert_index":-1},"filename":"hello.k","line":4,"column":4,"end_line":5,"end_column":0}]}},"filename":"hello.k","line":2,"column":8,"end_line":5,"end_column":1},"type_annotation":null,"ty":null}},"filename":"hello.k","line":2,"column":0,"end_line":5,"end_column":1}],"comments":[]} "#]], ); diff --git a/kclvm/parser/src/tests/error_recovery.rs b/kclvm/parser/src/tests/error_recovery.rs index e82199262..85522a4f3 100644 --- a/kclvm/parser/src/tests/error_recovery.rs +++ b/kclvm/parser/src/tests/error_recovery.rs @@ -68,6 +68,8 @@ 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! { config_recovery_13, "{if True: key: {}}" } +parse_expr_snapshot! { config_recovery_14, "{if True: key: []}" } 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]]" } diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_11.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_11.snap index 2dda6b298..702db6551 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_11.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_11.snap @@ -1,5 +1,6 @@ --- source: parser/src/tests/error_recovery.rs +assertion_line: 69 expression: "crate::tests::parsing_expr_string(\"{if True: a = , b = 2}\")" --- Node { @@ -64,83 +65,6 @@ Node { end_line: 1, end_column: 15, }, - Node { - node: ConfigEntry { - key: Some( - Node { - node: Identifier( - Identifier { - names: [ - "b", - ], - pkgpath: "", - ctx: Load, - }, - ), - filename: "", - line: 1, - column: 16, - end_line: 1, - end_column: 17, - }, - ), - value: Node { - node: NumberLit( - NumberLit { - binary_suffix: None, - value: Int( - 2, - ), - }, - ), - filename: "", - line: 1, - column: 20, - end_line: 1, - end_column: 21, - }, - operation: Override, - insert_index: -1, - }, - filename: "", - line: 1, - column: 20, - end_line: 1, - end_column: 21, - }, - Node { - node: ConfigEntry { - key: Some( - Node { - node: Missing( - MissingExpr, - ), - filename: "", - line: 1, - column: 21, - end_line: 1, - end_column: 22, - }, - ), - value: Node { - node: Missing( - MissingExpr, - ), - filename: "", - line: 1, - column: 21, - end_line: 1, - end_column: 22, - }, - operation: Override, - insert_index: -1, - }, - filename: "", - line: 1, - column: 21, - end_line: 1, - end_column: 22, - }, ], orelse: None, }, @@ -149,7 +73,7 @@ Node { line: 1, column: 10, end_line: 1, - end_column: 22, + end_column: 13, }, operation: Union, insert_index: -1, @@ -158,7 +82,51 @@ Node { line: 1, column: 1, end_line: 1, - end_column: 22, + end_column: 15, + }, + Node { + node: ConfigEntry { + key: Some( + Node { + node: Identifier( + Identifier { + names: [ + "b", + ], + pkgpath: "", + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 16, + end_line: 1, + end_column: 17, + }, + ), + value: Node { + node: NumberLit( + NumberLit { + binary_suffix: None, + value: Int( + 2, + ), + }, + ), + filename: "", + line: 1, + column: 20, + end_line: 1, + end_column: 21, + }, + operation: Override, + insert_index: -1, + }, + filename: "", + line: 1, + column: 16, + end_line: 1, + end_column: 21, }, ], }, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_12.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_12.snap index ee18eda3b..897a66ec5 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_12.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_12.snap @@ -1,5 +1,6 @@ --- source: parser/src/tests/error_recovery.rs +assertion_line: 70 expression: "crate::tests::parsing_expr_string(\"{if True: *a, b = 2}\")" --- Node { @@ -88,83 +89,6 @@ Node { end_line: 1, end_column: 13, }, - Node { - node: ConfigEntry { - key: Some( - Node { - node: Identifier( - Identifier { - names: [ - "b", - ], - pkgpath: "", - ctx: Load, - }, - ), - filename: "", - line: 1, - column: 14, - end_line: 1, - end_column: 15, - }, - ), - value: Node { - node: NumberLit( - NumberLit { - binary_suffix: None, - value: Int( - 2, - ), - }, - ), - filename: "", - line: 1, - column: 18, - end_line: 1, - end_column: 19, - }, - operation: Override, - insert_index: -1, - }, - filename: "", - line: 1, - column: 18, - end_line: 1, - end_column: 19, - }, - Node { - node: ConfigEntry { - key: Some( - Node { - node: Missing( - MissingExpr, - ), - filename: "", - line: 1, - column: 19, - end_line: 1, - end_column: 20, - }, - ), - value: Node { - node: Missing( - MissingExpr, - ), - filename: "", - line: 1, - column: 19, - end_line: 1, - end_column: 20, - }, - operation: Override, - insert_index: -1, - }, - filename: "", - line: 1, - column: 19, - end_line: 1, - end_column: 20, - }, ], orelse: None, }, @@ -173,7 +97,7 @@ Node { line: 1, column: 10, end_line: 1, - end_column: 20, + end_column: 12, }, operation: Union, insert_index: -1, @@ -182,7 +106,51 @@ Node { line: 1, column: 1, end_line: 1, - end_column: 20, + end_column: 13, + }, + Node { + node: ConfigEntry { + key: Some( + Node { + node: Identifier( + Identifier { + names: [ + "b", + ], + pkgpath: "", + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 14, + end_line: 1, + end_column: 15, + }, + ), + value: Node { + node: NumberLit( + NumberLit { + binary_suffix: None, + value: Int( + 2, + ), + }, + ), + filename: "", + line: 1, + column: 18, + end_line: 1, + end_column: 19, + }, + operation: Override, + insert_index: -1, + }, + filename: "", + line: 1, + column: 14, + end_line: 1, + end_column: 19, }, ], }, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap new file mode 100644 index 000000000..181c8aff6 --- /dev/null +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap @@ -0,0 +1,98 @@ +--- +source: parser/src/tests/error_recovery.rs +assertion_line: 71 +expression: "crate::tests::parsing_expr_string(\"{if True: key: {}}\")" +--- +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: 1, + column: 4, + end_line: 1, + end_column: 8, + }, + items: [ + Node { + node: ConfigEntry { + key: Some( + Node { + node: Identifier( + Identifier { + names: [ + "key", + ], + pkgpath: "", + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 10, + end_line: 1, + end_column: 13, + }, + ), + value: Node { + node: Config( + ConfigExpr { + items: [], + }, + ), + filename: "", + line: 1, + column: 15, + end_line: 1, + end_column: 17, + }, + operation: Union, + insert_index: -1, + }, + filename: "", + line: 1, + column: 15, + end_line: 1, + end_column: 17, + }, + ], + orelse: None, + }, + ), + filename: "", + line: 1, + column: 10, + end_line: 1, + end_column: 17, + }, + operation: Union, + insert_index: -1, + }, + filename: "", + line: 1, + column: 1, + end_line: 1, + end_column: 17, + }, + ], + }, + ), + filename: "", + line: 1, + column: 0, + end_line: 1, + end_column: 18, +} + diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap new file mode 100644 index 000000000..b3db187b9 --- /dev/null +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap @@ -0,0 +1,99 @@ +--- +source: parser/src/tests/error_recovery.rs +assertion_line: 72 +expression: "crate::tests::parsing_expr_string(\"{if True: key: []}\")" +--- +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: 1, + column: 4, + end_line: 1, + end_column: 8, + }, + items: [ + Node { + node: ConfigEntry { + key: Some( + Node { + node: Identifier( + Identifier { + names: [ + "key", + ], + pkgpath: "", + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 10, + end_line: 1, + end_column: 13, + }, + ), + value: Node { + node: List( + ListExpr { + elts: [], + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 15, + end_line: 1, + end_column: 17, + }, + operation: Union, + insert_index: -1, + }, + filename: "", + line: 1, + column: 15, + end_line: 1, + end_column: 17, + }, + ], + orelse: None, + }, + ), + filename: "", + line: 1, + column: 10, + end_line: 1, + end_column: 17, + }, + operation: Union, + insert_index: -1, + }, + filename: "", + line: 1, + column: 1, + end_line: 1, + end_column: 17, + }, + ], + }, + ), + filename: "", + line: 1, + column: 0, + end_line: 1, + end_column: 18, +} + diff --git a/test/grammar/datatype/dict/if_item_10/main.k b/test/grammar/datatype/dict/if_item_10/main.k new file mode 100644 index 000000000..3d7073424 --- /dev/null +++ b/test/grammar/datatype/dict/if_item_10/main.k @@ -0,0 +1,6 @@ +a = 123 +b = 456 +data = { + **{key = "value1"} + if a == 123: if b == 456: key = "value2" +} diff --git a/test/grammar/datatype/dict/if_item_10/stdout.golden b/test/grammar/datatype/dict/if_item_10/stdout.golden new file mode 100644 index 000000000..41ace265d --- /dev/null +++ b/test/grammar/datatype/dict/if_item_10/stdout.golden @@ -0,0 +1,4 @@ +a: 123 +b: 456 +data: + key: value2 diff --git a/test/grammar/datatype/dict/if_item_11/main.k b/test/grammar/datatype/dict/if_item_11/main.k new file mode 100644 index 000000000..80c691b79 --- /dev/null +++ b/test/grammar/datatype/dict/if_item_11/main.k @@ -0,0 +1 @@ +data = {if True: key = "val"} diff --git a/test/grammar/datatype/dict/if_item_11/stdout.golden b/test/grammar/datatype/dict/if_item_11/stdout.golden new file mode 100644 index 000000000..77e8e7950 --- /dev/null +++ b/test/grammar/datatype/dict/if_item_11/stdout.golden @@ -0,0 +1,2 @@ +data: + key: val