Skip to content

Commit 6c287ff

Browse files
committed
Reject regex literals involving unbalanced close parentheses
1 parent e2d04d6 commit 6c287ff

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Sources/SwiftParser/Lexer.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,8 @@ extension Lexer.Cursor {
20072007
) -> RawTokenKind? {
20082008
var Tmp = TokStart
20092009
var poundCount = 0
2010+
var parenCount = 0
2011+
20102012
while Tmp.advance(matching: UInt8(ascii: "#")) != nil {
20112013
poundCount += 1
20122014
}
@@ -2072,6 +2074,15 @@ extension Lexer.Cursor {
20722074
// // delimiter.
20732075
// throw DelimiterLexError(.unprintableASCII, resumeAt: cursor.successor())
20742076

2077+
case UInt8(ascii: "("):
2078+
parenCount += 1
2079+
2080+
case UInt8(ascii: ")"):
2081+
if parenCount == 0 {
2082+
return nil
2083+
}
2084+
parenCount -= 1
2085+
20752086
default:
20762087
continue DELIMITLOOP
20772088
}

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ public class LexerTests: XCTestCase {
347347
lexeme(.pound, "#"),
348348
lexeme(.eof, ""),
349349
]),
350+
("/a)/", [
351+
lexeme(.prefixOperator, "/"),
352+
lexeme(.identifier, "a"),
353+
lexeme(.rightParen, ")"),
354+
lexeme(.postfixOperator, "/"),
355+
lexeme(.eof, ""),
356+
]),
350357
]
351358
for (fixture, expectation) in fixtures {
352359
var fixture = fixture

0 commit comments

Comments
 (0)