Просмотр исходного кода

implemented Swift accessors tokenization

Jan Wittler 4 лет назад
Родитель
Сommit
8f8771bd93

+ 2 - 2
languages/swift/README.md

@@ -21,5 +21,5 @@ To use the Swift frontend, add the `-l swift` flag in the CLI, or use a `JPlagOp
 <br>
 
 #### Footnotes
-<section id="footnote-1"><sup>1 </sup>The grammar files are taken from grammar-v4, with the most recent modification in <a href="https://github.com/antlr/grammars-v4/tree/2f6c19cc742c60541227b19c45ac2acc844d9b1a/swift/swift5">commit 2f6c19c</a> from May 2021.
-They received some minor modifications to support identifying the end of do-blocks.</section>
+<section id="footnote-1"><sup>1 </sup>The grammar files are taken from grammar-v4, with the most recent modification in <a href="https://github.com/antlr/grammars-v4/tree/8a9c082e086e78e62708d5130e8bcf44954e0ddc/swift/swift5">commit 2f6c19c</a> from Sep 2022.
+They received some minor modifications to support identifying the end of do-blocks and a fix for implicit getters.</section>

+ 2 - 2
languages/swift/src/main/antlr4/de/jplag/swift/grammar/Swift5Parser.g4

@@ -315,9 +315,9 @@ variable_declaration:
 		variable_name (
 			initializer willSet_didSet_block
 			| type_annotation (
-				getter_setter_block // contains code_block
+			    initializer? willSet_didSet_block
+				| getter_setter_block // contains code_block
 				| getter_setter_keyword_block
-				| initializer? willSet_didSet_block
 			)
 		)
 		| pattern_initializer_list

+ 18 - 0
languages/swift/src/main/java/de/jplag/swift/JPlagSwiftListener.java

@@ -185,6 +185,24 @@ public class JPlagSwiftListener extends Swift5ParserBaseListener {
         super.enterWillSet_clause(context);
     }
 
+    @Override
+    public void enterGetter_setter_block(Getter_setter_blockContext context) {
+        // if (implicit getter) var example: Int { /* */ }
+        if (context.getChildCount() == 1 && context.getChild(0) instanceof Code_blockContext) {
+            transformToken(PROPERTY_ACCESSOR_BEGIN, context.getStart());
+        }
+        super.enterGetter_setter_block(context);
+    }
+
+    @Override
+    public void exitGetter_setter_block(Getter_setter_blockContext context) {
+        // if (implicit getter) var example: Int { /* */ }
+        if (context.getChildCount() == 1 && context.getChild(0) instanceof Code_blockContext) {
+            transformToken(PROPERTY_ACCESSOR_END, context.getStop());
+        }
+        super.exitGetter_setter_block(context);
+    }
+
     @Override
     public void exitWillSet_clause(WillSet_clauseContext context) {
         transformToken(PROPERTY_ACCESSOR_END, context.getStop());

+ 2 - 1
languages/swift/src/main/java/de/jplag/swift/Language.java

@@ -3,9 +3,10 @@ package de.jplag.swift;
 import java.io.File;
 import java.util.List;
 
-import de.jplag.Token;
 import org.kohsuke.MetaInfServices;
 
+import de.jplag.Token;
+
 /**
  * This represents the Swift language as a language supported by JPlag.
  */

+ 16 - 1
languages/swift/src/main/java/de/jplag/swift/grammar/SwiftSupport.java

@@ -161,6 +161,8 @@ public class SwiftSupport {
     }
 
     public static boolean isOpNext(TokenStream tokens) {
+        int start = tokens.index();
+        Token lt = tokens.get(start);
         int stop = getLastOpTokenIndex(tokens);
         return stop != -1;
         // System.out.printf("isOpNext: i=%d t='%s'", start, lt.getText());
@@ -171,12 +173,12 @@ public class SwiftSupport {
      * Find stop token index of next operator; return -1 if not operator.
      */
     public static int getLastOpTokenIndex(TokenStream tokens) {
+        SwiftSupport.fillUp(tokens);
         int currentTokenIndex = tokens.index(); // current on-channel lookahead token index
         Token currentToken = tokens.get(currentTokenIndex);
 
         // System.out.println("getLastOpTokenIndex: "+currentToken.getText());
 
-        tokens.getText(); // Ensures that tokens can be read
         // operator → dot-operator-head­ dot-operator-characters
         if (currentToken.getType() == Swift5Parser.DOT && tokens.get(currentTokenIndex + 1).getType() == Swift5Parser.DOT) {
             // System.out.println("DOT");
@@ -220,6 +222,7 @@ public class SwiftSupport {
      * example, the + operator in a+b and a + b is treated as a binary operator."
      */
     public static boolean isBinaryOp(TokenStream tokens) {
+        SwiftSupport.fillUp(tokens);
         int stop = getLastOpTokenIndex(tokens);
         if (stop == -1)
             return false;
@@ -250,6 +253,7 @@ public class SwiftSupport {
      * operator in a ++b is treated as a prefix unary operator."
      */
     public static boolean isPrefixOp(TokenStream tokens) {
+        SwiftSupport.fillUp(tokens);
         int stop = getLastOpTokenIndex(tokens);
         if (stop == -1)
             return false;
@@ -273,6 +277,7 @@ public class SwiftSupport {
      * ++ .b)."
      */
     public static boolean isPostfixOp(TokenStream tokens) {
+        SwiftSupport.fillUp(tokens);
         int stop = getLastOpTokenIndex(tokens);
         if (stop == -1)
             return false;
@@ -288,6 +293,7 @@ public class SwiftSupport {
     }
 
     public static boolean isOperator(TokenStream tokens, String op) {
+        SwiftSupport.fillUp(tokens);
         int stop = getLastOpTokenIndex(tokens);
         if (stop == -1)
             return false;
@@ -312,6 +318,7 @@ public class SwiftSupport {
     }
 
     public static boolean isSeparatedStatement(TokenStream tokens, int indexOfPreviousStatement) {
+        SwiftSupport.fillUp(tokens);
         // System.out.println("------");
         // System.out.println("indexOfPreviousStatement: " + indexOfPreviousStatement);
 
@@ -342,4 +349,12 @@ public class SwiftSupport {
             return true;
         }
     }
+
+    public static void fillUp(TokenStream tokens) {
+        for (int jj = 1;; ++jj) {
+            int t = tokens.LA(jj);
+            if (t == -1)
+                break;
+        }
+    }
 }

+ 23 - 18
languages/swift/src/test/resources/de/jplag/swift/Complete.swift

@@ -6,19 +6,25 @@ public class MyClass {
     public static let classConstant = 0.0
     
     private let x: Int
-    //TODO: this will not be parsed correctly when removing the assignment
+
     public var name: String = "" {
         willSet {
             precondition(!name.isEmpty, "name must not be empty")
         }
         didSet {
             nameLength = name.count
-        } 
+        }
     } //NO TOKEN
-    
+
+    public var optionalName: String? {
+        didSet {
+            nameLength = name.count
+        }
+    } //NO TOKEN
+
     private var nameLength = 0
     private let a = "A"
-    
+
     public init(name: String) {
         self.name = name
         self.x = 0
@@ -32,17 +38,17 @@ public class MyClass {
             }
             sum += i
         }
-        
+
         var i = x
         while i < max {
             sum -= i
         }
-        
+
         i = x
         repeat {
             sum *= i
         } while i < max
-                
+
         switch max {
         case 0:
             sum = 0
@@ -53,7 +59,7 @@ public class MyClass {
         default:
             sum /= 2
         }
-                
+
         sum += (x..<max).reduce(0, +)
         return sum
     }
@@ -73,7 +79,7 @@ private struct MyStruct {
         get { nameComponents }
         set { fullName = newValue.joined(separator: " ") }
     } //NO TOKEN
-    
+
     func hasMultipleComponents() -> Bool {
         defer {
             print("later")
@@ -91,24 +97,23 @@ private struct MyStruct {
 enum MyEnum: String {
     case one = "ONE"
 
-    //TODO: currently incorrectly parsed
-    // var value: Int {
-    //     switch self {
-    //         case .one: return 1
-    //     }
-    // }
-    
+    var value: Int {
+        switch self {
+        case .one: return 1
+        }
+    }
+
     func throwAny() throws {
         throw NSError(domain: "error", code: -1)
     }
 
-    func catch() throws {
+    func catchSome() throws {
         let _ = try? throwAny()
         do {
             try throwAny()
         }
         catch let error {
-            print("catched")
+            print(error)
         }
         let _ = try! throwAny()
         do {