Skip to content

Macro declaration #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,79 @@ public let DECL_NODES: [Node] = [
])
]),

Node(name: "MacroDecl",
nameForDiagnostics: "macro",
kind: "Decl",
traits: [
"IdentifiedDecl"
],
children: [
Child(name: "Attributes",
kind: "AttributeList",
isOptional: true,
collectionElementName: "Attribute"),
Child(name: "Modifiers",
kind: "ModifierList",
isOptional: true,
collectionElementName: "Modifier"),
Child(name: "MacroKeyword",
kind: "ContextualKeywordToken",
tokenChoices: [
"ContextualKeyword"
],
textChoices: [
"macro"
]),
Child(name: "Identifier",
kind: "IdentifierToken",
tokenChoices: [
"Identifier"
]),
Child(name: "GenericParameterClause",
kind: "GenericParameterClause",
isOptional: true),
Child(name: "Signature",
kind: "Syntax",
nodeChoices: [
Child(name: "FunctionLike",
kind: "FunctionSignature"),
Child(name: "ValueLike",
kind: "TypeAnnotation")
]),
Child(name: "Equal",
kind: "EqualToken",
tokenChoices: [
"Equal"
]),
Child(name: "ExternalName",
kind: "ExternalMacroName",
isOptional: true),
Child(name: "GenericWhereClause",
kind: "GenericWhereClause",
isOptional: true)
]),

Node(name: "ExternalMacroName",
nameForDiagnostics: "external macro name",
kind: "Syntax",
children: [
Child(name: "ModuleName",
kind: "IdentifierToken",
tokenChoices: [
"Identifier"
]),
Child(name: "Period",
kind: "PeriodToken",
tokenChoices: [
"Period"
]),
Child(name: "MacroTypeName",
kind: "IdentifierToken",
tokenChoices: [
"Identifier"
])
]),

Node(name: "MacroExpansionDecl",
nameForDiagnostics: "pound literal declaration",
kind: "Decl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,6 @@ public let SYNTAX_NODE_SERIALIZATION_CODES: [String: Int] = [
"OldKeyPathExpr": 287,
"MacroExpansionExpr": 288,
"MacroExpansionDecl": 289,
"MacroDecl": 290,
"ExternalMacroName": 291,
]
65 changes: 65 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ extension TokenConsumer {
return false
case .initKeyword:
return allowInitDecl
case .macroContextualKeyword:
// macro Foo ...
return subparser.peek().tokenKind == .identifier
case .some(_):
// All other decl start keywords unconditonally start a decl.
return true
Expand Down Expand Up @@ -149,6 +152,7 @@ extension Parser {
/// declaration → subscript-declaration
/// declaration → operator-declaration
/// declaration → precedence-group-declaration
/// declaration → macro-declaration
///
/// declarations → declaration declarations?
///
Expand Down Expand Up @@ -236,6 +240,8 @@ extension Parser {
return RawDeclSyntax(self.parsePrecedenceGroupDeclaration(attrs, handle))
case (.actorContextualKeyword, let handle)?:
return RawDeclSyntax(self.parseNominalTypeDeclaration(for: RawActorDeclSyntax.self, attrs: attrs, introucerHandle: handle))
case (.macroContextualKeyword, let handle)?:
return RawDeclSyntax(self.parseMacroDeclaration(attrs: attrs, introducerHandle: handle))
case nil:
if inMemberDeclList {
let isProbablyVarDecl = self.at(any: [.identifier, .wildcardKeyword]) && self.peek().tokenKind.is(any: [.colon, .equal, .comma])
Expand Down Expand Up @@ -2007,6 +2013,65 @@ extension Parser {
}
}

/// Parse a macro declaration.
mutating func parseMacroDeclaration(
attrs: DeclAttributes,
introducerHandle: RecoveryConsumptionHandle
) -> RawMacroDeclSyntax {
let (unexpectedBeforeIntroducerKeyword, introducerKeyword) = self.eat(introducerHandle)
let (unexpectedBeforeName, name) = self.expectIdentifier(keywordRecovery: true)

// Optional generic parameters.
let genericParams: RawGenericParameterClauseSyntax?
if self.currentToken.starts(with: "<") {
genericParams = self.parseGenericParameters()
} else {
genericParams = nil
}

// Macro signature, which is either value-like or function-like.
let signature: RawMacroDeclSyntax.Signature
if let colon = self.consume(if: .colon) {
let type = self.parseType()
signature = .valueLike(
RawTypeAnnotationSyntax(colon: colon, type: type, arena: self.arena))
} else {
signature = .functionLike(self.parseFunctionSignature())
}

// External macro name
let (unexpectedBeforeEqual, equal) = self.expect(.equal)
let (unexpectedBeforeModuleName, moduleName) = self.expectIdentifier()
let (unexpectedBeforePeriod, period) = self.expect(.period)
let (unexpectedBeforeMacroTypeName, macroTypeName) = self.expectIdentifier()

let externalMacroName = RawExternalMacroNameSyntax(
unexpectedBeforeModuleName, moduleName: moduleName,
unexpectedBeforePeriod, period: period,
unexpectedBeforeMacroTypeName, macroTypeName: macroTypeName,
arena: self.arena
)

// Parse a 'where' clause if present.
let whereClause: RawGenericWhereClauseSyntax?
if self.at(.whereKeyword) {
whereClause = self.parseGenericWhereClause()
} else {
whereClause = nil
}

return RawMacroDeclSyntax(
attributes: attrs.attributes, modifiers: attrs.modifiers,
unexpectedBeforeIntroducerKeyword, macroKeyword: introducerKeyword,
unexpectedBeforeName, identifier: name,
genericParameterClause: genericParams,
signature: signature, unexpectedBeforeEqual, equal: equal,
externalName: externalMacroName,
genericWhereClause: whereClause,
arena: self.arena
)
}

/// Parse a macro expansion as an declaration.
///
///
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftParser/RawTokenKindSubset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ enum DeclarationStart: RawTokenKindSubset {
case importKeyword
case initKeyword
case letKeyword
case macroContextualKeyword
case operatorKeyword
case precedencegroupKeyword
case protocolKeyword
Expand All @@ -333,6 +334,7 @@ enum DeclarationStart: RawTokenKindSubset {
init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .identifier where lexeme.tokenText == "actor": self = .actorContextualKeyword
case .identifier where lexeme.tokenText == "macro": self = .macroContextualKeyword
case .associatedtypeKeyword: self = .associatedtypeKeyword
case .caseKeyword: self = .caseKeyword
case .classKeyword: self = .classKeyword
Expand Down Expand Up @@ -367,6 +369,7 @@ enum DeclarationStart: RawTokenKindSubset {
case .importKeyword: return .importKeyword
case .initKeyword: return .initKeyword
case .letKeyword: return .letKeyword
case .macroContextualKeyword: return .identifier
case .operatorKeyword: return .operatorKeyword
case .precedencegroupKeyword: return .precedencegroupKeyword
case .protocolKeyword: return .protocolKeyword
Expand All @@ -380,6 +383,7 @@ enum DeclarationStart: RawTokenKindSubset {
var contextualKeyword: SyntaxText? {
switch self {
case .actorContextualKeyword: return "actor"
case .macroContextualKeyword: return "macro"
default: return nil
}
}
Expand All @@ -388,6 +392,7 @@ enum DeclarationStart: RawTokenKindSubset {
switch self {
case .actorContextualKeyword: return .declKeyword
case .caseKeyword: return .declKeyword
case .macroContextualKeyword: return .declKeyword
default: return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/EnumDeclSyntax>
- <doc:SwiftSyntax/OperatorDeclSyntax>
- <doc:SwiftSyntax/PrecedenceGroupDeclSyntax>
- <doc:SwiftSyntax/MacroDeclSyntax>
- <doc:SwiftSyntax/MacroExpansionDeclSyntax>

### Statements
Expand Down Expand Up @@ -354,6 +355,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/PrecedenceGroupNameElementSyntax>
- <doc:SwiftSyntax/PrecedenceGroupAssignmentSyntax>
- <doc:SwiftSyntax/PrecedenceGroupAssociativitySyntax>
- <doc:SwiftSyntax/ExternalMacroNameSyntax>
- <doc:SwiftSyntax/TokenListSyntax>
- <doc:SwiftSyntax/NonEmptyTokenListSyntax>
- <doc:SwiftSyntax/CustomAttributeSyntax>
Expand Down
Loading