From a06679fec747d00ef0646486e2ce1ebbb75d3f44 Mon Sep 17 00:00:00 2001 From: parrt Date: Sat, 9 Jul 2011 17:00:15 -0800 Subject: [PATCH] cleaned up [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8849] --- .../v4/tool/templates/codegen/Java/Java.stg | 12 +- .../antlr/v4/codegen/ParserASTExtension.java | 145 ++++++++++-------- .../org/antlr/v4/codegen/ParserFactory.java | 4 +- .../{RewriteAddChild.java => AddChild.java} | 4 +- ...RewriteBecomeRoot.java => BecomeRoot.java} | 4 +- .../ast/{AddRuleLeaf.java => RuleAST.java} | 4 +- .../v4/codegen/model/ast/RuleBecomeRoot.java | 40 ----- .../ast/{AddTokenLeaf.java => TokenAST.java} | 4 +- .../v4/codegen/model/ast/TokenBecomeRoot.java | 41 ----- 9 files changed, 98 insertions(+), 160 deletions(-) rename tool/src/org/antlr/v4/codegen/model/ast/{RewriteAddChild.java => AddChild.java} (92%) rename tool/src/org/antlr/v4/codegen/model/ast/{RewriteBecomeRoot.java => BecomeRoot.java} (92%) rename tool/src/org/antlr/v4/codegen/model/ast/{AddRuleLeaf.java => RuleAST.java} (92%) delete mode 100644 tool/src/org/antlr/v4/codegen/model/ast/RuleBecomeRoot.java rename tool/src/org/antlr/v4/codegen/model/ast/{AddTokenLeaf.java => TokenAST.java} (92%) delete mode 100644 tool/src/org/antlr/v4/codegen/model/ast/TokenBecomeRoot.java 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 4c323606c..bcdbcce83 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 @@ -365,12 +365,10 @@ labelref(x) ::= "_localctx." // AST stuff (TODO: separate?) RootDecl(d) ::= "Object = _adaptor.nil();" -RootName(level) ::= "_root" +RootName(level) ::= "_root" -AddTokenLeaf(a) ::= "_adaptor.addChild(_root0, _adaptor.create());" -AddRuleLeaf(a) ::= "_adaptor.addChild(_root0, .tree);" -RuleBecomeRoot(r) ::= "_root0 = _adaptor.becomeRoot(.tree, _root0);" -TokenBecomeRoot(t) ::= "_root0 = _adaptor.becomeRoot(_adaptor.create(), _root0);" +TokenAST(t) ::= "_adaptor.create()" +RuleAST(r) ::= ".tree" AssignTreeResult(a) ::= "_localctx.tree = _root0;" RuleASTCleanup(r) ::= << _localctx.tree = _adaptor.rulePostProcessing(_localctx.tree); @@ -444,10 +442,10 @@ RewriteSelfRuleLabelRef(s) ::= "_localctx.tree" RewriteAction(a, chunks) ::= "" /** how to add child in rewrite section */ -RewriteAddChild(x, rootName, child) ::= "_adaptor.addChild(, );" +AddChild(x, rootName, child) ::= "_adaptor.addChild(, );" /** how to make something a new root in rewrite section */ -RewriteBecomeRoot(x, newRoot) ::= +BecomeRoot(x, rootName, newRoot) ::= " = _adaptor.becomeRoot(, );" diff --git a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java b/tool/src/org/antlr/v4/codegen/ParserASTExtension.java index 741c441e5..856855c99 100644 --- a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java +++ b/tool/src/org/antlr/v4/codegen/ParserASTExtension.java @@ -31,6 +31,7 @@ package org.antlr.v4.codegen; import org.antlr.v4.codegen.model.*; import org.antlr.v4.codegen.model.ast.*; +import org.antlr.v4.codegen.model.ast.RuleAST; import org.antlr.v4.codegen.model.decl.*; import org.antlr.v4.misc.Utils; import org.antlr.v4.parse.ANTLRParser; @@ -71,8 +72,10 @@ public class ParserASTExtension extends CodeGeneratorExtension { } else { InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); - SrcOp treeOp = new RuleBecomeRoot(factory, invokeOp.ast, invokeOp.getLabels().get(0)); - return DefaultOutputModelFactory.list(ops, treeOp); + SrcOp treeOp = new RuleAST(factory, invokeOp.ast, invokeOp.getLabels().get(0)); + String rootName = factory.getGenerator().target.getRootName(0); + SrcOp add = new BecomeRoot(factory, rootName, treeOp); + return DefaultOutputModelFactory.list(ops, add); } } @@ -84,8 +87,10 @@ public class ParserASTExtension extends CodeGeneratorExtension { } else { MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); - SrcOp treeOp = new TokenBecomeRoot(factory, matchOp.ast, matchOp.getLabels().get(0)); - return DefaultOutputModelFactory.list(ops, treeOp); + SrcOp treeOp = new TokenAST(factory, matchOp.ast, matchOp.getLabels().get(0)); + String rootName = factory.getGenerator().target.getRootName(0); + SrcOp add = new BecomeRoot(factory, rootName, treeOp); + return DefaultOutputModelFactory.list(ops, add); } } @@ -93,77 +98,93 @@ public class ParserASTExtension extends CodeGeneratorExtension { public List leafRule(List ops) { InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); Alternative alt = factory.getCurrentAlt(); - RuleContextDecl label = (RuleContextDecl)invokeOp.getLabels().get(0); if ( alt.hasRewrite() ) { - CodeBlock blk = factory.getCurrentAlternativeBlock(); - String elemListName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); - blk.addLocalDecl(new ElementListDecl(factory, elemListName)); - // track any explicit label like _track_label but not implicit label - if ( !label.isImplicit ) { - String labelListName = - factory.getGenerator().target.getElementListName(label.name); - blk.addLocalDecl(new ElementListDecl(factory, labelListName)); - } - String trackName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); - TrackRuleElement t = new TrackRuleElement(factory, invokeOp.ast, trackName, label); - ops.add(t); - if ( !label.isImplicit ) { - trackName = factory.getGenerator().target.getElementListName(label.name); - TrackRuleElement t2 = new TrackRuleElement(factory, invokeOp.ast, trackName, - label); - if ( invokeOp.ast.parent.getType() == ANTLRParser.ASSIGN ) { - // if x=A must keep it a single-element list; clear before add - ClearElementList c = new ClearElementList(factory, invokeOp.ast, trackName); - ops.add(c); - } - ops.add(t2); - } + return leafRuleInRewriteAlt(invokeOp, ops); } else { - SrcOp treeOp = new AddRuleLeaf(factory, invokeOp.ast, label); - ops.add(treeOp); + RuleContextDecl label = (RuleContextDecl)invokeOp.getLabels().get(0); + SrcOp treeOp = new RuleAST(factory, invokeOp.ast, label); + String rootName = factory.getGenerator().target.getRootName(0); + SrcOp add = new AddChild(factory, rootName, treeOp); + ops.add(add); + return ops; } - return ops; } @Override public List leafToken(List ops) { MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); - TokenDecl label = (TokenDecl)matchOp.getLabels().get(0); Alternative alt = factory.getCurrentAlt(); if ( alt.hasRewrite() ) { - CodeBlock blk = factory.getCurrentAlternativeBlock(); - // First declare tracking lists for elements, labels - // track the named element like _track_A - String elemListName = factory.getGenerator().target.getElementListName(matchOp.ast.getText()); - blk.addLocalDecl(new ElementListDecl(factory, elemListName)); - // track any explicit label like _track_label but not implicit label - if ( !label.isImplicit ) { - String labelListName = - factory.getGenerator().target.getElementListName(label.name); - blk.addLocalDecl(new ElementListDecl(factory, labelListName)); - } - // Now, generate track instructions for element and any labels - // do element - String trackName = factory.getGenerator().target.getElementListName(matchOp.ast.getText()); - TrackTokenElement t = new TrackTokenElement(factory, matchOp.ast, trackName, - label); - ops.add(t); - if ( !label.isImplicit ) { // track all explicit labels - trackName = factory.getGenerator().target.getElementListName(label.name); - TrackTokenElement t2 = new TrackTokenElement(factory, matchOp.ast, trackName, - label); - if ( matchOp.ast.parent.getType() == ANTLRParser.ASSIGN ) { - // if x=A must keep it a single-element list; clear before add - ClearElementList c = new ClearElementList(factory, matchOp.ast, trackName); - ops.add(c); - } - ops.add(t2); - } + return leafTokenInRewriteAlt(matchOp, ops); } else { - SrcOp treeOp = new AddTokenLeaf(factory, matchOp.ast, label); - ops.add(treeOp); + TokenDecl label = (TokenDecl)matchOp.getLabels().get(0); + SrcOp treeOp = new TokenAST(factory, matchOp.ast, label); + String rootName = factory.getGenerator().target.getRootName(0); + SrcOp add = new AddChild(factory, rootName, treeOp); + ops.add(add); + return ops; + } + } + + public List leafRuleInRewriteAlt(InvokeRule invokeOp, List ops) { + RuleContextDecl label = (RuleContextDecl)invokeOp.getLabels().get(0); + CodeBlock blk = factory.getCurrentAlternativeBlock(); + String elemListName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); + blk.addLocalDecl(new ElementListDecl(factory, elemListName)); + // track any explicit label like _track_label but not implicit label + if ( !label.isImplicit ) { + String labelListName = + factory.getGenerator().target.getElementListName(label.name); + blk.addLocalDecl(new ElementListDecl(factory, labelListName)); + } + String trackName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText()); + TrackRuleElement t = new TrackRuleElement(factory, invokeOp.ast, trackName, label); + ops.add(t); + if ( !label.isImplicit ) { + trackName = factory.getGenerator().target.getElementListName(label.name); + TrackRuleElement t2 = new TrackRuleElement(factory, invokeOp.ast, trackName, + label); + if ( invokeOp.ast.parent.getType() == ANTLRParser.ASSIGN ) { + // if x=A must keep it a single-element list; clear before add + ClearElementList c = new ClearElementList(factory, invokeOp.ast, trackName); + ops.add(c); + } + ops.add(t2); + } + return ops; + } + + public List leafTokenInRewriteAlt(MatchToken matchOp, List ops) { + CodeBlock blk = factory.getCurrentAlternativeBlock(); + TokenDecl label = (TokenDecl)matchOp.getLabels().get(0); + // First declare tracking lists for elements, labels + // track the named element like _track_A + String elemListName = factory.getGenerator().target.getElementListName(matchOp.ast.getText()); + blk.addLocalDecl(new ElementListDecl(factory, elemListName)); + // track any explicit label like _track_label but not implicit label + if ( !label.isImplicit ) { + String labelListName = + factory.getGenerator().target.getElementListName(label.name); + blk.addLocalDecl(new ElementListDecl(factory, labelListName)); + } + // Now, generate track instructions for element and any labels + // do element + String trackName = factory.getGenerator().target.getElementListName(matchOp.ast.getText()); + TrackTokenElement t = new TrackTokenElement(factory, matchOp.ast, trackName, + label); + ops.add(t); + if ( !label.isImplicit ) { // track all explicit labels + trackName = factory.getGenerator().target.getElementListName(label.name); + TrackTokenElement t2 = new TrackTokenElement(factory, matchOp.ast, trackName, + label); + if ( matchOp.ast.parent.getType() == ANTLRParser.ASSIGN ) { + // if x=A must keep it a single-element list; clear before add + ClearElementList c = new ClearElementList(factory, matchOp.ast, trackName); + ops.add(c); + } + ops.add(t2); } return ops; } diff --git a/tool/src/org/antlr/v4/codegen/ParserFactory.java b/tool/src/org/antlr/v4/codegen/ParserFactory.java index 442e03d4e..54a3740e0 100644 --- a/tool/src/org/antlr/v4/codegen/ParserFactory.java +++ b/tool/src/org/antlr/v4/codegen/ParserFactory.java @@ -288,8 +288,8 @@ public class ParserFactory extends DefaultOutputModelFactory { public SrcOp makeChildOrRoot(SrcOp elemToAdd, boolean isRoot) { String rootName = gen.target.getRootName(getTreeLevel()); SrcOp op; - if ( isRoot ) op = new RewriteBecomeRoot(this, rootName, elemToAdd); - else op = new RewriteAddChild(this, rootName, elemToAdd); + if ( isRoot ) op = new BecomeRoot(this, rootName, elemToAdd); + else op = new AddChild(this, rootName, elemToAdd); return op; } diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteAddChild.java b/tool/src/org/antlr/v4/codegen/model/ast/AddChild.java similarity index 92% rename from tool/src/org/antlr/v4/codegen/model/ast/RewriteAddChild.java rename to tool/src/org/antlr/v4/codegen/model/ast/AddChild.java index 9f71089ca..844d1c0f4 100644 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteAddChild.java +++ b/tool/src/org/antlr/v4/codegen/model/ast/AddChild.java @@ -32,11 +32,11 @@ package org.antlr.v4.codegen.model.ast; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.model.*; -public class RewriteAddChild extends SrcOp { +public class AddChild extends SrcOp { public String rootName; @ModelElement public SrcOp child; - public RewriteAddChild(OutputModelFactory factory, String rootName, SrcOp child) { + public AddChild(OutputModelFactory factory, String rootName, SrcOp child) { super(factory); this.rootName = rootName; this.child = child; diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RewriteBecomeRoot.java b/tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java similarity index 92% rename from tool/src/org/antlr/v4/codegen/model/ast/RewriteBecomeRoot.java rename to tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java index 74339a5f2..008175217 100644 --- a/tool/src/org/antlr/v4/codegen/model/ast/RewriteBecomeRoot.java +++ b/tool/src/org/antlr/v4/codegen/model/ast/BecomeRoot.java @@ -32,11 +32,11 @@ package org.antlr.v4.codegen.model.ast; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.model.*; -public class RewriteBecomeRoot extends SrcOp { +public class BecomeRoot extends SrcOp { public String rootName; @ModelElement public SrcOp newRoot; - public RewriteBecomeRoot(OutputModelFactory factory, String rootName, SrcOp newRoot) { + public BecomeRoot(OutputModelFactory factory, String rootName, SrcOp newRoot) { super(factory); this.rootName = rootName; this.newRoot = newRoot; diff --git a/tool/src/org/antlr/v4/codegen/model/ast/AddRuleLeaf.java b/tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java similarity index 92% rename from tool/src/org/antlr/v4/codegen/model/ast/AddRuleLeaf.java rename to tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java index 9af93d6c9..259f772ab 100644 --- a/tool/src/org/antlr/v4/codegen/model/ast/AddRuleLeaf.java +++ b/tool/src/org/antlr/v4/codegen/model/ast/RuleAST.java @@ -33,8 +33,8 @@ import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.model.decl.Decl; import org.antlr.v4.tool.GrammarAST; -public class AddRuleLeaf extends ElementASTOp { - public AddRuleLeaf(OutputModelFactory factory, GrammarAST ast, Decl label) { +public class RuleAST extends ElementASTOp { + public RuleAST(OutputModelFactory factory, GrammarAST ast, Decl label) { super(factory, ast, label); } } \ No newline at end of file diff --git a/tool/src/org/antlr/v4/codegen/model/ast/RuleBecomeRoot.java b/tool/src/org/antlr/v4/codegen/model/ast/RuleBecomeRoot.java deleted file mode 100644 index 356572a75..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/RuleBecomeRoot.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 Terence Parr - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.antlr.v4.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.GrammarAST; - -public class RuleBecomeRoot extends ElementASTOp { - public RuleBecomeRoot(OutputModelFactory factory, GrammarAST ast, Decl label) { - super(factory, ast, label); - } -} diff --git a/tool/src/org/antlr/v4/codegen/model/ast/AddTokenLeaf.java b/tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java similarity index 92% rename from tool/src/org/antlr/v4/codegen/model/ast/AddTokenLeaf.java rename to tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java index 9eccbb68a..9a4161661 100644 --- a/tool/src/org/antlr/v4/codegen/model/ast/AddTokenLeaf.java +++ b/tool/src/org/antlr/v4/codegen/model/ast/TokenAST.java @@ -34,8 +34,8 @@ import org.antlr.v4.codegen.model.decl.Decl; import org.antlr.v4.tool.GrammarAST; /** */ -public class AddTokenLeaf extends ElementASTOp { - public AddTokenLeaf(OutputModelFactory factory, GrammarAST ast, Decl label) { +public class TokenAST extends ElementASTOp { + public TokenAST(OutputModelFactory factory, GrammarAST ast, Decl label) { super(factory, ast, label); } } diff --git a/tool/src/org/antlr/v4/codegen/model/ast/TokenBecomeRoot.java b/tool/src/org/antlr/v4/codegen/model/ast/TokenBecomeRoot.java deleted file mode 100644 index 2b8ed75eb..000000000 --- a/tool/src/org/antlr/v4/codegen/model/ast/TokenBecomeRoot.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2011 Terence Parr - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.antlr.v4.codegen.model.ast; - -import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.codegen.model.decl.Decl; -import org.antlr.v4.tool.GrammarAST; - -/** */ -public class TokenBecomeRoot extends ElementASTOp { - public TokenBecomeRoot(OutputModelFactory factory, GrammarAST ast, Decl label) { - super(factory, ast, label); - } -}