Skip to content

Commit

Permalink
Merge pull request #4689 from unisonweb/cp/fix-lexer-parens
Browse files Browse the repository at this point in the history
Add parens to annotation of lexed symboly segments
  • Loading branch information
aryairani authored Feb 12, 2024
2 parents 6a6b7b8 + 72727af commit 36fdcd8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions unison-src/transcripts/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ x y =
x + y
-- Should keep comments after
-- symbolyDefinition
(<|>) : Nat -> Nat -> (Nat, Nat)
(<|>) a b = (a, b)
symbolyEndOfBlock =
x = 1
(+:)
-- Test for a previous regression that added extra brackets.
oneLiner = {{ one liner }}
-- After
Expand Down
18 changes: 18 additions & 0 deletions unison-src/transcripts/formatter.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ x y =
x + y
-- Should keep comments after
-- symbolyDefinition
(<|>) : Nat -> Nat -> (Nat, Nat)
(<|>) a b = (a, b)
symbolyEndOfBlock =
x = 1
(+:)
-- Test for a previous regression that added extra brackets.
oneLiner = {{ one liner }}
-- After
Expand Down Expand Up @@ -94,6 +103,15 @@ x y =
x + y
-- Should keep comments after
-- symbolyDefinition
(<|>) : Nat -> Nat -> (Nat, Nat)
a <|> b = (a, b)
symbolyEndOfBlock =
x = 1
(+:)
-- Test for a previous regression that added extra brackets.
oneLiner = {{ one liner }}
-- After
Expand Down
12 changes: 10 additions & 2 deletions unison-syntax/src/Unison/Syntax/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,16 @@ symbolyDefinitionName = queryToken $ \case
L.SymbolyId n -> Just $ Name.toVar (HQ'.toName n)
_ -> Nothing

parenthesize :: (Ord v) => P v m a -> P v m a
parenthesize p = P.try (openBlockWith "(" *> p) <* closeBlock
-- | Expect parentheses around a token, includes the parentheses within the start/end
-- annotations of the resulting token.
parenthesize :: (Ord v) => P v m (L.Token a) -> P v m (L.Token a)
parenthesize p = do
(start, a) <- P.try do
start <- L.start <$> openBlockWith "("
a <- p
pure (start, a)
end <- L.end <$> closeBlock
pure (L.Token {payload = L.payload a, start, end})

hqPrefixId, hqInfixId :: (Ord v) => P v m (L.Token (HQ.HashQualified Name))
hqPrefixId = hqWordyId_ <|> parenthesize hqSymbolyId_
Expand Down

0 comments on commit 36fdcd8

Please sign in to comment.