* 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]
This commit is contained in:
sharwell 2011-11-09 10:16:51 -08:00
parent 2767530a47
commit 783c3b2e50
3 changed files with 17 additions and 9 deletions

View File

@ -308,8 +308,8 @@ public class LexerATNSimulator extends ATNSimulator {
if ( actionIndex>=0 && recog!=null ) recog.action(null, ruleIndex, actionIndex); if ( actionIndex>=0 && recog!=null ) recog.action(null, ruleIndex, actionIndex);
// seek to after last char in token // seek to after last char in token
input.release(prevAccept.marker);
input.seek(prevAccept.index); input.seek(prevAccept.index);
input.release(prevAccept.marker);
line = prevAccept.line; line = prevAccept.line;
charPositionInLine = prevAccept.charPos; charPositionInLine = prevAccept.charPos;
consume(input); consume(input);

View File

@ -96,12 +96,14 @@ public class ParserATNSimulator extends ATNSimulator {
//dump(dfa); //dump(dfa);
// start with the DFA // start with the DFA
int m = input.mark(); int m = input.mark();
int index = input.index();
try { try {
int alt = execDFA(input, dfa, dfa.s0, outerContext); int alt = execDFA(input, dfa, dfa.s0, outerContext);
return alt; return alt;
} }
finally { finally {
input.seek(m); input.seek(index);
input.release(m);
} }
} }
} }
@ -127,6 +129,7 @@ public class ParserATNSimulator extends ATNSimulator {
int alt = 0; int alt = 0;
int m = input.mark(); int m = input.mark();
int index = input.index();
try { try {
alt = execATN(input, dfa, m, s0_closure, useContext); alt = execATN(input, dfa, m, s0_closure, useContext);
} }
@ -135,7 +138,8 @@ public class ParserATNSimulator extends ATNSimulator {
throw nvae; throw nvae;
} }
finally { finally {
input.seek(m); input.seek(index);
input.release(m);
} }
if ( debug ) System.out.println("DFA after predictATN: "+dfa.toString()); if ( debug ) System.out.println("DFA after predictATN: "+dfa.toString());
return alt; return alt;

View File

@ -182,13 +182,14 @@ public class TestASTNodeStream extends BaseTest {
ASTNodeStream stream = newStream(r0); ASTNodeStream stream = newStream(r0);
int m = stream.mark(); // MARK int m = stream.mark(); // MARK
int index = stream.index();
for (int k=1; k<=13; k++) { // consume til end for (int k=1; k<=13; k++) { // consume til end
stream.LT(1); stream.LT(1);
stream.consume(); stream.consume();
} }
assertEquals(Token.EOF, ((AST)stream.LT(1)).getType()); assertEquals(Token.EOF, ((AST)stream.LT(1)).getType());
stream.seek(index);
stream.release(m); stream.release(m);
stream.seek(m); // i know it's an index
// consume til end again :) // consume til end again :)
for (int k=1; k<=13; k++) { // consume til end 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()); assertEquals(107, ((AST)stream.LT(1)).getType());
int m = stream.mark(); // MARK int m = stream.mark(); // MARK
int index = stream.index();
stream.consume(); // consume 107 stream.consume(); // consume 107
stream.consume(); // consume UP stream.consume(); // consume UP
stream.consume(); // consume UP stream.consume(); // consume UP
stream.consume(); // consume 104 stream.consume(); // consume 104
stream.seek(index);
stream.release(m); // REWIND stream.release(m); // REWIND
stream.seek(m);
stream.mark(); // keep saving nodes though stream.mark(); // keep saving nodes though
assertEquals(107, ((AST)stream.LT(1)).getType()); assertEquals(107, ((AST)stream.LT(1)).getType());
@ -260,22 +262,24 @@ public class TestASTNodeStream extends BaseTest {
ASTNodeStream stream = newStream(r0); ASTNodeStream stream = newStream(r0);
int m = stream.mark(); // MARK at start int m = stream.mark(); // MARK at start
int index = stream.index();
stream.consume(); // consume 101 stream.consume(); // consume 101
stream.consume(); // consume DN stream.consume(); // consume DN
int m2 = stream.mark(); // MARK on 102 int m2 = stream.mark(); // MARK on 102
int index2 = stream.index();
stream.consume(); // consume 102 stream.consume(); // consume 102
stream.consume(); // consume DN stream.consume(); // consume DN
stream.consume(); // consume 103 stream.consume(); // consume 103
stream.consume(); // consume 106 stream.consume(); // consume 106
stream.release(m2); // REWIND to 102 stream.seek(index2); // REWIND to 102
stream.seek(m2); stream.release(m2);
assertEquals(102, ((AST)stream.LT(1)).getType()); assertEquals(102, ((AST)stream.LT(1)).getType());
stream.consume(); stream.consume();
assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType());
stream.consume(); stream.consume();
// stop at 103 and rewind to start // stop at 103 and rewind to start
stream.release(m); // REWIND to 101 stream.seek(index); // REWIND to 101
stream.seek(m); stream.release(m);
assertEquals(101, ((AST)stream.LT(1)).getType()); assertEquals(101, ((AST)stream.LT(1)).getType());
stream.consume(); stream.consume();
assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType()); assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType());