Skip to content

Commit 589ab48

Browse files
authored
Support type applications (#110)
* Support type applications * Update * Update to released parser version
1 parent e76de5a commit 589ab48

File tree

4 files changed

+123
-9
lines changed

4 files changed

+123
-9
lines changed

packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ in upstream
3939
}
4040

4141
with language-cst-parser =
42-
(upstream.language-cst-parser with version = "v0.12.2")
42+
(upstream.language-cst-parser with version = "v0.13.0")

src/Tidy.purs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Data.Tuple (Tuple(..), fst, snd)
3333
import Dodo as Dodo
3434
import Partial.Unsafe (unsafeCrashWith)
3535
import PureScript.CST.Errors (RecoveredError(..))
36-
import PureScript.CST.Types (Binder(..), ClassFundep(..), ClassHead, Comment(..), DataCtor(..), DataHead, DataMembers(..), Declaration(..), Delimited, DelimitedNonEmpty, DoStatement(..), Export(..), Expr(..), FixityOp(..), Foreign(..), Guarded(..), GuardedExpr(..), Ident, IfThenElse, Import(..), ImportDecl(..), Instance(..), InstanceBinding(..), InstanceHead, Label, Labeled(..), LetBinding(..), LineFeed, Module(..), ModuleBody(..), ModuleHeader(..), ModuleName, Name(..), OneOrDelimited(..), Operator, PatternGuard(..), Proper, QualifiedName(..), RecordLabeled(..), RecordUpdate(..), Row(..), Separated(..), SourceStyle(..), SourceToken, Token(..), Type(..), TypeVarBinding(..), ValueBindingFields, Where(..), Wrapped(..))
36+
import PureScript.CST.Types (AppSpine(..), Binder(..), ClassFundep(..), ClassHead, Comment(..), DataCtor(..), DataHead, DataMembers(..), Declaration(..), Delimited, DelimitedNonEmpty, DoStatement(..), Export(..), Expr(..), FixityOp(..), Foreign(..), Guarded(..), GuardedExpr(..), Ident, IfThenElse, Import(..), ImportDecl(..), Instance(..), InstanceBinding(..), InstanceHead, Label, Labeled(..), LetBinding(..), LineFeed, Module(..), ModuleBody(..), ModuleHeader(..), ModuleName, Name(..), OneOrDelimited(..), Operator, PatternGuard(..), Prefixed(..), Proper, QualifiedName(..), RecordLabeled(..), RecordUpdate(..), Row(..), Separated(..), SourceStyle(..), SourceToken, Token(..), Type(..), TypeVarBinding(..), ValueBindingFields, Where(..), Wrapped(..))
3737
import Tidy.Doc (FormatDoc, align, alignCurrentColumn, anchor, break, flexDoubleBreak, flexGroup, flexSoftBreak, flexSpaceBreak, forceMinSourceBreaks, fromDoc, indent, joinWith, joinWithMap, leadingBlockComment, leadingLineComment, locally, softBreak, softSpace, sourceBreak, space, spaceBreak, text, trailingBlockComment, trailingLineComment)
3838
import Tidy.Doc (FormatDoc, toDoc) as Exports
3939
import Tidy.Doc as Doc
@@ -193,6 +193,10 @@ formatString = splitStringEscapeLines >>> Array.uncons >>> foldMap \{ head, tail
193193
formatName :: forall e a n. Format (Name n) e a
194194
formatName conf (Name { token }) = formatToken conf token
195195

196+
formatPrefixedName :: forall e a n. Format (Prefixed (Name n)) e a
197+
formatPrefixedName conf (Prefixed { prefix, value: Name { token } }) =
198+
foldMap (formatToken conf) prefix <> formatToken conf token
199+
196200
formatQualifiedName :: forall e a n. Format (QualifiedName n) e a
197201
formatQualifiedName conf (QualifiedName { token }) = formatToken conf token
198202

@@ -514,7 +518,7 @@ formatDataHead :: forall e a. Format (DataHead e) e a
514518
formatDataHead conf { keyword, name, vars } =
515519
formatToken conf keyword `space` indent do
516520
anchor (formatName conf name)
517-
`flexSpaceBreak` joinWithMap spaceBreak (formatTypeVarBinding conf) vars
521+
`flexSpaceBreak` joinWithMap spaceBreak (formatTypeVarBindingPlain conf) vars
518522

519523
formatDataCtor :: forall e a. Format (DataCtor e) e a
520524
formatDataCtor conf = Hang.toFormatDoc <<< formatHangingDataCtor conf
@@ -536,7 +540,7 @@ formatClassHead conf (Tuple cls wh) =
536540
flexGroup do
537541
formatName conf cls.name
538542
`spaceBreak`
539-
joinWithMap spaceBreak (indent <<< formatTypeVarBinding conf) cls.vars
543+
joinWithMap spaceBreak (indent <<< formatTypeVarBindingPlain conf) cls.vars
540544
`spaceBreak`
541545
flexGroup do
542546
anchor (foldMap formatFundeps cls.fundeps)
@@ -618,15 +622,28 @@ formatInstanceBinding conf = case _ of
618622
InstanceBindingName vbf ->
619623
formatValueBinding conf vbf
620624

621-
formatTypeVarBinding :: forall e a. Format (TypeVarBinding e) e a
625+
formatTypeVarBinding :: forall e a. Format (TypeVarBinding (Prefixed (Name Ident)) e) e a
622626
formatTypeVarBinding conf = case _ of
623627
TypeVarKinded w ->
624628
formatParensBlock formatKindedTypeVarBinding conf w
625629
TypeVarName n ->
626-
formatName conf n
630+
formatPrefixedName conf n
627631

628-
formatKindedTypeVarBinding :: forall e a. Format (Labeled (Name Ident) (Type e)) e a
632+
formatKindedTypeVarBinding :: forall e a. Format (Labeled (Prefixed (Name Ident)) (Type e)) e a
629633
formatKindedTypeVarBinding conf (Labeled { label, separator, value }) =
634+
formatPrefixedName conf label `space` indent do
635+
anchor (formatToken conf separator)
636+
`flexSpaceBreak` formatType conf value
637+
638+
formatTypeVarBindingPlain :: forall e a. Format (TypeVarBinding (Name Ident) e) e a
639+
formatTypeVarBindingPlain conf = case _ of
640+
TypeVarKinded w ->
641+
formatParensBlock formatKindedTypeVarBindingPlain conf w
642+
TypeVarName n ->
643+
formatName conf n
644+
645+
formatKindedTypeVarBindingPlain :: forall e a. Format (Labeled (Name Ident) (Type e)) e a
646+
formatKindedTypeVarBindingPlain conf (Labeled { label, separator, value }) =
630647
formatName conf label `space` indent do
631648
anchor (formatToken conf separator)
632649
`flexSpaceBreak` formatType conf value
@@ -720,7 +737,7 @@ formatHangingType :: forall e a. FormatHanging (Type e) e a
720737
formatHangingType conf = formatHangingPolytype identity conf <<< toPolytype
721738

722739
data Poly e
723-
= PolyForall SourceToken (NonEmptyArray (TypeVarBinding e)) SourceToken
740+
= PolyForall SourceToken (NonEmptyArray (TypeVarBinding (Prefixed (Name Ident)) e)) SourceToken
724741
| PolyArrow (Type e) SourceToken
725742

726743
type Polytype e =
@@ -887,7 +904,7 @@ formatHangingExpr conf = case _ of
887904
ExprApp expr exprs ->
888905
hangApp
889906
(formatHangingExpr conf expr)
890-
(map (formatHangingExpr conf) exprs)
907+
(map (formatHangingExprAppSpine conf) exprs)
891908

892909
ExprLambda lmb ->
893910
hang
@@ -947,6 +964,13 @@ formatHangingExpr conf = case _ of
947964
ExprError e ->
948965
hangBreak $ conf.formatError e
949966

967+
formatHangingExprAppSpine :: forall e a. FormatHanging (AppSpine Expr e) e a
968+
formatHangingExprAppSpine conf = case _ of
969+
AppType tok ty ->
970+
hangBreak $ formatToken conf tok <> formatType conf ty
971+
AppTerm expr ->
972+
formatHangingExpr conf expr
973+
950974
data ElseIfChain e
951975
= IfThen SourceToken (Expr e) SourceToken (Expr e)
952976
| ElseIfThen SourceToken SourceToken (Expr e) SourceToken (Expr e)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module VisibleTypeApplications where
2+
3+
test :: forall @a. a -> a
4+
5+
test
6+
:: forall
7+
@
8+
-- wat
9+
a
10+
. a
11+
-> a
12+
13+
test :: forall @ {- wat -}a. a -> a
14+
15+
test :: forall (@a :: Type). a -> a
16+
17+
test
18+
:: forall
19+
( @
20+
-- wat
21+
a :: Type
22+
)
23+
. a
24+
-> a
25+
26+
test :: forall (@ {- wat -}a :: Type). a -> a
27+
28+
test = foo @Bar 42
29+
30+
test = foo @Bar
31+
42
32+
33+
test = foo
34+
@Bar
35+
42
36+
37+
test = foo
38+
@( Bar
39+
{ baz :: Int
40+
, quux :: String
41+
}
42+
)
43+
42
44+
45+
test = foo @Bar 42
46+
@( Baz
47+
{ baz :: Int
48+
, quux :: String
49+
}
50+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module VisibleTypeApplications where
2+
3+
test :: forall @a. a -> a
4+
5+
test :: forall @
6+
-- wat
7+
a. a -> a
8+
9+
test :: forall @{- wat -}a. a -> a
10+
11+
test :: forall (@a :: Type). a -> a
12+
13+
test :: forall (@
14+
-- wat
15+
a :: Type). a -> a
16+
17+
test :: forall (@{- wat -}a :: Type). a -> a
18+
19+
test = foo @Bar 42
20+
21+
test = foo @Bar
22+
42
23+
24+
test = foo
25+
@Bar
26+
42
27+
28+
test = foo @(
29+
Bar
30+
{ baz :: Int
31+
, quux :: String
32+
}
33+
) 42
34+
35+
test = foo @Bar 42 @(
36+
Baz
37+
{ baz :: Int
38+
, quux :: String
39+
}
40+
)

0 commit comments

Comments
 (0)