better-parse icon indicating copy to clipboard operation
better-parse copied to clipboard

implementing and

Open mgood7123 opened this issue 5 years ago • 2 comments

how would i design the and of a parser combinator so that i do not need to create an infinite number of classes for it example (probably incorrect)

/*
val A = F1("1")
val B = F1("2")
val C = F1("3")

val C: F1AndF1 = A.and(B)
val D: F1AndF1 = B.and(D)

val E: F1AndF1AndF2AndF2 = C.and(D)

// ...

*/
class F1(left: Any) {
    // F1 And F1
    infix fun and(right: F1)  = F1AndF1(this, right)
}
class F1AndF1(left: F1, right: F1) {
    // F1AndF1 And F1AndF1
    infix fun and(right: F1AndF1)  = F1AndF1AndF2AndF2(this, right)
}
class F1AndF1AndF2AndF2(left: F1AndF1, right: F1AndF1) {
    // F1AndF1AndF2AndF2 And F1AndF1AndF2AndF2
    infix and(right: F1AndF1AndF2AndF2)  = F1AndF1AndF2AndF2AndF3AndF3AndF4AndF4(this, right)
}
// and so on for ever

mgood7123 avatar Apr 26 '19 13:04 mgood7123

I couldn't find a way to do that preserving type safety. My and combinators are based on generated tuples and functions for adding another component, for each number of components (up to 16).

h0tk3y avatar Jul 01 '20 22:07 h0tk3y

it could be implemented as a binary tree

mgood7123 avatar Jul 02 '20 14:07 mgood7123