-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add SplicePattern AST to parse and type quote pattern splices #17396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -737,6 +737,22 @@ object Trees { | |
type ThisTree[+T <: Untyped] = Splice[T] | ||
} | ||
|
||
/** A tree representing a pattern splice `${ pattern }`, `$ident` or `$ident(args*)` in a quote pattern. | ||
* | ||
* Parser will only create `${ pattern }` and `$ident`, hence they will not have args. | ||
* While typing, the `$ident(args*)` the args are identified and desugared into a `SplicePattern` | ||
* containing them. | ||
* | ||
* SplicePattern are removed after typing the pattern and are not present in TASTy. | ||
* | ||
* @param body The tree that was spliced | ||
* @param args The arguments of the splice (the HOAS arguments) | ||
*/ | ||
case class SplicePattern[+T <: Untyped] private[ast] (body: Tree[T], args: List[Tree[T]])(implicit @constructorOnly src: SourceFile) | ||
extends TermTree[T] { | ||
type ThisTree[+T <: Untyped] = SplicePattern[T] | ||
} | ||
|
||
/** A type tree that represents an existing or inferred type */ | ||
case class TypeTree[+T <: Untyped]()(implicit @constructorOnly src: SourceFile) | ||
extends DenotingTree[T] with TypTree[T] { | ||
|
@@ -1147,6 +1163,7 @@ object Trees { | |
type Inlined = Trees.Inlined[T] | ||
type Quote = Trees.Quote[T] | ||
type Splice = Trees.Splice[T] | ||
type SplicePattern = Trees.SplicePattern[T] | ||
type TypeTree = Trees.TypeTree[T] | ||
type InferredTypeTree = Trees.InferredTypeTree[T] | ||
type SingletonTypeTree = Trees.SingletonTypeTree[T] | ||
|
@@ -1325,6 +1342,10 @@ object Trees { | |
case tree: Splice if (expr eq tree.expr) => tree | ||
case _ => finalize(tree, untpd.Splice(expr)(sourceFile(tree))) | ||
} | ||
def SplicePattern(tree: Tree)(body: Tree, args: List[Tree])(using Context): SplicePattern = tree match { | ||
case tree: SplicePattern if (body eq tree.body) && (args eq tree.args) => tree | ||
case _ => finalize(tree, untpd.SplicePattern(body, args)(sourceFile(tree))) | ||
} | ||
def SingletonTypeTree(tree: Tree)(ref: Tree)(using Context): SingletonTypeTree = tree match { | ||
case tree: SingletonTypeTree if (ref eq tree.ref) => tree | ||
case _ => finalize(tree, untpd.SingletonTypeTree(ref)(sourceFile(tree))) | ||
|
@@ -1566,6 +1587,8 @@ object Trees { | |
cpy.Quote(tree)(transform(body)(using quoteContext), transform(tags)) | ||
case tree @ Splice(expr) => | ||
cpy.Splice(tree)(transform(expr)(using spliceContext)) | ||
case tree @ SplicePattern(body, args) => | ||
cpy.SplicePattern(tree)(transform(body)(using spliceContext), transform(args)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't spliceContext used for the arguments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguments of the case '{ (x: T) => $f(x) } => In this example |
||
case tree @ Hole(isTerm, idx, args, content) => | ||
cpy.Hole(tree)(isTerm, idx, transform(args), transform(content)) | ||
case _ => | ||
|
@@ -1711,6 +1734,8 @@ object Trees { | |
this(this(x, body)(using quoteContext), tags) | ||
case Splice(expr) => | ||
this(x, expr)(using spliceContext) | ||
case SplicePattern(body, args) => | ||
this(this(x, body)(using spliceContext), args) | ||
case Hole(_, _, args, content) => | ||
this(this(x, args), content) | ||
case _ => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not have the parser take care of this since it's based purely on syntactic information?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the next PR I will introduce
QuotePattern
. At that pointSplicePattern
s will be kept after parser and until we encode the pattern intounapply
and'{..}
s.