Skip to content

Commit c7d9e9c

Browse files
committed
Merge branch 'develop'
2 parents da5d178 + ebeafe2 commit c7d9e9c

25 files changed

+341
-137
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 2
4+
tab_width = 8
5+
max_line_length = 80
6+
trim_trailing_whitespace = false
7+
end_of_line = lf
8+
insert_final_newline = true

Plugins/Libraries/LighterCodeGenAST/Generation/GenExtensions.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Created by Helge Heß.
3-
// Copyright © 2022 ZeeZide GmbH.
3+
// Copyright © 2022-2024 ZeeZide GmbH.
44
//
55

66
public extension CodeGenerator {
@@ -23,10 +23,16 @@ public extension CodeGenerator {
2323
}
2424

2525
appendIndent()
26-
if value.public { append("public ") }
26+
if value.public && value.conformances.isEmpty { append("public ") }
2727
append("extension ")
2828
append(string(for: value.extendedType))
2929

30+
if !value.conformances.isEmpty {
31+
append(configuration.typeConformanceSeparator) // " : "
32+
append(value.conformances.map(string(for:))
33+
.joined(separator: configuration.identifierListSeparator))
34+
}
35+
3036
if !value.genericConstraints.isEmpty {
3137
let constraints = value.genericConstraints.map({ string(for: $0) })
3238
.joined(separator: configuration.identifierListSeparator)
@@ -42,9 +48,17 @@ public extension CodeGenerator {
4248
}
4349

4450
indent {
45-
for structure in value.structures {
51+
for typeDefinition in value.typeDefinitions {
4652
writeln()
47-
generateStruct(structure, omitPublic: value.public)
53+
generateTypeDefinition(typeDefinition, omitPublic: value.public)
54+
}
55+
56+
var lastHadComment = false
57+
if !value.typeVariables.isEmpty { writeln() }
58+
for variable in value.typeVariables {
59+
if lastHadComment { writeln() }
60+
generateInstanceVariable(variable, static: true)
61+
lastHadComment = variable.comment != nil
4862
}
4963

5064
for function in value.typeFunctions {

Plugins/Libraries/LighterCodeGenAST/Generation/GenFunctions.swift

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public extension CodeGenerator {
135135
+ ">"
136136
}()
137137
var preamble = ""
138+
if value.override { preamble.append("override ") }
138139
if value.public && !omitPublic { preamble.append("public ") }
139140
if `static` { preamble.append("static ") }
140141
if value.mutating { preamble.append("mutating ") }
@@ -271,38 +272,55 @@ public extension CodeGenerator {
271272
append(string(for: value.type))
272273

273274
let allowSingleReturn = true // make an option?
274-
if allowSingleReturn && value.statements.count == 1,
275-
value.setStatements.isEmpty,
276-
let statement = value.statements.first,
277-
case .return(let expression) = statement
278-
{
279-
append(" { ")
280-
appendExpression(expression)
281-
append(" }")
282-
appendEOL()
283-
}
284-
else {
285-
if value.setStatements.isEmpty {
286-
indentedCodeBlock {
287-
generateStatements(value.statements, allowSingleReturn: true)
275+
276+
func genStatements(_ statements: [ Statement ]) {
277+
if allowSingleReturn && statements.count == 1,
278+
let statement = statements.first,
279+
case .return(let expression) = statement
280+
{
281+
append(" { ")
282+
appendExpression(expression)
283+
append(" }")
284+
appendEOL()
285+
}
286+
else if statements.count == 1,
287+
let statement = statements.first,
288+
case .variableAssignment(let instance, let name, let value)
289+
= statement
290+
{
291+
append(" { ")
292+
if let instance = instance {
293+
append(instance) // "self" can be used here
294+
append(".")
288295
}
296+
append(tickedWhenReserved(name))
297+
append(configuration.propertyValueSeparator) // " = "
298+
appendExpression(value)
299+
append(" }")
300+
appendEOL()
289301
}
290302
else {
291303
indentedCodeBlock {
292-
appendIndent()
293-
append("set")
294-
indentedCodeBlock {
295-
generateStatements(value.setStatements, allowSingleReturn: true)
296-
}
297-
appendIndent()
298-
append("get")
299-
indentedCodeBlock {
300-
generateStatements(value.statements, allowSingleReturn: true)
301-
}
304+
generateStatements(statements, allowSingleReturn: allowSingleReturn)
302305
}
303306
}
304307
}
305308

309+
if value.setStatements.isEmpty {
310+
genStatements(value.statements)
311+
}
312+
else {
313+
indentedCodeBlock {
314+
appendIndent()
315+
append("set")
316+
genStatements(value.setStatements)
317+
318+
appendIndent()
319+
append("get")
320+
genStatements(value.statements)
321+
}
322+
}
323+
306324
if let ( major, minor ) = value.minimumSwiftVersion {
307325
assert(major >= 5)
308326
writeln("#endif // swift(>=\(major).\(minor))")

Plugins/Libraries/LighterCodeGenAST/Generation/GenStatements.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public extension CodeGenerator {
5757
append("return ")
5858
appendExpression(expression)
5959
appendEOLIfMissing()
60-
60+
case .`throw`(let expression):
61+
appendIndent()
62+
append("throw ")
63+
appendExpression(expression)
64+
appendEOLIfMissing()
65+
6166
case .forInRange(let counter, let from, let to, let stmts):
6267
appendIndent()
6368
append("for \(counter) in \(string(for: from))..<\(string(for: to))")
@@ -72,6 +77,46 @@ public extension CodeGenerator {
7277
}
7378
case .break: writeln("break")
7479

80+
case .`switch`(let expression, let pairs, let def):
81+
assert(!pairs.isEmpty)
82+
83+
func generateCaseStatements(_ statements: [ Statement ]) {
84+
if statements.count == 1, let stmt = statements.first {
85+
switch stmt {
86+
case .return(let expression):
87+
append(" return ")
88+
appendExpression(expression)
89+
appendEOLIfMissing()
90+
return
91+
case .throw(let expression):
92+
append(" throw ")
93+
appendExpression(expression)
94+
appendEOLIfMissing()
95+
return
96+
default: break
97+
}
98+
}
99+
appendEOL()
100+
indent {
101+
generateStatements(statements)
102+
}
103+
}
104+
105+
appendIndent()
106+
append("switch ")
107+
appendExpression(expression)
108+
indentedCodeBlock {
109+
for pair in pairs {
110+
appendIndent()
111+
append("case \(string(for: pair.condition)):")
112+
generateCaseStatements(pair.statements)
113+
}
114+
if !def.isEmpty {
115+
appendIndent()
116+
append("default:")
117+
generateCaseStatements(def)
118+
}
119+
}
75120
case .ifElseSwitch(let pairs):
76121
assert(!pairs.isEmpty)
77122
var isFirst = true

Plugins/Libraries/LighterCodeGenAST/Generation/GenStructures.swift

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public extension CodeGenerator {
77

88
/// public static let schema = Schema()
99
/// public var personId : Int
10-
func generateInstanceVariable(_ value : Struct.InstanceVariable,
10+
func generateInstanceVariable(_ value : TypeDefinition.InstanceVariable,
1111
`static` : Bool = false,
1212
omitPublic : Bool = false)
1313
{
@@ -77,7 +77,34 @@ public extension CodeGenerator {
7777
writePlain()
7878
}
7979
}
80-
80+
81+
/**
82+
* ```swift
83+
* public struct Person: SQLKeyedTableRecord, Identifiable {
84+
*
85+
* public static let schema = Schema()
86+
*
87+
* @inlinable
88+
* public var id : Int { personId }
89+
*
90+
* public var personId : Int
91+
* public var lastname : String
92+
* public var firstname : String?
93+
*
94+
* @inlinable
95+
* public init(personId: Int = 0, lastname: String, firstname: String? = nil) {
96+
* self.personId = personId
97+
* self.lastname = lastname
98+
* self.firstname = firstname
99+
* }
100+
* }
101+
* ```
102+
*/
103+
func generateStruct(_ value: TypeDefinition, omitPublic: Bool = false) {
104+
assert(value.kind == .struct)
105+
generateTypeDefinition(value, omitPublic: omitPublic)
106+
}
107+
81108
/**
82109
* ```swift
83110
* public struct Person: SQLKeyedTableRecord, Identifiable {
@@ -100,7 +127,8 @@ public extension CodeGenerator {
100127
* }
101128
* ```
102129
*/
103-
func generateStruct(_ value: Struct, omitPublic: Bool = false) {
130+
func generateTypeDefinition(_ value: TypeDefinition, omitPublic: Bool = false)
131+
{
104132
if !source.isEmpty { appendEOLIfMissing() }
105133

106134
if let comment = value.comment {
@@ -113,8 +141,15 @@ public extension CodeGenerator {
113141

114142
do { // header
115143
appendIndent()
144+
assert(!value.final || value.kind == .class)
116145
if value.public && !omitPublic { append("public ") }
117-
append("struct ")
146+
if value.final { append("final ") }
147+
switch value.kind {
148+
case .struct : append("struct ")
149+
case .class : append("class ")
150+
case .enum : append("enum ")
151+
case .actor : append("actor ")
152+
}
118153
append(tickedWhenReserved(value.name))
119154

120155
if !value.conformances.isEmpty {
@@ -129,12 +164,13 @@ public extension CodeGenerator {
129164

130165
if !value.typeAliases.isEmpty { writeln() }
131166
for ( name, ref ) in value.typeAliases {
132-
writeln("\(pubPrefix)typealias \(tickedWhenReserved(name))\(configuration.propertyValueSeparator)\(string(for: ref))")
167+
writeln("\(pubPrefix)typealias \(tickedWhenReserved(name))"
168+
+ "\(configuration.propertyValueSeparator)\(string(for: ref))")
133169
}
134170

135-
for structure in value.structures {
171+
for nestedType in value.nestedTypes {
136172
writeln()
137-
generateStruct(structure)
173+
generateTypeDefinition(nestedType)
138174
}
139175

140176
// Later: I'd really like to vertically align the colors and equals.

Plugins/Libraries/LighterCodeGenAST/Generation/GenUnit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Created by Helge Heß.
3-
// Copyright © 2022 ZeeZide GmbH.
3+
// Copyright © 2022-2024 ZeeZide GmbH.
44
//
55

66
public extension CodeGenerator {
@@ -18,9 +18,9 @@ public extension CodeGenerator {
1818
generateFunctionDefinition(function)
1919
}
2020

21-
for structure in unit.structures {
21+
for typeDefinition in unit.typeDefinitions {
2222
writeln()
23-
generateStruct(structure)
23+
generateTypeDefinition(typeDefinition)
2424
}
2525

2626
for ext in unit.extensions {

Plugins/Libraries/LighterCodeGenAST/Generation/ReservedWords.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Created by Helge Heß.
3-
// Copyright © 2022 ZeeZide GmbH.
3+
// Copyright © 2022-2024 ZeeZide GmbH.
44
//
55

66
// hm, this doesn't actually work:
@@ -22,9 +22,9 @@ let SwiftReservedWords : Set<String> = [
2222
"if", "guard", "else", "switch", "catch", "throw",
2323
"true", "false", "nil", "self",
2424
"as", "is",
25-
"async", "await",
2625
"typealias", "associatedtype",
2726
"associativity", "dynamic", "convenience", "required", "final",
2827
"didSet", "willSet", "get", "set",
29-
"weak", "unowned"
28+
"weak", "unowned",
29+
"actor", "async", "await", "sending"
3030
]

Plugins/Libraries/LighterCodeGenAST/Nodes/CompilationUnit.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Created by Helge Heß.
3-
// Copyright © 2022 ZeeZide GmbH.
3+
// Copyright © 2022-2024 ZeeZide GmbH.
44
//
55

66
/**
@@ -20,25 +20,35 @@ public struct CompilationUnit {
2020
public var reexports : [ String ] = []
2121

2222
/// The structures that are part of the unit.
23-
public var structures : [ Struct ]
23+
public var typeDefinitions : [ TypeDefinition ]
2424
/// The functions that are part of the unit.
25-
public var functions : [ FunctionDefinition ]
25+
public var functions : [ FunctionDefinition ]
2626
/// The extensions that are part of the unit.
27-
public var extensions : [ Extension ]
27+
public var extensions : [ Extension ]
2828

2929
/// Initialize a new CompilationUnit, only name and extensions are required.
30-
public init(name : String,
31-
imports : [ String ] = [],
32-
structures : [ Struct ] = [],
33-
functions : [ FunctionDefinition ] = [],
34-
extensions : [ Extension ] = [])
30+
public init(name : String,
31+
imports : [ String ] = [],
32+
typeDefinitions : [ TypeDefinition ] = [],
33+
functions : [ FunctionDefinition ] = [],
34+
extensions : [ Extension ] = [])
3535
{
3636
self.name = name
3737
self.imports = imports
38-
self.structures = structures
38+
self.typeDefinitions = typeDefinitions
3939
self.functions = functions
4040
self.extensions = extensions
4141
}
42+
/// Initialize a new CompilationUnit, only name and extensions are required.
43+
public init(name : String,
44+
imports : [ String ] = [],
45+
structures : [ TypeDefinition ], // legacy compat
46+
functions : [ FunctionDefinition ] = [],
47+
extensions : [ Extension ] = [])
48+
{
49+
self.init(name: name, imports: imports, typeDefinitions: structures,
50+
functions: functions, extensions: extensions)
51+
}
4252
}
4353

4454

0 commit comments

Comments
 (0)