-
Notifications
You must be signed in to change notification settings - Fork 0
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
11 changed files
with
563 additions
and
338 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
206 changes: 206 additions & 0 deletions
206
parsley-cats/shared/src/main/scala/parsley/ApplicativeForParsley.scala
Large diffs are not rendered by default.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
parsley-cats/shared/src/main/scala/parsley/FunctorFilterForParsley.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,17 @@ | ||
/* SPDX-FileCopyrightText: © 2022 Parsley Cats Contributors <https://github.com/j-mie6/parsley-cats/graphs/contributors> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley | ||
|
||
import cats.{Functor, FunctorFilter} | ||
|
||
private [parsley] trait FilterFunctorForParsley extends FunctorFilter[Parsley] { self: Functor[Parsley] => | ||
override def functor: Functor[Parsley] = this | ||
|
||
override def mapFilter[A, B](mx: Parsley[A])(f: A => Option[B]): Parsley[B] = mx.mapFilter(f) | ||
|
||
// FunctorFilter Overrides | ||
override def filter[A](mx: Parsley[A])(f: A => Boolean): Parsley[A] = mx.filter(f) | ||
override def filterNot[A](mx: Parsley[A])(f: A => Boolean): Parsley[A] = mx.filterNot(f) | ||
override def collect[A, B](mx: Parsley[A])(f: PartialFunction[A,B]): Parsley[B] = mx.collect(f) | ||
} |
12 changes: 12 additions & 0 deletions
12
parsley-cats/shared/src/main/scala/parsley/FunctorForParsley.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,12 @@ | ||
/* SPDX-FileCopyrightText: © 2022 Parsley Cats Contributors <https://github.com/j-mie6/parsley-cats/graphs/contributors> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley | ||
|
||
import cats.Functor | ||
|
||
private [parsley] trait FunctorForParsley extends Functor[Parsley] { | ||
override def map[A, B](mx: Parsley[A])(f: A => B): Parsley[B] = mx.map(f) | ||
override def as[A, B](mx: Parsley[A], y: B): Parsley[B] = mx #> y | ||
override def void[A](mx: Parsley[A]): Parsley[Unit] = mx.void | ||
} |
60 changes: 60 additions & 0 deletions
60
parsley-cats/shared/src/main/scala/parsley/MonadForParsley.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,60 @@ | ||
/* SPDX-FileCopyrightText: © 2022 Parsley Cats Contributors <https://github.com/j-mie6/parsley-cats/graphs/contributors> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley | ||
|
||
import cats.{Monad, Alternative} | ||
import registers._ | ||
|
||
private [parsley] trait MonadForParsley extends Monad[Parsley] { | ||
override def flatMap[A, B](mx: Parsley[A])(f: A => Parsley[B]): Parsley[B] = mx.flatMap(f) | ||
|
||
override def tailRecM[A, B](x: A)(f: A => Parsley[Either[A, B]]): Parsley[B] = f(x).flatMap { | ||
case Left(nextX) => tailRecM(nextX)(f) | ||
case Right(y) => Parsley.pure(y) | ||
} | ||
|
||
// Monad Overrides | ||
override def ifM[B](mx: Parsley[Boolean])(ifTrue: => Parsley[B], ifFalse: => Parsley[B]): Parsley[B] = combinator.ifP(mx, ifTrue, ifFalse) | ||
override def whileM_[A](p: Parsley[Boolean])(body: =>Parsley[A]): Parsley[Unit] = { | ||
combinator.when(p, combinator.whileP(body ~> p)) | ||
} | ||
override def untilM_[A](body: Parsley[A])(p: => Parsley[Boolean]): Parsley[Unit] = combinator.whileP(body *> p.map(!_)) | ||
|
||
override def whileM[G[_]: Alternative, A](p: Parsley[Boolean])(body: => Parsley[A]): Parsley[G[A]] = { | ||
val G = implicitly[Alternative[G]] | ||
G.empty[A].makeReg { acc => | ||
whileM_(p) { | ||
acc.modify(body.map(x => (xs: G[A]) => G.appendK(xs, x))) | ||
} *> acc.get | ||
} | ||
} | ||
|
||
override def untilM[G[_]: Alternative, A](body: Parsley[A])(cond: => Parsley[Boolean]): Parsley[G[A]] = { | ||
val G = implicitly[Alternative[G]] | ||
map2(body, whileM(cond)(body))(G.prependK(_, _)) | ||
} | ||
|
||
override def untilDefinedM[A](mox: Parsley[Option[A]]): Parsley[A] = { | ||
lazy val loop: Parsley[A] = combinator.decide(mox, loop) | ||
loop | ||
} | ||
|
||
override def iterateUntil[A](mx: Parsley[A])(p: A => Boolean): Parsley[A] = { | ||
lazy val loop: Parsley[A] = mx.persist { mx => | ||
combinator.ifP(mx.map(p), mx, loop) | ||
} | ||
loop | ||
} | ||
override def iterateWhile[A](mx: Parsley[A])(p: A => Boolean): Parsley[A] = { | ||
lazy val loop: Parsley[A] = mx.persist { mx => | ||
combinator.ifP(mx.map(p), loop, mx) | ||
} | ||
loop | ||
} | ||
override def ifElseM[A](branches: (Parsley[Boolean], Parsley[A])*)(els: Parsley[A]): Parsley[A] = { | ||
branches.foldRight(els) { | ||
case ((cond, t), e) => combinator.ifP(cond, t, e) | ||
} | ||
} | ||
} |
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,23 @@ | ||
/* SPDX-FileCopyrightText: © 2022 Parsley Cats Contributors <https://github.com/j-mie6/parsley-cats/graphs/contributors> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley | ||
|
||
import cats.{Monad, MonoidK, FunctorFilter} | ||
|
||
/** Contains instances for `cats` typeclasses. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
object catsinstances { | ||
/** Instance for the core `cats` typeclasses used with parser combinators. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
implicit val monadPlusForParsley: Monad[Parsley] with MonoidK[Parsley] with FunctorFilter[Parsley] = | ||
// This must be kept in this ordering, with more generic further up | ||
new MonadForParsley with ApplicativeForParsley | ||
with FunctorForParsley | ||
with MonoidKForParsley | ||
with FilterFunctorForParsley | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.