Przeglądaj źródła

added Swift tokens for do try catch

Jan Wittler 4 lat temu
rodzic
commit
5635d47da7

+ 2 - 1
languages/swift/README.md

@@ -21,4 +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.</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/2f6c19cc742c60541227b19c45ac2acc844d9b1a/swift/swift5">commit 2f6c19c</a> from May 2021.
+They received some minor modifications to support identifying the end of do-blocks.</section>

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

@@ -128,7 +128,8 @@ throw_statement: THROW expression;
 defer_statement: DEFER code_block;
 
 // Do Statement
-do_statement: DO code_block catch_clauses?;
+do_statement: DO do_block catch_clauses?;
+do_block: code_block;
 catch_clauses: catch_clause+;
 catch_clause: CATCH catch_pattern_list? code_block;
 catch_pattern_list:

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

@@ -369,6 +369,30 @@ public class JPlagSwiftListener extends Swift5ParserBaseListener {
         super.exitDefer_statement(context);
     }
 
+    @Override
+    public void enterDo_block(Do_blockContext context) {
+        transformToken(DO_TRY_BODY_BEGIN, context.getStart());
+        super.enterDo_block(context);
+    }
+
+    @Override
+    public void exitDo_block(Do_blockContext context) {
+        transformToken(DO_TRY_BODY_END, context.getStop());
+        super.exitDo_block(context);
+    }
+
+    @Override
+    public void enterCatch_clause(Catch_clauseContext context) {
+        transformToken(CATCH_BODY_BEGIN, context.getStart());
+        super.enterCatch_clause(context);
+    }
+
+    @Override
+    public void exitCatch_clause(Catch_clauseContext context) {
+        transformToken(CATCH_BODY_END, context.getStop());
+        super.exitCatch_clause(context);
+    }
+
     @Override
     public void enterThrow_statement(Throw_statementContext context) {
         transformToken(THROW, context.getStart(), context.getStop());

+ 4 - 0
languages/swift/src/main/java/de/jplag/swift/SwiftTokenType.java

@@ -39,6 +39,10 @@ public enum SwiftTokenType implements TokenType {
     REPEAT_WHILE_BODY_END("}REPEAT"),
     DEFER_BODY_BEGIN("DEFER{"),
     DEFER_BODY_END("}DEFER"),
+    DO_TRY_BODY_BEGIN("DO{"),
+    DO_TRY_BODY_END("}DO"),
+    CATCH_BODY_BEGIN("CATCH{"),
+    CATCH_BODY_END("}CATCH"),
     THROW("THROW"),
     RETURN("RETURN"),
     CONTINUE("CONTINUE"),

+ 15 - 1
languages/swift/src/test/resources/de/jplag/swift/Complete.swift

@@ -7,7 +7,7 @@ public class MyClass {
     
     private let x: Int
     //TODO: this will not be parsed correctly when removing the assignment
-    public var name: String = 0 {
+    public var name: String = "" {
         willSet {
             precondition(!name.isEmpty, "name must not be empty")
         }
@@ -101,6 +101,20 @@ enum MyEnum: String {
     func throwAny() throws {
         throw NSError(domain: "error", code: -1)
     }
+
+    func catch() throws {
+        let _ = try? throwAny()
+        do {
+            try throwAny()
+        }
+        catch let error {
+            print("catched")
+        }
+        let _ = try! throwAny()
+        do {
+            try throwAny()
+        }
+    }
 }
 
 enum ListEnum {