From e334b0df862ed42b86eca3a1187f6bee63ded635 Mon Sep 17 00:00:00 2001 From: Hanzhou Shi Date: Wed, 22 Mar 2017 21:18:32 -0700 Subject: [PATCH] Adding visitor tests. --- .../Swift/Antlr4.xcodeproj/project.pbxproj | 97 +++++++- .../Sources/Antlr4/BaseErrorListener.swift | 12 +- .../tree/AbstractParseTreeVisitor.swift | 9 +- .../Swift/Tests/Antlr4Tests/VisitorCalc.g4 | 18 ++ .../Tests/Antlr4Tests/VisitorTests.swift | 207 ++++++++++++++++++ runtime/Swift/test.py | 3 +- 6 files changed, 322 insertions(+), 24 deletions(-) create mode 100644 runtime/Swift/Tests/Antlr4Tests/VisitorCalc.g4 create mode 100644 runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift diff --git a/runtime/Swift/Antlr4.xcodeproj/project.pbxproj b/runtime/Swift/Antlr4.xcodeproj/project.pbxproj index 6885ca686..077d3cb02 100644 --- a/runtime/Swift/Antlr4.xcodeproj/project.pbxproj +++ b/runtime/Swift/Antlr4.xcodeproj/project.pbxproj @@ -15,6 +15,17 @@ DBA2FEF51E837D1F00BB60D9 /* VisitorBasicListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEEF1E837D1F00BB60D9 /* VisitorBasicListener.swift */; }; DBA2FEF61E837D1F00BB60D9 /* VisitorBasicParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEF01E837D1F00BB60D9 /* VisitorBasicParser.swift */; }; DBA2FEF71E837D1F00BB60D9 /* VisitorBasicParserATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEF11E837D1F00BB60D9 /* VisitorBasicParserATN.swift */; }; + DBA2FF011E837E2D00BB60D9 /* VisitorCalcBaseListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEFA1E837E2D00BB60D9 /* VisitorCalcBaseListener.swift */; }; + DBA2FF021E837E2D00BB60D9 /* VisitorCalcLexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEFB1E837E2D00BB60D9 /* VisitorCalcLexer.swift */; }; + DBA2FF031E837E2D00BB60D9 /* VisitorCalcLexerATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEFD1E837E2D00BB60D9 /* VisitorCalcLexerATN.swift */; }; + DBA2FF041E837E2D00BB60D9 /* VisitorCalcListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEFE1E837E2D00BB60D9 /* VisitorCalcListener.swift */; }; + DBA2FF051E837E2D00BB60D9 /* VisitorCalcParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FEFF1E837E2D00BB60D9 /* VisitorCalcParser.swift */; }; + DBA2FF061E837E2D00BB60D9 /* VisitorCalcParserATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF001E837E2D00BB60D9 /* VisitorCalcParserATN.swift */; }; + DBA2FF081E837ED900BB60D9 /* VisitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF071E837ED900BB60D9 /* VisitorTests.swift */; }; + DBA2FF0B1E83802600BB60D9 /* VisitorBasicVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF091E83802500BB60D9 /* VisitorBasicVisitor.swift */; }; + DBA2FF0C1E83802600BB60D9 /* VisitorCalcVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF0A1E83802600BB60D9 /* VisitorCalcVisitor.swift */; }; + DBA2FF0F1E83810C00BB60D9 /* VisitorBasicBaseVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF0D1E83810C00BB60D9 /* VisitorBasicBaseVisitor.swift */; }; + DBA2FF101E83810C00BB60D9 /* VisitorCalcBaseVisitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBA2FF0E1E83810C00BB60D9 /* VisitorCalcBaseVisitor.swift */; }; DBBAE6351E22DCAC00C2FEA6 /* ANTLRErrorListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE58E1E22DCAC00C2FEA6 /* ANTLRErrorListener.swift */; }; DBBAE6361E22DCAC00C2FEA6 /* ANTLRErrorListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE58E1E22DCAC00C2FEA6 /* ANTLRErrorListener.swift */; }; DBBAE6371E22DCAC00C2FEA6 /* ANTLRErrorStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE58F1E22DCAC00C2FEA6 /* ANTLRErrorStrategy.swift */; }; @@ -352,14 +363,28 @@ DB0991951E22DE6000FE71AA /* TokenStreamTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokenStreamTests.swift; sourceTree = ""; }; DB0991A01E22DF3B00FE71AA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; DB4EB8A41E2CB1B60095F402 /* VisitorBasic.g4 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = VisitorBasic.g4; sourceTree = ""; }; - DBA2FEEA1E837D1F00BB60D9 /* VisitorBasic.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = VisitorBasic.tokens; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasic.tokens"; sourceTree = ""; }; - DBA2FEEB1E837D1F00BB60D9 /* VisitorBasicBaseListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicBaseListener.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicBaseListener.swift"; sourceTree = ""; }; - DBA2FEEC1E837D1F00BB60D9 /* VisitorBasicLexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicLexer.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexer.swift"; sourceTree = ""; }; - DBA2FEED1E837D1F00BB60D9 /* VisitorBasicLexer.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = VisitorBasicLexer.tokens; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexer.tokens"; sourceTree = ""; }; - DBA2FEEE1E837D1F00BB60D9 /* VisitorBasicLexerATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicLexerATN.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexerATN.swift"; sourceTree = ""; }; - DBA2FEEF1E837D1F00BB60D9 /* VisitorBasicListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicListener.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicListener.swift"; sourceTree = ""; }; - DBA2FEF01E837D1F00BB60D9 /* VisitorBasicParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicParser.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicParser.swift"; sourceTree = ""; }; - DBA2FEF11E837D1F00BB60D9 /* VisitorBasicParserATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicParserATN.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicParserATN.swift"; sourceTree = ""; }; + DBA2FEEA1E837D1F00BB60D9 /* VisitorBasic.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file; name = VisitorBasic.tokens; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasic.tokens; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEEB1E837D1F00BB60D9 /* VisitorBasicBaseListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicBaseListener.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicBaseListener.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEEC1E837D1F00BB60D9 /* VisitorBasicLexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicLexer.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexer.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEED1E837D1F00BB60D9 /* VisitorBasicLexer.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file; name = VisitorBasicLexer.tokens; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexer.tokens; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEEE1E837D1F00BB60D9 /* VisitorBasicLexerATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicLexerATN.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicLexerATN.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEEF1E837D1F00BB60D9 /* VisitorBasicListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicListener.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicListener.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEF01E837D1F00BB60D9 /* VisitorBasicParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicParser.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicParser.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEF11E837D1F00BB60D9 /* VisitorBasicParserATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicParserATN.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicParserATN.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEF81E837DB500BB60D9 /* VisitorCalc.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; path = VisitorCalc.g4; sourceTree = ""; }; + DBA2FEF91E837E2D00BB60D9 /* VisitorCalc.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file; name = VisitorCalc.tokens; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalc.tokens; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFA1E837E2D00BB60D9 /* VisitorCalcBaseListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcBaseListener.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcBaseListener.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFB1E837E2D00BB60D9 /* VisitorCalcLexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcLexer.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcLexer.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFC1E837E2D00BB60D9 /* VisitorCalcLexer.tokens */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file; name = VisitorCalcLexer.tokens; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcLexer.tokens; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFD1E837E2D00BB60D9 /* VisitorCalcLexerATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcLexerATN.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcLexerATN.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFE1E837E2D00BB60D9 /* VisitorCalcListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcListener.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcListener.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FEFF1E837E2D00BB60D9 /* VisitorCalcParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcParser.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcParser.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FF001E837E2D00BB60D9 /* VisitorCalcParserATN.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcParserATN.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcParserATN.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FF071E837ED900BB60D9 /* VisitorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VisitorTests.swift; sourceTree = ""; }; + DBA2FF091E83802500BB60D9 /* VisitorBasicVisitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicVisitor.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicVisitor.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FF0A1E83802600BB60D9 /* VisitorCalcVisitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcVisitor.swift; path = ../../Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcVisitor.swift; sourceTree = BUILT_PRODUCTS_DIR; }; + DBA2FF0D1E83810C00BB60D9 /* VisitorBasicBaseVisitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorBasicBaseVisitor.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorBasicBaseVisitor.swift"; sourceTree = ""; }; + DBA2FF0E1E83810C00BB60D9 /* VisitorCalcBaseVisitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VisitorCalcBaseVisitor.swift; path = "../../../../../../../../Library/Developer/Xcode/DerivedData/Antlr4-dgqyozbmfhoivufxxoucdcelhjkw/Build/Intermediates/Antlr4.build/Debug/Antlr4Tests.build/DerivedSources/Tests/Antlr4Tests/VisitorCalcBaseVisitor.swift"; sourceTree = ""; }; DBBAE58E1E22DCAC00C2FEA6 /* ANTLRErrorListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ANTLRErrorListener.swift; sourceTree = ""; }; DBBAE58F1E22DCAC00C2FEA6 /* ANTLRErrorStrategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ANTLRErrorStrategy.swift; sourceTree = ""; }; DBBAE5901E22DCAC00C2FEA6 /* ANTLRFileStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ANTLRFileStream.swift; sourceTree = ""; }; @@ -574,9 +599,11 @@ DB4EB8B21E2CBA240095F402 /* gen */, DB0991951E22DE6000FE71AA /* TokenStreamTests.swift */, DBF000B21E7F59B3006DB586 /* TokenStreamRewriterTests.swift */, + DBA2FF071E837ED900BB60D9 /* VisitorTests.swift */, DB4EB8A41E2CB1B60095F402 /* VisitorBasic.g4 */, DBF000B51E7F614D006DB586 /* LexerA.g4 */, DBF000BD1E7F64DE006DB586 /* LexerB.g4 */, + DBA2FEF81E837DB500BB60D9 /* VisitorCalc.g4 */, ); path = Antlr4Tests; sourceTree = ""; @@ -595,7 +622,19 @@ DBA2FEEC1E837D1F00BB60D9 /* VisitorBasicLexer.swift */, DBA2FEED1E837D1F00BB60D9 /* VisitorBasicLexer.tokens */, DBA2FEEE1E837D1F00BB60D9 /* VisitorBasicLexerATN.swift */, + DBA2FF0D1E83810C00BB60D9 /* VisitorBasicBaseVisitor.swift */, DBA2FEEF1E837D1F00BB60D9 /* VisitorBasicListener.swift */, + DBA2FF091E83802500BB60D9 /* VisitorBasicVisitor.swift */, + DBA2FEF91E837E2D00BB60D9 /* VisitorCalc.tokens */, + DBA2FEFA1E837E2D00BB60D9 /* VisitorCalcBaseListener.swift */, + DBA2FEFB1E837E2D00BB60D9 /* VisitorCalcLexer.swift */, + DBA2FEFC1E837E2D00BB60D9 /* VisitorCalcLexer.tokens */, + DBA2FEFD1E837E2D00BB60D9 /* VisitorCalcLexerATN.swift */, + DBA2FEFE1E837E2D00BB60D9 /* VisitorCalcListener.swift */, + DBA2FEFF1E837E2D00BB60D9 /* VisitorCalcParser.swift */, + DBA2FF001E837E2D00BB60D9 /* VisitorCalcParserATN.swift */, + DBA2FF0A1E83802600BB60D9 /* VisitorCalcVisitor.swift */, + DBA2FF0E1E83810C00BB60D9 /* VisitorCalcBaseVisitor.swift */, DBA2FEF01E837D1F00BB60D9 /* VisitorBasicParser.swift */, DBA2FEF11E837D1F00BB60D9 /* VisitorBasicParserATN.swift */, ); @@ -1003,15 +1042,38 @@ files = ( ); inputPaths = ( - "$(SRCROOT)/VisitorBasic.g4", - "$(SRCROOT)/LexerA.g4", - "$(SRCROOT)/LexerB.g4", ); outputPaths = ( + "$(DERIVED_FILE_DIR)/LexerA.swift", + "$(DERIVED_FILE_DIR)/LexerA.tokens", + "$(DERIVED_FILE_DIR)/LexerAATN.swift", + "$(DERIVED_FILE_DIR)/LexerB.swift", + "$(DERIVED_FILE_DIR)/LexerB.tokens", + "$(DERIVED_FILE_DIR)/LexerBATN.swift", + "$(DERIVED_FILE_DIR)/VisitorBasic.tokens", + "$(DERIVED_FILE_DIR)/VisitorBasicBaseListener.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicLexer.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicLexer.tokens", + "$(DERIVED_FILE_DIR)/VisitorBasicLexerATN.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicBaseVisitor.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicListener.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicVisitor.swift", + "$(DERIVED_FILE_DIR)/VisitorCalc.tokens", + "$(DERIVED_FILE_DIR)/VisitorCalcBaseListener.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcLexer.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcLexer.tokens", + "$(DERIVED_FILE_DIR)/VisitorCalcLexerATN.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcListener.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcParser.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcParserATN.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcVisitor.swift", + "$(DERIVED_FILE_DIR)/VisitorCalcBaseVisitor.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicParser.swift", + "$(DERIVED_FILE_DIR)/VisitorBasicParserATN.swift", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "echo \"Generating parser in $DERIVED_FILE_DIR\"\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/VisitorBasic.g4 -o $DERIVED_FILE_DIR\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/LexerA.g4 -o $DERIVED_FILE_DIR\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/LexerB.g4 -o $DERIVED_FILE_DIR"; + shellScript = "echo \"Generating parser in $DERIVED_FILE_DIR\"\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/LexerA.g4 -o $DERIVED_FILE_DIR\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/LexerB.g4 -o $DERIVED_FILE_DIR\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/VisitorCalc.g4 -visitor -o $DERIVED_FILE_DIR\njava -jar ~/.m2/repository/org/antlr/antlr4/4.7-SNAPSHOT/antlr4-4.7-SNAPSHOT-complete.jar -Dlanguage=Swift Tests/Antlr4Tests/VisitorBasic.g4 -visitor -o $DERIVED_FILE_DIR"; }; /* End PBXShellScriptBuildPhase section */ @@ -1350,16 +1412,27 @@ files = ( DBA2FEF71E837D1F00BB60D9 /* VisitorBasicParserATN.swift in Sources */, DBA2FEF21E837D1F00BB60D9 /* VisitorBasicBaseListener.swift in Sources */, + DBA2FF081E837ED900BB60D9 /* VisitorTests.swift in Sources */, + DBA2FF031E837E2D00BB60D9 /* VisitorCalcLexerATN.swift in Sources */, DBA2FEF51E837D1F00BB60D9 /* VisitorBasicListener.swift in Sources */, + DBA2FF0C1E83802600BB60D9 /* VisitorCalcVisitor.swift in Sources */, + DBA2FF061E837E2D00BB60D9 /* VisitorCalcParserATN.swift in Sources */, DBF000B91E7F627D006DB586 /* LexerA.swift in Sources */, DB09919D1E22DE9B00FE71AA /* TokenStreamTests.swift in Sources */, + DBA2FF051E837E2D00BB60D9 /* VisitorCalcParser.swift in Sources */, DBA2FEF41E837D1F00BB60D9 /* VisitorBasicLexerATN.swift in Sources */, + DBA2FF0B1E83802600BB60D9 /* VisitorBasicVisitor.swift in Sources */, DBA2FEF61E837D1F00BB60D9 /* VisitorBasicParser.swift in Sources */, DBF000BB1E7F627D006DB586 /* LexerAATN.swift in Sources */, + DBA2FF021E837E2D00BB60D9 /* VisitorCalcLexer.swift in Sources */, DBA2FEF31E837D1F00BB60D9 /* VisitorBasicLexer.swift in Sources */, + DBA2FF101E83810C00BB60D9 /* VisitorCalcBaseVisitor.swift in Sources */, DBF000C31E7F6546006DB586 /* LexerBATN.swift in Sources */, DBF000C11E7F6546006DB586 /* LexerB.swift in Sources */, + DBA2FF041E837E2D00BB60D9 /* VisitorCalcListener.swift in Sources */, + DBA2FF011E837E2D00BB60D9 /* VisitorCalcBaseListener.swift in Sources */, DBF000B31E7F59B3006DB586 /* TokenStreamRewriterTests.swift in Sources */, + DBA2FF0F1E83810C00BB60D9 /* VisitorBasicBaseVisitor.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift b/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift index f282d53f3..aa64f3abe 100644 --- a/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift @@ -9,9 +9,11 @@ /// /// - Sam Harwell -public class BaseErrorListener: ANTLRErrorListener { +open class BaseErrorListener: ANTLRErrorListener { + public init() { + } - public func syntaxError(_ recognizer: Recognizer, + open func syntaxError(_ recognizer: Recognizer, _ offendingSymbol: AnyObject?, _ line: Int, _ charPositionInLine: Int, @@ -21,7 +23,7 @@ public class BaseErrorListener: ANTLRErrorListener { } - public func reportAmbiguity(_ recognizer: Parser, + open func reportAmbiguity(_ recognizer: Parser, _ dfa: DFA, _ startIndex: Int, _ stopIndex: Int, @@ -31,7 +33,7 @@ public class BaseErrorListener: ANTLRErrorListener { } - public func reportAttemptingFullContext(_ recognizer: Parser, + open func reportAttemptingFullContext(_ recognizer: Parser, _ dfa: DFA, _ startIndex: Int, _ stopIndex: Int, @@ -40,7 +42,7 @@ public class BaseErrorListener: ANTLRErrorListener { } - public func reportContextSensitivity(_ recognizer: Parser, + open func reportContextSensitivity(_ recognizer: Parser, _ dfa: DFA, _ startIndex: Int, _ stopIndex: Int, diff --git a/runtime/Swift/Sources/Antlr4/tree/AbstractParseTreeVisitor.swift b/runtime/Swift/Sources/Antlr4/tree/AbstractParseTreeVisitor.swift index 5cfa95aec..963ec6e3d 100644 --- a/runtime/Swift/Sources/Antlr4/tree/AbstractParseTreeVisitor.swift +++ b/runtime/Swift/Sources/Antlr4/tree/AbstractParseTreeVisitor.swift @@ -16,7 +16,6 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { *

The default implementation calls {@link org.antlr.v4.runtime.tree.ParseTree#accept} on the * specified tree.

*/ - open override func visit(_ tree: ParseTree) -> T? { return tree.accept(self) } @@ -36,7 +35,6 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { * the tree structure. Visitors that modify the tree should override this * method to behave properly in respect to the specific algorithm in use.

*/ - open override func visitChildren(_ node: RuleNode) -> T? { var result: T? = defaultResult() let n: Int = node.getChildCount() @@ -60,7 +58,6 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { *

The default implementation returns the result of * {@link #defaultResult defaultResult}.

*/ - open override func visitTerminal(_ node: TerminalNode) -> T? { return defaultResult() } @@ -87,7 +84,7 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { * * @return The default value returned by visitor methods. */ - internal func defaultResult() -> T? { + open func defaultResult() -> T? { return nil } @@ -110,7 +107,7 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { * * @return The updated aggregate result. */ - internal func aggregateResult(_ aggregate: T?, _ nextResult: T?) -> T? { + open func aggregateResult(_ aggregate: T?, _ nextResult: T?) -> T? { return nextResult } @@ -138,7 +135,7 @@ open class AbstractParseTreeVisitor: ParseTreeVisitor { * {@code false} to stop visiting children and immediately return the * current aggregate result from {@link #visitChildren}. */ - internal func shouldVisitNextChild(_ node: RuleNode, _ currentResult: T?) -> Bool { + open func shouldVisitNextChild(_ node: RuleNode, _ currentResult: T?) -> Bool { return true } diff --git a/runtime/Swift/Tests/Antlr4Tests/VisitorCalc.g4 b/runtime/Swift/Tests/Antlr4Tests/VisitorCalc.g4 new file mode 100644 index 000000000..0ba780431 --- /dev/null +++ b/runtime/Swift/Tests/Antlr4Tests/VisitorCalc.g4 @@ -0,0 +1,18 @@ +grammar VisitorCalc; + +s + : expr EOF + ; + +expr + : INT # number + | expr (MUL | DIV) expr # multiply + | expr (ADD | SUB) expr # add + ; + +INT : [0-9]+; +MUL : '*'; +DIV : '/'; +ADD : '+'; +SUB : '-'; +WS : [ \t]+ -> channel(HIDDEN); diff --git a/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift b/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift new file mode 100644 index 000000000..ddaf12731 --- /dev/null +++ b/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift @@ -0,0 +1,207 @@ +/// Copyright (c) 2012-2016 The ANTLR Project. All rights reserved. +/// Use of this file is governed by the BSD 3-clause license that +/// can be found in the LICENSE.txt file in the project root. + +import XCTest +import Antlr4 + +class VisitorTests: XCTestCase { + + /// + /// This test verifies the basic behavior of visitors, with an emphasis on + /// {@link AbstractParseTreeVisitor#visitTerminal}. + /// + func testVisitTerminalNode() throws { + let lexer = VisitorBasicLexer(ANTLRInputStream("A")) + let parser = try VisitorBasicParser(CommonTokenStream(lexer)) + + let context = try parser.s() + XCTAssertEqual("(s A )", context.toStringTree(parser)) + + class Visitor: VisitorBasicBaseVisitor { + override func visitTerminal(_ node: TerminalNode) -> String? { + return "\(node.getSymbol()!)\n" + } + + override func defaultResult() -> String? { + return "" + } + + override func aggregateResult(_ aggregate: String?, _ nextResult: String?) -> String? { + return aggregate! + nextResult! + } + } + + let visitor = Visitor() + let result = visitor.visit(context) + let expected = + "[@0,0:0='A',<1>,1:0]\n" + + "[@1,1:0='',<-1>,1:1]\n" + XCTAssertEqual(expected, result) + } + + /// + /// This test verifies the basic behavior of visitors, with an emphasis on + /// {@link AbstractParseTreeVisitor#visitErrorNode}. + /// + func testVisitErrorNode() throws { + let lexer = VisitorBasicLexer(ANTLRInputStream("")) + let parser = try VisitorBasicParser(CommonTokenStream(lexer)) + + class ErrorListener: BaseErrorListener { + override init() { + super.init() + } + + var errors = [String]() + + override func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, _ charPositionInLine: Int, + _ msg: String, _ e: AnyObject?) { + errors.append("line \(line):\(charPositionInLine) \(msg)") + } + } + + parser.removeErrorListeners() + let errorListener = ErrorListener() + parser.addErrorListener(errorListener) + + let context = try parser.s() + let errors = errorListener.errors + XCTAssertEqual("(s )", context.toStringTree(parser)) + XCTAssertEqual(1, errors.count) + XCTAssertEqual("line 1:0 missing 'A' at ''", errors[0]) + + class Visitor: VisitorBasicBaseVisitor { + override func visitErrorNode(_ node: ErrorNode) -> String? { + return "Error encountered: \(node.getSymbol()!)" + } + + override func defaultResult() -> String? { + return "" + } + + override func aggregateResult(_ aggregate: String?, _ nextResult: String?) -> String? { + return aggregate! + nextResult! + } + } + + let visitor = Visitor() + let result = visitor.visit(context) + let expected = "Error encountered: [@-1,-1:-1='',<1>,1:0]" + XCTAssertEqual(expected, result) + } + + /// + /// This test verifies that {@link AbstractParseTreeVisitor#visitChildren} does not call + /// {@link ParseTreeVisitor#visit} after {@link AbstractParseTreeVisitor#shouldVisitNextChild} returns + /// {@code false}. + /// + func testShouldNotVisitEOF() throws { + let input = "A" + let lexer = VisitorBasicLexer(ANTLRInputStream(input)) + let parser = try VisitorBasicParser(CommonTokenStream(lexer)) + + let context = try parser.s() + XCTAssertEqual("(s A )", context.toStringTree(parser)) + + class Visitor: VisitorBasicBaseVisitor { + override func visitTerminal(_ node: TerminalNode) -> String? { + return "\(node.getSymbol()!)\n" + } + + override func shouldVisitNextChild(_ node: RuleNode, _ currentResult: String?) -> Bool { + return currentResult == nil || currentResult!.isEmpty + } + } + + let visitor = Visitor() + let result = visitor.visit(context) + let expected = "[@0,0:0='A',<1>,1:0]\n" + XCTAssertEqual(expected, result) + } + + /// + /// This test verifies that {@link AbstractParseTreeVisitor#shouldVisitNextChild} is called before visiting the first + /// child. It also verifies that {@link AbstractParseTreeVisitor#defaultResult} provides the default return value for + /// visiting a tree. + /// + func testShouldNotVisitTerminal() throws { + let input = "A" + let lexer = VisitorBasicLexer(ANTLRInputStream(input)) + let parser = try VisitorBasicParser(CommonTokenStream(lexer)) + + let context = try parser.s() + XCTAssertEqual("(s A )", context.toStringTree(parser)) + + class Visitor: VisitorBasicBaseVisitor { + override func visitTerminal(_ node: TerminalNode) -> String? { + XCTFail() + return nil + } + + override func defaultResult() -> String? { + return "default result" + } + + override func shouldVisitNextChild(_ node: RuleNode, _ currentResult: String?) -> Bool { + return false + } + } + + let visitor = Visitor() + let result = visitor.visit(context) + let expected = "default result" + XCTAssertEqual(expected, result) + } + + /// + /// This test verifies that the visitor correctly dispatches calls for labeled outer alternatives. + /// + func testCalculatorVisitor() throws { + let input = "2 + 8 / 2" + let lexer = VisitorCalcLexer(ANTLRInputStream(input)) + let parser = try VisitorCalcParser(CommonTokenStream(lexer)) + + let context = try parser.s() + XCTAssertEqual("(s (expr (expr 2) + (expr (expr 8) / (expr 2))) )", context.toStringTree(parser)) + + class Visitor: VisitorCalcBaseVisitor { + override func visitS(_ ctx: VisitorCalcParser.SContext) -> Int? { + return visit(ctx.expr()!) + } + + override func visitNumber(_ ctx: VisitorCalcParser.NumberContext) -> Int? { + return Int((ctx.INT()?.getText())!) + } + + override func visitMultiply(_ ctx: VisitorCalcParser.MultiplyContext) -> Int? { + let left = visit(ctx.expr(0)!)! + let right = visit(ctx.expr(1)!)! + if ctx.MUL() != nil { + return left * right + } + else { + return left / right + } + } + + override func visitAdd(_ ctx: VisitorCalcParser.AddContext) -> Int? { + let left = visit(ctx.expr(0)!)! + let right = visit(ctx.expr(1)!)! + if ctx.ADD() != nil { + return left + right + } + else { + return left - right + } + } + } + + let visitor = Visitor() + let result = visitor.visit(context) + let expected = 6 + XCTAssertEqual(expected, result!) + } +} diff --git a/runtime/Swift/test.py b/runtime/Swift/test.py index cdda05a90..7b00a7ac6 100755 --- a/runtime/Swift/test.py +++ b/runtime/Swift/test.py @@ -56,7 +56,8 @@ def gen_parser(grammar): return call([java, '-jar', ANTLR4_JAR,\ - '-Dlanguage=Swift', grammar, '-o', grammar_folder + '/gen']) + '-Dlanguage=Swift', grammar, '-visitor',\ + '-o', grammar_folder + '/gen']) def swift_test():