|
| 1 | +// |
| 2 | +// github.com/screensailor 2022 |
| 3 | +// |
| 4 | + |
| 5 | +import Lexicon |
| 6 | +import UniformTypeIdentifiers |
| 7 | + |
| 8 | +public extension UTType { |
| 9 | + static var typescript = UTType(filenameExtension: "ts", conformingTo: .sourceCode)! |
| 10 | +} |
| 11 | + |
| 12 | +public enum Generator: CodeGenerator { |
| 13 | + |
| 14 | + // TODO: prefixes? |
| 15 | + |
| 16 | + public static let utType = UTType.typescript |
| 17 | + public static let command = "ts" |
| 18 | + |
| 19 | + public static func generate(_ json: Lexicon.Graph.JSON) throws -> Data { |
| 20 | + return Data(json.ts().utf8) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +private extension Lexicon.Graph.JSON { |
| 25 | + |
| 26 | + func ts() -> String { |
| 27 | + return """ |
| 28 | +interface I { } |
| 29 | +
|
| 30 | +// L |
| 31 | +class L implements I { |
| 32 | + protected id: string; |
| 33 | + constructor(id: string) { |
| 34 | + this.id = id; |
| 35 | + } |
| 36 | + get ['__']() { |
| 37 | + return this.id; |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +// MARK: generated types |
| 42 | +\(classes.flatMap{ $0.ts(prefix: ("L", "I"), classes: classes) }.joined(separator: "\n")) |
| 43 | +const \(name) = new L_\(name)("\(name)"); |
| 44 | +
|
| 45 | +""" |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +private extension Lexicon.Graph.Node.Class.JSON { |
| 50 | + |
| 51 | + // TODO: make this more readable |
| 52 | + |
| 53 | + func ts(prefix: (class: String, protocol: String), classes: [Lexicon.Graph.Node.Class.JSON]) -> [String] { |
| 54 | + |
| 55 | + guard mixin == nil else { |
| 56 | + return [] |
| 57 | + } |
| 58 | + |
| 59 | + var lines: [String] = [] |
| 60 | + let T = id.idToClassSuffix |
| 61 | + let (L, I) = prefix |
| 62 | + |
| 63 | + if let protonym = protonym { |
| 64 | + lines += "type \(L)_\(T) = \(L)_\(protonym.idToClassSuffix)" |
| 65 | + return lines |
| 66 | + } |
| 67 | + |
| 68 | + lines += "class \(L)_\(T) extends \(L) implements \(I)_\(T) {" |
| 69 | + |
| 70 | + let supertype = supertype? |
| 71 | + .replacingOccurrences(of: "_", with: "__") |
| 72 | + .replacingOccurrences(of: ".", with: "_") |
| 73 | + .replacingOccurrences(of: "__&__", with: ", I_") |
| 74 | + |
| 75 | + if hasNoProperties { |
| 76 | + if supertype != nil { |
| 77 | + let superChildren = classes.filter {$0.id == supertype}.first?.children |
| 78 | + |
| 79 | + for child in superChildren ?? [] { |
| 80 | + lines += " \(child)!: \(L)_\(supertype!)_\(child);" |
| 81 | + } |
| 82 | + lines += "}" |
| 83 | + |
| 84 | + lines += "type \(I)_\(T) = I_\(supertype!);" |
| 85 | + } else { |
| 86 | + lines += "}" |
| 87 | + lines += "type \(I)_\(T) = I;" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + guard hasProperties else { |
| 92 | + return lines |
| 93 | + } |
| 94 | + |
| 95 | + for t in type ?? [] { |
| 96 | + let subClass = classes.filter{$0.id == t}.first |
| 97 | + for child in subClass?.children ?? [] { |
| 98 | + let id = "L.\(t).\(child)" |
| 99 | + lines += " \(child)!: \(id.idToClassSuffix);" |
| 100 | + } |
| 101 | + if let keys = subClass?.synonyms?.keys { |
| 102 | + for synonym in keys { |
| 103 | + let id = "L.\(t).\(synonym)" |
| 104 | + lines += " \(synonym)!: \(id.idToClassSuffix);" |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + for child in children ?? [] { |
| 111 | + let id = "\(id).\(child)" |
| 112 | + lines += " \(child) = new \(L)_\(id.idToClassSuffix)(`${this.__}.\(child)`);" |
| 113 | + } |
| 114 | + |
| 115 | + for (synonym, protonym) in (synonyms?.sortedByLocalizedStandard(by: \.key) ?? []) { |
| 116 | + lines += " \(synonym) = this.\(protonym);" |
| 117 | + } |
| 118 | + lines += "}" |
| 119 | + |
| 120 | + lines += "interface \(I)_\(T) extends \(I)\(supertype.map{ "_\($0)" } ?? "") {" |
| 121 | + |
| 122 | + for child in children ?? [] { |
| 123 | + let id = "\(id).\(child)" |
| 124 | + lines += " \(child): \(I)_\(id.idToClassSuffix);" |
| 125 | + } |
| 126 | + lines += "}" |
| 127 | + return lines |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +private extension String { |
| 132 | + var idToClassSuffix: String { |
| 133 | + replacingOccurrences(of: "_", with: "__") |
| 134 | + .replacingOccurrences(of: ".", with: "_") |
| 135 | + .replacingOccurrences(of: "_&_", with: "_") |
| 136 | + } |
| 137 | +} |
0 commit comments