( s: SequenceIntersectionState )
| 186 | } |
| 187 | |
| 188 | const _intersectSequences = ( |
| 189 | s: SequenceIntersectionState |
| 190 | ): SequenceIntersectionState => { |
| 191 | const [lHead, ...lTail] = s.l |
| 192 | const [rHead, ...rTail] = s.r |
| 193 | |
| 194 | if (!lHead || !rHead) return s |
| 195 | |
| 196 | const lHasPostfix = lTail[lTail.length - 1]?.kind === "postfix" |
| 197 | const rHasPostfix = rTail[rTail.length - 1]?.kind === "postfix" |
| 198 | |
| 199 | const kind: SequenceElementKind = |
| 200 | lHead.kind === "prefix" || rHead.kind === "prefix" ? "prefix" |
| 201 | : lHead.kind === "postfix" || rHead.kind === "postfix" ? "postfix" |
| 202 | : lHead.kind === "variadic" && rHead.kind === "variadic" ? "variadic" |
| 203 | // if either operand has postfix elements, the full-length |
| 204 | // intersection can't include optional elements (though they may |
| 205 | // exist in some of the fixed length variants) |
| 206 | : lHasPostfix || rHasPostfix ? "prefix" |
| 207 | : lHead.kind === "defaultables" || rHead.kind === "defaultables" ? |
| 208 | "defaultables" |
| 209 | : "optionals" |
| 210 | |
| 211 | // an element before a variadic followed by postfix elements may also meet |
| 212 | // those elements directly, in a fixed length variant |
| 213 | const postfixSide = |
| 214 | kind !== "prefix" ? undefined |
| 215 | : rHead.kind === "variadic" && rHasPostfix ? "r" |
| 216 | : lHead.kind === "variadic" && lHasPostfix ? "l" |
| 217 | : undefined |
| 218 | if (postfixSide) { |
| 219 | s.fixedVariants.push( |
| 220 | _intersectSequences({ |
| 221 | ...s, |
| 222 | disjoint: new Disjoint(...s.disjoint), |
| 223 | [postfixSide]: asPrefix(postfixSide === "l" ? lTail : rTail), |
| 224 | realigned: true |
| 225 | }) |
| 226 | ) |
| 227 | } |
| 228 | |
| 229 | const result = intersectOrPipeNodes(lHead.node, rHead.node, s.ctx) |
| 230 | if (result instanceof Disjoint) { |
| 231 | if (kind === "prefix" || kind === "postfix") { |
| 232 | s.disjoint.push( |
| 233 | ...result.withPrefixKey( |
| 234 | // ideally we could handle disjoint paths more precisely here, |
| 235 | // but not trivial to serialize postfix elements as keys |
| 236 | kind === "prefix" ? s.result.length : `-${lTail.length + 1}`, |
| 237 | // a discriminant must be at an index both elements have at any length |
| 238 | ( |
| 239 | kind === "prefix" && |
| 240 | !s.realigned && |
| 241 | elementIsRequired(lHead) && |
| 242 | elementIsRequired(rHead) |
| 243 | ) ? |
| 244 | "required" |
| 245 | : "optional" |
no test coverage detected