Skip to content

Consider regex literals as identifier-like #946

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
Oct 14, 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
11 changes: 11 additions & 0 deletions Sources/SwiftParser/RawTokenKindSubset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ enum OperatorLike: RawTokenKindSubset {
case postfixQuestionMark
case equal
case arrow
case regexLiteral // regex literals can look like operators, e.g. '/^/'

init?(lexeme: Lexer.Lexeme) {
if let op = Operator(lexeme: lexeme) {
Expand All @@ -536,6 +537,7 @@ enum OperatorLike: RawTokenKindSubset {
case .postfixQuestionMark: self = .postfixQuestionMark
case .equal: self = .equal
case .arrow: self = .arrow
case .regexLiteral: self = .regexLiteral
default: return nil
}
}
Expand All @@ -547,6 +549,7 @@ enum OperatorLike: RawTokenKindSubset {
.postfixQuestionMark,
.equal,
.arrow,
.regexLiteral
]
}

Expand All @@ -558,6 +561,14 @@ enum OperatorLike: RawTokenKindSubset {
case .postfixQuestionMark: return .postfixQuestionMark
case .equal: return .equal
case .arrow: return .arrow
case .regexLiteral: return .regexLiteral
}
}

var precedence: TokenPrecedence? {
switch self {
case .regexLiteral: return TokenPrecedence(.spacedBinaryOperator)
default: return nil
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/SwiftParserTest/translated/OperatorDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,7 @@ final class OperatorDeclTests: XCTestCase {
)
}

func testRegexLikeOperator() {
AssertParse("prefix operator /^/")
}
}