codanna 0.9.19

Code Intelligence for Large Language Models
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Swift Comprehensive Test File
// This file covers major Swift language constructs for parser testing

import Foundation
import UIKit

// MARK: - Global Constants and Variables

let globalConstant = "Global Constant"
var globalVariable = 42

// MARK: - Enumerations

enum NetworkError: Error {
    case timeout
    case unauthorized(message: String)
    case serverError(code: Int)
}

enum Direction {
    case north, south, east, west
}

enum Planet: Int {
    case mercury = 1, venus, earth, mars
}

// MARK: - Protocols

protocol Drawable {
    func draw()
    var lineWidth: Double { get set }
}

protocol Named {
    var name: String { get }
}

// MARK: - Structures

struct Point {
    var x: Double
    var y: Double

    init(x: Double, y: Double) {
        self.x = x
        self.y = y
    }

    func distance(to other: Point) -> Double {
        let dx = x - other.x
        let dy = y - other.y
        return sqrt(dx * dx + dy * dy)
    }

    mutating func moveBy(dx: Double, dy: Double) {
        x += dx
        y += dy
    }
}

struct Rectangle: Drawable {
    var origin: Point
    var size: Size
    var lineWidth: Double

    var area: Double {
        return size.width * size.height
    }

    func draw() {
        print("Drawing rectangle at \(origin)")
    }
}

struct Size {
    var width: Double
    var height: Double
}

// MARK: - Classes

class Animal: Named {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func makeSound() {
        print("Some generic animal sound")
    }

    deinit {
        print("\(name) is being deinitialized")
    }
}

class Dog: Animal {
    var breed: String

    init(name: String, age: Int, breed: String) {
        self.breed = breed
        super.init(name: name, age: age)
    }

    override func makeSound() {
        print("Woof!")
    }

    func fetch() {
        print("\(name) is fetching")
    }
}

final class Cat: Animal {
    var indoor: Bool

    init(name: String, age: Int, indoor: Bool) {
        self.indoor = indoor
        super.init(name: name, age: age)
    }

    override func makeSound() {
        print("Meow!")
    }
}

// MARK: - Generics

struct Stack<Element> {
    private var items: [Element] = []

    mutating func push(_ item: Element) {
        items.append(item)
    }

    mutating func pop() -> Element? {
        return items.popLast()
    }

    func peek() -> Element? {
        return items.last
    }

    var isEmpty: Bool {
        return items.isEmpty
    }
}

func swapValues<T>(_ a: inout T, _ b: inout T) {
    let temp = a
    a = b
    b = temp
}

// MARK: - Extensions

extension Int {
    var squared: Int {
        return self * self
    }

    func times(_ operation: () -> Void) {
        for _ in 0..<self {
            operation()
        }
    }
}

extension String {
    func reversed() -> String {
        return String(self.reversed())
    }
}

// MARK: - Closures and Higher-Order Functions

func performOperation(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) -> Int {
    return operation(a, b)
}

let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
let evens = numbers.filter { $0 % 2 == 0 }
let sum = numbers.reduce(0, +)

// MARK: - Error Handling

func fetchData(from url: String) throws -> Data {
    guard !url.isEmpty else {
        throw NetworkError.timeout
    }

    if url.contains("unauthorized") {
        throw NetworkError.unauthorized(message: "Invalid credentials")
    }

    return Data()
}

func processData() {
    do {
        let data = try fetchData(from: "https://example.com")
        print("Data received: \(data.count) bytes")
    } catch NetworkError.timeout {
        print("Request timed out")
    } catch NetworkError.unauthorized(let message) {
        print("Unauthorized: \(message)")
    } catch {
        print("Unknown error: \(error)")
    }
}

// MARK: - Optionals and Optional Chaining

class Person {
    var name: String
    var address: Address?

    init(name: String) {
        self.name = name
    }
}

class Address {
    var street: String
    var city: String

    init(street: String, city: String) {
        self.street = street
        self.city = city
    }
}

func printAddress(for person: Person?) {
    if let city = person?.address?.city {
        print("City: \(city)")
    } else {
        print("No address available")
    }
}

// MARK: - Property Wrappers

@propertyWrapper
struct Capitalized {
    private var value: String = ""

    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
}

struct User {
    @Capitalized var firstName: String
    @Capitalized var lastName: String
}

// MARK: - Async/Await

func fetchUserData(id: Int) async throws -> String {
    try await Task.sleep(nanoseconds: 1_000_000_000)
    return "User \(id)"
}

func processUsers() async {
    do {
        let user = try await fetchUserData(id: 1)
        print("Fetched: \(user)")
    } catch {
        print("Error fetching user: \(error)")
    }
}

// MARK: - Actor for Concurrency

actor Counter {
    private var value = 0

    func increment() {
        value += 1
    }

    func getValue() -> Int {
        return value
    }
}

// MARK: - Type Aliases

typealias Coordinate = (x: Double, y: Double)
typealias CompletionHandler = (Result<String, Error>) -> Void

// MARK: - Subscripts

struct Matrix {
    let rows: Int
    let columns: Int
    private var grid: [Double]

    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        self.grid = Array(repeating: 0.0, count: rows * columns)
    }

    subscript(row: Int, column: Int) -> Double {
        get {
            return grid[row * columns + column]
        }
        set {
            grid[row * columns + column] = newValue
        }
    }
}

// MARK: - Static and Class Methods

class MathUtilities {
    static let pi = 3.14159

    static func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }

    class func multiply(_ a: Int, _ b: Int) -> Int {
        return a * b
    }
}

// MARK: - Nested Types

class Vehicle {
    enum VehicleType {
        case car, truck, motorcycle
    }

    struct Specification {
        var engineSize: Double
        var horsepower: Int
    }

    var type: VehicleType
    var spec: Specification

    init(type: VehicleType, spec: Specification) {
        self.type = type
        self.spec = spec
    }
}

// MARK: - Main Function

@main
struct App {
    static func main() {
        print("Swift Comprehensive Example")

        let dog = Dog(name: "Rex", age: 3, breed: "Labrador")
        dog.makeSound()

        var stack = Stack<Int>()
        stack.push(1)
        stack.push(2)
        print("Stack top: \(stack.peek() ?? 0)")

        processData()
    }
}

// MARK: - Visibility Test Cases (added for parsing validation)

public class PublicClass {
    private var privateVar: Int = 0
    internal func internalMethod() {}
    fileprivate let fileprivateProperty = "test"
    open func openMethod() {}
}

// MARK: - Switch Statements

func describeNumber(_ num: Int) -> String {
    switch num {
    case 0:
        return "zero"
    case 1...9:
        return "single digit"
    case 10..<100:
        return "double digit"
    default:
        return "large number"
    }
}

func describeValue(_ value: Any) -> String {
    switch value {
    case let x as Int:
        return "Integer: \(x)"
    case let s as String:
        return "String: \(s)"
    case is Double:
        return "It's a Double"
    default:
        return "Unknown type"
    }
}

// MARK: - While Loops

func countDown(from start: Int) {
    var count = start
    while count > 0 {
        print(count)
        count -= 1
    }
}

// MARK: - Type Casting

func processMixedArray(_ items: [Any]) {
    for item in items {
        if let number = item as? Int {
            print("Number: \(number)")
        } else if let text = item as? String {
            print("Text: \(text)")
        }
    }

    let forcedString = "hello" as String
    let optionalInt = 42 as? Int
    let definiteInt = 42 as! Int
    _ = forcedString
    _ = optionalInt
    _ = definiteInt
}

// MARK: - Ternary Expressions

func absoluteValue(_ x: Int) -> Int {
    return x >= 0 ? x : -x
}

let isEven = 4 % 2 == 0 ? true : false

// MARK: - Dictionary Operations

let emptyDict: [String: Int] = [:]
let ages: [String: Int] = ["Alice": 30, "Bob": 25]
var mutableDict = ["key1": "value1", "key2": "value2"]

func processDict(_ dict: [String: Any]) {
    for (key, value) in dict {
        print("\(key): \(value)")
    }
}

// MARK: - Boolean Literals

let isActive = true
let isDisabled = false
let condition: Bool = true && false || true

// MARK: - Property Observers (willSet/didSet)

class ObservableValue {
    var value: Int = 0 {
        willSet {
            print("About to change from \(value) to \(newValue)")
        }
        didSet {
            print("Changed from \(oldValue) to \(value)")
        }
    }

    var name: String = "" {
        didSet {
            print("Name changed to \(name)")
        }
    }
}

// MARK: - Generic Constraints with Where Clause

func findIndex<T: Equatable>(of valueToFind: T, in array: [T]) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}

func allItemsMatch<C1: Collection, C2: Collection>(_ c1: C1, _ c2: C2) -> Bool
    where C1.Element == C2.Element, C1.Element: Equatable {
    if c1.count != c2.count {
        return false
    }
    for (item1, item2) in zip(c1, c2) {
        if item1 != item2 {
            return false
        }
    }
    return true
}

// MARK: - Associated Types in Protocols

protocol Container {
    associatedtype Item
    mutating func append(_ item: Item)
    var count: Int { get }
    subscript(i: Int) -> Item { get }
}

protocol SuffixableContainer: Container {
    associatedtype Suffix: SuffixableContainer where Suffix.Item == Item
    func suffix(_ size: Int) -> Suffix
}

struct IntStack: Container {
    typealias Item = Int
    var items: [Int] = []

    mutating func append(_ item: Int) {
        items.append(item)
    }

    var count: Int {
        return items.count
    }

    subscript(i: Int) -> Int {
        return items[i]
    }
}

// MARK: - Opaque Types (some)

protocol Shape {
    func draw() -> String
}

struct Triangle: Shape {
    var size: Int
    func draw() -> String {
        var result: [String] = []
        for length in 1...size {
            result.append(String(repeating: "*", count: length))
        }
        return result.joined(separator: "\n")
    }
}

func makeOpaqueShape() -> some Shape {
    return Triangle(size: 3)
}