diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index a73939c..74e40f0 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -29,6 +29,7 @@ public struct SwiftGrammar: Grammar { NumberRule(), TypeRule(), CallRule(), + KeyPathRule(), PropertyRule(), DotAccessRule(), KeywordRule() @@ -371,6 +372,14 @@ private extension SwiftGrammar { } } + struct KeyPathRule: SyntaxRule { + var tokenType: TokenType { return .property } + + func matches(_ segment: Segment) -> Bool { + return segment.tokens.previous == "\\." + } + } + struct PropertyRule: SyntaxRule { var tokenType: TokenType { return .property } diff --git a/Tests/SplashTests/Tests/LiteralTests.swift b/Tests/SplashTests/Tests/LiteralTests.swift index 052f117..e5fb333 100644 --- a/Tests/SplashTests/Tests/LiteralTests.swift +++ b/Tests/SplashTests/Tests/LiteralTests.swift @@ -205,6 +205,24 @@ final class LiteralTests: SyntaxHighlighterTestCase { ]) } + func testKeyPathLiteral() { + let components = highlighter.highlight("let value = object[keyPath: \\.property]") + + XCTAssertEqual(components, [ + .token("let", .keyword), + .whitespace(" "), + .plainText("value"), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .plainText("object[keyPath:"), + .whitespace(" "), + .plainText("\\."), + .token("property", .property), + .plainText("]") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -223,7 +241,8 @@ extension LiteralTests { ("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral), ("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral), ("testDoubleLiteral", testDoubleLiteral), - ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators) + ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators), + ("testKeyPathLiteral", testKeyPathLiteral) ] } }