Scala 字符串插值和宏:如何获取StringContext和表达式位置
我正在尝试用宏实现一个自定义字符串插值方法,我需要一些关于使用API的指导 以下是我想做的:Scala 字符串插值和宏:如何获取StringContext和表达式位置,scala,macros,scala-2.10,string-interpolation,Scala,Macros,Scala 2.10,String Interpolation,我正在尝试用宏实现一个自定义字符串插值方法,我需要一些关于使用API的指导 以下是我想做的: /** expected * LocatedPieces(List(("\nHello ", Place("world"), Position()), ("\nHow are you, ", Name("Eric"), Position(...))) */ val locatedPieces: LocatedPieces = s2"""
/** expected
* LocatedPieces(List(("\nHello ", Place("world"), Position()),
("\nHow are you, ", Name("Eric"), Position(...)))
*/
val locatedPieces: LocatedPieces =
s2"""
Hello $place
How are you, $name
"""
val place: Piece = Place("world")
val name: Piece = Name("Eric")
trait Piece
case class Place(p: String) extends Piece
case class Name(n: String) extends Piece
/** sequence of each interpolated Piece object with:
* the preceding text and its location
*/
case class LocatedPieces(located: Seq[(String, Piece, Position)])
implicit class s2pieces(sc: StringContext) {
def s2(parts: Piece*) = macro s2Impl
}
def impl(c: Context)(pieces: c.Expr[Piece]*): c.Expr[LocatedPieces] = {
// I want to build a LocatedPieces object with the positions for all
// the pieces + the pieces + the (sc: StringContext).parts
// with the method createLocatedPieces below
// ???
}
def createLocatedPieces(parts: Seq[String], pieces: Seq[Piece], positions: Seq[Position]):
LocatedPieces =
// zip the text parts, pieces and positions together to create a LocatedPieces object
???
我的问题是:
StringContext
对象以获取所有StringContext.parts
字符串createLocatedPieces
方法并具体化结果以获得宏调用的结果经过几个小时的努力,我找到了一个可行的解决方案:
object Macros {
import scala.reflect.macros.Context
import language.experimental.macros
sealed trait Piece
case class Place(str: String) extends Piece
case class Name(str: String) extends Piece
case class Pos(column: Int, line: Int)
case class LocatedPieces(located: List[(String, Piece, Pos)])
implicit class s2pieces(sc: StringContext) {
def s2(pieces: Piece*) = macro s2impl
}
// pieces contain all the Piece instances passed inside of the string interpolation
def s2impl(c: Context)(pieces: c.Expr[Piece]*): c.Expr[LocatedPieces] = {
import c.universe.{ Name => _, _ }
c.prefix.tree match {
// access data of string interpolation
case Apply(_, List(Apply(_, rawParts))) =>
// helper methods
def typeIdent[A : TypeTag] =
Ident(typeTag[A].tpe.typeSymbol)
def companionIdent[A : TypeTag] =
Ident(typeTag[A].tpe.typeSymbol.companionSymbol)
def identFromString(tpt: String) =
Ident(c.mirror.staticModule(tpt))
// We need to translate the data calculated inside of the macro to an AST
// in order to write it back to the compiler.
def toAST(any: Any) =
Literal(Constant(any))
def toPosAST(column: Tree, line: Tree) =
Apply(
Select(companionIdent[Pos], newTermName("apply")),
List(column, line))
def toTupleAST(t1: Tree, t2: Tree, t3: Tree) =
Apply(
TypeApply(
Select(identFromString("scala.Tuple3"), newTermName("apply")),
List(typeIdent[String], typeIdent[Piece], typeIdent[Pos])),
List(t1, t2, t3))
def toLocatedPiecesAST(located: Tree) =
Apply(
Select(companionIdent[LocatedPieces], newTermName("apply")),
List(located))
def toListAST(xs: List[Tree]) =
Apply(
TypeApply(
Select(identFromString("scala.collection.immutable.List"), newTermName("apply")),
List(AppliedTypeTree(
typeIdent[Tuple3[String, Piece, Pos]],
List(typeIdent[String], typeIdent[Piece], typeIdent[Pos])))),
xs)
// `parts` contain the strings a string interpolation is built of
val parts = rawParts map { case Literal(Constant(const: String)) => const }
// translate compiler positions to a data structure that can live outside of the compiler
val positions = pieces.toList map (_.tree.pos) map (p => Pos(p.column, p.line))
// discard last element of parts, `transpose` does not work otherwise
// trim parts to discard unnecessary white space
val data = List(parts.init map (_.trim), pieces.toList, positions).transpose
// create an AST containing a List[(String, Piece, Pos)]
val tupleAST = data map { case List(part: String, piece: c.Expr[_], Pos(column, line)) =>
toTupleAST(toAST(part), piece.tree, toPosAST(toAST(column), toAST(line)))
}
// create an AST of `LocatedPieces`
val locatedPiecesAST = toLocatedPiecesAST(toListAST(tupleAST))
c.Expr(locatedPiecesAST)
case _ =>
c.abort(c.enclosingPosition, "invalid")
}
}
}
用法:
object StringContextTest {
val place: Piece = Place("world")
val name: Piece = Name("Eric")
val pieces = s2"""
Hello $place
How are you, $name?
"""
pieces.located foreach println
}
结果:
(Hello,Place(world),Pos(12,9))
(How are you,,Name(Eric),Pos(19,10))
我没想到要花这么多时间才能把所有的东西都整理好,但这是一段很有趣的时光。我希望代码符合您的要求。如果您需要更多关于具体工作方式的信息,请查看其他问题及其答案:
object Macros {
import scala.reflect.macros.Context
import language.experimental.macros
sealed trait Piece
case class Place(str: String) extends Piece
case class Name(str: String) extends Piece
case class Pos(column: Int, line: Int)
case class LocatedPieces(located: Seq[(String, Piece, Pos)])
implicit class s2pieces(sc: StringContext) {
def s2(pieces: Piece*) = macro s2impl
}
def s2impl(c: Context)(pieces: c.Expr[Piece]*): c.Expr[LocatedPieces] = {
import c.universe.{ Name => _, _ }
def toAST[A : TypeTag](xs: Tree*): Tree =
Apply(
Select(Ident(typeOf[A].typeSymbol.companionSymbol), newTermName("apply")),
xs.toList)
val parts = c.prefix.tree match {
case Apply(_, List(Apply(_, rawParts))) =>
rawParts zip (pieces map (_.tree)) map {
case (Literal(Constant(rawPart: String)), piece) =>
val line = c.literal(piece.pos.line).tree
val column = c.literal(piece.pos.column).tree
val part = c.literal(rawPart.trim).tree
toAST[(_, _, _)](part, piece, toAST[Pos](line, column))
}
}
c.Expr(toAST[LocatedPieces](toAST[Seq[_]](parts: _*)))
}
}
它对冗长的AST结构进行了抽象,其逻辑略有不同,但几乎相同。如果您在理解代码如何工作方面有困难,请首先尝试理解第一种解决方案。它的功能更加明确。我已经尝试了API的各个部分,但还没有组装完整的解决方案。任何建议或一般指导都会有所帮助。一个完整的解决方案将得到我永远的感激:-)我不确定它是否有答案,但你的问题让我想起了这篇文章:我已经读过了,我的用例做得稍微多一些。但是我认识托尼,如果在此期间我没有得到答案,我会请他在本周的下一次Scalasy会议上帮助我:-)。嗨,你能举个例子,也许不止一件吗?输入是什么,输出应该是什么样子。我只能猜测,可能是错的。我的建议-看看github上的一些。我更新了示例,我将看看github项目,这是一个很好的建议。+1,我不是想抢你的风头,但这是可能的。我今天早上开始了这个实现,但没有发布它,因为(像您一样)它没有返回完整的
位置。感谢您的辛勤工作!我将根据我的具体用例调整您的解决方案,但看起来我这里有关于如何提取原始文本、位置和打包整个内容的所有必要信息。特拉维斯,我还要看看你的要点,乍一看,在某些位置上有一些有趣的下划线。@特拉维斯布朗:非常感谢,你的解决方案太棒了。我知道一定有办法把所有这些东西抽象出来,但我没有想出解决办法。将多个参数和元组看作一个参数列表是非常酷的。我的第二次尝试现在要短得多。@Eric:我已经把Travis的解决方案和我的结合起来了。结果是令人印象深刻的。请注意,这可能会使它更简洁,但也可以看到我的评论,为什么你可能不想这样做。