forked from jasder/antlr
add unit test for left-recur grammars
This commit is contained in:
parent
46094f57ba
commit
f278d3b453
|
@ -88,7 +88,7 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
|
|||
/** The token type for the current token */
|
||||
public int _type;
|
||||
|
||||
public ArrayDeque<Integer> _modeStack;
|
||||
public ArrayDeque<Integer> _modeStack = new ArrayDeque<Integer>();
|
||||
public int _mode = Lexer.DEFAULT_MODE;
|
||||
|
||||
/** You can set the text for the current token to override what is in
|
||||
|
@ -115,9 +115,7 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
|
|||
|
||||
_hitEOF = false;
|
||||
_mode = Lexer.DEFAULT_MODE;
|
||||
if (_modeStack != null) {
|
||||
_modeStack.clear();
|
||||
}
|
||||
|
||||
getInterpreter().reset();
|
||||
}
|
||||
|
@ -184,14 +182,13 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
|
|||
|
||||
public void pushMode(int m) {
|
||||
if ( LexerATNSimulator.debug ) System.out.println("pushMode "+m);
|
||||
if ( _modeStack ==null ) _modeStack = new ArrayDeque<Integer>();
|
||||
getInterpreter().tracePushMode(m);
|
||||
_modeStack.push(_mode);
|
||||
mode(m);
|
||||
}
|
||||
|
||||
public int popMode() {
|
||||
if ( _modeStack ==null ) throw new EmptyStackException();
|
||||
if ( _modeStack.isEmpty() ) throw new EmptyStackException();
|
||||
if ( LexerATNSimulator.debug ) System.out.println("popMode back to "+ _modeStack.peek());
|
||||
getInterpreter().tracePopMode();
|
||||
mode( _modeStack.pop() );
|
||||
|
|
|
@ -228,6 +228,33 @@ public class TestLeftRecursion extends BaseTest {
|
|||
runTests(grammar, tests, "s");
|
||||
}
|
||||
|
||||
@Test public void testReturnValueAndActionsAndLabels() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"s : q=e {System.out.println($e.v);} ;\n" +
|
||||
"\n" +
|
||||
"e returns [int v]\n" +
|
||||
" : a=e op='*' b=e {$v = $a.v * $b.v;} -> mult\n" +
|
||||
" | a=e '+' b=e {$v = $a.v + $b.v;} -> add\n" +
|
||||
" | INT {$v = $INT.int;}\n" +
|
||||
" | '(' x=e ')' {$v = $x.v;}\n" +
|
||||
" | x=e '++' {$v = $x.v+1;} -> inc\n" +
|
||||
" | e '--'\n" +
|
||||
" | ID {$v = 3;} -> anID\n" +
|
||||
" ; \n" +
|
||||
"\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+ ;\n" +
|
||||
"WS : (' '|'\\n') {skip();} ;\n";
|
||||
String[] tests = {
|
||||
"4", "4",
|
||||
"1+2", "3",
|
||||
"1+2*3", "7",
|
||||
"i++*3", "12",
|
||||
};
|
||||
runTests(grammar, tests, "s");
|
||||
}
|
||||
|
||||
public void runTests(String grammar, String[] tests, String startRule) {
|
||||
rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer");
|
||||
writeRecognizerAndCompile("TParser",
|
||||
|
|
Loading…
Reference in New Issue