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

Handle empty code blocks in Doc2 #5352

Merged
merged 2 commits into from
Sep 20, 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
21 changes: 21 additions & 0 deletions unison-src/transcripts/fix5349.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```ucm:hide
scratch/main> builtins.mergeio
```

Empty code blocks are invalid in Unison, but shouldn’t crash the parser.

````unison:error
README = {{
```
```
}}
````

````unison:error
README = {{ {{ }} }}
````


````unison:error
README = {{ `` `` }}
````
78 changes: 78 additions & 0 deletions unison-src/transcripts/fix5349.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Empty code blocks are invalid in Unison, but shouldn’t crash the parser.

```` unison
README = {{
```
```
}}
````

``` ucm

Loading changes detected in scratch.u.

I expected a block after this (in red), but there wasn't one. Maybe check your indentation:
0 | README = {{


```
``` unison
README = {{ {{ }} }}
```

``` ucm

Loading changes detected in scratch.u.

I got confused here:



I was surprised to find an end of input here.
I was expecting one of these instead:

* bang
* do
* false
* force
* handle
* if
* lambda
* let
* quote
* termLink
* true
* tuple
* typeLink

```
``` unison
README = {{ `` `` }}
```

``` ucm

Loading changes detected in scratch.u.

I got confused here:



I was surprised to find an end of input here.
I was expecting one of these instead:

* bang
* do
* false
* force
* handle
* if
* lambda
* let
* quote
* termLink
* true
* tuple
* typeLink

```
17 changes: 10 additions & 7 deletions unison-syntax/src/Unison/Syntax/Lexer/Unison.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ data ParsingEnv = ParsingEnv
}
deriving (Show)

initialEnv :: BlockName -> ParsingEnv
initialEnv scope = ParsingEnv [] (Just scope) True

type P = P.ParsecT (Token Err) String (S.State ParsingEnv)

data Err
Expand Down Expand Up @@ -196,7 +199,7 @@ token'' tok p = do
pops p = do
env <- S.get
let l = layout env
if top l == column p && topContainsVirtualSemis l
if column p == top l && topContainsVirtualSemis l
then pure [Token (Semi True) p p]
else
if column p > top l || topHasClosePair l
Expand Down Expand Up @@ -289,7 +292,7 @@ lexer scope rem =
(P.EndOfInput) -> "end of input"
customErrs es = [Err <$> e | P.ErrorCustom e <- toList es]
toPos (P.SourcePos _ line col) = Pos (P.unPos line) (P.unPos col)
env0 = ParsingEnv [] (Just scope) True
env0 = initialEnv scope

-- | hacky postprocessing pass to do some cleanup of stuff that's annoying to
-- fix without adding more state to the lexer:
Expand Down Expand Up @@ -422,15 +425,15 @@ doc2 = do

lexemes' :: P () -> P [Token Lexeme]
lexemes' eof =
-- NB: `postLex` requires the token stream to start with an `Open`, otherwise it can’t create a `T`, so this adds one,
-- runs `postLex`, then removes it.
-- NB: `postLex` requires the token stream to start with an `Open`, otherwise it can’t create a `BlockTree`, so this
-- adds one, runs `postLex`, then removes it.
fmap (tail . postLex . (Token (Open "fake") mempty mempty :)) $
local (\env -> env {inLayout = True, opening = Just "DUMMY"}) do
local (const $ initialEnv "DUMMY") do
p <- lexemes $ [] <$ eof
-- deals with a final "unclosed" block at the end of `p`)
unclosed <- takeWhile (("DUMMY" /=) . fst) . layout <$> S.get
let pos = end $ last p
pure $ p <> replicate (length unclosed) (Token Close pos pos)
finalPos <- posP
pure $ p <> replicate (length unclosed) (Token Close finalPos finalPos)

-- | Consumes an entire Unison “module”.
lexemes :: P [Token Lexeme] -> P [Token Lexeme]
Expand Down
3 changes: 2 additions & 1 deletion unison-syntax/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ main =
test :: Test ()
test =
scope "lexer" . tests $
[ t "1" [Numeric "1"],
[ t "" [],
t "1" [Numeric "1"],
t "+1" [Numeric "+1"],
t "-1" [Numeric "-1"],
t "-1.0" [Numeric "-1.0"],
Expand Down