Skip to content

Regex Literals Are Never Left Bound #869

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
Sep 30, 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: 8 additions & 3 deletions Sources/SwiftParser/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,14 @@ extension Lexer.Cursor {
}

// Try lex a regex literal.
if let token = self.tryLexRegexLiteral(start) {
if let token = self.tryLexRegexLiteral(start, ContentStart) {
return (token, [])
}
// Otherwise try lex a magic pound literal.
return self.lexMagicPoundLiteral()
case UInt8(ascii: "/"):
// Try lex a regex literal.
if let token = self.tryLexRegexLiteral(start) {
if let token = self.tryLexRegexLiteral(start, ContentStart) {
return (token, [])
}

Expand Down Expand Up @@ -2027,8 +2027,13 @@ extension Lexer.Cursor {

extension Lexer.Cursor {
mutating func tryLexRegexLiteral(
_ TokStart: Lexer.Cursor
_ TokStart: Lexer.Cursor,
_ ContentStart: Lexer.Cursor
) -> RawTokenKind? {
guard !TokStart.isLeftBound(ContentStart) else {
return nil
}

var Tmp = TokStart
var poundCount = 0
var parenCount = 0
Expand Down
55 changes: 48 additions & 7 deletions Tests/SwiftParserTest/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,6 @@ public class LexerTests: XCTestCase {
lexeme(.regexLiteral, "#/abc|#def/#"),
lexeme(.eof, ""),
]),
("#/abc|#def/", [
lexeme(.pound, "#"),
lexeme(.regexLiteral, "/abc|#def/"),
lexeme(.eof, ""),
]),
("#/abc\n/#", [
lexeme(.pound, "#"),
lexeme(.unspacedBinaryOperator, "/"),
Expand Down Expand Up @@ -567,15 +562,61 @@ public class LexerTests: XCTestCase {
"""
n /= 2 // foo
"""
let lexemes = data.withUTF8 { buf in
data.withUTF8 { buf in
let lexemes = Lexer.lex(buf)
AssertEqualTokens(lexemes, [
lexeme(.identifier, "n ", trailing: 1),
lexeme(.spacedBinaryOperator, "/= ", trailing: 1),
lexeme(.integerLiteral, "2 ", trailing: 1),
lexeme(.eof, "// foo", leading: 6),
])
return lexemes
}
}

do {
var data =
"""
UIColor(white: 216.0/255.0, alpha: 44.0/255.0)
"""
data.withUTF8 { buf in
let lexemes = Lexer.lex(buf)
AssertEqualTokens(lexemes, [
lexeme(.identifier, "UIColor"),
lexeme(.leftParen, "("),
lexeme(.identifier, "white"),
lexeme(.colon, ": ", trailing: 1),
lexeme(.floatingLiteral, "216.0"),
lexeme(.unspacedBinaryOperator, "/"),
lexeme(.floatingLiteral, "255.0"),
lexeme(.comma, ", ", trailing: 1),
lexeme(.identifier, "alpha"),
lexeme(.colon, ": ", trailing: 1),
lexeme(.floatingLiteral, "44.0"),
lexeme(.unspacedBinaryOperator, "/"),
lexeme(.floatingLiteral, "255.0"),
lexeme(.rightParen, ")"),
lexeme(.eof, ""),
])
}
}

do {
var data =
"""
#/abc|#def/
"""
data.withUTF8 { buf in
let lexemes = Lexer.lex(buf)
AssertEqualTokens(lexemes, [
lexeme(.pound, "#"),
lexeme(.unspacedBinaryOperator, "/"),
lexeme(.identifier, "abc"),
lexeme(.unspacedBinaryOperator, "|"),
lexeme(.pound, "#"),
lexeme(.identifier, "def"),
lexeme(.postfixOperator, "/"),
lexeme(.eof, ""),
])
}
}
}
Expand Down