Skip to content

Commit f1ef59f

Browse files
committed
[Parser] Stop parsing #colorLiteral, #fileLiteral, and #imageLiteral
Remove the "object literal" parsing grammar entirely, sending `#colorLiteral`, `#fileLiteral`, and `#imageLiteral` through the normal macro-expansion expressions. Implement `FileLiteral` and `ImageLiteral` macros in the example macro system.
1 parent 84ff3a1 commit f1ef59f

File tree

6 files changed

+39
-50
lines changed

6 files changed

+39
-50
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,11 +1104,6 @@ extension Parser {
11041104
case (.poundKeyPathKeyword, _)?:
11051105
return RawExprSyntax(self.parseObjectiveCKeyPathExpression())
11061106

1107-
case (.poundColorLiteralKeyword, _)?,
1108-
(.poundImageLiteralKeyword, _)?,
1109-
(.poundFileLiteralKeyword, _)?:
1110-
return RawExprSyntax(self.parseObjectLiteralExpression())
1111-
11121107
case (.leftBrace, _)?: // expr-closure
11131108
return RawExprSyntax(self.parseClosureExpression())
11141109
case (.period, let handle)?, //=.foo
@@ -1170,32 +1165,6 @@ extension Parser {
11701165
}
11711166
}
11721167

1173-
extension Parser {
1174-
/// Parse an identifier as an expression.
1175-
///
1176-
/// Grammar
1177-
/// =======
1178-
///
1179-
/// playground-literal → '#colorLiteral' '(' red ':' expression , green ':' expression , blue ':' expression , alpha ':' expression )
1180-
/// playground-literal → '#fileLiteral' '(' resourceName ':' expression ')'
1181-
/// playground-literal → '#imageLiteral' '(' resourceName ':' expression ')'
1182-
@_spi(RawSyntax)
1183-
public mutating func parseObjectLiteralExpression() -> RawObjectLiteralExprSyntax {
1184-
let poundKeyword = self.consumeAnyToken()
1185-
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
1186-
let arguments = self.parseArgumentListElements(pattern: .none)
1187-
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
1188-
return RawObjectLiteralExprSyntax(
1189-
identifier: poundKeyword,
1190-
unexpectedBeforeLeftParen,
1191-
leftParen: leftParen,
1192-
arguments: RawTupleExprElementListSyntax(elements: arguments, arena: self.arena),
1193-
unexpectedBeforeRightParen,
1194-
rightParen: rightParen,
1195-
arena: self.arena)
1196-
}
1197-
}
1198-
11991168
extension Parser {
12001169
/// Parse a macro expansion as an expression.
12011170
///

Sources/SwiftParser/Lexer.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,9 +1553,6 @@ extension Lexer.Cursor {
15531553
case "endif": kind = .poundEndifKeyword
15541554
case "available": kind = .poundAvailableKeyword
15551555
case "unavailable": kind = .poundUnavailableKeyword
1556-
case "fileLiteral": kind = .poundFileLiteralKeyword
1557-
case "imageLiteral": kind = .poundImageLiteralKeyword
1558-
case "colorLiteral": kind = .poundColorLiteralKeyword
15591556
case "_hasSymbol": kind = .poundHasSymbolKeyword
15601557
default:
15611558
// If we didn't find a match, then just return `.pound`. This is highly

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,9 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
738738
case nilKeyword
739739
case period
740740
case pound
741-
case poundColorLiteralKeyword
742741
case poundFileIDKeyword
743742
case poundFileKeyword
744-
case poundFileLiteralKeyword
745743
case poundFilePathKeyword
746-
case poundImageLiteralKeyword
747744
case poundKeyPathKeyword
748745
case poundSelectorKeyword
749746
case prefixPeriod
@@ -774,12 +771,9 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
774771
case .nilKeyword: self = .nilKeyword
775772
case .period: self = .period
776773
case .pound: self = .pound
777-
case .poundColorLiteralKeyword: self = .poundColorLiteralKeyword
778774
case .poundFileIDKeyword: self = .poundFileIDKeyword
779775
case .poundFileKeyword: self = .poundFileKeyword
780-
case .poundFileLiteralKeyword: self = .poundFileLiteralKeyword
781776
case .poundFilePathKeyword: self = .poundFilePathKeyword
782-
case .poundImageLiteralKeyword: self = .poundImageLiteralKeyword
783777
case .poundKeyPathKeyword: self = .poundKeyPathKeyword
784778
case .poundSelectorKeyword: self = .poundSelectorKeyword
785779
case .prefixPeriod: self = .prefixPeriod
@@ -813,12 +807,9 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
813807
case .nilKeyword: return .nilKeyword
814808
case .period: return .period
815809
case .pound: return .pound
816-
case .poundColorLiteralKeyword: return .poundColorLiteralKeyword
817810
case .poundFileIDKeyword: return .poundFileIDKeyword
818811
case .poundFileKeyword: return .poundFileKeyword
819-
case .poundFileLiteralKeyword: return .poundFileLiteralKeyword
820812
case .poundFilePathKeyword: return .poundFilePathKeyword
821-
case .poundImageLiteralKeyword: return .poundImageLiteralKeyword
822813
case .poundKeyPathKeyword: return .poundKeyPathKeyword
823814
case .poundSelectorKeyword: return .poundSelectorKeyword
824815
case .prefixPeriod: return .prefixPeriod

Sources/SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct FunctionMacro: ExpressionMacro {
145145
}
146146
}
147147

148-
struct Stringify: ExpressionMacro {
148+
struct StringifyMacro: ExpressionMacro {
149149
static var name: String { "stringify" }
150150

151151
static func apply(
@@ -160,8 +160,36 @@ struct Stringify: ExpressionMacro {
160160
}
161161
}
162162

163-
struct ColorLiteral: ExpressionMacro {
164-
static var name: String { "myColorLiteral" }
163+
struct ColorLiteralMacro: ExpressionMacro {
164+
static var name: String { "colorLiteral" }
165+
166+
static func apply(
167+
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
168+
) -> MacroResult<ExprSyntax> {
169+
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
170+
if let leadingTrivia = macro.leadingTrivia {
171+
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
172+
}
173+
return MacroResult(initSyntax)
174+
}
175+
}
176+
177+
struct FileLiteralMacro: ExpressionMacro {
178+
static var name: String { "fileLiteral" }
179+
180+
static func apply(
181+
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
182+
) -> MacroResult<ExprSyntax> {
183+
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
184+
if let leadingTrivia = macro.leadingTrivia {
185+
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
186+
}
187+
return MacroResult(initSyntax)
188+
}
189+
}
190+
191+
struct ImageLiteralMacro: ExpressionMacro {
192+
static var name: String { "imageLiteral" }
165193

166194
static func apply(
167195
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
@@ -177,11 +205,13 @@ struct ColorLiteral: ExpressionMacro {
177205
extension MacroSystem {
178206
public static var exampleSystem: MacroSystem {
179207
var macroSystem = MacroSystem()
208+
try! macroSystem.add(ColorLiteralMacro.self)
180209
try! macroSystem.add(ColumnMacro.self)
210+
try! macroSystem.add(FileLiteralMacro.self)
181211
try! macroSystem.add(FunctionMacro.self)
212+
try! macroSystem.add(ImageLiteralMacro.self)
182213
try! macroSystem.add(LineMacro.self)
183-
try! macroSystem.add(Stringify.self)
184-
try! macroSystem.add(ColorLiteral.self)
214+
try! macroSystem.add(StringifyMacro.self)
185215
return macroSystem
186216
}
187217
}

Sources/SwiftSyntaxMacros/SwiftSyntaxMacros.docc/SwiftSyntaxMacros.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ Use `swift-parser-cli` to test out your macros and see what kind of syntactic tr
2525
A number of example macros have been implemented in the file `MacroSytem+Examples.swift`. They include:
2626

2727
* `#line`: Implements the `#line` expression.
28+
* `#column`: Implements the `#column` expression.
29+
* `#function`: Implements the `#function` expression.
2830
* `#stringify(x)`: returns both the value `x` and the string form of its expression, as a tuple.
29-
* `#myColorLiteral(red: <value>, green: <value>, blue: <value>, alpha: <value>)`: behaves like the `#colorLiteral` expression.
31+
* `#colorLiteral(red: <value>, green: <value>, blue: <value>, alpha: <value>)`: Implements the `#colorLiteral` expression.
3032

3133
New example macros can be added by defining new types that conform to the appropriate `*Macro` protocols, and adding those macros to the example macro system created by `MacroSystem.exampleSystem`. Have fun!
3234

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class MacroSystemTests: XCTestCase {
1111
#line
1212
let a = (#line)
1313
let b = #stringify(x + y)
14-
#myColorLiteral(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
14+
#colorLiteral(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
1515
let c = #column
1616
"""
1717
let converter = SourceLocationConverter(file: "test.swift", tree: sf)

0 commit comments

Comments
 (0)