cleaned up

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8849]
This commit is contained in:
parrt 2011-07-09 17:00:15 -08:00
parent a20912ba73
commit a06679fec7
9 changed files with 98 additions and 160 deletions

View File

@ -365,12 +365,10 @@ labelref(x) ::= "<if(!x.isLocal)>_localctx.<endif><x.name>"
// AST stuff (TODO: separate?)
RootDecl(d) ::= "Object <d.name> = _adaptor.nil();"
RootName(level) ::= "_root<level>"
RootName(level) ::= "_root<level>"
AddTokenLeaf(a) ::= "_adaptor.addChild(_root0, _adaptor.create(<labelref(a.label)>));"
AddRuleLeaf(a) ::= "_adaptor.addChild(_root0, <labelref(a.label)>.tree);"
RuleBecomeRoot(r) ::= "_root0 = _adaptor.becomeRoot(<labelref(r.label)>.tree, _root0);"
TokenBecomeRoot(t) ::= "_root0 = _adaptor.becomeRoot(_adaptor.create(<labelref(t.label)>), _root0);"
TokenAST(t) ::= "_adaptor.create(<labelref(t.label)>)"
RuleAST(r) ::= "<labelref(r.label)>.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) ::= "<chunks>"
/** how to add child in rewrite section */
RewriteAddChild(x, rootName, child) ::= "_adaptor.addChild(<x.rootName>, <child>);"
AddChild(x, rootName, child) ::= "_adaptor.addChild(<x.rootName>, <child>);"
/** how to make something a new root in rewrite section */
RewriteBecomeRoot(x, newRoot) ::=
BecomeRoot(x, rootName, newRoot) ::=
"<x.rootName> = _adaptor.becomeRoot(<newRoot>, <x.rootName>);"

View File

@ -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<SrcOp> leafRule(List<SrcOp> 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<SrcOp> leafToken(List<SrcOp> 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<SrcOp> leafRuleInRewriteAlt(InvokeRule invokeOp, List<SrcOp> 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<SrcOp> leafTokenInRewriteAlt(MatchToken matchOp, List<SrcOp> 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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}