Skip to content

Commit f3f9fe4

Browse files
mbovelajafri2001
andcommitted
WIP: record application style
Co-authored-by: Abdullah Arif Jafri <[email protected]>
1 parent 0cd43fe commit f3f9fe4

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ object Trees {
513513
case Using // r.f(using x)
514514
case InfixTuple // r f (x1, ..., xN) where N != 1; needs to be treated specially for an error message in typedApply
515515

516+
enum ApplyStyle:
517+
case Parentheses // f(x)
518+
case TrailingLambda // f { x => ... }
519+
case Colon // f: x => ...
520+
case Unknown
521+
516522
/** fun(args) */
517523
case class Apply[+T <: Untyped] private[ast] (fun: Tree[T], args: List[Tree[T]])(implicit @constructorOnly src: SourceFile)
518524
extends GenericApply[T] {
@@ -527,6 +533,13 @@ object Trees {
527533
*/
528534
def applyKind: ApplyKind =
529535
attachmentOrElse(untpd.KindOfApply, ApplyKind.Regular)
536+
537+
def setApplyStyle(style: ApplyStyle) =
538+
putAttachment(untpd.StyleOfApply, style)
539+
this
540+
541+
def applyStyle: ApplyStyle =
542+
attachmentOrElse(untpd.StyleOfApply, ApplyStyle.Unknown)
530543
}
531544

532545
/** fun[args] */

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
386386
/** Property key for contextual Apply trees of the form `fn given arg` */
387387
val KindOfApply: Property.StickyKey[ApplyKind] = Property.StickyKey()
388388

389+
/** Property key for Apply, which is used to mark the kind of apply that is
390+
* being used: parentheses (`f(x)`), trailing lambda (`f {x => ...}`) , or
391+
* column `f: x => ...`.
392+
*/
393+
val StyleOfApply: Property.StickyKey[ApplyStyle] = Property.StickyKey()
394+
389395
// ------ Creation methods for untyped only -----------------
390396

391397
def Ident(name: Name)(implicit src: SourceFile): Ident = new Ident(name)

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,8 @@ object Parsers {
28052805
val tapp = atSpan(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true, wildOK = false)) }
28062806
simpleExprRest(tapp, location, canApply = true)
28072807
case LPAREN | LBRACE | INDENT if canApply =>
2808-
val app = atSpan(startOffset(t), in.offset) { mkApply(t, argumentExprs()) }
2808+
val openingToken = in.token
2809+
val app = atSpan(startOffset(t), in.offset) { mkApply(t, argumentExprs(), openingToken) }
28092810
if in.rewriteToIndent then
28102811
app match
28112812
case Apply(Apply(_, List(Block(_, _))), List(blk @ Block(_, _))) =>
@@ -2892,8 +2893,15 @@ object Parsers {
28922893
def argumentExprs(): (List[Tree], Boolean) =
28932894
if (in.isNestedStart) (blockExpr() :: Nil, false) else parArgumentExprs()
28942895

2895-
def mkApply(fn: Tree, args: (List[Tree], Boolean)): Tree =
2896+
def mkApply(fn: Tree, args: (List[Tree], Boolean), openingToken: Token): Tree =
28962897
val res = Apply(fn, args._1)
2898+
println(openingToken)
2899+
val applyStyle =
2900+
openingToken match
2901+
case LPAREN => ApplyStyle.Parentheses
2902+
case LBRACE => ApplyStyle.TrailingLambda
2903+
case COLONop => ApplyStyle.Colon
2904+
res.setApplyStyle(applyStyle)
28972905
if args._2 then res.setApplyKind(ApplyKind.Using)
28982906
res
28992907

@@ -2905,7 +2913,9 @@ object Parsers {
29052913
*/
29062914
def argumentExprss(fn: Tree): Tree = {
29072915
argumentStart()
2908-
if (in.token == LPAREN || in.isNestedStart) argumentExprss(mkApply(fn, argumentExprs()))
2916+
if in.token == LPAREN || in.isNestedStart then
2917+
val openingToken = in.token
2918+
argumentExprss(mkApply(fn, argumentExprs(), openingToken))
29092919
else fn
29102920
}
29112921

@@ -2931,7 +2941,7 @@ object Parsers {
29312941
}
29322942
if (in.token == LPAREN && (!inClassConstrAnnots || isLegalAnnotArg))
29332943
parArgumentExprss(
2934-
atSpan(startOffset(fn)) { mkApply(fn, parArgumentExprs()) }
2944+
atSpan(startOffset(fn)) { mkApply(fn, parArgumentExprs(), LPAREN) }
29352945
)
29362946
else fn
29372947
}
@@ -4046,7 +4056,7 @@ object Parsers {
40464056
def selfInvocation(): Tree =
40474057
atSpan(accept(THIS)) {
40484058
argumentStart()
4049-
argumentExprss(mkApply(Ident(nme.CONSTRUCTOR), argumentExprs()))
4059+
argumentExprss(mkApply(Ident(nme.CONSTRUCTOR), argumentExprs(), LPAREN))
40504060
}
40514061

40524062
/** TypeDef ::= id [HkTypeParamClause] {FunParamClause} TypeAndCtxBounds [‘=’ Type]

0 commit comments

Comments
 (0)