Skip to content

Commit 2f7950e

Browse files
committed
Parse @_semantics
1 parent 8280afe commit 2f7950e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extension Parser {
5555
return RawSyntax(self.parseSPIAttribute())
5656
case ._implements:
5757
return RawSyntax(self.parseImplementsAttribute())
58+
case ._semantics:
59+
return RawSyntax(self.parseSemanticsAttribute())
5860
default:
5961
break
6062
}
@@ -737,6 +739,28 @@ extension Parser {
737739
}
738740
}
739741

742+
extension Parser {
743+
mutating func parseSemanticsAttribute() -> RawAttributeSyntax {
744+
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
745+
let (unexpectedBeforeSemanticsToken, semanticsToken) = self.expectContextualKeyword("_semantics")
746+
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
747+
let label = self.parseStringLiteral()
748+
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
749+
return RawAttributeSyntax(
750+
unexpectedBeforeAtSign,
751+
atSignToken: atSign,
752+
unexpectedBeforeSemanticsToken,
753+
attributeName: semanticsToken,
754+
unexpectedBeforeLeftParen,
755+
leftParen: leftParen,
756+
argument: RawSyntax(label),
757+
unexpectedBeforeRightParen,
758+
rightParen: rightParen,
759+
tokenList: nil,
760+
arena: self.arena)
761+
}
762+
}
763+
740764
// MARK: Lookahead
741765

742766
extension Parser.Lookahead {

Tests/SwiftParserTest/Attributes.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,19 @@ final class AttributeTests: XCTestCase {
192192
}
193193
""")
194194
}
195+
196+
func testSemanticsAttribute() throws {
197+
AssertParse(
198+
"""
199+
@_semantics("constant_evaluable")
200+
func testRecursion(_ a: Int) -> Int {
201+
return a <= 0 ? 0 : testRecursion(a-1)
202+
}
203+
204+
@_semantics("test_driver")
205+
internal func interpretRecursion() -> Int {
206+
return testRecursion(10)
207+
}
208+
""")
209+
}
195210
}

0 commit comments

Comments
 (0)