Fixed printing of RewriteOps, now tests are passing.

This commit is contained in:
Hanzhou Shi 2017-03-21 22:17:00 -07:00
parent cb396a5047
commit 2dab85ce2b
5 changed files with 50 additions and 74 deletions

View File

@ -8,7 +8,6 @@
/* Begin PBXBuildFile section */
DB09919D1E22DE9B00FE71AA /* TokenStreamTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB0991951E22DE6000FE71AA /* TokenStreamTests.swift */; };
DB0991A11E22DF3B00FE71AA /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = DB0991A01E22DF3B00FE71AA /* Info.plist */; };
DB4EB8A51E2CB1B60095F402 /* SwiftTest.g4 in Resources */ = {isa = PBXBuildFile; fileRef = DB4EB8A41E2CB1B60095F402 /* SwiftTest.g4 */; };
DBBAE6351E22DCAC00C2FEA6 /* ANTLRErrorListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE58E1E22DCAC00C2FEA6 /* ANTLRErrorListener.swift */; };
DBBAE6361E22DCAC00C2FEA6 /* ANTLRErrorListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE58E1E22DCAC00C2FEA6 /* ANTLRErrorListener.swift */; };
@ -324,22 +323,17 @@
DBBAE76C1E22DCAC00C2FEA6 /* VocabularySingle.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE6311E22DCAC00C2FEA6 /* VocabularySingle.swift */; };
DBBAE76D1E22DCAC00C2FEA6 /* WritableToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE6321E22DCAC00C2FEA6 /* WritableToken.swift */; };
DBBAE76E1E22DCAC00C2FEA6 /* WritableToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBBAE6321E22DCAC00C2FEA6 /* WritableToken.swift */; };
DBBAE7701E22DCAC00C2FEA6 /* Info-IOS.plist in Resources */ = {isa = PBXBuildFile; fileRef = DBBAE6331E22DCAC00C2FEA6 /* Info-IOS.plist */; };
DBBAE7721E22DCAC00C2FEA6 /* Info-OSX.plist in Resources */ = {isa = PBXBuildFile; fileRef = DBBAE6341E22DCAC00C2FEA6 /* Info-OSX.plist */; };
DBF000AA1E7F521E006DB586 /* SwiftTest.tokens in Resources */ = {isa = PBXBuildFile; fileRef = DBF000A21E7F521E006DB586 /* SwiftTest.tokens */; };
DBF000AB1E7F521E006DB586 /* SwiftTestBaseListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A31E7F521E006DB586 /* SwiftTestBaseListener.swift */; };
DBF000AC1E7F521E006DB586 /* SwiftTestLexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A41E7F521E006DB586 /* SwiftTestLexer.swift */; };
DBF000AD1E7F521E006DB586 /* SwiftTestLexer.tokens in Resources */ = {isa = PBXBuildFile; fileRef = DBF000A51E7F521E006DB586 /* SwiftTestLexer.tokens */; };
DBF000AE1E7F521E006DB586 /* SwiftTestLexerATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A61E7F521E006DB586 /* SwiftTestLexerATN.swift */; };
DBF000AF1E7F521E006DB586 /* SwiftTestListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A71E7F521E006DB586 /* SwiftTestListener.swift */; };
DBF000B01E7F521E006DB586 /* SwiftTestParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A81E7F521E006DB586 /* SwiftTestParser.swift */; };
DBF000B11E7F521E006DB586 /* SwiftTestParserATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000A91E7F521E006DB586 /* SwiftTestParserATN.swift */; };
DBF000B31E7F59B3006DB586 /* TokenStreamRewriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000B21E7F59B3006DB586 /* TokenStreamRewriterTests.swift */; };
DBF000B91E7F627D006DB586 /* LexerA.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000B61E7F627D006DB586 /* LexerA.swift */; };
DBF000BA1E7F627D006DB586 /* LexerA.tokens in Resources */ = {isa = PBXBuildFile; fileRef = DBF000B71E7F627D006DB586 /* LexerA.tokens */; };
DBF000BB1E7F627D006DB586 /* LexerAATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000B81E7F627D006DB586 /* LexerAATN.swift */; };
DBF000C11E7F6546006DB586 /* LexerB.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000BE1E7F6546006DB586 /* LexerB.swift */; };
DBF000C21E7F6546006DB586 /* LexerB.tokens in Resources */ = {isa = PBXBuildFile; fileRef = DBF000BF1E7F6546006DB586 /* LexerB.tokens */; };
DBF000C31E7F6546006DB586 /* LexerBATN.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBF000C01E7F6546006DB586 /* LexerBATN.swift */; };
F9A424AD1BCD348C00A9CD35 /* Antlr4.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F9A4211C1BCCC35000A9CD35 /* Antlr4.framework */; };
/* End PBXBuildFile section */
@ -928,7 +922,6 @@
DBF000911E7F4D07006DB586 /* ShellScript */,
F9A424A41BCD348C00A9CD35 /* Sources */,
F9A424A51BCD348C00A9CD35 /* Frameworks */,
F9A424A61BCD348C00A9CD35 /* Resources */,
);
buildRules = (
);
@ -946,7 +939,7 @@
F9A421131BCCC35000A9CD35 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = jlabs;
TargetAttributes = {
F9A4211B1BCCC35000A9CD35 = {
@ -997,23 +990,10 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DBBAE7701E22DCAC00C2FEA6 /* Info-IOS.plist in Resources */,
DBBAE7721E22DCAC00C2FEA6 /* Info-OSX.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
F9A424A61BCD348C00A9CD35 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DBF000C21E7F6546006DB586 /* LexerB.tokens in Resources */,
DBF000BA1E7F627D006DB586 /* LexerA.tokens in Resources */,
DB0991A11E22DF3B00FE71AA /* Info.plist in Resources */,
DBF000AD1E7F521E006DB586 /* SwiftTestLexer.tokens in Resources */,
DBF000AA1E7F521E006DB586 /* SwiftTest.tokens in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "0820"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"

View File

@ -95,18 +95,20 @@ public class TokenStreamRewriter {
/** Token buffer index. */
internal var index: Int
internal var text: String?
weak var tokens: TokenStream!
internal var lastIndex: Int = 0
internal weak var tokens: TokenStream!
init(_ index: Int, _ tokens: TokenStream) {
self.index = index
self.tokens = tokens
}
//_ tokens : TokenStream ,_ tokens : TokenStream
init(_ index: Int, _ text: String?, _ tokens: TokenStream) {
self.index = index
self.text = text
self.tokens = tokens
}
/** Execute the rewrite operation by possibly adding to the buffer.
* Return the index of the next token to operate on.
*/
@ -115,10 +117,8 @@ public class TokenStreamRewriter {
}
public var description: String {
let opName: String = NSStringFromClass(RewriteOperation.self)
// var index : Int = opName.indexOf("$");
// opName = opName.substring( index+1);
return "<\(opName) @ \(try? tokens.get(index)):\\\(text)\">"
let opName = String(describing: type(of: self))
return "<\(opName)@\(try! tokens.get(index)):\"\(text!)\">"
}
}
@ -165,11 +165,10 @@ public class TokenStreamRewriter {
override
public var description: String {
if text == nil {
return "<DeleteOp@\(try? tokens.get(index))..\(try? tokens.get(lastIndex))>"
return "<DeleteOp@\(try! tokens.get(index))..\(try! tokens.get(lastIndex))>"
}
return "<ReplaceOp@\(try? tokens.get(index))..\(try? tokens.get(lastIndex)):\\\(text)>"
return "<ReplaceOp@\(try! tokens.get(index))..\(try! tokens.get(lastIndex)):\"\(text!)\">"
}
}
public class RewriteOperationArray{
@ -288,7 +287,8 @@ public class TokenStreamRewriter {
rop.lastIndex = max(prevRop.lastIndex, rop.lastIndex)
print("new rop \(rop)")
} else if !disjoint {
throw ANTLRError.illegalArgument(msg: "replace op boundaries of \(rop.description) overlap with previous \(prevRop.description)")
throw ANTLRError.illegalArgument(msg: "replace op boundaries of \(rop.description) " +
"overlap with previous \(prevRop.description)")
}
}
}
@ -333,7 +333,8 @@ public class TokenStreamRewriter {
continue
}
if iop.index >= rop.index && iop.index <= rop.lastIndex {
throw ANTLRError.illegalArgument(msg: "insert op \(iop.description) within boundaries of previous \(rop.description)")
throw ANTLRError.illegalArgument(msg: "insert op \(iop.description) within" +
" boundaries of previous \(rop.description)")
}
}

View File

@ -199,7 +199,6 @@ class TokenStreamRewriterTests: XCTestCase {
XCTAssertEqual(expecting, result)
}
// TODO: tweak printing
func testInsertInPriorReplace() throws {
let input = ANTLRInputStream("abc")
let lexer = LexerA(input)
@ -209,9 +208,9 @@ class TokenStreamRewriterTests: XCTestCase {
try tokens.replace(0, 2, "x")
tokens.insertBefore(1, "0")
var err: Error?
do {
_ = try tokens.getText()
XCTFail("Expected exception not thrown.")
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "insert op <InsertBeforeOp@[@1,1:1='b',<2>,1:1]:\"0\"> within boundaries of previous <ReplaceOp@[@0,0:0='a',<1>,1:0]..[@2,2:2='c',<3>,1:2]:\"x\">"
@ -311,7 +310,6 @@ class TokenStreamRewriterTests: XCTestCase {
XCTAssertEqual(expecting, result)
}
// TODO: tweak printing
func testReplaceRangeThenInsertAtRightEdge() throws {
let input = ANTLRInputStream("abcccba")
let lexer = LexerA(input)
@ -321,9 +319,9 @@ class TokenStreamRewriterTests: XCTestCase {
try tokens.replace(2, 4, "x")
tokens.insertBefore(4, "y")
var err: Error?
do {
_ = try tokens.getText()
XCTFail("Expected exception not thrown.")
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "insert op <InsertBeforeOp@[@4,4:4='c',<3>,1:4]:\"y\"> within boundaries of previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"x\">"
@ -368,43 +366,41 @@ class TokenStreamRewriterTests: XCTestCase {
XCTAssertEqual(expecting, result)
}
// TODO: tweak printing
func testReplaceThenReplaceSuperset() throws {
let input = ANTLRInputStream("abcccba")
let lexer = LexerA(input)
let stream = CommonTokenStream(lexer)
try stream.fill()
let tokens = TokenStreamRewriter(stream)
try tokens.replace(2, 4, "xyz")
try tokens.replace(3, 5, "foo")
func testReplaceThenReplaceSuperset() throws {
let input = ANTLRInputStream("abcccba")
let lexer = LexerA(input)
let stream = CommonTokenStream(lexer)
try stream.fill()
let tokens = TokenStreamRewriter(stream)
try tokens.replace(2, 4, "xyz")
try tokens.replace(3, 5, "foo")
var err: Error?
do {
_ = try tokens.getText()
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "replace op boundaries of <ReplaceOp@[@3,3:3='c',<3>,1:3]..[@5,5:5='b',<2>,1:5]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">"
XCTAssertEqual(expecting, msg)
}
}
do {
_ = try tokens.getText()
XCTFail("Expected exception not thrown.")
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "replace op boundaries of <ReplaceOp@[@3,3:3='c',<3>,1:3]..[@5,5:5='b',<2>,1:5]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">"
XCTAssertEqual(expecting, msg)
}
}
// TODO: tweak printing
func testReplaceThenReplaceLowerIndexedSuperset() throws {
let input = ANTLRInputStream("abcccba")
let lexer = LexerA(input)
let stream = CommonTokenStream(lexer)
try stream.fill()
let tokens = TokenStreamRewriter(stream)
try tokens.replace(2, 4, "xyz")
try tokens.replace(1, 3, "foo")
func testReplaceThenReplaceLowerIndexedSuperset() throws {
let input = ANTLRInputStream("abcccba")
let lexer = LexerA(input)
let stream = CommonTokenStream(lexer)
try stream.fill()
let tokens = TokenStreamRewriter(stream)
try tokens.replace(2, 4, "xyz")
try tokens.replace(1, 3, "foo")
var err: Error?
do {
_ = try tokens.getText()
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<2>,1:1]..[@3,3:3='c',<3>,1:3]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">"
XCTAssertEqual(expecting, msg)
}
}
do {
_ = try tokens.getText()
XCTFail("Expected exception not thrown.")
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<2>,1:1]..[@3,3:3='c',<3>,1:3]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">"
XCTAssertEqual(expecting, msg)
}
}
func testReplaceSingleMiddleThenOverlappingSuperset() throws {
let input = ANTLRInputStream("abcba")
@ -519,9 +515,9 @@ class TokenStreamRewriterTests: XCTestCase {
try stream.fill()
// cannot split earlier replace
var err: Error?
do {
_ = try tokens.getText()
XCTFail("Expected exception not thrown.")
} catch ANTLRError.illegalArgument(let msg) {
let expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<2>,1:1]..[@2,2:2='c',<3>,1:2]:\"foo\"> overlap with previous <ReplaceOp@[@0,0:0='a',<1>,1:0]..[@3,3:3='c',<3>,1:3]:\"bar\">"
XCTAssertEqual(expecting, msg)
@ -627,7 +623,6 @@ class TokenStreamRewriterTests: XCTestCase {
XCTAssertEqual(expecting, result)
}
// TODO: Test Fix for https://github.com/antlr/antlr4/issues/550
func testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder() throws {
let input = ANTLRInputStream("aa")
let lexer = LexerA(input)