From da56a30104fd288921fa475381089de44e7442a7 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 13 Jul 2024 15:00:47 -0700 Subject: [PATCH] --- prqlc/prqlc-parser/src/lexer/mod.rs | 3 + prqlc/prqlc-parser/src/parser/common.rs | 3 +- prqlc/prqlc-parser/src/parser/expr.rs | 66 ++++++++++++++----- prqlc/prqlc-parser/src/parser/test.rs | 20 +++--- .../tests/integration/bad_error_messages.rs | 9 +-- ...__queries__debug_lineage__aggregation.snap | 2 +- ...n__queries__debug_lineage__arithmetic.snap | 2 +- ...gration__queries__debug_lineage__cast.snap | 2 +- ...ueries__debug_lineage__constants_only.snap | 2 +- ..._queries__debug_lineage__date_to_text.snap | 2 +- ...ion__queries__debug_lineage__distinct.snap | 2 +- ...__queries__debug_lineage__distinct_on.snap | 2 +- ..._queries__debug_lineage__genre_counts.snap | 4 +- ...on__queries__debug_lineage__group_all.snap | 2 +- ...n__queries__debug_lineage__group_sort.snap | 2 +- ..._debug_lineage__group_sort_limit_take.snap | 4 +- ...ueries__debug_lineage__invoice_totals.snap | 6 +- ...tion__queries__debug_lineage__loop_01.snap | 2 +- ...__queries__debug_lineage__math_module.snap | 2 +- ...on__queries__debug_lineage__pipelines.snap | 2 +- ...ion__queries__debug_lineage__read_csv.snap | 2 +- ...ueries__debug_lineage__set_ops_remove.snap | 4 +- ...gration__queries__debug_lineage__sort.snap | 2 +- ...ation__queries__debug_lineage__switch.snap | 2 +- ...gration__queries__debug_lineage__take.snap | 2 +- ...__queries__debug_lineage__text_module.snap | 2 +- ...ation__queries__debug_lineage__window.snap | 4 +- 27 files changed, 99 insertions(+), 58 deletions(-) diff --git a/prqlc/prqlc-parser/src/lexer/mod.rs b/prqlc/prqlc-parser/src/lexer/mod.rs index f86cb319f1fa..5baf5e315df5 100644 --- a/prqlc/prqlc-parser/src/lexer/mod.rs +++ b/prqlc/prqlc-parser/src/lexer/mod.rs @@ -189,6 +189,9 @@ fn line_wrap() -> impl Parser> { fn comment() -> impl Parser> { just('#').ignore_then(choice(( + // One option would be to check that doc comments have new lines in the + // lexer (we currently do in the parser); which would give better error + // messages? just('!').ignore_then( newline() .not() diff --git a/prqlc/prqlc-parser/src/parser/common.rs b/prqlc/prqlc-parser/src/parser/common.rs index 9a070665322e..14ff246b5de8 100644 --- a/prqlc/prqlc-parser/src/parser/common.rs +++ b/prqlc/prqlc-parser/src/parser/common.rs @@ -27,6 +27,8 @@ pub fn keyword(kw: &'static str) -> impl Parser + pub fn new_line() -> impl Parser + Clone { just(TokenKind::NewLine) + // Start is considered a new line, so we can enforce things start on a new + // line while allowing them to be at the beginning of a file .or(just(TokenKind::Start)) .ignored() .labelled("new line") @@ -57,7 +59,6 @@ pub fn doc_comment() -> impl Parser + Clone { .repeated() .at_least(1) .collect() - .debug("doc_comment") .map(|lines: Vec| lines.join("\n")) .labelled("doc comment") } diff --git a/prqlc/prqlc-parser/src/parser/expr.rs b/prqlc/prqlc-parser/src/parser/expr.rs index 8447565c9e13..bec0df34ccea 100644 --- a/prqlc/prqlc-parser/src/parser/expr.rs +++ b/prqlc/prqlc-parser/src/parser/expr.rs @@ -85,7 +85,6 @@ fn tuple( .then(nested_expr) .map(|(alias, expr)| Expr { alias, ..expr }), ) - // .padded_by(new_line().repeated()) .separated_by(ctrl(',')) .allow_trailing() .then_ignore(new_line().repeated()) @@ -110,9 +109,7 @@ fn array( new_line() .repeated() .ignore_then( - expr - // .padded_by(new_line().repeated()) - .separated_by(ctrl(',')) + expr.separated_by(ctrl(',')) .allow_trailing() .then_ignore(new_line().repeated()) .delimited_by(ctrl('['), ctrl(']')) @@ -169,21 +166,19 @@ fn interpolation() -> impl Parser + Clone { fn case( expr: impl Parser + Clone, ) -> impl Parser + Clone { + // The `nickname != null => nickname,` part + let mapping = func_call(expr.clone()) + .map(Box::new) + .then_ignore(just(TokenKind::ArrowFat)) + .then(func_call(expr).map(Box::new)) + .map(|(condition, value)| SwitchCase { condition, value }); + keyword("case") .ignore_then( - new_line() - .repeated() - .ignore_then( - func_call(expr.clone()) - .map(Box::new) - .then_ignore(just(TokenKind::ArrowFat)) - .then(func_call(expr).map(Box::new)) - .map(|(condition, value)| SwitchCase { condition, value }), - ) - // .padded_by(new_line().repeated()) - .separated_by(ctrl(',')) + mapping + .separated_by(ctrl(',').then_ignore(new_line().repeated())) .allow_trailing() - .then_ignore(new_line().repeated()) + .padded_by(new_line().repeated()) .delimited_by(ctrl('['), ctrl(']')), ) .map(ExprKind::Case) @@ -629,4 +624,43 @@ mod tests { span: "0:13-50" "###); } + + #[test] + fn test_case() { + assert_yaml_snapshot!( + parse_with_parser(r#" + + case [ + + nickname != null => nickname, + true => null + + ] + "#, trim_start().then(case(expr()))).unwrap(), + @r###" + --- + - ~ + - Case: + - condition: + Binary: + left: + Ident: nickname + span: "0:30-38" + op: Ne + right: + Literal: "Null" + span: "0:42-46" + span: "0:30-46" + value: + Ident: nickname + span: "0:50-58" + - condition: + Literal: + Boolean: true + span: "0:72-76" + value: + Literal: "Null" + span: "0:80-84" + "###); + } } diff --git a/prqlc/prqlc-parser/src/parser/test.rs b/prqlc/prqlc-parser/src/parser/test.rs index e8cff7a53957..2d8a71839f0f 100644 --- a/prqlc/prqlc-parser/src/parser/test.rs +++ b/prqlc/prqlc-parser/src/parser/test.rs @@ -1255,33 +1255,35 @@ fn test_ident_with_keywords() { #[test] fn test_case() { - assert_yaml_snapshot!(parse_expr(r#"case [ + assert_yaml_snapshot!(parse_expr(r#" + case [ nickname != null => nickname, true => null - ]"#).unwrap(), @r###" + ] + "#).unwrap(), @r###" --- Case: - condition: Binary: left: Ident: nickname - span: "0:19-27" + span: "0:28-36" op: Ne right: Literal: "Null" - span: "0:31-35" - span: "0:19-35" + span: "0:40-44" + span: "0:28-44" value: Ident: nickname - span: "0:39-47" + span: "0:48-56" - condition: Literal: Boolean: true - span: "0:61-65" + span: "0:70-74" value: Literal: "Null" - span: "0:69-73" - span: "0:0-83" + span: "0:78-82" + span: "0:9-92" "###); } diff --git a/prqlc/prqlc/tests/integration/bad_error_messages.rs b/prqlc/prqlc/tests/integration/bad_error_messages.rs index 9f4b25bf2019..25e89ce05c3f 100644 --- a/prqlc/prqlc/tests/integration/bad_error_messages.rs +++ b/prqlc/prqlc/tests/integration/bad_error_messages.rs @@ -220,11 +220,12 @@ fn just_std() { std "###).unwrap_err(), @r###" Error: - ╭─[:2:5] + ╭─[:1:1] │ - 2 │ std - │ ──┬─ - │ ╰─── internal compiler error; tracked at https://github.com/PRQL/prql/issues/4474 + 1 │ ╭─▶ + 2 │ ├─▶ std + │ │ + │ ╰───────────── internal compiler error; tracked at https://github.com/PRQL/prql/issues/4474 ───╯ "###); } diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__aggregation.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__aggregation.snap index feb3f0fbad2f..bb10ce1c8881 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__aggregation.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__aggregation.snap @@ -286,4 +286,4 @@ ast: span: 1:178-243 span: 1:168-243 span: 1:102-243 - span: 1:102-244 + span: 1:0-243 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__arithmetic.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__arithmetic.snap index 9c5a750c0f08..b4f798314b68 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__arithmetic.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__arithmetic.snap @@ -1277,4 +1277,4 @@ ast: span: 1:830-832 span: 1:825-832 span: 1:13-832 - span: 1:13-833 + span: 1:0-832 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__cast.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__cast.snap index 310465f90e8a..a5f28ab5572d 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__cast.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__cast.snap @@ -209,4 +209,4 @@ ast: span: 1:103-105 span: 1:98-105 span: 1:13-105 - span: 1:13-106 + span: 1:0-105 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__constants_only.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__constants_only.snap index df827a1fedcf..0ec1fbdecd0d 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__constants_only.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__constants_only.snap @@ -192,4 +192,4 @@ ast: alias: d span: 1:52-65 span: 1:0-65 - span: 1:0-66 + span: 1:0-65 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__date_to_text.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__date_to_text.snap index 955e7d60a22b..322da4de0e44 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__date_to_text.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__date_to_text.snap @@ -627,4 +627,4 @@ ast: span: 1:86-718 span: 1:79-718 span: 1:57-718 - span: 1:57-719 + span: 1:0-718 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct.snap index 60c1240ce602..37c8167916d4 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct.snap @@ -228,4 +228,4 @@ ast: span: 1:88-90 span: 1:77-90 span: 1:13-90 - span: 1:13-91 + span: 1:0-90 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct_on.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct_on.snap index be7a54a80889..c3b19d9668db 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct_on.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__distinct_on.snap @@ -293,4 +293,4 @@ ast: span: 1:133-159 span: 1:128-159 span: 1:13-159 - span: 1:13-160 + span: 1:0-159 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__genre_counts.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__genre_counts.snap index 3d2bdc3a0345..24159d7d24df 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__genre_counts.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__genre_counts.snap @@ -125,7 +125,7 @@ ast: span: 1:167-183 span: 1:157-183 span: 1:135-185 - span: 1:117-185 + span: 1:0-185 - VarDef: kind: Main name: main @@ -170,4 +170,4 @@ ast: alias: a span: 1:217-230 span: 1:187-230 - span: 1:187-231 + span: 1:185-230 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_all.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_all.snap index 13ab542b8168..5149c491e3af 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_all.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_all.snap @@ -339,4 +339,4 @@ ast: span: 1:152-160 span: 1:147-160 span: 1:13-160 - span: 1:13-161 + span: 1:0-160 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort.snap index 7331f875ccb6..16722f0bae96 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort.snap @@ -325,4 +325,4 @@ ast: span: 1:136-150 span: 1:129-150 span: 1:13-150 - span: 1:13-151 + span: 1:0-150 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort_limit_take.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort_limit_take.snap index c8e9c4bc4627..d8e94bf276d7 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort_limit_take.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__group_sort_limit_take.snap @@ -362,7 +362,7 @@ ast: Integer: 3 span: 1:168-169 span: 1:163-169 - span: 1:140-169 + span: 1:137-169 span: 1:119-171 - FuncCall: name: @@ -411,4 +411,4 @@ ast: span: 1:230-251 span: 1:225-251 span: 1:76-251 - span: 1:76-252 + span: 1:0-251 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__invoice_totals.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__invoice_totals.snap index 4471a56f7d03..dadcbbb337cb 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__invoice_totals.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__invoice_totals.snap @@ -871,7 +871,7 @@ ast: alias: total_price span: 1:338-466 span: 1:328-466 - span: 1:281-466 + span: 1:276-466 span: 1:254-468 - FuncCall: name: @@ -920,7 +920,7 @@ ast: Boolean: true span: 1:521-525 span: 1:504-592 - span: 1:488-592 + span: 1:483-592 span: 1:469-594 - FuncCall: name: @@ -984,5 +984,5 @@ ast: span: 1:789-791 span: 1:784-791 span: 1:131-791 - span: 1:131-792 + span: 1:130-791 doc_comment: ' Calculate a number of metrics about the sales of tracks in each city.' diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__loop_01.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__loop_01.snap index 8a0143ae52e8..1436d51ee9d2 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__loop_01.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__loop_01.snap @@ -363,4 +363,4 @@ ast: span: 1:255-256 span: 1:250-256 span: 1:162-256 - span: 1:162-257 + span: 1:0-256 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__math_module.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__math_module.snap index 49c84b3bf375..7b92c56f9b1d 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__math_module.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__math_module.snap @@ -945,4 +945,4 @@ ast: span: 1:110-839 span: 1:103-839 span: 1:82-839 - span: 1:82-840 + span: 1:0-839 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__pipelines.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__pipelines.snap index 705569b7ab1d..77e89cb74ca6 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__pipelines.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__pipelines.snap @@ -342,4 +342,4 @@ ast: span: 1:281-297 span: 1:274-297 span: 1:166-297 - span: 1:166-298 + span: 1:0-297 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__read_csv.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__read_csv.snap index e3b20518dfde..f12478b04ec2 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__read_csv.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__read_csv.snap @@ -74,4 +74,4 @@ ast: span: 1:97-110 span: 1:92-110 span: 1:43-110 - span: 1:43-111 + span: 1:0-110 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__set_ops_remove.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__set_ops_remove.snap index 5af44fc9bec6..0dd6dd7a607b 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__set_ops_remove.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__set_ops_remove.snap @@ -289,7 +289,7 @@ ast: named_params: [] generic_type_params: [] span: 1:28-79 - span: 1:13-79 + span: 1:0-79 - VarDef: kind: Main name: main @@ -339,4 +339,4 @@ ast: span: 1:244-245 span: 1:239-245 span: 1:81-245 - span: 1:81-246 + span: 1:79-245 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__sort.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__sort.snap index a1aa70338287..82f3ab247866 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__sort.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__sort.snap @@ -323,4 +323,4 @@ ast: span: 1:224-271 span: 1:217-271 span: 1:13-271 - span: 1:13-272 + span: 1:0-271 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__switch.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__switch.snap index c0fb2ecf4d47..957538b8f03c 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__switch.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__switch.snap @@ -234,4 +234,4 @@ ast: span: 1:252-254 span: 1:247-254 span: 1:89-254 - span: 1:89-255 + span: 1:0-254 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__take.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__take.snap index 8c02c5a85974..2b2a4492caf9 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__take.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__take.snap @@ -116,4 +116,4 @@ ast: span: 1:47-51 span: 1:42-51 span: 1:13-51 - span: 1:13-52 + span: 1:0-51 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__text_module.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__text_module.snap index 69bf68a2073c..b3cf60d07916 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__text_module.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__text_module.snap @@ -705,4 +705,4 @@ ast: span: 1:484-588 span: 1:477-588 span: 1:113-588 - span: 1:113-589 + span: 1:0-588 diff --git a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__window.snap b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__window.snap index 999eb235b48a..8c6cc9a41d7e 100644 --- a/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__window.snap +++ b/prqlc/prqlc/tests/integration/snapshots/integration__queries__debug_lineage__window.snap @@ -453,7 +453,7 @@ ast: Integer: 10 span: 1:914-916 span: 1:909-916 - span: 1:793-916 + span: 1:790-916 span: 1:774-918 - FuncCall: name: @@ -502,4 +502,4 @@ ast: span: 1:1006-1020 span: 1:999-1020 span: 1:762-1020 - span: 1:762-1021 + span: 1:0-1020