From 783c3b2e5062280ac8270c247f84ccaced4cb520 Mon Sep 17 00:00:00 2001 From: sharwell Date: Wed, 9 Nov 2011 10:16:51 -0800 Subject: [PATCH] v4: * Use mark->seek->release instead of mark->release->seek * mark() returns a marker. must pass the result of index() to seek(), and the result of mark() to release(). [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9268] --- .../antlr/v4/runtime/atn/LexerATNSimulator.java | 2 +- .../antlr/v4/runtime/atn/ParserATNSimulator.java | 8 ++++++-- .../org/antlr/v4/test/TestASTNodeStream.java | 16 ++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index 6b72e004c..5fbf03ddb 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -308,8 +308,8 @@ public class LexerATNSimulator extends ATNSimulator { if ( actionIndex>=0 && recog!=null ) recog.action(null, ruleIndex, actionIndex); // seek to after last char in token - input.release(prevAccept.marker); input.seek(prevAccept.index); + input.release(prevAccept.marker); line = prevAccept.line; charPositionInLine = prevAccept.charPos; consume(input); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index fe45d61c7..175bd35df 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -96,12 +96,14 @@ public class ParserATNSimulator extends ATNSimulator { //dump(dfa); // start with the DFA int m = input.mark(); + int index = input.index(); try { int alt = execDFA(input, dfa, dfa.s0, outerContext); return alt; } finally { - input.seek(m); + input.seek(index); + input.release(m); } } } @@ -127,6 +129,7 @@ public class ParserATNSimulator extends ATNSimulator { int alt = 0; int m = input.mark(); + int index = input.index(); try { alt = execATN(input, dfa, m, s0_closure, useContext); } @@ -135,7 +138,8 @@ public class ParserATNSimulator extends ATNSimulator { throw nvae; } finally { - input.seek(m); + input.seek(index); + input.release(m); } if ( debug ) System.out.println("DFA after predictATN: "+dfa.toString()); return alt; diff --git a/tool/test/org/antlr/v4/test/TestASTNodeStream.java b/tool/test/org/antlr/v4/test/TestASTNodeStream.java index 9b34a0173..1a76b3857 100644 --- a/tool/test/org/antlr/v4/test/TestASTNodeStream.java +++ b/tool/test/org/antlr/v4/test/TestASTNodeStream.java @@ -182,13 +182,14 @@ public class TestASTNodeStream extends BaseTest { ASTNodeStream stream = newStream(r0); int m = stream.mark(); // MARK + int index = stream.index(); for (int k=1; k<=13; k++) { // consume til end stream.LT(1); stream.consume(); } assertEquals(Token.EOF, ((AST)stream.LT(1)).getType()); + stream.seek(index); stream.release(m); - stream.seek(m); // i know it's an index // consume til end again :) for (int k=1; k<=13; k++) { // consume til end @@ -219,12 +220,13 @@ public class TestASTNodeStream extends BaseTest { } assertEquals(107, ((AST)stream.LT(1)).getType()); int m = stream.mark(); // MARK + int index = stream.index(); stream.consume(); // consume 107 stream.consume(); // consume UP stream.consume(); // consume UP stream.consume(); // consume 104 + stream.seek(index); stream.release(m); // REWIND - stream.seek(m); stream.mark(); // keep saving nodes though assertEquals(107, ((AST)stream.LT(1)).getType()); @@ -260,22 +262,24 @@ public class TestASTNodeStream extends BaseTest { ASTNodeStream stream = newStream(r0); int m = stream.mark(); // MARK at start + int index = stream.index(); stream.consume(); // consume 101 stream.consume(); // consume DN int m2 = stream.mark(); // MARK on 102 + int index2 = stream.index(); stream.consume(); // consume 102 stream.consume(); // consume DN stream.consume(); // consume 103 stream.consume(); // consume 106 - stream.release(m2); // REWIND to 102 - stream.seek(m2); + stream.seek(index2); // REWIND to 102 + stream.release(m2); assertEquals(102, ((AST)stream.LT(1)).getType()); stream.consume(); assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); stream.consume(); // stop at 103 and rewind to start - stream.release(m); // REWIND to 101 - stream.seek(m); + stream.seek(index); // REWIND to 101 + stream.release(m); assertEquals(101, ((AST)stream.LT(1)).getType()); stream.consume(); assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType());