Skip to content
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
76 changes: 76 additions & 0 deletions unison-src/transcripts-round-trip/main.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,82 @@ So we can see the pretty-printed output:
fix_4352 : Doc2
fix_4352 = {{ `` +1 `` }}

fix_4384 : Doc2
fix_4384 = {{ {{ docExampleBlock 0 '2 }} }}

fix_4384a : Doc2
fix_4384a =
use Nat +
{{ {{ docExampleBlock 0 '(1 + 1) }} }}

fix_4384b : Doc2
fix_4384b = {{ {{ docExampleBlock 0 '99 }} }}

fix_4384c : Doc2
fix_4384c =
use Nat +
{{ {{ docExampleBlock 0 do
x = 1
y = 2
x + y }} }}

fix_4384d : Doc2
fix_4384d =
{{
{{
docExampleBlock 0 '[ 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
, 10
, 11
, 12
, 13
, 14
, 15
, 16
, 17
, 18
] }}
}}

fix_4384e : Doc2
fix_4384e =
id : x -> x
id x = x
{{
{{
docExampleBlock
0
(id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
id
(x -> 0)) }}
}}

Fix_525.bar.quaffle : Nat
Fix_525.bar.quaffle = 32

Expand Down
18 changes: 17 additions & 1 deletion unison-src/transcripts-round-trip/reparses-with-same-hash.u
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,20 @@ stew_issue3 =
Debug a b = ()
error
(Debug None '("Failed to get timestamp of config file " ++
toText configPath))
toText configPath))

fix_4384 = {{ {{ docExampleBlock 0 do 2 }} }}
fix_4384a = {{ {{ docExampleBlock 0 '(1 + 1) }} }}
fix_4384b = {{ {{ docExampleBlock 0 '99 }} }}
fix_4384c = {{ {{ docExampleBlock 0 do
x = 1
y = 2
x + y
}} }}

fix_4384d = {{ {{ docExampleBlock 0 '[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] }} }}

fix_4384e =
id : x -> x
id x = x
{{ {{ docExampleBlock 0 (id id id id id id id id id id id id id id id id id id id id id (x -> 0) }} }}
138 changes: 79 additions & 59 deletions unison-syntax/src/Unison/Syntax/Lexer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,6 @@ lexemes' eof =
<|> token wordyId
<|> (asum . map token) [semi, textual, hash]

wordySep c = isSpace c || not (wordyIdChar c)
positioned p = do start <- pos; a <- p; stop <- pos; pure (start, a, stop)

tok :: P a -> P [Token a]
tok p = do
(start, a, stop) <- positioned p
pure [Token a start stop]

doc2 :: P [Token Lexeme]
doc2 = do
let start = token'' ignore (lit "{{")
Expand Down Expand Up @@ -535,7 +527,7 @@ lexemes' eof =

docClose = [] <$ lit "}}"
docOpen = [] <$ lit "{{"

link =
P.label "link (examples: {type List}, {Nat.+})" $
wrap "syntax.docLink" $
Expand All @@ -544,8 +536,21 @@ lexemes' eof =

expr =
P.label "transclusion (examples: {{ doc2 }}, {{ sepBy s [doc1, doc2] }})" $
wrap "syntax.docTransclude" $
docOpen *> lexemes' docClose
openAs "{{" "syntax.docTransclude"
<+> do {
env0 <- S.get;
-- we re-allow layout within a transclusion, then restore it to its
-- previous state after
S.put (env0 { inLayout = True });
-- Note: this P.lookAhead ensures the }} isn't consumed,
-- so it can be consumed below by the `close` which will
-- pop items off the layout stack up to the nearest enclosing
-- syntax.docTransclude.
ts <- lexemes' (P.lookAhead ([] <$ lit "}}"));
S.modify (\env -> env { inLayout = inLayout env0 });
pure ts
}
<+> close ["syntax.docTransclude"] (lit "}}")
Comment on lines 538 to +553
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the actual fix / change. The rest is just shuffling things around.


nonNewlineSpace ch = isSpace ch && ch /= '\n' && ch /= '\r'
nonNewlineSpaces = P.takeWhileP Nothing nonNewlineSpace
Expand Down Expand Up @@ -879,9 +884,6 @@ lexemes' eof =
Nothing -> err start (InvalidShortHash potentialHash)
Just sh -> pure sh

separated :: (Char -> Bool) -> P a -> P a
separated ok p = P.try $ p <* P.lookAhead (void (P.satisfy ok) <|> P.eof)

numeric = bytes <|> otherbase <|> float <|> intOrNat
where
intOrNat = P.try $ num <$> sign <*> LP.decimal
Expand Down Expand Up @@ -1073,55 +1075,73 @@ lexemes' eof =
where
ok c = isDelayOrForce c || isSpace c || isAlphaNum c || Set.member c delimiters || c == '\"'

open :: String -> P [Token Lexeme]
open b = do
(start, _, end) <- positioned $ lit b
env <- S.get
S.put (env {opening = Just b})
pure [Token (Open b) start end]
separated :: (Char -> Bool) -> P a -> P a
separated ok p = P.try $ p <* P.lookAhead (void (P.satisfy ok) <|> P.eof)

openKw :: String -> P [Token Lexeme]
openKw s = separated wordySep $ do
(pos1, s, pos2) <- positioned $ lit s
env <- S.get
S.put (env {opening = Just s})
pure [Token (Open s) pos1 pos2]
open :: String -> P [Token Lexeme]
open b = openAs b b

close = close' Nothing
positioned :: P a -> P (Pos, a, Pos)
positioned p = do start <- pos; a <- p; stop <- pos; pure (start, a, stop)

closeKw' :: Maybe String -> [String] -> P String -> P [Token Lexeme]
closeKw' reopenBlockname open closeP = close' reopenBlockname open (separated wordySep closeP)
openAs :: String -> String -> P [Token Lexeme]
openAs syntax b = do
(start, _, end) <- positioned $ lit syntax
env <- S.get
S.put (env {opening = Just b})
pure [Token (Open b) start end]

blockDelimiter :: [String] -> P String -> P [Token Lexeme]
blockDelimiter open closeP = do
(pos1, close, pos2) <- positioned $ closeP
env <- S.get
case findClose open (layout env) of
Nothing -> err pos1 (UnexpectedDelimiter (quote close))
where
quote s = "'" <> s <> "'"
Just (_, n) -> do
S.put (env {layout = drop (n - 1) (layout env)})
let delims = [Token (Reserved close) pos1 pos2]
pure $ replicate (n - 1) (Token Close pos1 pos2) ++ delims

close' :: Maybe String -> [String] -> P String -> P [Token Lexeme]
close' reopenBlockname open closeP = do
(pos1, close, pos2) <- positioned $ closeP
env <- S.get
case findClose open (layout env) of
Nothing -> err pos1 (CloseWithoutMatchingOpen msgOpen (quote close))
where
msgOpen = intercalate " or " (quote <$> open)
quote s = "'" <> s <> "'"
Just (_, n) -> do
S.put (env {layout = drop n (layout env), opening = reopenBlockname})
let opens = maybe [] (const $ [Token (Open close) pos1 pos2]) reopenBlockname
pure $ replicate n (Token Close pos1 pos2) ++ opens

findClose :: [String] -> Layout -> Maybe (String, Int)
findClose _ [] = Nothing
findClose s ((h, _) : tl) = if h `elem` s then Just (h, 1) else fmap (1 +) <$> findClose s tl
openKw :: String -> P [Token Lexeme]
openKw s = separated wordySep $ do
(pos1, s, pos2) <- positioned $ lit s
env <- S.get
S.put (env {opening = Just s})
pure [Token (Open s) pos1 pos2]

wordySep :: Char -> Bool
wordySep c = isSpace c || not (wordyIdChar c)

tok :: P a -> P [Token a]
tok p = do
(start, a, stop) <- positioned p
pure [Token a start stop]

blockDelimiter :: [String] -> P String -> P [Token Lexeme]
blockDelimiter open closeP = do
(pos1, close, pos2) <- positioned $ closeP
env <- S.get
case findClose open (layout env) of
Nothing -> err pos1 (UnexpectedDelimiter (quote close))
where
quote s = "'" <> s <> "'"
Just (_, n) -> do
S.put (env {layout = drop (n - 1) (layout env)})
let delims = [Token (Reserved close) pos1 pos2]
pure $ replicate (n - 1) (Token Close pos1 pos2) ++ delims

close :: [String] -> P String -> P [Token Lexeme]
close = close' Nothing

closeKw' :: Maybe String -> [String] -> P String -> P [Token Lexeme]
closeKw' reopenBlockname open closeP = close' reopenBlockname open (separated wordySep closeP)

close' :: Maybe String -> [String] -> P String -> P [Token Lexeme]
close' reopenBlockname open closeP = do
(pos1, close, pos2) <- positioned $ closeP
env <- S.get
case findClose open (layout env) of
Nothing -> err pos1 (CloseWithoutMatchingOpen msgOpen (quote close))
where
msgOpen = intercalate " or " (quote <$> open)
quote s = "'" <> s <> "'"
Just (_, n) -> do
S.put (env {layout = drop n (layout env), opening = reopenBlockname})
let opens = maybe [] (const $ [Token (Open close) pos1 pos2]) reopenBlockname
pure $ replicate n (Token Close pos1 pos2) ++ opens

findClose :: [String] -> Layout -> Maybe (String, Int)
findClose _ [] = Nothing
findClose s ((h, _) : tl) = if h `elem` s then Just (h, 1) else fmap (1 +) <$> findClose s tl

simpleWordyId :: String -> Lexeme
simpleWordyId = flip WordyId Nothing
Expand Down