Skip to content

Commit

Permalink
Make using lifted more uniform across scala versions
Browse files Browse the repository at this point in the history
Specifically, make both versions of LiftFunction and LiftedInterpolator have
the same type parameters so cross-compiled dependencies can share sources
when passing around values of those types
  • Loading branch information
rayrobdod committed Nov 18, 2024
1 parent d3db69f commit 4d03782
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
10 changes: 5 additions & 5 deletions Base/src/main/scala-2/VersionSpecificInterpolator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ trait VersionSpecificInterpolatorModule {
* Create a Interpolators that can parse Exprs belonging to the specified Context
* @group InterpolatorGroup
*/
def contextInterpolators(c:Context):Interpolator.Interpolators[c.Expr, c.universe.Liftable, c.TypeTag] with LiftedInterpolator[c.type] = {
def contextInterpolators(c:Context):Interpolator.Interpolators[c.Expr, c.universe.Liftable, c.TypeTag] with LiftedInterpolator[c.Expr, c.TypeTag] = {
new Interpolator.Interpolators[c.Expr, c.universe.Liftable, c.TypeTag]
with ExprIndependentInterpolators[c.Expr[Any]]
with LiftedInterpolator[c.type] {
with LiftedInterpolator[c.Expr, c.TypeTag] {
override def `lazy`[A](fn:Function0[Interpolator[A]]):Interpolator[A] =
new Interpolator[A](internal.DelayedConstruction.interpolator(fn))

override def ofType[A](implicit tpe: c.TypeTag[A]): Interpolator[c.Expr[A]] =
new Interpolator[c.Expr[A]](new internal.OfType[c.type, A](tpe))

override def lifted[Lifter[_], Z](lift:LiftFunction[c.type, Lifter, Z], description:String)(implicit lifterTypeTag:c.TypeTag[Lifter[_]]):Interpolator[Z] =
override def lifted[Lifter[_], Z](lift:LiftFunction[c.Expr, c.TypeTag, Lifter, Z], description:String)(implicit lifterTypeTag:c.TypeTag[Lifter[_]]):Interpolator[Z] =
new Interpolator[Z](internal.Lifted(c)(lift, ExpectingDescription(description)))
}
}
Expand All @@ -102,13 +102,13 @@ trait VersionSpecificInterpolatorModule {
*
* @group InterpolatorGroup
*/
trait LiftedInterpolator[C <: Context with Singleton] {
trait LiftedInterpolator[Expr[+_], Type[_]] {
/**
* A parser that succeeds if the next part of the in put is an `arg` and Lifter parameterized on `arg`'s type can be implicitly summoned
*
* The implicitly summoned value and the `arg` value are passed to `lift`; the returned value is returned by this parser
* @group Arg
*/
def lifted[Lifter[_], Z](lift:LiftFunction[C, Lifter, Z], description:String)(implicit lifterTypeTag:C#TypeTag[Lifter[_]]):SCInterpolator[C#Expr[Any], Z]
def lifted[Lifter[_], Z](lift:LiftFunction[Expr, Type, Lifter, Z], description:String)(implicit lifterTypeTag:Type[Lifter[_]]):SCInterpolator[Expr[Any], Z]
}
}
2 changes: 1 addition & 1 deletion Base/src/main/scala-2/internal/Lifted.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private[stringContextParserCombinator]
object Lifted {
def apply[Lifter[_], Z](
c:Context)(
lift:LiftFunction[c.type, Lifter, Z],
lift:LiftFunction[c.Expr, c.TypeTag, Lifter, Z],
description:ExpectingDescription
)(implicit lifterTypeTag:c.TypeTag[Lifter[_]]
):Interpolator[c.Expr[_], Z] = {
Expand Down
4 changes: 3 additions & 1 deletion Base/src/main/scala-2/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ package object stringContextParserCombinator {

package stringContextParserCombinator {
/** Support for Interpolator.contextInterpolators.lifted; represents a macro-level function that combines a CC[A] and an A. */
trait LiftFunction[U <: Context with Singleton, -CC[_], +Z] {def apply[A](lifter:U#Expr[CC[A]], elem:U#Expr[A]):Z}
trait LiftFunction[Expr[+_], Type[_], -CC[_], +Z] {
def apply[A](lifter:Expr[CC[A]], elem:Expr[A]):Z
}
}
13 changes: 7 additions & 6 deletions Base/src/main/scala-3/VersionSpecificInterpolator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,26 @@ trait VersionSpecificInterpolatorModule extends ExprIndependentInterpolators[Any
* The implicitly summoned value and the `arg` value are passed to `lift`; the returned value is returned by this parser
* @group Arg
*/
def lifted[Lifter[_], Z](lift:LiftFunction[Lifter, Z], description:String)(using Quotes, Type[Lifter]):SCInterpolator[Expr[Any], Z] =
def lifted[Lifter[_], Z](lift:LiftFunction[Expr, Type, Lifter, Z], description:String)(using Quotes, Type[Lifter]):SCInterpolator[Expr[Any], Z] =
new SCInterpolator(internal.Lifted(lift, ExpectingDescription(description)))


/**
* Create an Interpolators that can parse `quoted.Expr`s
* @group InterpolatorGroup
*/
def quotedInterpolators(using Quotes):Interpolator.Interpolators[quoted.Expr, quoted.ToExpr, quoted.Type] & LiftedInterpolator = {
def quotedInterpolators(using Quotes):Interpolator.Interpolators[quoted.Expr, quoted.ToExpr, quoted.Type] & LiftedInterpolator[quoted.Expr, quoted.Type] = {
new Interpolator.Interpolators[quoted.Expr, quoted.ToExpr, quoted.Type]
with ExprIndependentInterpolators[quoted.Expr[Any]]
with LiftedInterpolator {
with LiftedInterpolator[quoted.Expr, quoted.Type]
{
override def `lazy`[A](fn:Function0[SCInterpolator[quoted.Expr[Any], A]]):SCInterpolator[quoted.Expr[Any], A] =
new SCInterpolator(internal.DelayedConstruction.interpolator(fn))

override def ofType[A](implicit tpe: Type[A]): SCInterpolator[Expr[Any], Expr[A]] =
new SCInterpolator(new internal.OfType[A])

override def lifted[Lifter[_], Z](lift:LiftFunction[Lifter, Z], description:String)(using quoted.Type[Lifter]):SCInterpolator[Expr[Any], Z] =
override def lifted[Lifter[_], Z](lift:LiftFunction[quoted.Expr, quoted.Type, Lifter, Z], description:String)(using quoted.Type[Lifter]):SCInterpolator[Expr[Any], Z] =
new SCInterpolator(internal.Lifted(lift, ExpectingDescription(description)))
}
}
Expand All @@ -95,13 +96,13 @@ trait VersionSpecificInterpolatorModule extends ExprIndependentInterpolators[Any
*
* @group InterpolatorGroup
*/
trait LiftedInterpolator {
trait LiftedInterpolator[Expr[+_], Type[_ <: AnyKind]] {
/**
* A parser that succeeds if the next part of the in put is an `arg` and Lifter parameterized on `arg`'s type can be implicitly summoned
*
* The implicitly summoned value and the `arg` value are passed to `lift`; the returned value is returned by this parser
* @group Arg
*/
def lifted[Lifter[_], Z](lift:LiftFunction[Lifter, Z], description:String)(using quoted.Type[Lifter]):SCInterpolator[Expr[Any], Z]
def lifted[Lifter[_], Z](lift:LiftFunction[Expr, Type, Lifter, Z], description:String)(using Type[Lifter]):SCInterpolator[Expr[Any], Z]
}
}
4 changes: 2 additions & 2 deletions Base/src/main/scala-3/internal/Lifted.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ object Lifted {
def transpose:Option[MyTuple[A, Lifter]] = lifter.map(x => MyTuple(value, x))
}
private final class MyTuple[A : Type, Lifter[_]](value:Expr[A], lifter:Expr[Lifter[A]]) {
def liftApply[Z](lift:LiftFunction[Lifter, Z])(using Quotes):Z = lift.apply[A](lifter, value)
def liftApply[Z](lift:LiftFunction[Expr, Type, Lifter, Z])(using Quotes):Z = lift.apply[A](lifter, value)
}

def apply[Lifter[_], Z](
lift:LiftFunction[Lifter, Z],
lift:LiftFunction[Expr, Type, Lifter, Z],
description:ExpectingDescription,
)(using
Type[Lifter],
Expand Down
6 changes: 3 additions & 3 deletions Base/src/main/scala-3/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package name.rayrobdod

import scala.language.higherKinds
import scala.quoted.Expr
import scala.quoted.Quotes
import scala.quoted.Type

/**
* A library for implementing custom string interpolation implementations using Parser Combinators
Expand All @@ -28,7 +26,9 @@ package object stringContextParserCombinator {

package stringContextParserCombinator {
/** Support for [[Interpolator.lifted]]; represents a macro-level function that combines a CC[A] and an A. */
trait LiftFunction[-CC[_], +Z] {def apply[A](lifter:Expr[CC[A]], elem:Expr[A])(using Type[A], Quotes):Z}
trait LiftFunction[Expr[+_], Type[_], -CC[_], +Z] {
def apply[A](lifter:Expr[CC[A]], elem:Expr[A])(using Type[A], Quotes):Z
}
/** An identity context - for parsing outside of a macro */
type Id[+A] = A
/** An identity function for lifting into the identity context */
Expand Down
4 changes: 2 additions & 2 deletions JsonParser/src/main/scala-2/MacroImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.json4s._
import name.rayrobdod.stringContextParserCombinator._

final class MacroImpl(val c:Context {type PrefixType = JsonStringContext}) {
private[this] def myLiftFunction[Z, Lifter[A] <: Lift[A, Z]]:LiftFunction[c.type, Lifter, c.Expr[Z]] = {
new LiftFunction[c.type, Lifter, c.Expr[Z]] {
private[this] def myLiftFunction[Z, Lifter[A] <: Lift[A, Z]]:LiftFunction[c.Expr, c.TypeTag, Lifter, c.Expr[Z]] = {
new LiftFunction[c.Expr, c.TypeTag, Lifter, c.Expr[Z]] {
def apply[A](lifter:c.Expr[Lifter[A]], a:c.Expr[A]):c.Expr[Z] = {
c.Expr(
c.universe.Apply(
Expand Down
6 changes: 3 additions & 3 deletions JsonParser/src/main/scala-3/MacroImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import name.rayrobdod.stringContextParserCombinator._

object MacroImpl {
import scala.language.higherKinds
private def myLiftFunction[Z : Type, Lifter[A] <: Lift[A, Z] : Type]:LiftFunction[Lifter, Expr[Z]] = {
new LiftFunction[Lifter, Expr[Z]] {
private def myLiftFunction[Z : Type, Lifter[A] <: Lift[A, Z] : Type]:LiftFunction[Expr, Type, Lifter, Expr[Z]] = {
new LiftFunction[Expr, Type, Lifter, Expr[Z]] {
def apply[A](lifter:Expr[Lifter[A]], a:Expr[A])(using Type[A], Quotes):Expr[Z] = lifter match {
// Lifter being a Lift.jvalue implies that A =:= Z
case '{ Lift.jvalue } => a.asExprOf[Z]
Expand All @@ -27,7 +27,7 @@ object MacroImpl {
* (i.e. `Lift.string.apply("abcd").values`).
* This will bypass that wrapping, at least for the built-in `Lift.string`.
*/
private object stringLiftFunction extends LiftFunction[Lift.String, Expr[String]] {
private object stringLiftFunction extends LiftFunction[Expr, Type, Lift.String, Expr[String]] {
def apply[A](lifter:Expr[Lift.String[A]], a:Expr[A])(using Type[A], Quotes):Expr[String] = lifter match {
// Lifter being a Lift.jvalue implies that A =:= Z, and for Lift.String, Z =:= JString
case '{ Lift.jvalue } => '{ ${a.asExprOf[JString]}.values }
Expand Down

0 comments on commit 4d03782

Please sign in to comment.