Skip to content

Commit

Permalink
Exposed a withWidth combinator, cleaned up old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Sep 12, 2023
1 parent bbb6ac2 commit b347a0d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
8 changes: 4 additions & 4 deletions parsley/shared/src/main/scala/parsley/errors/combinator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ object combinator {
*/
@deprecated("This combinator will be removed in 5.0.0, without direct replacement", "4.2.0")
def !(msggen: A => String): Parsley[Nothing] = partialAmendThenDislodge {
parsley.position.internalOffsetSpan(entrench(con(p))).flatMap { case (os, x, oe) =>
combinator.fail(oe - os, msggen(x))
parsley.position.withWidth(entrench(con(p))).flatMap { case (x, width) =>
combinator.fail(width, msggen(x))
}
}

Expand All @@ -592,8 +592,8 @@ object combinator {
*/
@deprecated("This combinator will be removed in 5.0.0, without direct replacement", "4.2.0")
def unexpected(msggen: A => String): Parsley[Nothing] = partialAmendThenDislodge {
parsley.position.internalOffsetSpan(entrench(con(p))).flatMap { case (os, x, oe) =>
combinator.unexpected(oe - os, msggen(x))
parsley.position.withWidth(entrench(con(p))).flatMap { case (x, width) =>
combinator.unexpected(width, msggen(x))
}
}
// $COVERAGE-ON$
Expand Down
14 changes: 3 additions & 11 deletions parsley/shared/src/main/scala/parsley/errors/patterns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
package parsley.errors

import parsley.Parsley, Parsley.{atomic, select, unit}
import parsley.implicits.zipped.Zipped3
import parsley.errors.combinator.{ErrorMethods, amend}
import parsley.position.offset
import parsley.position.withWidth

/** This module contains combinators that help facilitate the error message generational patterns ''Verified Errors'' and ''Preventative Errors''.
*
Expand Down Expand Up @@ -112,11 +111,7 @@ object patterns {
def verifiedUnexpected(reason: A => String): Parsley[Nothing] = this.verifiedWithVanillaRaw(x => Some(reason(x)))

// TODO: document and test
def verifiedWith(err: ErrorGen[A]) = amend {
err((offset, atomic(con(p)).newHide, offset).zipped {
(s, x, e) => (x, e-s)
})
}
def verifiedWith(err: ErrorGen[A]) = amend(err(withWidth(atomic(con(p)).newHide)))

@inline private def verifiedWithVanilla(unexGen: A => UnexpectedItem, reasonGen: A => Option[String]) = verifiedWith {
new VanillaGen[A] {
Expand All @@ -143,10 +138,7 @@ object patterns {

// TODO: document and test
def preventWith(err: ErrorGen[A], labels: String*) = {
val inner: Parsley[Either[(A, Int), Unit]] =
(offset, atomic(con(p)).newHide, offset).zipped {
(s, x, e) => (x, e-s)
} <+> unit
val inner: Parsley[Either[(A, Int), Unit]] = withWidth(atomic(con(p)).newHide) <+> unit
val labelledErr = labels match {
case l1 +: ls => err.parser.label(l1, ls: _*)
case _ => err.parser
Expand Down
5 changes: 2 additions & 3 deletions parsley/shared/src/main/scala/parsley/position.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ object position {
*/
val offset: Parsley[Int] = internalOffset

private [parsley] def spanWith[A, S](end: Parsley[S])(p: Parsley[A]): Parsley[(S, A, S)] = (end, p, end).zipped
// this is subject to change at the slightest notice, do NOT expose
private [parsley] def internalOffsetSpan[A](p: Parsley[A]): Parsley[(Int, A, Int)] = spanWith(internalOffset)(p)
//TODO: document and test
def withWidth[A](p: Parsley[A]): Parsley[(A, Int)] = (offset, p, offset).zipped((s, x, e) => (x, e-s))
}

0 comments on commit b347a0d

Please sign in to comment.