Skip to content

Instantly share code, notes, and snippets.

@markd2
Created January 22, 2018 18:16
Show Gist options
  • Save markd2/cf48360a20928d9812beef4fa98942c2 to your computer and use it in GitHub Desktop.
Save markd2/cf48360a20928d9812beef4fa98942c2 to your computer and use it in GitHub Desktop.
libsyntax typealias creation
import SwiftSyntax
@greeble(bork) typealias Element = Int
let typeAliasKeyword = SyntaxFactory.makeTypealiasKeyword(leadingTrivia: .spaces(1),
trailingTrivia: .spaces(1))
let elementID = SyntaxFactory.makeIdentifier("Element", leadingTrivia: .zero, trailingTrivia: .spaces(1))
let equal = SyntaxFactory.makeEqualToken(leadingTrivia: Trivia.zero, trailingTrivia: .spaces(1))
let intType = SyntaxFactory.makeTypeIdentifier("Int", leadingTrivia: .zero, trailingTrivia: .zero)
let initializer = SyntaxFactory.makeTypeInitializerClause(equal: equal, value: intType)
let openParen = SyntaxFactory.makeLeftParenToken()
let closeParen = SyntaxFactory.makeRightParenToken()
let balancedTokens = [openParen, SyntaxFactory.makeIdentifier("bork", leadingTrivia: .zero,
trailingTrivia: .zero), closeParen]
let balancedTokenSyntax = SyntaxFactory.makeTokenList(balancedTokens)
let atsign = SyntaxFactory.makeAtSignToken(leadingTrivia: .zero, trailingTrivia: .zero)
let attributeName = SyntaxFactory.makeIdentifier("greeble", leadingTrivia: .zero, trailingTrivia: .zero)
let attribute = SyntaxFactory.makeAttribute(atSignToken: atsign,
attributeName: attributeName,
balancedTokens: balancedTokenSyntax)
let attributes = SyntaxFactory.makeAttributeList([attribute])
let typeAlias = SyntaxFactory.makeTypealiasDecl(attributes: attributes,
accessLevelModifier: nil,
typealiasKeyword: typeAliasKeyword,
identifier: elementID,
genericParameterClause: nil,
initializer: initializer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment