1 // Copyright (C) 2024 Rubén Beltrán del Río
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
19 static func parse(content: String) -> ParsedMap {
22 AnyMapParserStrategy(NoteParserStrategy()),
23 AnyMapParserStrategy(VertexParserStrategy()),
24 AnyMapParserStrategy(EdgeParserStrategy()),
25 AnyMapParserStrategy(BlockerParserStrategy()),
26 AnyMapParserStrategy(OpportunityParserStrategy()),
27 AnyMapParserStrategy(StageParserStrategy()),
28 AnyMapParserStrategy(GroupParserStrategy()),
30 let builder = MapBuilder()
32 let lines = content.split(whereSeparator: \.isNewline)
34 for (index, line) in lines.enumerated() {
35 for parser in parsers {
36 if parser.canHandle(line: String(line)) {
37 let (type, object) = parser.handle(
38 index: index, line: String(line), vertices: builder.vertices)
39 builder.addObjectToMap(type: type, object: object)
45 return builder.build()
51 struct MapParsingPatterns {
52 static let vertex = try! NSRegularExpression(
54 "^([^\\(\\[\\]]*?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?[\\s]*$",
55 options: [.caseInsensitive, .anchorsMatchLines])
56 static let edge = try! NSRegularExpression(
57 pattern: "^(.+?)[\\s]*-([->])[\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
58 static let blocker = try! NSRegularExpression(
59 pattern: "^\\[(Blocker)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
60 static let opportunity = try! NSRegularExpression(
61 pattern: "^\\[(Evolution)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)",
62 options: [.caseInsensitive, .anchorsMatchLines])
63 static let note = try! NSRegularExpression(
65 "^\\[(Note)\\][\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(.*)",
66 options: [.caseInsensitive, .anchorsMatchLines])
67 static let stage = try! NSRegularExpression(
68 pattern: "^\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)",
69 options: [.caseInsensitive, .anchorsMatchLines])
70 static let group = try! NSRegularExpression(
71 pattern: "^\\[(Group)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
75 let vertices: [Vertex]
77 let blockers: [Blocker]
78 let opportunities: [Opportunity]
81 let groups: [[Vertex]]
83 static let empty: ParsedMap = ParsedMap(
84 vertices: [], edges: [], blockers: [], opportunities: [], notes: [], stages: defaultDimensions,
88 struct Vertex: Identifiable, Hashable {
92 var shape: VertexShape = .circle
101 enum VertexShape: String {
111 let destination: CGPoint
117 let position: CGPoint
123 let destination: CGPoint
126 struct StageDimensions {
128 let dimensions: CGFloat
131 private let defaultDimensions: [CGFloat] = [
137 // MARK: - MapParserStrategy protocol
139 protocol MapParserStrategy {
140 func canHandle(line: String) -> Bool
141 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
144 struct AnyMapParserStrategy: MapParserStrategy {
146 private let base: MapParserStrategy
148 init<T: MapParserStrategy>(_ base: T) {
152 func canHandle(line: String) -> Bool {
153 return base.canHandle(line: line)
155 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
156 return base.handle(index: index, line: line, vertices: vertices)
160 // MARK: - Map Builder
163 var vertices: [String: Vertex] = [:]
164 private var edges: [MapEdge] = []
165 private var blockers: [Blocker] = []
166 private var opportunities: [Opportunity] = []
167 private var notes: [Note] = []
168 private var stages: [CGFloat] = defaultDimensions
169 private var groups: [[Vertex]] = []
171 func addObjectToMap(type: Any.Type, object: Any) {
172 if type == Vertex.self {
173 let vertex = object as! Vertex
174 vertices[vertex.label] = vertex
177 if type == MapEdge.self {
178 let edge = object as! MapEdge
182 if type == Blocker.self {
183 let blocker = object as! Blocker
184 blockers.append(blocker)
187 if type == Opportunity.self {
188 let opportunity = object as! Opportunity
189 opportunities.append(opportunity)
192 if type == Note.self {
193 let note = object as! Note
197 if type == StageDimensions.self {
198 let stageDimensions = object as! StageDimensions
199 stages[stageDimensions.index] = stageDimensions.dimensions
202 if type == [Vertex].self {
203 let group = object as! [Vertex]
208 func build() -> ParsedMap {
209 let mappedVertices = vertices.map { label, vertex in return vertex }
211 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities,
213 stages: stages, groups: groups)