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

feat: prqlc compile --debug-log #4646

Merged
merged 4 commits into from
Jun 24, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ _*.sql
*.snap.new

*.my.csv
log*.json
log*.html

cobertura.xml
codecov.json
Expand Down
8 changes: 4 additions & 4 deletions prqlc/bindings/prqlc-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ mod debug {
#[pyfunction]
pub fn prql_lineage(prql_query: &str) -> PyResult<String> {
prqlc_lib::prql_to_pl(prql_query)
.and_then(prqlc_lib::debug::pl_to_lineage)
.and_then(|x| prqlc_lib::debug::json::from_lineage(&x))
.and_then(prqlc_lib::internal::pl_to_lineage)
.and_then(|x| prqlc_lib::internal::json::from_lineage(&x))
.map_err(|err| (PyErr::new::<exceptions::PyValueError, _>(err.to_json())))
}

#[pyfunction]
pub fn pl_to_lineage(pl_json: &str) -> PyResult<String> {
prqlc_lib::json::to_pl(pl_json)
.and_then(prqlc_lib::debug::pl_to_lineage)
.and_then(|x| prqlc_lib::debug::json::from_lineage(&x))
.and_then(prqlc_lib::internal::pl_to_lineage)
.and_then(|x| prqlc_lib::internal::json::from_lineage(&x))
.map_err(|err| (PyErr::new::<exceptions::PyValueError, _>(err.to_json())))
}
}
Expand Down
7 changes: 6 additions & 1 deletion prqlc/prqlc-parser/src/lexer/lr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pub mod token;
mod token;

pub use token::*;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]

Check warning on line 7 in prqlc/prqlc-parser/src/lexer/lr/mod.rs

View check run for this annotation

Codecov / codecov/patch

prqlc/prqlc-parser/src/lexer/lr/mod.rs#L7

Added line #L7 was not covered by tests
pub struct Tokens(pub Vec<Token>);
16 changes: 5 additions & 11 deletions prqlc/prqlc-parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use chumsky::{
error::Cheap,
prelude::*,
text::{newline, Character},
};
use serde::{Deserialize, Serialize};
use chumsky::error::Cheap;
use chumsky::prelude::*;
use chumsky::text::{newline, Character};

use crate::error::{Error, ErrorSource, Reason, WithErrorInfo};
use crate::lexer::lr::{Literal, Token, TokenKind, ValueAndUnit};
Expand All @@ -24,8 +21,8 @@ pub fn lex_string_recovery(source: &str, source_id: u16) -> (Option<Vec<Token>>,
(tokens, errors)
}

pub fn lex_source(source: &str) -> Result<TokenVec, Vec<Error>> {
lexer().parse(source).map(TokenVec).map_err(|e| {
pub fn lex_source(source: &str) -> Result<lr::Tokens, Vec<Error>> {
lexer().parse(source).map(lr::Tokens).map_err(|e| {
e.into_iter()
.map(|x| convert_lexer_error(source, x, 0))
.collect()
Expand Down Expand Up @@ -470,6 +467,3 @@ fn end_expr() -> impl Parser<char, (), Error = Cheap<char>> {
))
.rewind()
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct TokenVec(pub Vec<Token>);
163 changes: 81 additions & 82 deletions prqlc/prqlc-parser/src/lexer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@ use chumsky::Parser;
use insta::assert_debug_snapshot;
use insta::assert_snapshot;

use crate::lexer::lr::token::{Literal, TokenKind};
use crate::lexer::TokenVec;
use crate::lexer::lr::{Literal, TokenKind, Tokens};
use crate::lexer::{lex_source, lexer, literal, quoted_string};

#[test]
fn line_wrap() {
assert_debug_snapshot!(TokenVec(lexer().parse(r"5 +
assert_debug_snapshot!(Tokens(lexer().parse(r"5 +
\ 3 "
).unwrap()), @r###"
TokenVec(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
3..9: LineWrap([]),
10..11: Literal(Integer(3)),
],
)
"###);
Tokens(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
3..9: LineWrap([]),
10..11: Literal(Integer(3)),
],
)
"###);

// Comments are included; no newline after the comments
assert_debug_snapshot!(TokenVec(lexer().parse(r"5 +
assert_debug_snapshot!(Tokens(lexer().parse(r"5 +
# comment
# comment with whitespace
\ 3 "
).unwrap()), @r###"
TokenVec(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
3..46: LineWrap([Comment(" comment"), Comment(" comment with whitespace")]),
47..48: Literal(Integer(3)),
],
)
"###);
Tokens(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
3..46: LineWrap([Comment(" comment"), Comment(" comment with whitespace")]),
47..48: Literal(Integer(3)),
],
)
"###);

// Check display, for the test coverage (use `assert_eq` because the
// line-break doesn't work well with snapshots)
Expand Down Expand Up @@ -75,28 +74,28 @@ fn numbers() {

#[test]
fn debug_display() {
assert_debug_snapshot!(TokenVec(lexer().parse("5 + 3").unwrap()), @r###"
TokenVec(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
4..5: Literal(Integer(3)),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("5 + 3").unwrap()), @r###"
Tokens(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
4..5: Literal(Integer(3)),
],
)
"###);
}

#[test]
fn comment() {
assert_debug_snapshot!(TokenVec(lexer().parse("# comment\n# second line").unwrap()), @r###"
TokenVec(
[
0..9: Comment(" comment"),
9..10: NewLine,
10..23: Comment(" second line"),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("# comment\n# second line").unwrap()), @r###"
Tokens(
[
0..9: Comment(" comment"),
9..10: NewLine,
10..23: Comment(" second line"),
],
)
"###);

assert_snapshot!(TokenKind::Comment(" This is a single-line comment".to_string()), @r###"
# This is a single-line comment
Expand All @@ -105,13 +104,13 @@ fn comment() {

#[test]
fn doc_comment() {
assert_debug_snapshot!(TokenVec(lexer().parse("#! docs").unwrap()), @r###"
TokenVec(
[
0..7: DocComment(" docs"),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("#! docs").unwrap()), @r###"
Tokens(
[
0..7: DocComment(" docs"),
],
)
"###);
}

#[test]
Expand Down Expand Up @@ -151,43 +150,43 @@ fn quotes() {

#[test]
fn range() {
assert_debug_snapshot!(TokenVec(lexer().parse("1..2").unwrap()), @r###"
TokenVec(
[
0..1: Literal(Integer(1)),
1..3: Range { bind_left: true, bind_right: true },
3..4: Literal(Integer(2)),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("1..2").unwrap()), @r###"
Tokens(
[
0..1: Literal(Integer(1)),
1..3: Range { bind_left: true, bind_right: true },
3..4: Literal(Integer(2)),
],
)
"###);

assert_debug_snapshot!(TokenVec(lexer().parse("..2").unwrap()), @r###"
TokenVec(
[
0..2: Range { bind_left: true, bind_right: true },
2..3: Literal(Integer(2)),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("..2").unwrap()), @r###"
Tokens(
[
0..2: Range { bind_left: true, bind_right: true },
2..3: Literal(Integer(2)),
],
)
"###);

assert_debug_snapshot!(TokenVec(lexer().parse("1..").unwrap()), @r###"
TokenVec(
[
0..1: Literal(Integer(1)),
1..3: Range { bind_left: true, bind_right: true },
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("1..").unwrap()), @r###"
Tokens(
[
0..1: Literal(Integer(1)),
1..3: Range { bind_left: true, bind_right: true },
],
)
"###);

assert_debug_snapshot!(TokenVec(lexer().parse("in ..5").unwrap()), @r###"
TokenVec(
[
0..2: Ident("in"),
2..5: Range { bind_left: false, bind_right: true },
5..6: Literal(Integer(5)),
],
)
"###);
assert_debug_snapshot!(Tokens(lexer().parse("in ..5").unwrap()), @r###"
Tokens(
[
0..2: Ident("in"),
2..5: Range { bind_left: false, bind_right: true },
5..6: Literal(Integer(5)),
],
)
"###);
}

#[test]
Expand All @@ -196,7 +195,7 @@ fn test_lex_source() {

assert_debug_snapshot!(lex_source("5 + 3"), @r###"
Ok(
TokenVec(
Tokens(
[
0..1: Literal(Integer(5)),
2..3: Control('+'),
Expand Down
Loading
Loading