<commit_before>/* sbt -- Simple Build Tool
 * Copyright 2008, 2010, 2011  <NAME>
 */
package sbt.complete

	import Parser._
	import sbt.Types.{const, left, right, some}
	import sbt.Util.{makeList,separate}

sealed trait Parser[+T]
{
	def derive(i: Char): Parser[T]
	def resultEmpty: Result[T]
	def result: Option[T]
	def completions(level: Int): Completions
	def failure: Option[Failure]
	def isTokenStart = false
	def ifValid[S](p: => Parser[S]): Parser[S]
	def valid: Boolean
}
sealed trait RichParser[A]
{
	/** Produces a Parser that applies the original Parser and then applies `next` (in order).*/
	def ~[B](next: Parser[B]): Parser[(A,B)]
	/** Produces a Parser that applies the original Parser one or more times.*/
	def + : Parser[Seq[A]]
	/** Produces a Parser that applies the original Parser zero or more times.*/
	def * : Parser[Seq[A]]
	/** Produces a Parser that applies the original Parser zero or one times.*/
	def ? : Parser[Option[A]]
	/** Produces a Parser that applies either the original Parser or `b`.*/
	def |[B >: A](b: Parser[B]): Parser[B]
	/** Produces a Parser that applies either the original Parser or `b`.*/
	def ||[B](b: Parser[B]): Parser[Either[A,B]]
	/** Produces a Parser that applies the original Parser to the input and then applies `f` to the result.*/
	def map[B](f: A => B): Parser[B]
	/** Returns the original parser.  This is useful for converting literals to Parsers.
	* For example, `'c'.id` or `"asdf".id`*/
	def id: Parser[A]

	def ^^^[B](value: B): Parser[B]
	def ??[B >: A](alt: B): Parser[B]
	def <~[B](b: Parser[B]): Parser[A]
	def ~>[B](b: Parser[B]): Parser[B]

	/** Uses the specified message if the original Parser fails.*/
	def !!!(msg: String): Parser[A]
	/** If an exception is thrown by the original Parser,
	* capture it and fail locally instead of allowing the exception to propagate up and terminate parsing.*/
	def failOnException: Parser[A]
	
	@deprecated("Use `not` and explicitly provide the failure message", "0.12.2")
	def unary_- : Parser[Unit]
	def & (o: Parser[_]): Parser[A]

	@deprecated("Use `and` and `not` and explicitly provide the failure message", "0.12.2")
	def - (o: Parser[_]): Parser[A]

	/** Explicitly defines the completions for the original Parser.*/
	def examples(s: String*): Parser[A]
	/** Explicitly defines the completions for the original Parser.*/
	def examples(s: Set[String], check: Boolean = false): Parser[A]
	/** Converts a Parser returning a Char sequence to a Parser returning a String.*/
	def string(implicit ev: A <:< Seq[Char]): Parser[String]
	/** Produces a Parser that filters the original parser.
	* If 'f' is not true when applied to the output of the original parser, the Parser returned by this method fails.*/
	def filter(f: A => Boolean, msg: String => String): Parser[A]

	def flatMap[B](f: A => Parser[B]): Parser[B]
}
object Parser extends ParserMain
{
	sealed abstract class Result[+T] {
		def isFailure: Boolean
		def isValid: Boolean
		def errors: Seq[String]
		def or[B >: T](b: => Result[B]): Result[B]
		def either[B](b: => Result[B]): Result[Either[T,B]]
		def map[B](f: T => B): Result[B]
		def flatMap[B](f: T => Result[B]): Result[B]
		def &&(b: => Result[_]): Result[T]
		def filter(f: T => Boolean, msg: => String): Result[T]
		def seq[B](b: => Result[B]): Result[(T,B)] = app(b)( (m,n) => (m,n) )
		def app[B,C](b: => Result[B])(f: (T, B) => C): Result[C]
		def toEither: Either[() => Seq[String], T]
	}
	final case class Value[+T](value: T) extends Result[T] {
		def isFailure = false
		def isValid: Boolean = true
		def errors = Nil
		def app[B,C](b: => Result[B])(f: (T, B) => C): Result[C] = b match {
			case fail: Failure => fail
			case Value(bv) => Value(f(value, bv))
		}
		def &&(b: => Result[_]): Result[T] = b match { case f: Failure => f; case _ => this }
		def or[B >: T](b: => Result[B]): Result[B] = this
		def either[B](b: => Result[B]): Result[Either[T,B]] = Value(Left(value))
		def map[B](f: T => B): Result[B] = Value(f(value))
		def flatMap[B](f: T => Result[B]): Result[B] = f(value)
		def filter(f: T => Boolean, msg: => String): Result[T] = if(f(value)) this else mkFailure(msg)
		def toEither = Right(value)
	}
	final class Failure(mkErrors: => Seq[String]) extends Result[Nothing] {
		lazy val errors: Seq[String] = mkErrors
		def isFailure = true
		def isValid = false
		def map[B](f: Nothing => B) = this
		def flatMap[B](f: Nothing => Result[B]) = this
		def or[B](b: => Result[B]): Result[B] = b match {
			case v: Value[B] => v
			case f: Failure => concatErrors(f)
		}
		def either[B](b: => Result[B]): Result[Either[Nothing,B]] = b match {
			case Value(v) => Value(Right(v))
			case f: Failure => concatErrors(f)
		}
		def filter(f: Nothing => Boolean, msg: => String) = this
		def app[B,C](b: => Result[B])(f: (Nothing, B) => C): Result[C] = this
		def &&(b: => Result[_]) = this
		def toEither = Left(() => errors)

		private[this] def concatErrors(f: Failure) = mkFailures(errors ++ f.errors)
	}
	def mkFailures(errors: => Seq[String]): Failure = new Failure(errors.distinct)
	def mkFailure(error: => String): Failure = new Failure(error :: Nil)

	def checkMatches(a: Parser[_], completions: Seq[String])
	{
		val bad = completions.filter( apply(a)(_).resultEmpty.isFailure)
		if(!bad.isEmpty) sys.error("Invalid example completions: " + bad.mkString("'", "', '", "'"))
	}
	def tuple[A,B](a: Option[A], b: Option[B]): Option[(A,B)] =
		(a,b) match { case (Some(av), Some(bv)) => Some((av, bv)); case _ => None }

	def mapParser[A,B](a: Parser[A], f: A => B): Parser[B] =
		a.ifValid {
			a.result match
			{
				case Some(av) => success( f(av) )
				case None => new MapParser(a, f)
			}
		}

	def bindParser[A,B](a: Parser[A], f: A => Parser[B]): Parser[B] =
		a.ifValid {
			a.result match
			{
				case Some(av) => f(av)
				case None => new BindParser(a, f)
			}
		}

	def filterParser[T](a: Parser[T], f: T => Boolean, seen: String, msg: String => String): Parser[T] =
		a.ifValid {
			a.result match<commit_msg>'definitive' Parser failures<commit_after>/* sbt -- Simple Build Tool
 * Copyright 2008, 2010, 2011  <NAME>
 */
package sbt.complete

	import Parser._
	import sbt.Types.{const, left, right, some}
	import sbt.Util.{makeList,separate}

sealed trait Parser[+T]
{
	def derive(i: Char): Parser[T]
	def resultEmpty: Result[T]
	def result: Option[T]
	def completions(level: Int): Completions
	def failure: Option[Failure]
	def isTokenStart = false
	def ifValid[S](p: => Parser[S]): Parser[S]
	def valid: Boolean
}
sealed trait RichParser[A]
{
	/** Produces a Parser that applies the original Parser and then applies `next` (in order).*/
	def ~[B](next: Parser[B]): Parser[(A,B)]
	/** Produces a Parser that applies the original Parser one or more times.*/
	def + : Parser[Seq[A]]
	/** Produces a Parser that applies the original Parser zero or more times.*/
	def * : Parser[Seq[A]]
	/** Produces a Parser that applies the original Parser zero or one times.*/
	def ? : Parser[Option[A]]
	/** Produces a Parser that applies either the original Parser or `b`.*/
	def |[B >: A](b: Parser[B]): Parser[B]
	/** Produces a Parser that applies either the original Parser or `b`.*/
	def ||[B](b: Parser[B]): Parser[Either[A,B]]
	/** Produces a Parser that applies the original Parser to the input and then applies `f` to the result.*/
	def map[B](f: A => B): Parser[B]
	/** Returns the original parser.  This is useful for converting literals to Parsers.
	* For example, `'c'.id` or `"asdf".id`*/
	def id: Parser[A]

	def ^^^[B](value: B): Parser[B]
	def ??[B >: A](alt: B): Parser[B]
	def <~[B](b: Parser[B]): Parser[A]
	def ~>[B](b: Parser[B]): Parser[B]

	/** Uses the specified message if the original Parser fails.*/
	def !!!(msg: String): Parser[A]
	/** If an exception is thrown by the original Parser,
	* capture it and fail locally instead of allowing the exception to propagate up and terminate parsing.*/
	def failOnException: Parser[A]
	
	@deprecated("Use `not` and explicitly provide the failure message", "0.12.2")
	def unary_- : Parser[Unit]
	def & (o: Parser[_]): Parser[A]

	@deprecated("Use `and` and `not` and explicitly provide the failure message", "0.12.2")
	def - (o: Parser[_]): Parser[A]

	/** Explicitly defines the completions for the original Parser.*/
	def examples(s: String*): Parser[A]
	/** Explicitly defines the completions for the original Parser.*/
	def examples(s: Set[String], check: Boolean = false): Parser[A]
	/** Converts a Parser returning a Char sequence to a Parser returning a String.*/
	def string(implicit ev: A <:< Seq[Char]): Parser[String]
	/** Produces a Parser that filters the original parser.
	* If 'f' is not true when applied to the output of the original parser, the Parser returned by this method fails.*/
	def filter(f: A => Boolean, msg: String => String): Parser[A]

	def flatMap[B](f: A => Parser[B]): Parser[B]
}
object Parser extends ParserMain
{
	sealed abstract class Result[+T] {
		def isFailure: Boolean
		def isValid: Boolean
		def errors: Seq[String]
		def or[B >: T](b: => Result[B]): Result[B]
		def either[B](b: => Result[B]): Result[Either[T,B]]
		def map[B](f: T => B): Result[B]
		def flatMap[B](f: T => Result[B]): Result[B]
		def &&(b: => Result[_]): Result[T]
		def filter(f: T => Boolean, msg: => String): Result[T]
		def seq[B](b: => Result[B]): Result[(T,B)] = app(b)( (m,n) => (m,n) )
		def app[B,C](b: => Result[B])(f: (T, B) => C): Result[C]
		def toEither: Either[() => Seq[String], T]
	}
	final case class Value[+T](value: T) extends Result[T] {
		def isFailure = false
		def isValid: Boolean = true
		def errors = Nil
		def app[B,C](b: => Result[B])(f: (T, B) => C): Result[C] = b match {
			case fail: Failure => fail
			case Value(bv) => Value(f(value, bv))
		}
		def &&(b: => Result[_]): Result[T] = b match { case f: Failure => f; case _ => this }
		def or[B >: T](b: => Result[B]): Result[B] = this
		def either[B](b: => Result[B]): Result[Either[T,B]] = Value(Left(value))
		def map[B](f: T => B): Result[B] = Value(f(value))
		def flatMap[B](f: T => Result[B]): Result[B] = f(value)
		def filter(f: T => Boolean, msg: => String): Result[T] = if(f(value)) this else mkFailure(msg)
		def toEither = Right(value)
	}
	final class Failure private[sbt](mkErrors: => Seq[String], val definitive: Boolean) extends Result[Nothing] {
		lazy val errors: Seq[String] = mkErrors
		def isFailure = true
		def isValid = false
		def map[B](f: Nothing => B) = this
		def flatMap[B](f: Nothing => Result[B]) = this
		def or[B](b: => Result[B]): Result[B] = b match {
			case v: Value[B] => v
			case f: Failure => if(definitive) this else concatErrors(f)
		}
		def either[B](b: => Result[B]): Result[Either[Nothing,B]] = b match {
			case Value(v) => Value(Right(v))
			case f: Failure => if(definitive) this else concatErrors(f)
		}
		def filter(f: Nothing => Boolean, msg: => String) = this
		def app[B,C](b: => Result[B])(f: (Nothing, B) => C): Result[C] = this
		def &&(b: => Result[_]) = this
		def toEither = Left(() => errors)

		private[this] def concatErrors(f: Failure) = mkFailures(errors ++ f.errors)
	}
	def mkFailures(errors: => Seq[String], definitive: Boolean = false): Failure = new Failure(errors.distinct, definitive)
	def mkFailure(error: => String, definitive: Boolean = false): Failure = new Failure(error :: Nil, definitive)

	def checkMatches(a: Parser[_], completions: Seq[String])
	{
		val bad = completions.filter( apply(a)(_).resultEmpty.isFailure)
		if(!bad.isEmpty) sys.error("Invalid example completions: " + bad.mkString("'", "', '", "'"))
	}
	def tuple[A,B](a: Option[A], b: Option[B]): Option[(A,B)] =
		(a,b) match { case (Some(av), Some(bv)) => Some((av, bv)); case _ => None }

	def mapParser[A,B](a: Parser[A], f: A => B): Parser[B] =
		a.ifValid {
			a.result match
			{
				case Some(av) => success( f(av) )
				case None => new MapParser(a, f)
			}
		}

	def bindParser[A,B](a: Parser[A], f: A => Parser[B]): Parser[B] =
		a.ifValid {
			a.result match
			{
				case Some(av) => f(av)
				case None => new BindParser(a, f)
			}
		}

	def filterParser[T](a: Parser[T], f: T => Boolean, seen: String, msg: String => String): Parser[T] =
		a.ifValid {
			a.result match
{'fraction_non_alphanumeric': 0.17875072010534113, 'fraction_numerical': 0.003291910130853428, 'mean_word_length': 4.164470888227794, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3135300', 'n_tokens_mistral': 4742, 'n_tokens_neox': 4425, 'n_words': 1401}