From 32764b0720535ea2a7a80519c68f422ec51601cb Mon Sep 17 00:00:00 2001 From: Marco Capano Date: Tue, 4 Sep 2018 22:26:29 +0200 Subject: [PATCH 1/6] Add support for continue, fallthrough and repeat This PR introduces new keywords in SwiftGrammar --- Sources/Splash/Grammar/SwiftGrammar.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index b6110ef..f674f2d 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -47,7 +47,8 @@ private extension SwiftGrammar { "super", "self", "set", "true", "false", "nil", "override", "where", "_", "default", "break", "#selector", "required", "willSet", "didSet", - "lazy", "subscript", "defer" + "lazy", "subscript", "defer", "continue", + "fallthrough", "repeat" ] struct PreprocessingRule: SyntaxRule { From 338e3b6ef9571e102ee4dce421c6a28dae92f149 Mon Sep 17 00:00:00 2001 From: Marco Capano Date: Wed, 5 Sep 2018 18:40:27 +0200 Subject: [PATCH 2/6] Adding "while" keyword --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index f674f2d..30835b6 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -48,7 +48,7 @@ private extension SwiftGrammar { "override", "where", "_", "default", "break", "#selector", "required", "willSet", "didSet", "lazy", "subscript", "defer", "continue", - "fallthrough", "repeat" + "fallthrough", "repeat", "while" ] struct PreprocessingRule: SyntaxRule { From 3c638adcf57f06aa15f872fa154e93acd0d7536c Mon Sep 17 00:00:00 2001 From: Marco Capano Date: Wed, 5 Sep 2018 18:43:22 +0200 Subject: [PATCH 3/6] Adding tests for repeat, while, continue and Fallthrough --- Tests/SplashTests/Tests/StatementTests.swift | 111 +++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index 514c1d2..d8bd33e 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -134,6 +134,40 @@ final class StatementTests: SyntaxHighlighterTestCase { .plainText("}") ]) } + + func testSwitchStatementWithFallthrough() { + let components = highlighter.highlight(""" + switch variable { + case .one: fallthrough + default: + callB() + } + """) + + XCTAssertEqual(components, [ + .token("switch", .keyword), + .whitespace(" "), + .plainText("variable"), + .whitespace(" "), + .plainText("{"), + .whitespace("\n"), + .token("case", .keyword), + .whitespace(" "), + .plainText("."), + .token("one", .dotAccess), + .plainText(":"), + .whitespace(" "), + .token("fallthrough", .keyword), + .whitespace("\n"), + .token("default", .keyword), + .plainText(":"), + .whitespace("\n "), + .token("callB", .call), + .plainText("()"), + .whitespace("\n"), + .plainText("}") + ]) + } func testForStatementWithStaticProperty() { let components = highlighter.highlight("for value in Enum.allCases { }") @@ -154,6 +188,83 @@ final class StatementTests: SyntaxHighlighterTestCase { .plainText("}") ]) } + + func testForStatementWithContinue() { + let components = highlighter.highlight("for value in Enum.allCases { continue }") + + XCTAssertEqual(components, [ + .token("for", .keyword), + .whitespace(" "), + .plainText("value"), + .whitespace(" "), + .token("in", .keyword), + .whitespace(" "), + .token("Enum", .type), + .plainText("."), + .token("allCases", .property), + .whitespace(" "), + .plainText("{"), + .whitespace(" "), + .token("continue",.keyword), + .plainText("}") + ]) + } + + func testRepeatWhileStatement() { + let components = highlighter.highlight(""" + var x = 5 + repeat { + print(x) + x = x - 1 + } while x > 1 + """) + + XCTAssertEqual(components, [ + .token("var", .keyword), + .whitespace(" "), + .plainText("x"), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .token("5", .number), + .whitespace("\n"), + .token("repeat", .keyword), + .whitespace(" "), + .plainText("{"), + .whitespace("\n"), + .whitespace(" "), + .whitespace(" "), + .whitespace(" "), + .whitespace(" "), + .token("print", .call), + .plainText("(x)"), + .whitespace("\n"), + .whitespace(" "), + .whitespace(" "), + .whitespace(" "), + .whitespace(" "), + .plainText("x"), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .plainText("x"), + .whitespace(" "), + .plainText("-"), + .whitespace(" "), + .token("1", .number), + .whitespace("\n"), + .plainText("}"), + .whitespace(" "), + .token("while", .keyword), + .whitespace(" "), + .plainText("x"), + .whitespace(" "), + .plainText(">"), + .whitespace(" "), + .token("1", .number) + ]) + + } func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) From b8bc4ff2c308b36e14d936eeaa51ccc3fa7fda07 Mon Sep 17 00:00:00 2001 From: Marco Capano Date: Wed, 5 Sep 2018 18:45:53 +0200 Subject: [PATCH 4/6] Updating allTests static var --- Tests/SplashTests/Tests/StatementTests.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index d8bd33e..ad94f15 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -279,7 +279,10 @@ extension StatementTests { ("testChainedIfElseStatements", testChainedIfElseStatements), ("testSwitchStatement", testSwitchStatement), ("testSwitchStatementWithAssociatedValues", testSwitchStatementWithAssociatedValues), - ("testForStatementWithStaticProperty", testForStatementWithStaticProperty) + ("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough), + ("testForStatementWithStaticProperty", testForStatementWithStaticProperty), + ("testForStatementWithContinue", testForStatementWithContinue), + ("testRepeatWhileStatement", testRepeatWhileStatement) ] } } From 2af2ec9d57326e5dc390fab5b92ec3cfb59f1f80 Mon Sep 17 00:00:00 2001 From: Marco Capano Date: Wed, 5 Sep 2018 19:11:16 +0200 Subject: [PATCH 5/6] Fixing tests This fixes a missing .whitespace in testForStatementWithContinue and handles indentation in testRepeatWhileStatement --- Tests/SplashTests/Tests/StatementTests.swift | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index ad94f15..9d6b4e5 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -206,6 +206,7 @@ final class StatementTests: SyntaxHighlighterTestCase { .plainText("{"), .whitespace(" "), .token("continue",.keyword), + .whitespace(" "), .plainText("}") ]) } @@ -231,18 +232,10 @@ final class StatementTests: SyntaxHighlighterTestCase { .token("repeat", .keyword), .whitespace(" "), .plainText("{"), - .whitespace("\n"), - .whitespace(" "), - .whitespace(" "), - .whitespace(" "), - .whitespace(" "), + .whitespace("\n "), .token("print", .call), .plainText("(x)"), - .whitespace("\n"), - .whitespace(" "), - .whitespace(" "), - .whitespace(" "), - .whitespace(" "), + .whitespace("\n "), .plainText("x"), .whitespace(" "), .plainText("="), From b2c849e93e6e749fca989d1ffc7bbb25b17278d8 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 5 Sep 2018 23:36:12 +0200 Subject: [PATCH 6/6] SwiftGrammar: Add missing keyword separator Lost in merge conflict resolution --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 6d7dcee..591aef3 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -47,7 +47,7 @@ private extension SwiftGrammar { "super", "self", "set", "true", "false", "nil", "override", "where", "_", "default", "break", "#selector", "required", "willSet", "didSet", - "lazy", "subscript", "defer", "inout", "while" + "lazy", "subscript", "defer", "inout", "while", "continue", "fallthrough", "repeat" ]