Skip to content

Commit

Permalink
Stopped adding conditions to the output if they are never expected to…
Browse files Browse the repository at this point in the history
… be read
  • Loading branch information
RussBaz committed Mar 9, 2024
1 parent 7a39c79 commit eb20f0f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 71 deletions.
25 changes: 22 additions & 3 deletions Sources/Core/PageProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class PageProperties {
let fileExtension: String
let enumName: String
var lines: [LineDef] = []
var conditionTags: [String] = []
var conditionTags: [(name: String, read: Bool)] = []
var defaultValues: [String: String] = [:]
var mutableParameters: [String] = []
var modifiersPresent = false
Expand Down Expand Up @@ -51,6 +51,7 @@ public final class PageProperties {
lines.append(.init(indentation: indentation, line: .text(text)))
}

/// Use this function when the conditions are not yet known and knowing them will require a global resolution first
func append(at indentation: Int, deferred: @escaping () -> [String]) {
lines.append(.init(indentation: indentation, line: .deferred(deferred)))
}
Expand All @@ -59,6 +60,7 @@ public final class PageProperties {
lines.insert(.init(indentation: indentation, line: .text(text)), at: 0)
}

/// Use this function when the conditions are not yet known and knowing them will require a global resolution first
func prepend(at indentation: Int, deferred: @escaping () -> [String]) {
lines.insert(.init(indentation: indentation, line: .deferred(deferred)), at: 0)
}
Expand All @@ -72,8 +74,8 @@ public final class PageProperties {
}

func append(condition tag: String) {
if !conditionTags.contains(tag) {
conditionTags.append(tag)
if !conditionTags.contains(where: { $0.name == tag }) {
conditionTags.append((name: tag, read: false))
}
}

Expand All @@ -87,6 +89,23 @@ public final class PageProperties {
mutableParameters.append(name)
}

@discardableResult
func markAsRead(condition name: String) -> Bool {
for (i, c) in conditionTags.enumerated() {
guard c.name == name else { continue }
conditionTags[i] = (name: name, read: true)
return true
}

return false
}

func isRead(condition name: String) -> Bool {
guard let item = conditionTags.first(where: { $0.name == name }) else { return false }

return item.read
}

func asText(at indenation: Int = 0) -> String {
asLines(at: indenation).joined(separator: "\n")
}
Expand Down
115 changes: 83 additions & 32 deletions Sources/Core/SwiftCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ public final class SwiftCodeGenerator {
}
}
properties.append(at: indentation + 1) {
if self.properties.conditionTags.isEmpty {
return []
} else {
var result: [String] = []
guard !self.properties.conditionTags.isEmpty else { return [] }

var result: [String] = []

for tag in self.properties.conditionTags {
result.append("var \(tag) = false")
for tag in self.properties.conditionTags {
if tag.read {
result.append("var \(tag.name) = false")
}
}

result.append("")
guard !result.isEmpty else { return [] }

return result
}
result.append("")

return result
}
}

Expand Down Expand Up @@ -160,12 +162,18 @@ public final class SwiftCodeGenerator {
case .ifType:
properties.append("if \(condition.check) {", at: indentation)
case .elseIfType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn), \(condition.check) {", at: indentation)
case .elseType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn) {", at: indentation)
}

properties.append("\(cn) = true", at: indentation + 1)
properties.append(at: indentation + 1) {
guard self.properties.isRead(condition: cn) else { return [] }

return ["\(cn) = true"]
}

properties.append(at: indentation + 1) {
let signature = self.signatures.parameters(of: name, in: self.properties.name)
Expand Down Expand Up @@ -196,31 +204,45 @@ public final class SwiftCodeGenerator {
case .ifType:
properties.append("if \(check) {", at: indentation)
case .elseIfType:
properties.markAsRead(condition: name)
properties.append("if !\(name), \(check) {", at: indentation)
case .elseType:
properties.markAsRead(condition: name)
properties.append("if !\(name) {", at: indentation)
}

innerGenerator.generateBody(at: indentation + 1)

properties.append("\(name) = true", at: indentation + 1)
properties.append("} else {", at: indentation)
properties.append("\(name) = false", at: indentation + 1)
properties.append("}", at: indentation)
properties.append(at: indentation) {
guard self.properties.isRead(condition: name) else { return ["}"] }
guard type != .elseType else { return [" \(name) = true", "}"] }

return [
" \(name) = true",
"} else {",
" \(name) = false",
"}",
]
}
case let .loop(forEvery, name, contents):
guard !contents.isEmpty else { return }

let name = name ?? "previousUnnamedIfTaken"
properties.append(condition: name)
let innerGenerator = SwiftCodeGenerator(ast: contents, signatures: signatures, page: properties)

properties.append("if \(forEvery).isEmpty {", at: indentation)
properties.append("\(name) = false", at: indentation + 1)
properties.append("} else {", at: indentation)
properties.append("for (index, item) in \(forEvery).enumerated() {", at: indentation + 1)
innerGenerator.generateBody(at: indentation + 2)
properties.append("}", at: indentation + 1)
properties.append("\(name) = true", at: indentation + 1)
properties.append(at: indentation) {
guard self.properties.isRead(condition: name) else { return [] }

return ["if \(forEvery).isEmpty { \(name) = false }"]
}
properties.append("for (index, item) in \(forEvery).enumerated() {", at: indentation)
innerGenerator.generateBody(at: indentation + 1)
properties.append(at: indentation + 1) {
guard self.properties.isRead(condition: name) else { return [] }

return ["\(name) = true"]
}
properties.append("}", at: indentation)
case let .modifiers(applying: modifiers, tag: tag):
guard !modifiers.isEmpty else { return }
Expand All @@ -237,15 +259,24 @@ public final class SwiftCodeGenerator {
case .ifType:
properties.append("if \(condition.check) {", at: indentation)
case .elseIfType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn), \(condition.check) {", at: indentation)
case .elseType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn) {", at: indentation)
}
properties.append("attributes.append(to: \"\(name)\", value: \(value.codeString))", at: indentation + 1)
properties.append("\(cn) = true", at: indentation + 1)
properties.append("} else {", at: indentation)
properties.append("\(cn) = false", at: indentation + 1)
properties.append("}", at: indentation)
properties.append(at: indentation) {
guard self.properties.isRead(condition: cn) else { return ["}"] }
guard condition.type != .elseType else { return [" \(cn) = true", "}"] }

return [
" \(cn) = true",
"} else {",
" \(cn) = false",
"}",
]
}
} else {
properties.append("attributes.append(to: \"\(name)\", value: \(value.codeString))", at: indentation)
}
Expand All @@ -257,15 +288,24 @@ public final class SwiftCodeGenerator {
case .ifType:
properties.append("if \(condition.check) {", at: indentation)
case .elseIfType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn), \(condition.check) {", at: indentation)
case .elseType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn) {", at: indentation)
}
properties.append("attributes.replace(key: \"\(name)\", with: \(value.codeString))", at: indentation + 1)
properties.append("\(cn) = true", at: indentation + 1)
properties.append("} else {", at: indentation)
properties.append("\(cn) = false", at: indentation + 1)
properties.append("}", at: indentation)
properties.append(at: indentation) {
guard self.properties.isRead(condition: cn) else { return ["}"] }
guard condition.type != .elseType else { return [" \(cn) = true", "}"] }

return [
" \(cn) = true",
"} else {",
" \(cn) = false",
"}",
]
}
} else {
properties.append("attributes.replace(key: \"\(name)\", with: \(value.codeString))", at: indentation)
}
Expand All @@ -277,13 +317,24 @@ public final class SwiftCodeGenerator {
case .ifType:
properties.append("if \(condition.check) {", at: indentation)
case .elseIfType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn), \(condition.check) {", at: indentation)
case .elseType:
properties.markAsRead(condition: cn)
properties.append("if !\(cn) {", at: indentation)
}
properties.append("attributes.remove(\"\(name)\")", at: indentation + 1)
properties.append("\(cn) = true", at: indentation + 1)
properties.append("}", at: indentation)
properties.append(at: indentation) {
guard self.properties.isRead(condition: cn) else { return ["}"] }
guard condition.type != .elseType else { return [" \(cn) = true", "}"] }

return [
" \(cn) = true",
"} else {",
" \(cn) = false",
"}",
]
}
} else {
properties.append("attributes.remove(\"\(name)\")", at: indentation)
}
Expand All @@ -310,7 +361,7 @@ public final class SwiftCodeGenerator {
properties.append(at: indentation) {
if let _ = self.properties.defaultValues[name] {
["lines.append(\"\\(\(name))\")"]
} else if self.properties.conditionTags.contains(name) {
} else if self.properties.markAsRead(condition: name) {
["lines.append(\"\\(\(name))\")"]
} else if let defaultValue {
["lines.append(\"\\(\(name) ?? \"\(defaultValue)\")\")"]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/SwiftOutputBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class SwiftOutputBuilder {
let topLine = """
//
// ------------------------------
// reparse version: 0.0.7
// reparse version: 0.0.8
// ------------------------------
// This is an auto-generated file
// ------------------------------
Expand Down
61 changes: 26 additions & 35 deletions Sources/Example/pages.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ------------------------------
// reparse version: 0.0.7
// reparse version: 0.0.8
// ------------------------------
// This is an auto-generated file
// ------------------------------
Expand Down Expand Up @@ -95,53 +95,46 @@ enum Pages {
World?
""")
previousUnnamedIfTaken = true
} else {
previousUnnamedIfTaken = false
}
lines.append("""
</h1>
<ol>
""")
if context.isEmpty {
previousUnnamedIfTaken = false
} else {
for (index, item) in context.enumerated() {
lines.append("""
<li>
""")
attributes = SwiftAttributeStorage.from(attributes: ["class": .string("base", wrapper: .double)])
attributes.append(to: "class", value: .string(" rose", wrapper: .double))
lines.append("<p\(attributes)>")
lines.include(Pages.Components.HelloMe.include(req: req)) { lines in
lines.append("\(item)")
}
lines.append("""
if context.isEmpty { previousUnnamedIfTaken = false }
for (index, item) in context.enumerated() {
lines.append("""
<li>
</p>
<p>Index:
""")
lines.append("\(index)")
lines.append("""
or +1 =
""")
lines.append("\(index + 1)")
lines.append("""
</p>
</li>
""")
""")
attributes = SwiftAttributeStorage.from(attributes: ["class": .string("base", wrapper: .double)])
attributes.append(to: "class", value: .string(" rose", wrapper: .double))
lines.append("<p\(attributes)>")
lines.include(Pages.Components.HelloMe.include(req: req)) { lines in
lines.append("\(item)")
}
lines.append("""
</p>
<p>Index:
""")
lines.append("\(index)")
lines.append("""
or +1 =
""")
lines.append("\(index + 1)")
lines.append("""
</p>
</li>
""")
previousUnnamedIfTaken = true
}
if !previousUnnamedIfTaken {
lines.append("""
<li>No more heroes...</li>
""")
previousUnnamedIfTaken = true
} else {
previousUnnamedIfTaken = false
}
lines.append("""
Expand All @@ -152,7 +145,7 @@ enum Pages {
lines.append("\(req.url.string)")
lines.append("""
</p>
<button data-loading-disable class="button" data-loading-delay hx-post="/auth/logout?next=/" hx-target="body">
<button hx-target="body" hx-post="/auth/logout?next=/" data-loading-delay data-loading-disable class="button">
What's up?
</button>
</main>
Expand Down Expand Up @@ -220,8 +213,6 @@ enum Pages {
if !previousUnnamedIfTaken {
attributes.append(to: "class", value: .string("red", wrapper: .double))
previousUnnamedIfTaken = true
} else {
previousUnnamedIfTaken = false
}
lines.append("<body\(attributes)>")
lines.declare(slot: "default")
Expand Down

0 comments on commit eb20f0f

Please sign in to comment.