diff --git a/modules/parser-gen/src/main/scala/playground/parsergen/demo.scala b/modules/parser-gen/src/main/scala/playground/parsergen/demo.scala index 6a6764cb..a2117145 100644 --- a/modules/parser-gen/src/main/scala/playground/parsergen/demo.scala +++ b/modules/parser-gen/src/main/scala/playground/parsergen/demo.scala @@ -1,4 +1,4 @@ -case class Foo(bars: List[Bar]) { +case class Foo(bars: List[Bar], nodes: List[Node]) { def select[A](f: Foo.Selector => Selection[A]): List[A] = f(Foo.Selector(List(this))).path } @@ -6,6 +6,7 @@ object Foo { case class Selector(path: List[Foo]) extends Selection[Foo] { def bars: Bar.Selector = Bar.Selector(path.flatMap(_.bars)) + def nodes: Node.Selector = Node.Selector(path.flatMap(_.nodes)) } } @@ -38,6 +39,33 @@ object Identifier { case class Selector(path: List[Identifier]) extends Selection[Identifier] } +enum Node { + case Ident(i: Identifier) + case B(b: Baz) + + def asIdent: Option[Identifier] = + this match { + case Ident(i) => Some(i) + case _ => None + } + + def asB: Option[Baz] = + this match { + case B(b) => Some(b) + case _ => None + } + +} + +object Node { + + case class Selector(val path: List[Node]) extends Selection[Node] { + def ident: Identifier.Selector = Identifier.Selector(path.flatMap(_.asIdent)) + def b: Baz.Selector = Baz.Selector(path.flatMap(_.asB)) + } + +} + trait Selection[A] { def path: List[A] } @@ -45,8 +73,30 @@ trait Selection[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) + Foo( + List(Bar(Some(Baz(List("a", "b").map(Identifier.apply))))), + List(Node.Ident(Identifier("aa"))), + ).select(_.bars.baz).pipe(println) + + println( + Foo( + List(Bar(Some(Baz(List("a", "b").map(Identifier.apply))))), + List(Node.Ident(Identifier("aa"))), + ) + .select(_.bars.baz.names) == + Foo( + List(Bar(Some(Baz(List("a", "b").map(Identifier.apply))))), + List(Node.Ident(Identifier("aa"))), + ) + .select(_.bars.baz) + .flatMap(_.names) + ) + + Foo( + List(Bar(Some(Baz(List("a", "b").map(Identifier.apply))))), + List(Node.Ident(Identifier("aa"))), + ) + .select(_.nodes.ident) + // .select(_.nodes.b.names) .pipe(println) }