Skip to content

Commit e2bea30

Browse files
authored
Merge pull request #869 from CodaFi/re-gex-enter-the-gecko
Regex Literals Are Never Left Bound
2 parents f783d3d + 67a26da commit e2bea30

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

Sources/SwiftParser/Lexer.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,14 +783,14 @@ extension Lexer.Cursor {
783783
}
784784

785785
// Try lex a regex literal.
786-
if let token = self.tryLexRegexLiteral(start) {
786+
if let token = self.tryLexRegexLiteral(start, ContentStart) {
787787
return (token, [])
788788
}
789789
// Otherwise try lex a magic pound literal.
790790
return self.lexMagicPoundLiteral()
791791
case UInt8(ascii: "/"):
792792
// Try lex a regex literal.
793-
if let token = self.tryLexRegexLiteral(start) {
793+
if let token = self.tryLexRegexLiteral(start, ContentStart) {
794794
return (token, [])
795795
}
796796

@@ -2027,8 +2027,13 @@ extension Lexer.Cursor {
20272027

20282028
extension Lexer.Cursor {
20292029
mutating func tryLexRegexLiteral(
2030-
_ TokStart: Lexer.Cursor
2030+
_ TokStart: Lexer.Cursor,
2031+
_ ContentStart: Lexer.Cursor
20312032
) -> RawTokenKind? {
2033+
guard !TokStart.isLeftBound(ContentStart) else {
2034+
return nil
2035+
}
2036+
20322037
var Tmp = TokStart
20332038
var poundCount = 0
20342039
var parenCount = 0

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,6 @@ public class LexerTests: XCTestCase {
352352
lexeme(.regexLiteral, "#/abc|#def/#"),
353353
lexeme(.eof, ""),
354354
]),
355-
("#/abc|#def/", [
356-
lexeme(.pound, "#"),
357-
lexeme(.regexLiteral, "/abc|#def/"),
358-
lexeme(.eof, ""),
359-
]),
360355
("#/abc\n/#", [
361356
lexeme(.pound, "#"),
362357
lexeme(.unspacedBinaryOperator, "/"),
@@ -567,15 +562,61 @@ public class LexerTests: XCTestCase {
567562
"""
568563
n /= 2 // foo
569564
"""
570-
let lexemes = data.withUTF8 { buf in
565+
data.withUTF8 { buf in
571566
let lexemes = Lexer.lex(buf)
572567
AssertEqualTokens(lexemes, [
573568
lexeme(.identifier, "n ", trailing: 1),
574569
lexeme(.spacedBinaryOperator, "/= ", trailing: 1),
575570
lexeme(.integerLiteral, "2 ", trailing: 1),
576571
lexeme(.eof, "// foo", leading: 6),
577572
])
578-
return lexemes
573+
}
574+
}
575+
576+
do {
577+
var data =
578+
"""
579+
UIColor(white: 216.0/255.0, alpha: 44.0/255.0)
580+
"""
581+
data.withUTF8 { buf in
582+
let lexemes = Lexer.lex(buf)
583+
AssertEqualTokens(lexemes, [
584+
lexeme(.identifier, "UIColor"),
585+
lexeme(.leftParen, "("),
586+
lexeme(.identifier, "white"),
587+
lexeme(.colon, ": ", trailing: 1),
588+
lexeme(.floatingLiteral, "216.0"),
589+
lexeme(.unspacedBinaryOperator, "/"),
590+
lexeme(.floatingLiteral, "255.0"),
591+
lexeme(.comma, ", ", trailing: 1),
592+
lexeme(.identifier, "alpha"),
593+
lexeme(.colon, ": ", trailing: 1),
594+
lexeme(.floatingLiteral, "44.0"),
595+
lexeme(.unspacedBinaryOperator, "/"),
596+
lexeme(.floatingLiteral, "255.0"),
597+
lexeme(.rightParen, ")"),
598+
lexeme(.eof, ""),
599+
])
600+
}
601+
}
602+
603+
do {
604+
var data =
605+
"""
606+
#/abc|#def/
607+
"""
608+
data.withUTF8 { buf in
609+
let lexemes = Lexer.lex(buf)
610+
AssertEqualTokens(lexemes, [
611+
lexeme(.pound, "#"),
612+
lexeme(.unspacedBinaryOperator, "/"),
613+
lexeme(.identifier, "abc"),
614+
lexeme(.unspacedBinaryOperator, "|"),
615+
lexeme(.pound, "#"),
616+
lexeme(.identifier, "def"),
617+
lexeme(.postfixOperator, "/"),
618+
lexeme(.eof, ""),
619+
])
579620
}
580621
}
581622
}

0 commit comments

Comments
 (0)