From b00acbe182f64e68f232826f7f8b5afb514a5eb6 Mon Sep 17 00:00:00 2001 From: Chris Penner Date: Mon, 12 Feb 2024 10:46:38 -0800 Subject: [PATCH 1/2] Add parens to annotation of lexed symboly segments --- unison-src/transcripts/formatter.md | 9 +++++++++ unison-src/transcripts/formatter.output.md | 16 ++++++++++++++++ unison-syntax/src/Unison/Syntax/Parser.hs | 12 ++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/unison-src/transcripts/formatter.md b/unison-src/transcripts/formatter.md index 951153a75a..d360a8bb84 100644 --- a/unison-src/transcripts/formatter.md +++ b/unison-src/transcripts/formatter.md @@ -19,6 +19,15 @@ x y = x + y -- Should keep comments after +-- symbolyDefinition +(<|>) : Nat -> Nat -> (Nat, Nat) +(<|>) a b = (a, b) + +symbolyEndOfBlock = + use List +: + (+:) + + -- Test for a previous regression that added extra brackets. oneLiner = {{ one liner }} -- After diff --git a/unison-src/transcripts/formatter.output.md b/unison-src/transcripts/formatter.output.md index 5238f92e3f..c2199ac3df 100644 --- a/unison-src/transcripts/formatter.output.md +++ b/unison-src/transcripts/formatter.output.md @@ -15,6 +15,15 @@ x y = x + y -- Should keep comments after +-- symbolyDefinition +(<|>) : Nat -> Nat -> (Nat, Nat) +(<|>) a b = (a, b) + +f x y = + use List +: + (+:) + + -- Test for a previous regression that added extra brackets. oneLiner = {{ one liner }} -- After @@ -94,6 +103,13 @@ x y = x + y -- Should keep comments after +-- symbolyDefinition +(<|>) : Nat -> Nat -> (Nat, Nat) +a <|> b = (a, b) + +f x y = (+:) + + -- Test for a previous regression that added extra brackets. oneLiner = {{ one liner }} -- After diff --git a/unison-syntax/src/Unison/Syntax/Parser.hs b/unison-syntax/src/Unison/Syntax/Parser.hs index be115a6a92..e8645f715b 100644 --- a/unison-syntax/src/Unison/Syntax/Parser.hs +++ b/unison-syntax/src/Unison/Syntax/Parser.hs @@ -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_ From 72727afc46c8e6a855cadf49eb7b3f8b6c60ea5c Mon Sep 17 00:00:00 2001 From: Chris Penner Date: Mon, 12 Feb 2024 10:50:41 -0800 Subject: [PATCH 2/2] Update end-of block symboly test --- unison-src/transcripts/formatter.md | 2 +- unison-src/transcripts/formatter.output.md | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/unison-src/transcripts/formatter.md b/unison-src/transcripts/formatter.md index d360a8bb84..3848c0ba99 100644 --- a/unison-src/transcripts/formatter.md +++ b/unison-src/transcripts/formatter.md @@ -24,7 +24,7 @@ x y = (<|>) a b = (a, b) symbolyEndOfBlock = - use List +: + x = 1 (+:) diff --git a/unison-src/transcripts/formatter.output.md b/unison-src/transcripts/formatter.output.md index c2199ac3df..95af2a545d 100644 --- a/unison-src/transcripts/formatter.output.md +++ b/unison-src/transcripts/formatter.output.md @@ -19,8 +19,8 @@ x y = (<|>) : Nat -> Nat -> (Nat, Nat) (<|>) a b = (a, b) -f x y = - use List +: +symbolyEndOfBlock = + x = 1 (+:) @@ -107,7 +107,9 @@ x y = (<|>) : Nat -> Nat -> (Nat, Nat) a <|> b = (a, b) -f x y = (+:) +symbolyEndOfBlock = + x = 1 + (+:) -- Test for a previous regression that added extra brackets.