@@ -5,40 +5,44 @@ import com.github.h0tk3y.betterParse.parser.ErrorResult
55import com.github.h0tk3y.betterParse.parser.ParseResult
66import com.github.h0tk3y.betterParse.parser.Parsed
77import com.github.h0tk3y.betterParse.parser.Parser
8- import com.github.h0tk3y.betterParse.utils.Tuple
9- import com.github.h0tk3y.betterParse.utils.Tuple1
108import com.github.h0tk3y.betterParse.utils.Tuple2
119
1210/* * Parses the sequence with the receiver [Parser] and then with the [other] parser. If both suceed, returns a [Tuple2]
1311 * with the values from the [Parsed] results. Otherwise, returns the [ErrorResult] of the failed parser. */
1412infix inline fun <reified A , reified B > Parser<A>.and (other : Parser <B >) =
1513 AndCombinator (listOf (this , other)) { (a1, a2) -> Tuple2 (a1 as A , a2 as B ) }
1614
15+ /* * The same as `this `[and]` other`*/
16+ operator inline fun <reified A , reified B > Parser<A>.times (other : Parser <B >) = this and other
17+
1718/* * Parses the sequence with the receiver [Parser] and then with the [other] parser. If both suceed, returns a [Tuple2]
1819 * with the values from the [Parsed] results. Otherwise, returns the [ErrorResult] of the failed parser. */
19- infix inline fun <reified A , reified B > AndCombinator<Tuple1<A>>.and (other : Parser <B >) =
20- AndCombinator (consumers + other) { (a1, a2) -> Tuple2 (a1 as A , a2 as B ) }
20+ @JvmName(" and0" )
21+ infix inline fun <reified A , reified B > AndCombinator<A>.and (other : Parser <B >) =
22+ AndCombinator (consumers + listOf (other)) { (a1, a2) -> Tuple2 (a1 as A , a2 as B ) }
23+
24+ /* * The same as `this `[and]` other`*/
25+ operator inline fun <reified A , reified B > AndCombinator<A>.times (other : Parser <B >) = this and other
2126
22- class AndCombinator <out R : Tuple > internal @PublishedApi constructor(
27+ class AndCombinator <out R > internal @PublishedApi constructor(
2328 val consumers : List <Any >,
2429 val transform : (List <* >) -> R
2530) : Parser<R> {
2631
27- fun process (tokens : Sequence <TokenMatch >): Pair <List <ParseResult <* >>, Sequence<TokenMatch>> {
32+ private fun process (tokens : Sequence <TokenMatch >): Pair <List <ParseResult <* >>, Sequence<TokenMatch>> {
2833 var lastTokens = tokens
29- var errorResult: ErrorResult ? = null
3034 return consumers.map { consumer ->
3135 val parser = when (consumer) {
3236 is Parser <* > -> consumer
33- is SkipParser < * > -> consumer.innerParser
37+ is SkipParser -> consumer.innerParser
3438 else -> throw IllegalArgumentException ()
3539 }
36- val result = errorResult ? : parser.tryParse(lastTokens)
40+ val result = parser.tryParse(lastTokens)
3741 when (result) {
3842 is ErrorResult -> return @process listOf (result) to lastTokens
3943 is Parsed <* > -> lastTokens = result.remainder
4044 }
41- if (consumer is SkipParser < * > ) null else result
45+ if (consumer is SkipParser ) null else result
4246 }.filterNotNull() to lastTokens
4347 }
4448
@@ -47,25 +51,4 @@ class AndCombinator<out R : Tuple> internal @PublishedApi constructor(
4751 return results.firstOrNull { it is ErrorResult } as ? ErrorResult
4852 ? : results.filterIsInstance<Parsed <* >>().let { Parsed (transform(it.map { it.value }), remainder) }
4953 }
50- }
51-
52- /* * Wraps a [Parser] to distinguish it from other parsers when it is used in [and] functions. */
53- class SkipParser <T >(val innerParser : Parser <T >)
54-
55- /* * Wraps a [Parser] to distinguish it from other parsers when it is used in [and] functions. */
56- fun <T > skip (parser : Parser <T >) = SkipParser (parser)
57-
58- /* * Parses the sequence with the receiver [Parser] and the wrapped [other] parser, but returns the [Parsed] result
59- * from the receiver parser. */
60- infix fun <T : Tuple , B > AndCombinator<T>.and (other : SkipParser <B >) =
61- AndCombinator (consumers + other, transform)
62-
63- /* * Parses the sequence with the receiver [Parser] and the wrapped [other] parser, but returns the [Parsed] result
64- * with a value from the receiver parser in a [Tuple1]. */
65- infix inline fun <reified T > Parser<T>.and (other : SkipParser <* >) =
66- AndCombinator (listOf (this , other), { (a) -> Tuple1 (a as T ) })
67-
68- /* * Parses the wrapped receiver [Parser] and the [other] parser and returns the [Parsed] result
69- * with a value from the [other] parser in a [Tuple1]. */
70- infix inline fun <reified T > SkipParser <* >.and (other : Parser <T >) =
71- AndCombinator (listOf (this , other)) { (b) -> Tuple1 (b as T ) }
54+ }
0 commit comments