Skip to content

Commit

Permalink
More powerful select
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 11, 2024
1 parent 5458c9b commit 08fbbd6
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ private def renderUnion(u: Type.Union): String = {
|
| final case class Selector(path: List[$name]) extends Selection[$name] {
|${selectorMethods.indentTrim(4)}
|
| type Self = Selector
| protected val remake = Selector.apply
| }
|}
|""".stripMargin
Expand Down Expand Up @@ -249,6 +252,9 @@ private def renderProduct(p: Type.Product): String = {
|
| final case class Selector(path: List[$name]) extends Selection[$name] {
|${selectorMethods.indentTrim(4)}
|
| type Self = Selector
| protected val remake = Selector.apply
| }
|}
|""".stripMargin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object Binding {
final case class Selector(path: List[Binding]) extends Selection[Binding] {
def key: Identifier.Selector = Identifier.Selector(path.flatMap(_.key))
def value: InputNode.Selector = InputNode.Selector(path.flatMap(_.value))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Boolean_ {

final case class Selector(path: List[Boolean_]) extends Selection[Boolean_] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Comment {

final case class Selector(path: List[Comment]) extends Selection[Comment] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Identifier {

final case class Selector(path: List[Identifier]) extends Selection[Identifier] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ object InputNode {
def number : Number.Selector = Number.Selector(path.flatMap(_.asNumber))
def string : String_.Selector = String_.Selector(path.flatMap(_.asString))
def struct : Struct.Selector = Struct.Selector(path.flatMap(_.asStruct))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ object List_ {

final case class Selector(path: List[List_]) extends Selection[List_] {
def list_fields: InputNode.Selector = InputNode.Selector(path.flatMap(_.list_fields))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Null_ {

final case class Selector(path: List[Null_]) extends Selection[Null_] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Number {

final case class Selector(path: List[Number]) extends Selection[Number] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object OperationName {
final case class Selector(path: List[OperationName]) extends Selection[OperationName] {
def identifier: QualifiedIdentifier.Selector = QualifiedIdentifier.Selector(path.flatMap(_.identifier))
def name: Identifier.Selector = Identifier.Selector(path.flatMap(_.name))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ object Prelude {

final case class Selector(path: List[Prelude]) extends Selection[Prelude] {
def use_clause: UseClause.Selector = UseClause.Selector(path.flatMap(_.use_clause))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object QualifiedIdentifier {
final case class Selector(path: List[QualifiedIdentifier]) extends Selection[QualifiedIdentifier] {
def namespace: Identifier.Selector = Identifier.Selector(path.flatMap(_.namespace))
def selection: Identifier.Selector = Identifier.Selector(path.flatMap(_.selection))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object RunQuery {
final case class Selector(path: List[RunQuery]) extends Selection[RunQuery] {
def input: Struct.Selector = Struct.Selector(path.flatMap(_.input))
def operation_name: OperationName.Selector = OperationName.Selector(path.flatMap(_.operation_name))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object SourceFile {
final case class Selector(path: List[SourceFile]) extends Selection[SourceFile] {
def prelude: Prelude.Selector = Prelude.Selector(path.flatMap(_.prelude))
def statements: TopLevelStatement.Selector = TopLevelStatement.Selector(path.flatMap(_.statements))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object String_ {

final case class Selector(path: List[String_]) extends Selection[String_] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ object Struct {

final case class Selector(path: List[Struct]) extends Selection[Struct] {
def bindings: Binding.Selector = Binding.Selector(path.flatMap(_.bindings))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ object TopLevelStatement {

final case class Selector(path: List[TopLevelStatement]) extends Selection[TopLevelStatement] {
def run_query: RunQuery.Selector = RunQuery.Selector(path.flatMap(_.run_query))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ object UseClause {
final case class Selector(path: List[UseClause]) extends Selection[UseClause] {
def identifier: QualifiedIdentifier.Selector = QualifiedIdentifier.Selector(path.flatMap(_.identifier))
def whitespace: Whitespace.Selector = Whitespace.Selector(path.flatMap(_.whitespace))

type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ object Whitespace {

final case class Selector(path: List[Whitespace]) extends Selection[Whitespace] {


type Self = Selector
protected val remake = Selector.apply
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package playground.treesitter4s.std

trait Selection[A] {
type Self <: Selection[A]
def path: List[A]
protected def remake: List[A] => Self

def transform(f: List[A] => List[A]): Self = remake(f(path))
def find(f: A => Boolean): Self = transform(_.find(f).toList)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,20 @@ object TreeSitterParserTests extends FunSuite {
val in = parse("""use service foo.bar.baz.bax#Baz
|GetBaz { a = { x = 42 } }""".stripMargin)

// this ain't pretty huh
// watch out for the upcoming lookup DSL
val valueOfX =
in.statements
.head
.run_query
.get
.input
.get
.bindings
.find(_.key.get.source == "a")
.get
.value
.get
.asStruct
.get
.bindings
.find(_.key.get.source == "x")
.get
.value
.get
.asNumber
.get
in.select(
_.statements
.run_query
.input
.bindings
.find(_.key.get.source == "a")
.value
.struct
.bindings
.find(_.key.get.source == "x")
.value
.number
).head
.source
.toInt

Expand All @@ -86,26 +77,19 @@ object TreeSitterParserTests extends FunSuite {
// this ain't pretty huh
// watch out for the upcoming lookup DSL
val valueOfX =
in.statements
.head
.run_query
.get
.input
.get
.bindings
.find(_.key.get.source == "a")
.get
.value
.get
.asStruct
.get
.bindings
.find(_.key.get.source == "x")
.get
.value
.get
.asNumber
.get
in.select(
_.statements
.run_query
.input
.bindings
.find(_.key.get.source == "a")
.value
.struct
.bindings
.find(_.key.get.source == "x")
.value
.number
).head
.source
.toInt

Expand Down

0 comments on commit 08fbbd6

Please sign in to comment.