Skip to content

[Performance] Minimize RawTokenKindSubset.allCases usage #800

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
Sep 16, 2022
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
9 changes: 9 additions & 0 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ extension Parser {
case integerLiteral
case selfKeyword

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .identifier: self = .identifier
case .integerLiteral: self = .integerLiteral
case .selfKeyword: self = .selfKeyword
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .identifier: return .identifier
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftParser/DeclarationModifier.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ enum DeclarationModifier: SyntaxText, ContextualKeywords, RawTokenKindSubset {
case ${attr.swift_name} = "${attr.name}"
% end

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
% for attr in DECL_MODIFIER_KINDS:
% if attr.swift_name.endswith('Keyword'):
case .${attr.swift_name}: self = .${attr.swift_name}
% end
% end
case .identifier: self.init(rawValue: lexeme.tokenText)
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
% for attr in DECL_MODIFIER_KINDS:
Expand Down
20 changes: 16 additions & 4 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,14 @@ extension Parser {
case postfixOperator
case prefixOperator

func accepts(lexeme: Lexer.Lexeme) -> Bool {
switch self {
case .colon: return true
default: return lexeme.tokenText == "=="
init?(lexeme: Lexer.Lexeme) {
switch (lexeme.tokenKind, lexeme.tokenText) {
case (.colon, _): self = .colon
case (.spacedBinaryOperator, "=="): self = .spacedBinaryOperator
case (.unspacedBinaryOperator, "=="): self = .unspacedBinaryOperator
case (.postfixOperator, "=="): self = .postfixOperator
case (.prefixOperator, "=="): self = .prefixOperator
default: return nil
}
}

Expand Down Expand Up @@ -2045,6 +2049,14 @@ extension Parser {
case poundErrorKeyword
case poundWarningKeyword

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .poundErrorKeyword: self = .poundErrorKeyword
case .poundWarningKeyword: self = .poundWarningKeyword
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .poundErrorKeyword: return .poundErrorKeyword
Expand Down
15 changes: 15 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ extension Parser {
case arrow
case throwsKeyword

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .spacedBinaryOperator: self = .spacedBinaryOperator
case .unspacedBinaryOperator: self = .unspacedBinaryOperator
case .infixQuestionMark: self = .infixQuestionMark
case .equal: self = .equal
case .isKeyword: self = .isKeyword
case .asKeyword: self = .asKeyword
case .identifier where lexeme.tokenText == "async": self = .async
case .arrow: self = .arrow
case .throwsKeyword: self = .throwsKeyword
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .spacedBinaryOperator: return .spacedBinaryOperator
Expand Down
26 changes: 12 additions & 14 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,6 @@ extension Parser.Lookahead {
"_opaqueReturnTypeOf",
]

func isParenthesizedUnowned() -> Bool {
assert(self.atContextualKeyword("unowned") && self.peek().tokenKind == .leftParen,
"Invariant violated")

// Look ahead to parse the parenthesized expression.
var lookahead = self.lookahead()
lookahead.expectIdentifierWithoutRecovery()
guard lookahead.consume(if: .leftParen) != nil else {
return false
}
return lookahead.at(.identifier)
&& lookahead.peek().tokenKind == .rightParen
&& (lookahead.atContextualKeyword("safe") || lookahead.atContextualKeyword("unsafe"))
}
}

extension Parser.Lookahead {
Expand Down Expand Up @@ -365,6 +351,18 @@ extension Parser.Lookahead {
case poundElseKeyword
case poundElseifKeyword

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .leftParen: self = .leftParen
case .leftBrace: self = .leftBrace
case .leftSquareBracket: self = .leftSquareBracket
case .poundIfKeyword: self = .poundIfKeyword
case .poundElseKeyword: self = .poundElseKeyword
case .poundElseifKeyword: self = .poundElseifKeyword
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .leftParen: return .leftParen
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ extension Lexer.Lexeme {
}

func isContextualPunctuator(_ name: SyntaxText) -> Bool {
return Operator(self) != nil && self.tokenText == name
return Operator(lexeme: self) != nil && self.tokenText == name
}

var isKeyword: Bool {
self.tokenKind.isKeyword
}

func starts(with symbol: SyntaxText) -> Bool {
guard Operator(self) != nil || self.tokenKind.isPunctuation else {
guard Operator(lexeme: self) != nil || self.tokenKind.isPunctuation else {
return false
}

Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftParser/Patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ extension Parser {
case letKeyword
case varKeyword

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .leftParen: self = .leftParen
case .wildcardKeyword: self = .wildcardKeyword
case .identifier: self = .identifier
case .letKeyword: self = .letKeyword
case .varKeyword: self = .varKeyword
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .leftParen: return .leftParen
Expand Down Expand Up @@ -213,6 +224,17 @@ extension Parser.Lookahead {
case varKeyword
case leftParen

init?(lexeme: Lexer.Lexeme) {
switch lexeme.tokenKind {
case .identifier: self = .identifier
case .wildcardKeyword: self = .wildcardKeyword
case .letKeyword: self = .letKeyword
case .varKeyword: self = .varKeyword
case .leftParen: self = .leftParen
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .identifier: return .identifier
Expand Down
Loading