-
Notifications
You must be signed in to change notification settings - Fork 22
/
ConstraintsParser+VFLConstraint.swift
35 lines (27 loc) · 1.87 KB
/
ConstraintsParser+VFLConstraint.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Foundation
import Sparse
// MARK: - VFL Constraint
// (e.g. "V:[UIView:0x1f5a31b0]-(385)-| (Names: '|':UIView:0x1f5a3120 )")
extension ConstraintsParser {
static let vflSpaceConstraint = vflAxis.then(vflBoundedEntity).thenSkip(dash).then(spacer).thenSkip(dash).then(vflBoundedEntity).thenSkip(vflDirection).then(optionalInfo)
.map(flatten).map(AnonymousConstraint.init)
static let vflExtentConstraint = vflAxis.thenSkip(character("[")).then(vflEntity).then(vflExtent).thenSkip(character("]")).thenSkip(vflDirection).then(optionalInfo)
.map(flatten).map(AnonymousConstraint.init)
static let vflConstraint = vflExtentConstraint.otherwise(vflSpaceConstraint)
}
// MARK: - Private
private extension ConstraintsParser {
static let hAxis = character("H").map { _ in Attribute.Axis.horizontal }
static let vAxis = character("V").map { _ in Attribute.Axis.vertical }
static let superview = character("|")
static let vflAxis = hAxis.otherwise(vAxis).thenSkip(colon)
static let vflRelation = optional(relation).map { $0 ?? .equal }
static let vflExtent = character("(").skipThen(vflRelation).then(number).thenSkip(character(")"))
static let vflIdentifierCharacter = characterNot(in: "[]|()")
static let vflIdentifier = many(identifierCharacter.and(vflIdentifierCharacter)).asString()
static let vflEntity = instance.map({ PartialInstance.instance($0) }).otherwise(vflIdentifier.map({ PartialInstance.identifier($0) }))
static let vflBoundedEntity = character("[").skipThen(vflEntity).thenSkip(character("]")).otherwise(string("|").map({ _ in PartialInstance.superview }))
static let vflDirection = optional(string("(LTR)").otherwise(string("(RTL)")))
static let nsSpacer = character("(").skipThen(string("NSSpace")).skipThen(vflExtent).thenSkip(character(")"))
static let spacer = vflExtent.otherwise(nsSpacer)
}