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: Add DocComments to PR #4701

Merged
merged 44 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
063159d
feat(fmt): An attempt at aesthetic items into PL
max-sixty Jun 20, 2024
dda1b0c
Merge branch 'main' into add-comments-to-pl
max-sixty Jun 20, 2024
36bfd06
Merge branch 'main' into add-comments-to-pl
max-sixty Jun 20, 2024
d4a8fb7
clarify explanation
max-sixty Jun 20, 2024
3ee3cad
internal: Add `Clone` to parsers
max-sixty Jun 20, 2024
b7269a5
Merge branch 'add-clone-to-parsers' into add-comments-to-pl
max-sixty Jun 20, 2024
9900fc0
Merge branch 'main' into add-comments-to-pl
max-sixty Jun 20, 2024
391b0b3
Merge branch 'main' into add-comments-to-pl
max-sixty Jun 28, 2024
47ad774
Merge branch 'main' into add-annotations-to-pl
max-sixty Jun 30, 2024
b187e72
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 1, 2024
e4aeac0
max-sixty Jul 1, 2024
2c52644
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 3, 2024
2b52887
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 8, 2024
28001bf
max-sixty Jul 8, 2024
e5a4f26
fix: Enforce a line break between annotations
max-sixty Jul 8, 2024
518523c
Merge branch 'annotation-line-break' into add-annotations-to-pl
max-sixty Jul 8, 2024
8b9d45f
max-sixty Jul 8, 2024
f2eacc9
Merge branch 'annotation-line-break' into add-annotations-to-pl
max-sixty Jul 8, 2024
ab7905c
Revert ""
max-sixty Jul 8, 2024
577924a
Revert "fix: Enforce a line break between annotations"
max-sixty Jul 8, 2024
369ec03
Assume chumsky parser outputs are `Debug`
max-sixty Jul 8, 2024
2ba018f
Add some dbgs
max-sixty Jul 8, 2024
2e84eff
Merge branch 'chumsky-debug' into add-annotations-to-pl
max-sixty Jul 8, 2024
42acef1
max-sixty Jul 8, 2024
11f3781
max-sixty Jul 8, 2024
67d6328
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 10, 2024
419fbfa
.
max-sixty Jul 10, 2024
d670030
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 10, 2024
fd3e094
.
max-sixty Jul 11, 2024
6b700af
.
max-sixty Jul 11, 2024
da5221c
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 12, 2024
f5ed3f4
almost there
max-sixty Jul 13, 2024
d2447c4
.
max-sixty Jul 13, 2024
b6018f9
max-sixty Jul 13, 2024
c932c7c
max-sixty Jul 13, 2024
da56a30
max-sixty Jul 13, 2024
511bffa
max-sixty Jul 13, 2024
97a6ad1
max-sixty Jul 13, 2024
df678f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2024
a36dccb
max-sixty Jul 13, 2024
10ddf91
max-sixty Jul 13, 2024
c46a50d
max-sixty Jul 13, 2024
47e94c5
Merge branch 'main' into add-annotations-to-pl
max-sixty Jul 13, 2024
909bcd1
max-sixty Jul 13, 2024
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
4 changes: 4 additions & 0 deletions prqlc/prqlc-parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ pub enum MessageKind {
pub enum Reason {
Simple(String),
Expected {
/// Where we were
// (could rename to `where` / `location` / `within`?)
who: Option<String>,
/// What we expected
expected: String,
/// What we found
found: String,
},
Unexpected {
Expand Down
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 @@ -184,6 +184,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
95 changes: 89 additions & 6 deletions prqlc/prqlc-parser/src/parser/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use super::pr::{Annotation, Stmt, StmtKind};
use crate::lexer::lr::TokenKind;
use crate::span::Span;

pub fn ident_part() -> impl Parser<TokenKind, String, Error = PError> + Clone {
return select! {
use super::SupportsDocComment;

pub(crate) fn ident_part() -> impl Parser<TokenKind, String, Error = PError> + Clone {
select! {
TokenKind::Ident(ident) => ident,
TokenKind::Keyword(ident) if &ident == "module" => ident,
}
Expand All @@ -16,18 +18,27 @@ pub fn ident_part() -> impl Parser<TokenKind, String, Error = PError> + Clone {
[Some(TokenKind::Ident("".to_string()))],
e.found().cloned(),
)
});
})
}

pub fn keyword(kw: &'static str) -> impl Parser<TokenKind, (), Error = PError> + Clone {
pub(crate) fn keyword(kw: &'static str) -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::Keyword(kw.to_string())).ignored()
}

/// Our approach to new lines is each item consumes new lines _before_ itself,
/// but not newlines after itself. This allows us to enforce new lines between
/// some items. The only place we handle new lines after an item is in the root
/// parser.
pub fn new_line() -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::NewLine).ignored()
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")
}

pub fn ctrl(char: char) -> impl Parser<TokenKind, (), Error = PError> + Clone {
pub(crate) fn ctrl(char: char) -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::Control(char)).ignored()
}

Expand All @@ -36,5 +47,77 @@ pub fn into_stmt((annotations, kind): (Vec<Annotation>, StmtKind), span: Span) -
kind,
span: Some(span),
annotations,
doc_comment: None,
}
}

pub(crate) fn doc_comment() -> impl Parser<TokenKind, String, Error = PError> + Clone {
// doc comments must start on a new line, so we enforce a new line (which
// can also be a file start) before the doc comment
//
// TODO: we currently lose any empty newlines between doc comments;
// eventually we want to retain them
(new_line().repeated().at_least(1).ignore_then(select! {
TokenKind::DocComment(dc) => dc,
}))
.repeated()
.at_least(1)
.collect()
.map(|lines: Vec<String>| lines.join("\n"))
.labelled("doc comment")
}

pub(crate) fn with_doc_comment<'a, P, O>(
parser: P,
) -> impl Parser<TokenKind, O, Error = PError> + Clone + 'a
where
P: Parser<TokenKind, O, Error = PError> + Clone + 'a,
O: SupportsDocComment + 'a,
{
doc_comment()
.or_not()
.then(parser)
.map(|(doc_comment, inner)| inner.with_doc_comment(doc_comment))
}

#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;

use super::*;
use crate::test::parse_with_parser;

#[test]
fn test_doc_comment() {
assert_debug_snapshot!(parse_with_parser(r#"
#! doc comment
#! another line

"#, doc_comment()), @r###"
Ok(
" doc comment\n another line",
)
"###);
}

#[test]
fn test_doc_comment_or_not() {
assert_debug_snapshot!(parse_with_parser(r#"hello"#, doc_comment().or_not()).unwrap(), @"None");
assert_debug_snapshot!(parse_with_parser(r#"hello"#, doc_comment().or_not().then_ignore(new_line().repeated()).then(ident_part())).unwrap(), @r###"
(
None,
"hello",
)
"###);
}

#[test]
fn test_no_doc_comment_in_with_doc_comment() {
impl SupportsDocComment for String {
fn with_doc_comment(self, _doc_comment: Option<String>) -> Self {
self
}
}
assert_debug_snapshot!(parse_with_parser(r#"hello"#, with_doc_comment(new_line().ignore_then(ident_part()))).unwrap(), @r###""hello""###);
}
}
Loading
Loading