Skip to content

feat: Add a line continuation character - #3408

Merged
max-sixty merged 21 commits into
PRQL:mainfrom
max-sixty:line-continuation
Aug 27, 2023
Merged

max-sixty merged 21 commits into
PRQL:mainfrom
max-sixty:line-continuation

Conversation

@max-sixty

@max-sixty max-sixty commented Aug 25, 2023 •

Copy link
Copy Markdown
Member

Would close #3380, close #819

This kinda works but we need to decide how to handle comments. It's described inline — but basically:

derive x = 1 +
# \ 2 +
\ 3

...doesn't work, because the middle line is commented out, so is interpreted as a new line.

The big advantage of the line continuation character at the start of the line vs. the end is that it's easy to comment out parts of an expression, which this doesn't support.


One idea — have a line continuation character join to the previous line with content. So these would all be equal:

(the elm formatting makes this look confusing, it'll be better in PRQL formatting, I think?)

derive x = 1 + 3

derive x = 1 +
\ 3

derive x = 1 +
# \ 2 +
\ 3

derive x = 1 +
# hello

# \ 2 +
\ 3

A refinement of that idea would be to only allow comments, not blank lines, to be joined to.

What do folks think?

Comment thread crates/prql-parser/src/lexer.rs Resolved
@eitsupi

eitsupi commented Aug 25, 2023

Copy link
Copy Markdown
Member

This looks great!

When I look at the discussion in #819, I see that the similarity to the comma at the end of the line is an issue, so perhaps the following example would be more appropriate?

derive x = 1
# \ + 2
\ + 3

@aljazerzen

Copy link
Copy Markdown
Member

Nice work!

Looking at this I had quite a controversial idea: the fact that a new line has semantic meaning is quite strange for a language. How much worse would PRQL look like if we required the | for pipes?

from albums
| select {album_id, title}
| group title (
   sort album_id
   | take 1
)

@max-sixty

Copy link
Copy Markdown
Member Author

How much worse would PRQL look like if we required the | for pipes?

I thought about this a while ago. I think one of the beautiful features of PRQL is that it's completely based around pipelines, and so can design the syntax around that. And that means that we don't need special pipe characters...

This does seem mostly opinion / aesthetic. One more objective measure is comparing the number of times we need a line-continuation character with the number of times we need a pipe — the latter is 1-2 orders of magnitude greater.

@aljazerzen

Copy link
Copy Markdown
Member

That's true, continuation character is much less common than a pipeline. Cool, I concur.

@max-sixty

Copy link
Copy Markdown
Member Author

I implemented an approach to "skip over" lines with comments, which I think is the best approach.

One caveat — it does mean that when lexing PRQL, we always need to find the next non-comment, non-empty line to know when to finish lexing that "logical" line. I think that's probably OK given our performance requirements are lower than most...

@eitsupi

eitsupi commented Aug 26, 2023

Copy link
Copy Markdown
Member

One of my questions is that the meaning of line breaks changes (line breaks are ignored) in tuples {...} and arrays [...].
Outside of these the line break is equivalent to a pipe, but only within these it does not seem to be so?

@max-sixty

Copy link
Copy Markdown
Member Author

Outside of these the line break is equivalent to a pipe, but only within these it does not seem to be so?

Exactly correct!

A line-break doesn't create a pipeline in a couple of cases:

Lmk if we could make that clearer

@max-sixty

Copy link
Copy Markdown
Member Author

This is ready to go, let me know any concerns?

One reason not to merge is that we think it makes lexing less performant, and prevents us from optimizing in the future. It's basically doing exactly what @matlkad suggests not doing in https://matklad.github.io/2023/08/01/on-modularity-of-lexical-analysis.html ...

But otherwise I'll merge...

@eitsupi

eitsupi commented Aug 27, 2023

Copy link
Copy Markdown
Member

Outside of these the line break is equivalent to a pipe, but only within these it does not seem to be so?

Exactly correct!

A line-break doesn't create a pipeline in a couple of cases:

Lmk if we could make that clearer

Oh, I did not check that excellent document. Thanks for pointing that.
I think this is enough to clear up my question.

@max-sixty

Copy link
Copy Markdown
Member Author

Let me cross link that existing doc & this new documentation

@max-sixty

Copy link
Copy Markdown
Member Author

FYI I renamed this in the docs to "line wrapping", from "line continuation"...

Comment thread web/book/src/reference/syntax/pipes.md Resolved
- within an array
- when the following line is a new statement, which starts with a keyword of
`func`, `let` or `from`.
`func`, `let` or `from`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reference to func still correct here or is this outdated since the introduction of let?

IIRC, a function definition including the optional func would need to be written as

let my_func = func x -> ...

So a line would never start with func anymore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually just about to remove this! I was going to completely remove func...

@snth

snth commented Aug 27, 2023

Copy link
Copy Markdown
Member

Looking at this I had quite a controversial idea: the fact that a new line has semantic meaning is quite strange for a language.

I don't think it is that uncommon. bash, Python, and Julia all have \n generally equivalent to ; to name just a few that come to mind.

How much worse would PRQL look like if we required the | for pipes?

I see you've already moved on from this but I just wanted to add that I think it would be substantially worse.

@snth

snth commented Aug 27, 2023

Copy link
Copy Markdown
Member

One of my questions is that the meaning of line breaks changes (line breaks are ignored) in tuples {...} and arrays [...].
Outside of these the line break is equivalent to a pipe, but only within these it does not seem to be so?

Exactly correct!

Could we not add parentheses () to this list? That would address most, if not all, of my use cases raised in #3380 .

It's also quite a common pattern, for example in Python for things like the following:

s = ("this string "
     "reads better when "
     "written over multiple lines")

EDIT: I guess one counterpoint is that you would need to know whether you are inside a "column expression" or a parenthesis for a pipeline like after a group so I understand if that's not possible with our setup.

Similarly, many languages seem to be able to cope with unfinished expressions where a \n comes after an infix operator as in

derive x = 1 +
   3

IDK enough about compilers to know what makes that possible for them and what the performance impact is. I do see Max's point about the impact of \ on the Lexer.

@snth

snth commented Aug 27, 2023

Copy link
Copy Markdown
Member

Thank you very much for implementing this @max-sixty ! 🙏

@max-sixty

Copy link
Copy Markdown
Member Author

EDIT: I guess one counterpoint is that you would need to know whether you are inside a "column expression" or a parenthesis for a pipeline like after a group so I understand if that's not possible with our setup.

Yeah re pipelines.

And I also think that grammar being different inside vs. outside of parentheses is less consistent, and so worse, all-else-equal

@max-sixty
max-sixty enabled auto-merge (squash) August 27, 2023 19:47
@max-sixty
max-sixty merged commit 983bb3e into PRQL:main Aug 27, 2023
@max-sixty
max-sixty deleted the line-continuation branch August 27, 2023 19:59
@eitsupi

eitsupi commented Aug 27, 2023

Copy link
Copy Markdown
Member

And I also think that grammar being different inside vs. outside of parentheses is less consistent, and so worse, all-else-equal

I agree. I hate using parentheses in Python method chains just to bring the dot to the beginning of the line...... (Of course I don't have to do it myself because the wonderful Black handles it automatically.)

@eitsupi eitsupi mentioned this pull request Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line continuation inside parentheses Syntax to break an expression over multiple lines?

4 participants