Skip to content

Commit 6320f3e

Browse files
committed
Fixup Base Slash Literal Lexing
Break the ambiguity with bare slash regex literals that have any amount of whitespace after the slash delimiter. These now lex as spaced binary operators as appropriate. Fixes #627 rdar://98736315
1 parent 745922f commit 6320f3e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Sources/SwiftParser/Lexer.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,20 @@ extension Lexer.Cursor {
20152015
return nil
20162016
}
20172017

2018+
// For `/.../` regex literals, we need to ban space and tab at the start of
2019+
// a regex to avoid ambiguity with operator chains, e.g:
2020+
//
2021+
// Builder {
2022+
// 0
2023+
// / 1 /
2024+
// 2
2025+
// }
2026+
//
2027+
if poundCount == 0 && !Tmp.isAtEndOfFile &&
2028+
(Tmp.peek() == UInt8(ascii: " ") || Tmp.peek() == UInt8(ascii: "\t")) {
2029+
return nil
2030+
}
2031+
20182032
var isMultiline = false
20192033
while !Tmp.isAtEndOfFile {
20202034
switch Tmp.peek() {

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,31 @@ public class LexerTests: XCTestCase {
474474
])
475475
}
476476
}
477+
478+
func testNotARegex() {
479+
var data =
480+
"""
481+
min(reduced.count / 2, chunkSize / 2)
482+
"""
483+
let lexemes = data.withUTF8 { buf in
484+
Lexer.lex(buf)
485+
}
486+
AssertEqualTokens(lexemes, [
487+
lexeme(.identifier, "min"),
488+
lexeme(.leftParen, "("),
489+
lexeme(.identifier, "reduced"),
490+
lexeme(.period, "."),
491+
lexeme(.identifier, "count ", trailing: 1),
492+
lexeme(.spacedBinaryOperator, "/ ", trailing: 1),
493+
lexeme(.integerLiteral, "2"),
494+
lexeme(.comma, ", ", trailing: 1),
495+
lexeme(.identifier, "chunkSize ", trailing: 1),
496+
lexeme(.spacedBinaryOperator, "/ ", trailing: 1),
497+
lexeme(.integerLiteral, "2"),
498+
lexeme(.rightParen, ")"),
499+
lexeme(.eof, ""),
500+
])
501+
}
477502
}
478503

479504
extension Lexer {

0 commit comments

Comments
 (0)