-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVFLSyntax.swift
149 lines (128 loc) · 5.23 KB
/
VFLSyntax.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import Foundation
import CoreGraphics
import FootlessParser
// AST for VisualFormatLanguage with an extended format,
// that enables denoting `||` as Bound.layoutMargin used by VFL.edgeDecomposed
// for view layout margins, including Safe Area handling
struct VFL {
let orientation: Orientation
let firstBound: (Bound, Connection)?
let firstView: View
let views: [(Connection, View)]
var lastView: View {return views.last?.1 ?? firstView}
let lastBound: (Connection, Bound)?
enum Orientation {case h, v}
struct View {
let name: String
let predicateListWithParens: [Predicate]
}
enum Bound: String {
case superview = "|"
case layoutMargin = "||" // NOTE: custom VFL element introduced in NorthLayout. indicates bound is layoutMarginsGuide, including Safe Area + spacing
}
struct Connection {
let predicateList: PredicateList
}
typealias ViewName = String
typealias MetricName = String
enum PredicateList {
case simplePredicate(SimplePredicate)
case predicateListWithParens([Predicate])
}
enum SimplePredicate {
case metricName(MetricName)
case positiveNumber(CGFloat)
}
enum Relation {case eq, le, ge}
enum ObjectOfPredicate {
case constant(Constant)
case viewName(ViewName)
}
enum Constant {
case metricName(MetricName)
case number(CGFloat)
}
enum Priority {
case metricName(MetricName)
case number(CGFloat)
}
struct Predicate {
let relation: Relation?
let objectOfPredicate: ObjectOfPredicate
let priority: Priority?
}
}
private let identifier = {String($0)} <^> oneOrMore(char("_") <|> alphanumeric)
private let possibleNumber: Parser<Character, String> = (extend <^> optional(string("-"), otherwise: "") <*> oneOrMore(char(".") <|> digit))
private let numberParser: Parser<Character, CGFloat> = possibleNumber >>- {Double($0).map {pure(CGFloat($0))} ?? fail(.Mismatch(AnyCollection([]), "CGFloat", "not a number: \($0)"))}
extension VFL.Relation {
static var parser: Parser<Character, VFL.Relation> {
return {_ in .eq} <^> string("==")
<|> {_ in .le} <^> string("<=")
<|> {_ in .ge} <^> string(">=")
}
}
extension VFL.Priority {
static var parser: Parser<Character, VFL.Priority> {
return {.number($0)} <^> numberParser
<|> {.metricName($0)} <^> identifier
}
}
extension VFL.Constant {
static var parser: Parser<Character, VFL.Constant> {
return {.number($0)} <^> numberParser
<|> {.metricName($0)} <^> identifier
}
}
extension VFL.ObjectOfPredicate {
static var parser: Parser<Character, VFL.ObjectOfPredicate> {
return {.constant($0)} <^> VFL.Constant.parser
<|> {.viewName($0)} <^> identifier
}
}
extension VFL.Predicate {
static var parser: Parser<Character, VFL.Predicate> {
return curry(VFL.Predicate.init)
<^> optional(VFL.Relation.parser)
<*> VFL.ObjectOfPredicate.parser
<*> optional(char("@") *> VFL.Priority.parser)
}
}
extension VFL {
init(format: String) throws {
self = try parse(VFL.parser, format)
}
/// ```
/// <visualFormatString> ::=
/// (<orientation>:)?
/// (<superview><connection>)?
/// <view>(<connection><view>)*
/// (<connection><superview>)?```
static var parser: Parser<Character, VFL> {
let metricName = identifier
let positiveNumber: Parser<Character, CGFloat> = numberParser >>- {$0 > 0 ? pure($0) : fail(.Mismatch(AnyCollection([]), "positive", "negative: \($0)"))}
let simplePredicate: Parser<Character, VFL.SimplePredicate> = ({.positiveNumber($0)} <^> positiveNumber) <|> ({.metricName($0)} <^> metricName)
let predicateListWithParens: Parser<Character, [VFL.Predicate]> = extend
<^> (char("(") *> ({[$0]} <^> VFL.Predicate.parser))
<*> zeroOrMore(char(",") *> VFL.Predicate.parser) <* char(")")
let predicateList: Parser<Character, VFL.PredicateList> = {.simplePredicate($0)} <^> simplePredicate
<|> {.predicateListWithParens($0)} <^> predicateListWithParens
let bound: Parser<Character, VFL.Bound> = {_ in VFL.Bound.layoutMargin} <^> string(VFL.Bound.layoutMargin.rawValue)
<|> {_ in .superview} <^> string(VFL.Bound.superview.rawValue)
let connection = (VFL.Connection.init) <^> (char("-") *> predicateList <* char("-")
<|> {_ in VFL.PredicateList.simplePredicate(.positiveNumber(8))} <^> char("-")
<|> {_ in VFL.PredicateList.simplePredicate(.positiveNumber(0))} <^> string(""))
let view = curry(VFL.View.init)
<^> (char("[") *> identifier)
<*> optional(predicateListWithParens, otherwise: []) <* char("]")
let views = zeroOrMore({a in {(a, $0)}} <^> connection <*> view)
let orientation: Parser<Character, VFL.Orientation> = {_ in .v} <^> string("V:")
<|> {_ in .h} <^> (string("H:") <|> string(""))
return curry(VFL.init)
<^> orientation
<*> optional(tuple <^> bound <*> connection)
<*> view
<*> views
<*> optional(tuple <^> connection <*> bound)
}
}