From e2547c358e6fbf9781d1804b531c482865c22c9d Mon Sep 17 00:00:00 2001 From: kenji Date: Fri, 29 Sep 2023 14:28:33 +0200 Subject: [PATCH] Add swift 5.9 support --- .../pmd/lang/swift/antlr4/Swift.g4 | 5 + .../pmd/cpd/SwiftTokenizerTest.java | 5 + .../lang/swift/cpd/testdata/Swift5.9.swift | 37 ++++ .../pmd/lang/swift/cpd/testdata/Swift5.9.txt | 199 ++++++++++++++++++ 4 files changed, 246 insertions(+) create mode 100644 pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.swift create mode 100644 pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.txt diff --git a/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/antlr4/Swift.g4 b/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/antlr4/Swift.g4 index 773003f4efe..47ac24c3616 100644 --- a/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/antlr4/Swift.g4 +++ b/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/antlr4/Swift.g4 @@ -191,6 +191,7 @@ declaration | constantDeclaration ';'? | variableDeclaration ';'? | typealiasDeclaration ';'? + | macroDeclaration ';'? | functionDeclaration ';'? | enumDeclaration ';'? | structDeclaration ';'? @@ -277,6 +278,10 @@ typealiasHead : attributes? accessLevelModifier? 'typealias' typealiasName gener typealiasName : identifier ; typealiasAssignment : '=' sType ; +// GRAMMAR OF A MACRO DECLARATION + +macroDeclaration: '#' identifier ; + // GRAMMAR OF A FUNCTION DECLARATION /* HACK: functionBody? is intentionally not used to force the parser to try and match a functionBody first diff --git a/pmd-swift/src/test/java/net/sourceforge/pmd/cpd/SwiftTokenizerTest.java b/pmd-swift/src/test/java/net/sourceforge/pmd/cpd/SwiftTokenizerTest.java index c5bf7ab8063..9dcb4448d62 100644 --- a/pmd-swift/src/test/java/net/sourceforge/pmd/cpd/SwiftTokenizerTest.java +++ b/pmd-swift/src/test/java/net/sourceforge/pmd/cpd/SwiftTokenizerTest.java @@ -62,6 +62,11 @@ public void testSwift56() { doTest("Swift5.6"); } + @Test + public void testSwift59() { + doTest("Swift5.9"); + } + @Test public void testStackoverflowOnLongLiteral() { doTest("Issue628"); diff --git a/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.swift b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.swift new file mode 100644 index 00000000000..a9be749a495 --- /dev/null +++ b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.swift @@ -0,0 +1,37 @@ +import SwiftUI + +struct ContentView: View { + var body: some View { + TabBarView() + } +} + +#Preview { + ContentView() +} + +// https://www.swift.org/blog/swift-5.9-released/ +// Macors +let _: Font = #fontLiteral(name: "SF Mono", size: 14, weight: .regular) + +// Parameter packs +func all(_ optional: repeat (each Wrapped)?) -> (repeat each Wrapped)? + +func useAll() { + if let (int, double, string, bool) = all(optionalInt, optionalDouble, optionalString, optionalBool) { + print(int, double, string, bool) + } + else { + print("got a nil") + } +} + +// return value on if/else if/else +statusBar.text = if !hasConnection { "Disconnected" } + else if let error = lastError { error.localizedDescription } + else { "Ready" } + +// https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/ +@attached(member) +@attached(conformance) +public macro OptionSet() = #externalMacro(module: "SwiftMacros", type: "OptionSetMacro") diff --git a/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.txt b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.txt new file mode 100644 index 00000000000..a38fba7d091 --- /dev/null +++ b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/cpd/testdata/Swift5.9.txt @@ -0,0 +1,199 @@ + [Image] or [Truncated image[ Bcol Ecol +L1 + [import] 1 6 + [SwiftUI] 8 14 +L3 + [struct] 1 6 + [ContentView] 8 18 + [:] 19 19 + [View] 21 24 + [{] 26 26 +L4 + [var] 5 7 + [body] 9 12 + [:] 13 13 + [some] 15 18 + [View] 20 23 + [{] 25 25 +L5 + [TabBarView] 9 18 + [(] 19 19 + [)] 20 20 +L6 + [}] 5 5 +L7 + [}] 1 1 +L9 + [#] 1 1 + [Preview] 2 8 + [{] 10 10 +L10 + [ContentView] 5 15 + [(] 16 16 + [)] 17 17 +L11 + [}] 1 1 +L15 + [let] 1 3 + [_] 5 5 + [:] 6 6 + [Font] 8 11 + [=] 13 13 + [#] 15 15 + [fontLiteral] 16 26 + [(] 27 27 + [name] 28 31 + [:] 32 32 + ["SF Mono"] 34 42 + [,] 43 43 + [size] 45 48 + [:] 49 49 + [14] 51 52 + [,] 53 53 + [weight] 55 60 + [:] 61 61 + [.] 63 63 + [regular] 64 70 + [)] 71 71 +L18 + [func] 1 4 + [all] 6 8 + [<] 9 9 + [each] 10 13 + [Wrapped] 15 21 + [>] 22 22 + [(] 23 23 + [_] 24 24 + [optional] 26 33 + [:] 34 34 + [repeat] 36 41 + [(] 43 43 + [each] 44 47 + [Wrapped] 49 55 + [)] 56 56 + [?] 57 57 + [)] 58 58 + [->] 60 61 + [(] 63 63 + [repeat] 64 69 + [each] 71 74 + [Wrapped] 76 82 + [)] 83 83 + [?] 84 84 +L20 + [func] 1 4 + [useAll] 6 11 + [(] 12 12 + [)] 13 13 + [{] 15 15 +L21 + [if] 5 6 + [let] 8 10 + [(] 12 12 + [int] 13 15 + [,] 16 16 + [double] 18 23 + [,] 24 24 + [string] 26 31 + [,] 32 32 + [bool] 34 37 + [)] 38 38 + [=] 40 40 + [all] 42 44 + [(] 45 45 + [optionalInt] 46 56 + [,] 57 57 + [optionalDouble] 59 72 + [,] 73 73 + [optionalString] 75 88 + [,] 89 89 + [optionalBool] 91 102 + [)] 103 103 + [{] 105 105 +L22 + [print] 5 9 + [(] 10 10 + [int] 11 13 + [,] 14 14 + [double] 16 21 + [,] 22 22 + [string] 24 29 + [,] 30 30 + [bool] 32 35 + [)] 36 36 +L23 + [}] 5 5 +L24 + [else] 5 8 + [{] 10 10 +L25 + [print] 5 9 + [(] 10 10 + ["got a nil"] 11 21 + [)] 22 22 +L26 + [}] 5 5 +L27 + [}] 1 1 +L30 + [statusBar] 1 9 + [.] 10 10 + [text] 11 14 + [=] 16 16 + [if] 18 19 + [!] 21 21 + [hasConnection] 22 34 + [{] 36 36 + ["Disconnected"] 38 51 + [}] 53 53 +L31 + [else] 18 21 + [if] 23 24 + [let] 26 28 + [error] 30 34 + [=] 36 36 + [lastError] 38 46 + [{] 48 48 + [error] 50 54 + [.] 55 55 + [localizedDescription] 56 75 + [}] 77 77 +L32 + [else] 18 21 + [{] 23 23 + ["Ready"] 25 31 + [}] 33 33 +L35 + [@] 1 1 + [attached] 2 9 + [(] 10 10 + [member] 11 16 + [)] 17 17 +L36 + [@] 1 1 + [attached] 2 9 + [(] 10 10 + [conformance] 11 21 + [)] 22 22 +L37 + [public] 1 6 + [macro] 8 12 + [OptionSet] 14 22 + [<] 23 23 + [RawType] 24 30 + [>] 31 31 + [(] 32 32 + [)] 33 33 + [=] 35 35 + [#] 37 37 + [externalMacro] 38 50 + [(] 51 51 + [module] 52 57 + [:] 58 58 + ["SwiftMacros"] 60 72 + [,] 73 73 + [type] 75 78 + [:] 79 79 + ["OptionSetMacro"] 81 96 + [)] 97 97 +EOF