<commit_before>//  Copyright (c) 2014 <NAME>. All rights reserved.

/// Convenience for describing the types of parser combinators.
///
/// \param Tree  The type of parse tree generated by the parser.
public enum Parser<C: CollectionType, Tree> {
	/// The type of parser combinators.
	public typealias Function = (C, C.Index) -> Result

	/// The type produced by parser combinators.
	public typealias Result = Either<Error<C.Index>, (Tree, C.Index)>
}

/// Parses `input` with `parser`, returning the parse trees or `nil` if nothing could be parsed, or if parsing did not consume the entire input.
public func parse<C: CollectionType, Tree>(parser: Parser<C, Tree>.Function, input: C) -> Either<Error<C.Index>, Tree> {
	return parser(input, input.startIndex).flatMap { $1 == input.endIndex ? .right($0) : .left(.leaf("finished parsing before end of input", $1)) }
}

public func parse<Tree>(parser: Parser<String.CharacterView, Tree>.Function, input: String) -> Either<Error<String.Index>, Tree> {
	return parser(input.characters, input.startIndex).flatMap { $1 == input.endIndex ? .right($0) : .left(.leaf("finished parsing before end of input", $1)) }
}

// MARK: - Terminals

/// Returns a parser which never parses its input.
public func none<C: CollectionType, Tree>(string: String = "no way forward") -> Parser<C, Tree>.Function {
	return { _, index in Either.left(Error.leaf(string, index)) }
}

/// Returns a parser which parses any single character.
public func any<C: CollectionType>(input: C, index: C.Index) -> Parser<C, C.Generator.Element>.Result {

	if input.count > 0 {
		let parsed = input[index]
		let next = index.successor()
		
		return .Right((parsed, next))
	} else {
		return .Left(Error.leaf("", index))
	}
}


/// Returns a parser which parses a `literal` sequence of elements from the input.
///
/// This overload enables e.g. `%"xyz"` to produce `String -> (String, String)`.
public prefix func % <C: CollectionType where C.Generator.Element: Equatable> (literal: C) -> Parser<C, C>.Function {
	return { input, index in
		containsAt(input, index: index, needle: literal) ?
			.Right(literal, index.advancedBy(literal.count))
		:	.Left(.leaf("expected \(literal)", index))
	}
}

public prefix func %(literal: String) -> Parser<String.CharacterView, String>.Function {
	return String.init <^> %literal.characters
}

/// Returns a parser which parses a `literal` element from the input.<commit_msg>Skip a line break.<commit_after>//  Copyright (c) 2014 <NAME>. All rights reserved.

/// Convenience for describing the types of parser combinators.
///
/// \param Tree  The type of parse tree generated by the parser.
public enum Parser<C: CollectionType, Tree> {
	/// The type of parser combinators.
	public typealias Function = (C, C.Index) -> Result

	/// The type produced by parser combinators.
	public typealias Result = Either<Error<C.Index>, (Tree, C.Index)>
}

/// Parses `input` with `parser`, returning the parse trees or `nil` if nothing could be parsed, or if parsing did not consume the entire input.
public func parse<C: CollectionType, Tree>(parser: Parser<C, Tree>.Function, input: C) -> Either<Error<C.Index>, Tree> {
	return parser(input, input.startIndex).flatMap { $1 == input.endIndex ? .right($0) : .left(.leaf("finished parsing before end of input", $1)) }
}

public func parse<Tree>(parser: Parser<String.CharacterView, Tree>.Function, input: String) -> Either<Error<String.Index>, Tree> {
	return parser(input.characters, input.startIndex).flatMap { $1 == input.endIndex ? .right($0) : .left(.leaf("finished parsing before end of input", $1)) }
}

// MARK: - Terminals

/// Returns a parser which never parses its input.
public func none<C: CollectionType, Tree>(string: String = "no way forward") -> Parser<C, Tree>.Function {
	return { _, index in Either.left(Error.leaf(string, index)) }
}

/// Returns a parser which parses any single character.
public func any<C: CollectionType>(input: C, index: C.Index) -> Parser<C, C.Generator.Element>.Result {
	if input.count > 0 {
		let parsed = input[index]
		let next = index.successor()
		
		return .Right((parsed, next))
	} else {
		return .Left(Error.leaf("", index))
	}
}


/// Returns a parser which parses a `literal` sequence of elements from the input.
///
/// This overload enables e.g. `%"xyz"` to produce `String -> (String, String)`.
public prefix func % <C: CollectionType where C.Generator.Element: Equatable> (literal: C) -> Parser<C, C>.Function {
	return { input, index in
		containsAt(input, index: index, needle: literal) ?
			.Right(literal, index.advancedBy(literal.count))
		:	.Left(.leaf("expected \(literal)", index))
	}
}

public prefix func %(literal: String) -> Parser<String.CharacterView, String>.Function {
	return String.init <^> %literal.characters
}

/// Returns a parser which parses a `literal` element from the input.
{'fraction_non_alphanumeric': 0.14665841584158415, 'fraction_numerical': 0.004537953795379538, 'mean_word_length': 4.104210526315789, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 61, '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': '5354240', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1452, 'n_words': 510}