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

Allow blocks to appear as elements in lists and tuples #2415

Merged
merged 10 commits into from
Sep 17, 2021
Merged

Conversation

runarorama
Copy link
Contributor

@runarorama runarorama commented Sep 17, 2021

Fixes #2258.

This changes and unifies the way the parser handles brackets—namely parentheses (()) and square brackets ([])—and commas (,) inside them. A bracket now opens a block, and the corresponding closing bracket closes it. Any comma between brackets also closes any inner blocks that were opened inside the brackets.

As a result, blocks can now appear as elements in lists and tuples, so this is valid syntax:

[if true then 0 else 1]

as is

(if true then 0 else 1, "hi")

This incidentally also adds support for redundant commas in tuples, like (,"one",,"two",), making that syntax consistent with lists.

@runarorama runarorama marked this pull request as draft September 17, 2021 02:46
@runarorama runarorama marked this pull request as ready for review September 17, 2021 03:04
@runarorama runarorama added the ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved label Sep 17, 2021
Just (block, n) -> do
S.put (env { layout = drop n (layout env), opening = Just block })
let opens = [Token (Reserved close) pos1 pos2]
pure $ replicate (n-1) (Token Close pos1 pos2) ++ opens
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The n-1 here is key. Close as many blocks as necessary to get us back to the bracket block.

@@ -84,9 +84,9 @@ effectList = do

sequenceTyp :: Var v => TypeP v
sequenceTyp = do
open <- reserved "["
open <- openBlockWith "["
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's a bit weird that blocks can appear in list types, but there aren't any block type elements at this level anyway.

Copy link
Member

@pchiusano pchiusano left a comment

Choose a reason for hiding this comment

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

Cool. I left a few suggestions that I think might be good improvements in the Lexer. Have a look. I also added one additional test case, directly to the transcript.

I removed ready-to-merge so you can review my comments and address and merge at your leisure.

parser-typechecker/src/Unison/Lexer.hs Outdated Show resolved Hide resolved
parser-typechecker/src/Unison/Lexer.hs Outdated Show resolved Hide resolved
parser-typechecker/src/Unison/Lexer.hs Outdated Show resolved Hide resolved
@pchiusano pchiusano removed the ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved label Sep 17, 2021
@runarorama runarorama added the ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved label Sep 17, 2021
@mergify mergify bot merged commit 88cebf7 into trunk Sep 17, 2021
@mergify mergify bot deleted the topic/listblocks branch September 17, 2021 19:40
@mergify mergify bot removed the ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved label Sep 17, 2021
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.

Parser rejects if-then-expression if it's a direct element of a list
2 participants