Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jul 13, 2024
1 parent c932c7c commit da56a30
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 58 deletions.
3 changes: 3 additions & 0 deletions prqlc/prqlc-parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ fn line_wrap() -> impl Parser<char, TokenKind, Error = Cheap<char>> {

fn comment() -> impl Parser<char, TokenKind, Error = Cheap<char>> {
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()
Expand Down
3 changes: 2 additions & 1 deletion prqlc/prqlc-parser/src/parser/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub fn keyword(kw: &'static str) -> impl Parser<TokenKind, (), Error = PError> +

pub fn new_line() -> impl Parser<TokenKind, (), Error = PError> + 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")
Expand Down Expand Up @@ -57,7 +59,6 @@ pub fn doc_comment() -> impl Parser<TokenKind, String, Error = PError> + Clone {
.repeated()
.at_least(1)
.collect()
.debug("doc_comment")
.map(|lines: Vec<String>| lines.join("\n"))
.labelled("doc comment")
}
Expand Down
66 changes: 50 additions & 16 deletions prqlc/prqlc-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(']'))
Expand Down Expand Up @@ -169,21 +166,19 @@ fn interpolation() -> impl Parser<TokenKind, ExprKind, Error = PError> + Clone {
fn case(
expr: impl Parser<TokenKind, Expr, Error = PError> + Clone,
) -> impl Parser<TokenKind, ExprKind, Error = PError> + 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)
Expand Down Expand Up @@ -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"
"###);
}
}
20 changes: 11 additions & 9 deletions prqlc/prqlc-parser/src/parser/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"###);
}

Expand Down
9 changes: 5 additions & 4 deletions prqlc/prqlc/tests/integration/bad_error_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
───╯
"###);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ ast:
alias: d
span: 1:52-65
span: 1:0-65
span: 1:0-66
span: 1:0-65
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,4 +170,4 @@ ast:
alias: a
span: 1:217-230
span: 1:187-230
span: 1:187-231
span: 1:185-230
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit da56a30

Please sign in to comment.