|
| 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 tokensFile = SourceFile { |
| 18 | + ImportDecl(importTok: TokenSyntax.import.withLeadingTrivia(.docLineComment(copyrightHeader)), path: "SwiftSyntax") |
| 19 | + |
| 20 | + ExtensionDecl( |
| 21 | + extendedType: "TokenSyntax", |
| 22 | + modifiersBuilder: { |
| 23 | + TokenSyntax.public.withLeadingTrivia(.newlines(1) + .docLineComment("/// Namespace for commonly used tokens with default trivia.") + .newlines(1)) |
| 24 | + }, |
| 25 | + membersBuilder: { |
| 26 | + for token in SYNTAX_TOKENS { |
| 27 | + if token.isKeyword { |
| 28 | + VariableDecl( |
| 29 | + letOrVarKeyword: .var, |
| 30 | + modifiersBuilder: { |
| 31 | + if let text = token.text { |
| 32 | + TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` keyword") + .newlines(1)) |
| 33 | + } else { |
| 34 | + TokenSyntax.static |
| 35 | + } |
| 36 | + }, |
| 37 | + bindingsBuilder: { |
| 38 | + // We need to use `CodeBlock` here to ensure there is braces around. |
| 39 | + |
| 40 | + let accessor = CodeBlock { |
| 41 | + FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Keyword")) |
| 42 | + |
| 43 | + if token.requiresLeadingSpace { |
| 44 | + createWithLeadingTriviaCall() |
| 45 | + } |
| 46 | + |
| 47 | + if token.requiresTrailingSpace { |
| 48 | + createWithTrailingTriviaCall() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor) |
| 53 | + } |
| 54 | + ) |
| 55 | + } else if token.text != nil { |
| 56 | + VariableDecl( |
| 57 | + letOrVarKeyword: .var, |
| 58 | + modifiersBuilder: { |
| 59 | + if let text = token.text { |
| 60 | + TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` token") + .newlines(1)) |
| 61 | + } else { |
| 62 | + TokenSyntax.static |
| 63 | + } |
| 64 | + }, |
| 65 | + bindingsBuilder: { |
| 66 | + // We need to use `CodeBlock` here to ensure there is braces around. |
| 67 | + let accessor = CodeBlock { |
| 68 | + FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Token")) |
| 69 | + |
| 70 | + if token.requiresLeadingSpace { |
| 71 | + createWithLeadingTriviaCall() |
| 72 | + } |
| 73 | + |
| 74 | + if token.requiresTrailingSpace { |
| 75 | + createWithTrailingTriviaCall() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor) |
| 80 | + } |
| 81 | + ) |
| 82 | + } else { |
| 83 | + let signature = FunctionSignature( |
| 84 | + input: ParameterClause( |
| 85 | + parameterList: FunctionParameter( |
| 86 | + attributes: nil, |
| 87 | + firstName: .wildcard, |
| 88 | + secondName: .identifier("text"), |
| 89 | + colon: .colon, |
| 90 | + type: "String" |
| 91 | + ), |
| 92 | + rightParen: .rightParen.withTrailingTrivia(.spaces(1)) |
| 93 | + ), |
| 94 | + output: "TokenSyntax" |
| 95 | + ) |
| 96 | + |
| 97 | + FunctionDecl( |
| 98 | + identifier: .identifier("`\(token.name.withFirstCharacterLowercased)`"), |
| 99 | + signature: signature, |
| 100 | + modifiersBuilder: { |
| 101 | + if let text = token.text { |
| 102 | + TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` token")) |
| 103 | + } else { |
| 104 | + TokenSyntax.static |
| 105 | + } |
| 106 | + }, |
| 107 | + bodyBuilder: { |
| 108 | + FunctionCallExpr( |
| 109 | + MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)"), |
| 110 | + argumentListBuilder: { |
| 111 | + TupleExprElement(expression: IdentifierExpr("text")) |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + if token.requiresLeadingSpace { |
| 116 | + createWithLeadingTriviaCall() |
| 117 | + } |
| 118 | + |
| 119 | + if token.requiresTrailingSpace { |
| 120 | + createWithTrailingTriviaCall() |
| 121 | + } |
| 122 | + } |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + VariableDecl( |
| 127 | + letOrVarKeyword: .var, |
| 128 | + modifiersBuilder: { TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `eof` token") + .newlines(1)) }, |
| 129 | + bindingsBuilder: { |
| 130 | + // We need to use `CodeBlock` here to ensure there is braces around. |
| 131 | + let body = CodeBlock { |
| 132 | + FunctionCallExpr( |
| 133 | + MemberAccessExpr(base: "SyntaxFactory", name: "makeToken"), |
| 134 | + argumentListBuilder: { |
| 135 | + TupleExprElement(expression: MemberAccessExpr(name: "eof"), trailingComma: .comma) |
| 136 | + TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: MemberAccessExpr(name: "present")) |
| 137 | + } |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + createTokenSyntaxPatternBinding("eof", accessor: body) |
| 142 | + } |
| 143 | + ) |
| 144 | + VariableDecl( |
| 145 | + letOrVarKeyword: .var, |
| 146 | + modifiersBuilder: { TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `open` contextual token") + .newlines(1)) }, |
| 147 | + bindingsBuilder: { |
| 148 | + // We need to use `CodeBlock` here to ensure there is braces around. |
| 149 | + let body = CodeBlock { |
| 150 | + FunctionCallExpr( |
| 151 | + MemberAccessExpr(base: "SyntaxFactory", name: "makeContextualKeyword"), |
| 152 | + argumentListBuilder: { |
| 153 | + TupleExprElement(expression: StringLiteralExpr("open")) |
| 154 | + } |
| 155 | + ) |
| 156 | + |
| 157 | + createWithTrailingTriviaCall() |
| 158 | + } |
| 159 | + |
| 160 | + createTokenSyntaxPatternBinding("open", accessor: body) |
| 161 | + } |
| 162 | + ) |
| 163 | + } |
| 164 | + ) |
| 165 | +} |
0 commit comments