From f278d3b45334e44f6c0277d89b4988cac9d7469c Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 11 Feb 2012 11:35:27 -0800 Subject: [PATCH] add unit test for left-recur grammars --- .../Java/src/org/antlr/v4/runtime/Lexer.java | 9 +++---- .../org/antlr/v4/test/TestLeftRecursion.java | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java index aada783d8..6d4132782 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java @@ -88,7 +88,7 @@ public abstract class Lexer extends Recognizer /** The token type for the current token */ public int _type; - public ArrayDeque _modeStack; + public ArrayDeque _modeStack = new ArrayDeque(); 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 _hitEOF = false; _mode = Lexer.DEFAULT_MODE; - if (_modeStack != null) { - _modeStack.clear(); - } + _modeStack.clear(); getInterpreter().reset(); } @@ -184,14 +182,13 @@ public abstract class Lexer extends Recognizer public void pushMode(int m) { if ( LexerATNSimulator.debug ) System.out.println("pushMode "+m); - if ( _modeStack ==null ) _modeStack = new ArrayDeque(); 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() ); diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 4a4c12a22..ffdb45941 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -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",