-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
modules/parser-gen/src/main/scala/playground/parsergen/demo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
case class Foo(bars: List[Bar]) { | ||
def select[A](f: Foo.Selector => Selection[A]): List[A] = f(Foo.Selector(List(this))).path | ||
} | ||
|
||
object Foo { | ||
|
||
case class Selector(path: List[Foo]) extends Selection[Foo] { | ||
def bars: Bar.Selector = Bar.Selector(path.flatMap(_.bars)) | ||
} | ||
|
||
} | ||
|
||
case class Bar(baz: Option[Baz]) | ||
|
||
object Bar { | ||
|
||
case class Selector(path: List[Bar]) extends Selection[Bar] { | ||
def baz: Baz.Selector = Baz.Selector(path.flatMap(_.baz)) | ||
|
||
} | ||
|
||
} | ||
|
||
case class Baz(names: List[Identifier]) | ||
|
||
object Baz { | ||
|
||
case class Selector(path: List[Baz]) extends Selection[Baz] { | ||
def names: Identifier.Selector = Identifier.Selector(path.flatMap(_.names)) | ||
} | ||
|
||
} | ||
|
||
case class Identifier(value: String) | ||
|
||
object Identifier { | ||
|
||
case class Selector(path: List[Identifier]) extends Selection[Identifier] | ||
} | ||
|
||
trait Selection[A] { | ||
def path: List[A] | ||
} | ||
|
||
@main def demo = { | ||
import util.chaining.* | ||
|
||
Foo(List(Bar(Some(Baz(List("a", "b").map(Identifier.apply)))))).select(_.bars.baz).pipe(println) | ||
Foo(List(Bar(Some(Baz(List("a", "b").map(Identifier.apply)))))) | ||
.select(_.bars.baz.names) | ||
.pipe(println) | ||
} |
5 changes: 5 additions & 0 deletions
5
modules/treesitter/src/main/scala/playground/treesitter4s/std/Selection.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package playground.treesitter4s.std | ||
|
||
trait Selection[A] { | ||
def path: List[A] | ||
} |