Я пытаюсь реализовать собственный метод интерполяции строк с помощью макроса, и мне нужно руководствоваться использованием API.
Вот что я хочу сделать:
/** 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
выше и подтвердить результат, чтобы получить результат вызова макроса?