Skip to content

Commit b03345b

Browse files
authored
Merge pull request #735 from CodaFi/slasher-film
2 parents 40873da + da85a23 commit b03345b

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
@@ -2017,6 +2017,20 @@ extension Lexer.Cursor {
20172017
return nil
20182018
}
20192019

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

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,31 @@ public class LexerTests: XCTestCase {
483483
])
484484
}
485485
}
486+
487+
func testNotARegex() {
488+
var data =
489+
"""
490+
min(reduced.count / 2, chunkSize / 2)
491+
"""
492+
let lexemes = data.withUTF8 { buf in
493+
Lexer.lex(buf)
494+
}
495+
AssertEqualTokens(lexemes, [
496+
lexeme(.identifier, "min"),
497+
lexeme(.leftParen, "("),
498+
lexeme(.identifier, "reduced"),
499+
lexeme(.period, "."),
500+
lexeme(.identifier, "count ", trailing: 1),
501+
lexeme(.spacedBinaryOperator, "/ ", trailing: 1),
502+
lexeme(.integerLiteral, "2"),
503+
lexeme(.comma, ", ", trailing: 1),
504+
lexeme(.identifier, "chunkSize ", trailing: 1),
505+
lexeme(.spacedBinaryOperator, "/ ", trailing: 1),
506+
lexeme(.integerLiteral, "2"),
507+
lexeme(.rightParen, ")"),
508+
lexeme(.eof, ""),
509+
])
510+
}
486511
}
487512

488513
extension Lexer {

0 commit comments

Comments
 (0)