Skip to content

begeric/TQL-scalameta

Repository files navigation

TQL (Traversal Query Language)

Combinatorial Query/Transformation language for Traversable data structures in Scala and in particular scala.meta Trees.

The library comprises a set of combinators that you can compose in order to traverse or transform your data structures. For more information about its inner workings or an exhaustive list of combinators, please take a look at the Wiki.

The report can be found here. Some ideas to improve TQL are presented here.

Examples

Get all different variable names in a scala.meta AST

q"""
 val a = 5
 val c = 3
 if (3 == 17) {
  val c = 1
 }
 else 2
"""
val getAllVals = topDown(collectIn[Set]{
  case x: Defn.Val => x.pats.head.toString
})
println(getAllVals(x).result) //Set(a, c)

When we know that we will only use the traversal once for a specific tree, we can use this more conveniant way of writing traversals:

val getAllVals = x.collectIn[Set]{case x: Defn.Val => x.pats.head.toString}.result //topDown is implicit
println(getAllVals)

Of course we lose the combining power of combinators.

How to use

A snapshot is available on Sonatype. To use it with SBT, add the following lines in your build:

libraryDependencies += "com.github.begeric" % "tqlscalameta_2.11" % "0.1-SNAPSHOT"
resolvers += Resolver.sonatypeRepo("snapshots")

About

Combinatorial Query language for scala.meta trees.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages