Skip to content

Implement consume x operator with the accepted SE-0366 syntax. #1379

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

Merged
merged 1 commit into from
Mar 2, 2023
Merged
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
1 change: 1 addition & 0 deletions CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public let KEYWORDS: [KeywordSpec] = [
KeywordSpec("case", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("catch", isLexerClassified: true, requiresLeadingSpace: true),
KeywordSpec("class", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("consume"),
KeywordSpec("consuming"),
KeywordSpec("continue", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("convenience"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public let EXPR_NODES: [Node] = [
kind: "Expr",
children: [
Child(name: "MoveKeyword",
kind: .token(choices: [.keyword(text: "_move")])),
kind: .token(choices: [.keyword(text: "_move"), .keyword(text: "consume")])),
Child(name: "Expression",
kind: .node(kind: "Expr"))
]),
Expand Down
28 changes: 28 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,34 @@ extension Parser {
arena: self.arena
)
)

case (.consumeKeyword, let handle)?:
// `consume` is only contextually a keyword, if it's followed by an
// identifier or keyword on the same line.
let next = peek()
if next.isAtStartOfLine {
fallthrough
}
if next.rawTokenKind != .identifier,
next.rawTokenKind != .dollarIdentifier,
next.rawTokenKind != .keyword
{
fallthrough
}

let consumeTok = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor,
forDirective: forDirective,
pattern: pattern
)
return RawExprSyntax(
RawMoveExprSyntax(
moveKeyword: consumeTok,
expression: sub,
arena: self.arena
)
)
case nil:
return self.parseUnaryExpression(flavor, forDirective: forDirective, pattern: pattern)
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,12 @@ extension Parser.Lookahead {
return !self.peek().isAtStartOfLine
}
case .forgetKeyword?:
switch peek().rawTokenKind {
let next = peek()
// The thing to be forgotten must be on the same line as `forget`.
if next.isAtStartOfLine {
return false
}
switch next.rawTokenKind {
case .identifier, .keyword:
// Since some identifiers like "self" are classified as keywords,
// we want to recognize those too, to handle "forget self". We also
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,15 @@ enum AwaitTryMove: TokenSpecSet {
case _moveKeyword
case _borrowKeyword
case tryKeyword
case consumeKeyword

init?(lexeme: Lexer.Lexeme) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await): self = .awaitKeyword
case TokenSpec(._move): self = ._moveKeyword
case TokenSpec(._borrow): self = ._borrowKeyword
case TokenSpec(.try): self = .tryKeyword
case TokenSpec(.consume): self = .consumeKeyword
default: return nil
}
}
Expand All @@ -529,6 +531,7 @@ enum AwaitTryMove: TokenSpecSet {
case .awaitKeyword: return .keyword(.await)
case ._moveKeyword: return .keyword(._move)
case ._borrowKeyword: return .keyword(._borrow)
case .consumeKeyword: return .keyword(.consume)
case .tryKeyword: return .keyword(.try)
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/Keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public enum Keyword: UInt8, Hashable {
case `case`
case `catch`
case `class`
case consume
case consuming
case `continue`
case convenience
Expand Down Expand Up @@ -425,6 +426,8 @@ public enum Keyword: UInt8, Hashable {
self = ._linear
case "_modify":
self = ._modify
case "consume":
self = .consume
case "default":
self = .`default`
case "dynamic":
Expand Down Expand Up @@ -921,6 +924,7 @@ public enum Keyword: UInt8, Hashable {
"case",
"catch",
"class",
"consume",
"consuming",
"continue",
"convenience",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4328,7 +4328,7 @@ public struct MoveExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
public init<E: ExprSyntaxProtocol>(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeMoveKeyword: UnexpectedNodesSyntax? = nil,
moveKeyword: TokenSyntax = .keyword(._move),
moveKeyword: TokenSyntax,
_ unexpectedBetweenMoveKeywordAndExpression: UnexpectedNodesSyntax? = nil,
expression: E,
_ unexpectedAfterExpression: UnexpectedNodesSyntax? = nil,
Expand Down
5 changes: 4 additions & 1 deletion gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
Node('MoveExpr', name_for_diagnostics="'_move' expression", kind='Expr',
children=[
Child('MoveKeyword', kind='KeywordToken',
token_choices=['KeywordToken|_move']),
token_choices=[
'KeywordToken|_move',
'KeywordToken|consume',
]),
Child('Expression', kind='Expr'),
]),

Expand Down