Skip to content

Commit

Permalink
feat: Improving error messages when parsin
Browse files Browse the repository at this point in the history
Exclude suggestions to add 'start of file' :)

Also renames `parse_single` to `parse_source`
  • Loading branch information
max-sixty committed Jul 15, 2024
1 parent c58fb33 commit 1f2006f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 67 deletions.
45 changes: 44 additions & 1 deletion prqlc/prqlc-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,56 @@ fn operator_coalesce() -> impl Parser<TokenKind, BinOp, Error = PError> + Clone
#[cfg(test)]
mod tests {

use insta::assert_yaml_snapshot;
use insta::{assert_debug_snapshot, assert_yaml_snapshot};

use super::super::test::trim_start;
use crate::test::parse_with_parser;

use super::*;

#[test]
fn test_tuple() {
let tuple = || trim_start().ignore_then(tuple(expr()));
assert_yaml_snapshot!(
parse_with_parser(r#"{a = 5, b = 6}"#, tuple()).unwrap(),
@r###"
---
Tuple:
- Literal:
Integer: 5
span: "0:5-6"
alias: a
- Literal:
Integer: 6
span: "0:12-13"
alias: b
"###);

assert_debug_snapshot!(
parse_with_parser(r#"
{a = 5
b = 6}"#, tuple()).unwrap_err(),
@r###"
[
Error {
kind: Error,
span: Some(
0:33-34,
),
reason: Expected {
who: Some(
"new line",
),
expected: "}",
found: "b",
},
hints: [],
code: None,
},
]
"###);
}

#[test]
fn test_expr() {
assert_yaml_snapshot!(
Expand Down
9 changes: 7 additions & 2 deletions prqlc/prqlc-parser/src/parser/perror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ impl From<PError> for Error {
.all(|t| matches!(t, None | Some(TokenKind::NewLine)));
let expected: Vec<String> = e
.expected()
// Only include whitespace if we're _only_ expecting whitespace
.filter(|t| is_all_whitespace || !matches!(t, None | Some(TokenKind::NewLine)))
// Only include whitespace if we're _only_ expecting whitespace,
// otherwise we get "expected start of line or new line or }"
// when there's no ending `}`, since a new line is allowed.
.filter(|t| {
is_all_whitespace
|| !matches!(t, None | Some(TokenKind::NewLine) | Some(TokenKind::Start))
})
.cloned()
.map(token_to_string)
.collect();
Expand Down
Loading

0 comments on commit 1f2006f

Please sign in to comment.