Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enforce a line break between annotations #4699

Merged
merged 8 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion prqlc/prqlc-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn array<'a>(
fn pipeline_expr(
expr: impl Parser<TokenKind, Expr, Error = PError> + Clone,
) -> impl Parser<TokenKind, Expr, Error = PError> + Clone {
expr.padded_by(new_line().repeated())
expr.then_ignore(new_line().repeated())
.delimited_by(ctrl('('), ctrl(')'))
.recover_with(nested_delimiters(
TokenKind::Control('('),
Expand Down Expand Up @@ -772,4 +772,22 @@ mod tests {
@r###"
"###);
}

#[test]
fn forced_new_lines() {
// Not sure whether this is possible to adjust, putting a test here
// as a note.
//
// Check the opening new lines aren't consumed
assert!(parse_with_parser(
r#"
{
#! doc comment
derive x = 5
}
"#,
trim_start().ignore_then(tuple(expr())),
)
.is_err());
}
}
11 changes: 11 additions & 0 deletions prqlc/prqlc-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ where
parser
.separated_by(ctrl(',').then_ignore(new_line().repeated()))
.allow_trailing()
// Note because we pad rather than only take the ending new line, we
// can't put items that require a new line in a tuple, like:
//
// ```
// {
// !# doc comment
// a,
// }
// ```
// ...but I'm not sure there's a way around it, since we do need to
// consume newlines in tuples...
.padded_by(new_line().repeated())
}

Expand Down
43 changes: 37 additions & 6 deletions prqlc/prqlc-parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ fn module_contents() -> impl Parser<TokenKind, Vec<Stmt>, Error = PError> {
.ignore_then(ident_part())
.then(
module_contents
.padded_by(new_line().repeated())
.then_ignore(new_line().repeated())
.delimited_by(ctrl('{'), ctrl('}')),
)
.map(|(name, stmts)| StmtKind::ModuleDef(ModuleDef { name, stmts }))
.labelled("module definition");

let annotation = new_line()
.repeated()
// TODO: we could enforce annotations starting on a new line?
// .at_least(1)
.at_least(1)
.ignore_then(
just(TokenKind::Annotate)
.ignore_then(expr())
Expand Down Expand Up @@ -333,7 +332,7 @@ mod tests {
Literal:
Integer: 1
span: "0:50-51"
span: "0:38-51"
span: "0:25-51"
- VarDef:
kind: Let
name: man
Expand Down Expand Up @@ -408,7 +407,7 @@ mod tests {
Literal:
Integer: 1
span: "0:51-52"
span: "0:40-52"
span: "0:27-52"
- VarDef:
kind: Let
name: verona
Expand All @@ -426,7 +425,7 @@ mod tests {
}

#[test]
fn test_doc_comment_module() {
fn doc_comment_module() {
assert_yaml_snapshot!(parse_with_parser(r#"

#! first doc comment
Expand Down Expand Up @@ -492,4 +491,36 @@ mod tests {
doc_comment: " second doc comment"
"###);
}

#[test]
fn doc_comment_inline_module() {
// Check the newline doesn't get eated by the `{}` of the module
// TODO: could give a better error when we forget the module name
assert_yaml_snapshot!(parse_with_parser(r#"
module bar {
#! first doc comment
from foo
}
"#, module_contents()).unwrap(), @r###"
---
- ModuleDef:
name: bar
stmts:
- VarDef:
kind: Main
name: main
value:
FuncCall:
name:
Ident: from
span: "0:63-67"
args:
- Ident: foo
span: "0:68-71"
span: "0:63-71"
span: "0:52-71"
doc_comment: " first doc comment"
span: "0:0-81"
"###);
}
}
1 change: 0 additions & 1 deletion prqlc/prqlc-parser/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
)
}),
)
.padded_by(new_line().repeated())
.delimited_by(ctrl('{'), ctrl('}'))
.recover_with(nested_delimiters(
TokenKind::Control('{'),
Expand Down
8 changes: 8 additions & 0 deletions prqlc/prqlc-parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,14 @@ fn test_annotation() {
"#,
)
.unwrap();

parse_source(
r#"
@{binding_strength=1}@{binding_strength=2}
let add = a b -> a + b
"#,
)
.unwrap_err();
}

#[test]
Expand Down
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
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
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
Loading