diff --git a/CHANGES.txt b/CHANGES.txt index 28fbc2c9bd..82936d6771 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,13 @@ ANTLR v4 Honey Badger early access +Feb 17, 2012 + +* names changed. visit() -> visitX(). enter/exit() -> enter/exitX() +* capitalizing automatically now. rule s -> SContext not sContext +* no enter/exit method in generic rule context object if rule has alt labels, nor in interfaces. +* dup labels allowed in same rule +* label X or x illegal if rule x exists + Feb 14, 2012 * Fixed https://github.com/antlr/antlr4/issues/8 and lots of other little things. diff --git a/tool/playground/A.g4 b/tool/playground/A.g4 index 9c2d6ea73d..c62a6be7c9 100644 --- a/tool/playground/A.g4 +++ b/tool/playground/A.g4 @@ -4,9 +4,17 @@ s : e ; e : e '*' e -> Mult | e '+' e -> Add - | INT -> Int + | INT -> primary + | ID -> primary | '(' e ')' -> Parens ; +/* +primary : EEE; + +add : A ; +*/ + +ID : [a-z]+ ; INT : [0-9]+ ; WS : [ \t\n]+ -> skip ; diff --git a/tool/playground/AVisitor.java b/tool/playground/AVisitor.java index 5730fa2d6b..42686165fa 100644 --- a/tool/playground/AVisitor.java +++ b/tool/playground/AVisitor.java @@ -1,10 +1,7 @@ -import org.antlr.v4.runtime.tree.*; -import org.antlr.v4.runtime.Token; - public interface AVisitor { - T visit(AParser.MultContext ctx); - T visit(AParser.ParensContext ctx); - T visit(AParser.sContext ctx); - T visit(AParser.AddContext ctx); - T visit(AParser.IntContext ctx); + T visitMult(AParser.MultContext ctx); + T visitParens(AParser.ParensContext ctx); + T visitS(AParser.SContext ctx); + T visitPrimary(AParser.PrimaryContext ctx); + T visitAdd(AParser.AddContext ctx); } \ No newline at end of file diff --git a/tool/playground/TestA.java b/tool/playground/TestA.java index 5548162731..8798d31bf4 100644 --- a/tool/playground/TestA.java +++ b/tool/playground/TestA.java @@ -27,13 +27,9 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import org.antlr.v4.runtime.ANTLRFileStream; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.antlr.v4.runtime.*; -import java.util.Stack; +import java.io.*; public class TestA { /** An example listener that uses a stack to store return values @@ -66,45 +62,112 @@ public class TestA { * Compare this to A2.g4, which adds a field to the context objects * by using a "returns [int v]" on the expression rule. */ - public static class Do extends ABaseListener { - Stack results = new Stack(); +// public static class Do extends ABaseListener { +// Stack results = new Stack(); +// +// @Override +// public void exit(AParser.AddContext ctx) { +// results.push( results.pop() + results.pop() ); +// System.out.println("Add: " + results.peek()); +// } +// +// @Override +// public void exit(AParser.IntContext ctx) { +// results.push( Integer.valueOf(ctx.INT().getText()) ); +// System.out.println("Int: "+results.peek()); +// } +// +// @Override +// public void exit(AParser.MultContext ctx) { +// results.push( results.pop() * results.pop() ); +// System.out.println("Mult: " + results.peek()); +// } +// +// @Override +// public void exit(AParser.ParensContext ctx) { +// // result already on stack +// System.out.println("Parens: "+results.peek()); +// } +// } + public static class Tracer extends ABaseListener { + @Override + public void enterAdd(AParser.AddContext ctx) { + printMethodName(ctx); + } + + @Override + public void enterEveryRule(ParserRuleContext ctx) { + printMethodName(ctx); + } + + @Override + public void enterMult(AParser.MultContext ctx) { + printMethodName(ctx); + } + + @Override + public void enterParens(AParser.ParensContext ctx) { + printMethodName(ctx); + } @Override - public void exit(AParser.AddContext ctx) { - results.push( results.pop() + results.pop() ); - System.out.println("Add: " + results.peek()); + public void enterS(AParser.SContext ctx) { + printMethodName(ctx); } @Override - public void exit(AParser.IntContext ctx) { - results.push( Integer.valueOf(ctx.INT().getText()) ); - System.out.println("Int: "+results.peek()); + public void exitAdd(AParser.AddContext ctx) { + printMethodName(ctx); } @Override - public void exit(AParser.MultContext ctx) { - results.push( results.pop() * results.pop() ); - System.out.println("Mult: " + results.peek()); + public void exitMult(AParser.MultContext ctx) { + printMethodName(ctx); } @Override - public void exit(AParser.ParensContext ctx) { - // result already on stack - System.out.println("Parens: "+results.peek()); + public void exitParens(AParser.ParensContext ctx) { + printMethodName(ctx); + } + + @Override + public void exitS(AParser.SContext ctx) { + printMethodName(ctx); + } + + @Override + public void exitEveryRule(ParserRuleContext ctx) { + printMethodName(ctx); + } + + @Override + public void visitTerminal(ParserRuleContext ctx, Token symbol) { + printMethodName(ctx); + } + + public void printMethodName(ParserRuleContext ctx) { + Throwable t = new Throwable(); + StackTraceElement[] stack = t.getStackTrace(); + String m = stack[1].getMethodName(); + System.out.println(m+"("+ctx.getClass().getSimpleName()+")"); } } public static void main(String[] args) throws Exception { - ALexer lexer = new ALexer(new ANTLRFileStream(args[0])); + InputStream is = System.in; + if ( args.length>0 && args[0]!=null ) { + is = new FileInputStream(args[0]); + } + ALexer lexer = new ALexer(new ANTLRInputStream(is)); CommonTokenStream tokens = new CommonTokenStream(lexer); AParser p = new AParser(tokens); p.setBuildParseTree(true); - p.addParseListener(new Do()); + p.addParseListener(new Tracer()); ParserRuleContext t = p.s(); System.out.println("tree = "+t.toStringTree(p)); - ParseTreeWalker walker = new ParseTreeWalker(); - Do doer = new Do(); - walker.walk(doer, t); - System.out.println("result from tree walk = "+ doer.results.pop()); +// ParseTreeWalker walker = new ParseTreeWalker(); +// Do doer = new Do(); +// walker.walk(doer, t); +// System.out.println("result from tree walk = "+ doer.results.pop()); } } diff --git a/tool/playground/TestVisitor.java b/tool/playground/TestVisitor.java index 6d7e240cb5..4fd8f71f3d 100644 --- a/tool/playground/TestVisitor.java +++ b/tool/playground/TestVisitor.java @@ -28,33 +28,27 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT */ import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.ParseTreeVisitor; public class TestVisitor { public static class MyVisitor extends ABaseVisitor implements AVisitor { @Override - public Integer visit(AParser.AddContext ctx) { + public Integer visitAdd(AParser.AddContext ctx) { return ctx.e(0).accept(this) + ctx.e(1).accept(this); } @Override - public Integer visit(AParser.IntContext ctx) { - return Integer.valueOf(ctx.INT().getText()); - } - - @Override - public Integer visit(AParser.MultContext ctx) { + public Integer visitMult(AParser.MultContext ctx) { // return ctx.e(0).accept(this) * ctx.e(1).accept(this); return visit(ctx.e(0)) * visit(ctx.e(1)); } @Override - public Integer visit(AParser.ParensContext ctx) { + public Integer visitParens(AParser.ParensContext ctx) { return ctx.e().accept(this); } @Override - public Integer visit(AParser.sContext ctx) { + public Integer visitS(AParser.SContext ctx) { return visit(ctx.e()); //return ctx.e().accept(this); } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 6fd56d759f..8896567ec0 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -34,8 +34,8 @@ import org.antlr.v4.runtime.Token; public interface Listener extends ParseTreeListener\<\> { .Context ctx); -void exit(.Context ctx);}; separator="\n"> +void enter(.Context ctx); +void exit(.Context ctx);}; separator="\n"> } >> @@ -47,8 +47,8 @@ import org.antlr.v4.runtime.Token; public class BaseListener implements Listener { .Context ctx) { \} -@Override public void exit(.Context ctx) { \}}; separator="\n"> +@Override public void enter(.Context ctx) { \} +@Override public void exit(.Context ctx) { \}}; separator="\n"> @Override public void enterEveryRule(ParserRuleContext\<\> ctx) { } @Override public void exitEveryRule(ParserRuleContext\<\> ctx) { } @@ -63,7 +63,7 @@ import org.antlr.v4.runtime.Token; public interface Visitor\ { .Context ctx);}; separator="\n"> +T visit(.Context ctx);}; separator="\n"> } >> @@ -74,7 +74,7 @@ import org.antlr.v4.runtime.Token; public class BaseVisitor\ extends ParseTreeVisitor\ implements Visitor\ { .Context ctx) { visitChildren(ctx); return null; \}}; separator="\n"> +public T visit(.Context ctx) { visitChildren(ctx); return null; \}}; separator="\n"> } >> @@ -237,7 +237,7 @@ LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs, >> CodeBlockForOuterMostAlt(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= << -_localctx = new Context(_localctx); +_localctx = new Context(_localctx); enterOuterAlt(_localctx, ); >> @@ -503,9 +503,9 @@ ThisRulePropertyRef_stop(r) ::= "_localctx.stop" ThisRulePropertyRef_text(r) ::= "_input.toString(_localctx.start, _input.LT(-1))" ThisRulePropertyRef_ctx(r) ::= "_localctx" -NonLocalAttrRef(s) ::= "((Context)getInvokingContext())." +NonLocalAttrRef(s) ::= "((Context)getInvokingContext())." SetNonLocalAttr(s, rhsChunks) ::= - "((Context)getInvokingContext()). = ;" + "((Context)getInvokingContext()). = ;" AddToLabelList(a) ::= "..add();" @@ -526,17 +526,17 @@ public Token (int i) { >> ContextRuleGetterDecl(r) ::= << public () { - return ()getRuleContext(.class,0); + return getRuleContext(.class,0); } >> ContextRuleListGetterDecl(r) ::= << public List\\> () { - return (List\<\>)getRuleContexts(.class); + return getRuleContexts(.class); } >> ContextRuleListIndexedGetterDecl(r) ::= << public (int i) { - return ()getRuleContext(.class,i); + return getRuleContext(.class,i); } >> @@ -555,21 +555,21 @@ ListLabelName(label) ::= "