|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import SwiftSyntax |
| 15 | +import SwiftSyntaxBuilder |
| 16 | + |
| 17 | +let tokenSyntaxFile = SourceFile { |
| 18 | + ImportDecl( |
| 19 | + leadingTrivia: .docLineComment(copyrightHeader), |
| 20 | + path: "SwiftSyntax" |
| 21 | + ) |
| 22 | + |
| 23 | + let tokenType = SyntaxBuildableType(syntaxKind: "Token") |
| 24 | + let conformances = tokenType.generatedExpressibleAsConformances |
| 25 | + |
| 26 | + ExtensionDecl( |
| 27 | + extendedType: "TokenSyntax", |
| 28 | + inheritanceClause: TypeInheritanceClause { |
| 29 | + for conformance in conformances { |
| 30 | + InheritedType(typeName: conformance.expressibleAs) |
| 31 | + } |
| 32 | + } |
| 33 | + ) { |
| 34 | + for conformance in tokenType.elementInCollections { |
| 35 | + FunctionDecl( |
| 36 | + leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline, |
| 37 | + modifiers: [TokenSyntax.public], |
| 38 | + identifier: .identifier("create\(conformance.buildableBaseName)"), |
| 39 | + signature: FunctionSignature( |
| 40 | + input: ParameterClause(), |
| 41 | + output: conformance.buildable |
| 42 | + ) |
| 43 | + ) { |
| 44 | + ReturnStmt(expression: FunctionCallExpr(conformance.buildable) { |
| 45 | + TupleExprElement(expression: ArrayExpr { |
| 46 | + ArrayElement(expression: "self") |
| 47 | + }) |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | + for conformance in tokenType.convertibleToTypes { |
| 52 | + let param = Node.from(type: conformance).singleNonDefaultedChild |
| 53 | + FunctionDecl( |
| 54 | + leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline, |
| 55 | + modifiers: [TokenSyntax.public], |
| 56 | + identifier: .identifier("create\(conformance.buildableBaseName)"), |
| 57 | + signature: FunctionSignature( |
| 58 | + input: ParameterClause(), |
| 59 | + output: conformance.buildable |
| 60 | + ) |
| 61 | + ) { |
| 62 | + ReturnStmt(expression: FunctionCallExpr(conformance.buildable) { |
| 63 | + TupleExprElement(label: param.swiftName, expression: "self") |
| 64 | + }) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + ExtensionDecl( |
| 70 | + leadingTrivia: .newline + .docLineComment("/// `TokenSyntax` conforms to `SyntaxBuildable` and `ExprBuildable` via different paths, so we need to pick one default conversion path to create an `ExprSyntax` (and `Syntax`) from a `String`. We choose `IdentifierExpr`.") + .newline, |
| 71 | + extendedType: "TokenSyntax" |
| 72 | + ) { |
| 73 | + for buildable in ["SyntaxBuildable", "ExprBuildable"] { |
| 74 | + FunctionDecl( |
| 75 | + modifiers: [TokenSyntax.public], |
| 76 | + identifier: .identifier("create\(buildable)"), |
| 77 | + signature: FunctionSignature( |
| 78 | + input: ParameterClause(), |
| 79 | + output: buildable |
| 80 | + ) |
| 81 | + ) { |
| 82 | + ReturnStmt(expression: FunctionCallExpr("createIdentifierExpr")) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments