got rid of ...IsRoot templates and model elements!

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8848]
This commit is contained in:
parrt 2011-07-09 16:27:01 -08:00
parent a3da5c5f53
commit a20912ba73
15 changed files with 60 additions and 261 deletions

View File

@ -2,7 +2,7 @@ grammar T;
options {output=AST;}
tokens {I;}
a : x+=b x+=b -> {new CommonTree()} ;
a : A B -> ^(A B);
atom : A ;

View File

@ -428,44 +428,27 @@ RewriteTreeStructure(t, locals, ops) ::= <<
}
>>
RewriteTokenRef(t) ::= "_adaptor.addChild(<t.rootName>, <t.iterName>.next());"
RewriteTokenRefIsRoot(t) ::= <<
<t.rootName> = _adaptor.becomeRoot(<t.iterName>.next(), <t.rootName>);
RewriteTokenRef(t) ::= "<t.iterName>.next()"
RewriteImagTokenRef(t, argChunks) ::= <<
_adaptor.create(<t.tokenType>, <if(argChunks)><argChunks><else>"<t.tokenType>"<endif>)
>>
//adaptor.addChild(root_0, (Object)adaptor.create(D, "D"));
RewriteImagTokenRef(t, argChunks) ::= <%
_adaptor.addChild(<t.rootName>,
_adaptor.create(<t.tokenType>,
<if(argChunks)><argChunks><else>"<t.tokenType>"<endif>)
);
%>
RewriteImagTokenRefIsRoot(t, argChunks) ::= <%
<t.rootName> =
_adaptor.becomeRoot(
_adaptor.create(<t.tokenType>,
<if(argChunks)><argChunks><else>"<t.tokenType>"<endif>),
<t.rootName>
);
%>
RewriteRuleRef(r) ::= "_adaptor.addChild(<r.rootName>, <r.iterName>.next());"
RewriteRuleRefIsRoot(r) ::=
"<r.rootName> = _adaptor.becomeRoot(<r.iterName>.next(), <r.rootName>);"
RewriteRuleRef(r) ::= "<r.iterName>.next()"
RewriteLabelRef(t) ::= "<RewriteAddChild(t.rootName, {<t.iterName>.next()})>"
RewriteLabelRefIsRoot(t) ::= "<RewriteBecomeRoot({<t.iterName>.next()}, t.rootName)>"
RewriteLabelRef(t) ::= "<t.iterName>.next()"
// -> $e in rule e
RewriteSelfRuleLabelRef(s) ::= "<RewriteAddChild(s.rootName, {_localctx.tree})>"
RewriteSelfRuleLabelRefIsRoot(s) ::= "<RewriteBecomeRoot({_localctx.tree}, s.rootName)>"
RewriteSelfRuleLabelRef(s) ::= "_localctx.tree"
RewriteAction(a, chunks) ::= "<RewriteAddChild(a.rootName, chunks)>"
RewriteActionIsRoot(a, chunks) ::= "<RewriteBecomeRoot(chunks, a.rootName)>"
RewriteAction(a, chunks) ::= "<chunks>"
// how to add child, make root
RewriteAddChild(rootName, child) ::= "_adaptor.addChild(<rootName>, <child>);"
RewriteBecomeRoot(newRoot, rootName) ::=
"<rootName> = _adaptor.becomeRoot(<newRoot>, <rootName>);"
/** how to add child in rewrite section */
RewriteAddChild(x, rootName, child) ::= "_adaptor.addChild(<x.rootName>, <child>);"
/** how to make something a new root in rewrite section */
RewriteBecomeRoot(x, newRoot) ::=
"<x.rootName> = _adaptor.becomeRoot(<newRoot>, <x.rootName>);"
/*

View File

@ -244,68 +244,55 @@ public class ParserFactory extends DefaultOutputModelFactory {
}
public List<SrcOp> rewrite_ruleRef(GrammarAST ID, boolean isRoot) {
String rootName = gen.target.getRootName(getTreeLevel());
RewriteRuleRef ruleRef;
String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel());
if ( isRoot ) ruleRef = new RewriteRuleRefIsRoot(this, ID, rootName, iterName);
else ruleRef = new RewriteRuleRef(this, ID, rootName, iterName);
return list(ruleRef);
RewriteRuleRef ruleRef = new RewriteRuleRef(this, ID, iterName);
return list(makeChildOrRoot(ruleRef, isRoot));
}
public List<SrcOp> rewrite_tokenRef(GrammarAST ID, boolean isRoot, ActionAST argAST) {
Alternative alt = getCurrentAlt();
String rootName = gen.target.getRootName(getTreeLevel());
String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel());
// not ref'd on left hand side or it is but we have an argument like ID["x"]
// implies create new node
SrcOp tokenRef;
if ( alt.tokenRefs.get(ID.getText())==null || argAST!=null ) {
RewriteImagTokenRef tokenRef;
if ( isRoot ) {
tokenRef = new RewriteImagTokenRefIsRoot(this, ID, rootName,
ID.getText(), argAST);
}
else {
tokenRef = new RewriteImagTokenRef(this, ID, rootName,
ID.getText(), argAST);
}
return list(tokenRef);
tokenRef = new RewriteImagTokenRef(this, ID, ID.getText(), argAST);
}
// must be token ref on left of ->
RewriteTokenRef tokenRef;
if ( isRoot ) tokenRef = new RewriteTokenRefIsRoot(this, ID, rootName, iterName);
else tokenRef = new RewriteTokenRef(this, ID, rootName, iterName);
return list(tokenRef);
else { // must be token ref on left of ->
tokenRef = new RewriteTokenRef(this, ID, iterName);
}
return list(makeChildOrRoot(tokenRef, isRoot));
}
@Override
public List<SrcOp> rewrite_labelRef(GrammarAST ID, boolean isRoot) {
String rootName = gen.target.getRootName(getTreeLevel());
String iterName = gen.target.getRewriteIteratorName(ID, getCodeBlockLevel());
SrcOp labelRef;
if ( ID.getText().equals(getCurrentRuleFunction().rule.name) ) { // $e in rule e
RewriteSelfRuleLabelRef labelRef;
if ( isRoot ) labelRef = new RewriteSelfRuleLabelRef(this, ID, rootName);
else labelRef = new RewriteSelfRuleLabelRef(this, ID, rootName);
return list(labelRef);
labelRef = new RewriteSelfRuleLabelRef(this, ID);
}
else { // normal element label
RewriteLabelRef labelRef;
if ( isRoot ) labelRef = new RewriteLabelRefIsRoot(this, ID, rootName, iterName);
else labelRef = new RewriteLabelRef(this, ID, rootName, iterName);
return list(labelRef);
labelRef = new RewriteLabelRef(this, ID, iterName);
}
return list(makeChildOrRoot(labelRef, isRoot));
}
@Override
public List<SrcOp> rewrite_action(ActionAST actionAST, boolean isRoot) {
String rootName = gen.target.getRootName(getTreeLevel());
RewriteAction action;
if ( isRoot ) action = new RewriteActionIsRoot(this, actionAST, rootName);
else action = new RewriteAction(this, actionAST, rootName);
return list(action);
RewriteAction action = new RewriteAction(this, actionAST);
return list(makeChildOrRoot(action, isRoot));
}
// support
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);
return op;
}
public void defineImplicitLabel(GrammarAST ID, LabeledOp op) {
Decl d;
Rule r = g.getRule(ID.getText());

View File

@ -37,13 +37,10 @@ import org.antlr.v4.tool.ActionAST;
import java.util.List;
public class RewriteAction extends SrcOp {
public String rootName;
@ModelElement public List<ActionChunk> chunks;
public RewriteAction(OutputModelFactory factory, ActionAST ast, String rootName) {
public RewriteAction(OutputModelFactory factory, ActionAST ast) {
super(factory, ast);
this.rootName = rootName;
if ( ast!=null ) {
chunks = ActionTranslator.translateAction(factory,
factory.getCurrentRuleFunction(),

View File

@ -30,10 +30,15 @@
package org.antlr.v4.codegen.model.ast;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.ActionAST;
import org.antlr.v4.codegen.model.*;
public class RewriteActionIsRoot extends RewriteAction {
public RewriteActionIsRoot(OutputModelFactory factory, ActionAST ast, String rootName) {
super(factory, ast, rootName);
public class RewriteAddChild extends SrcOp {
public String rootName;
@ModelElement public SrcOp child;
public RewriteAddChild(OutputModelFactory factory, String rootName, SrcOp child) {
super(factory);
this.rootName = rootName;
this.child = child;
}
}

View File

@ -30,10 +30,15 @@
package org.antlr.v4.codegen.model.ast;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.GrammarAST;
import org.antlr.v4.codegen.model.*;
public class RewriteSelfRuleLabelRefIsRoot extends RewriteSelfRuleLabelRef {
public RewriteSelfRuleLabelRefIsRoot(OutputModelFactory factory, GrammarAST ast, String rootName) {
super(factory, ast, rootName);
public class RewriteBecomeRoot extends SrcOp {
public String rootName;
@ModelElement public SrcOp newRoot;
public RewriteBecomeRoot(OutputModelFactory factory, String rootName, SrcOp newRoot) {
super(factory);
this.rootName = rootName;
this.newRoot = newRoot;
}
}

View File

@ -37,19 +37,16 @@ import org.antlr.v4.tool.*;
import java.util.List;
public class RewriteImagTokenRef extends SrcOp {
public String rootName;
public String tokenType;
@ModelElement public List<ActionChunk> argChunks;
public RewriteImagTokenRef(OutputModelFactory factory,
GrammarAST ast,
String rootName,
String tokenType,
ActionAST argAST)
{
super(factory, ast);
this.rootName = rootName;
this.tokenType = tokenType;
if ( argAST!=null ) {
argChunks = ActionTranslator.translateAction(factory,

View File

@ -1,44 +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.tool.*;
public class RewriteImagTokenRefIsRoot extends RewriteImagTokenRef {
public RewriteImagTokenRefIsRoot(OutputModelFactory factory,
GrammarAST ast,
String rootName,
String tokenType,
ActionAST argAST)
{
super(factory, ast, rootName, tokenType, argAST);
}
}

View File

@ -34,13 +34,11 @@ import org.antlr.v4.codegen.model.SrcOp;
import org.antlr.v4.tool.GrammarAST;
public class RewriteLabelRef extends SrcOp {
public String rootName;
public String iterName;
public RewriteLabelRef(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName) {
String iterName) {
super(factory, ast);
this.rootName = rootName;
this.iterName = iterName;
}
}

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.tool.GrammarAST;
public class RewriteLabelRefIsRoot extends RewriteLabelRef {
public RewriteLabelRefIsRoot(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName) {
super(factory, ast, rootName, iterName);
}
}

View File

@ -34,14 +34,12 @@ import org.antlr.v4.codegen.model.SrcOp;
import org.antlr.v4.tool.GrammarAST;
public class RewriteRuleRef extends SrcOp {
public String rootName;
public String iterName;
public RewriteRuleRef(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName)
String iterName)
{
super(factory, ast);
this.rootName = rootName;
this.iterName = iterName;
}
}

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.tool.GrammarAST;
public class RewriteRuleRefIsRoot extends RewriteRuleRef {
public RewriteRuleRefIsRoot(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName)
{
super(factory, ast, rootName, iterName);
}
}

View File

@ -34,10 +34,7 @@ import org.antlr.v4.codegen.model.SrcOp;
import org.antlr.v4.tool.GrammarAST;
public class RewriteSelfRuleLabelRef extends SrcOp {
public String rootName;
public RewriteSelfRuleLabelRef(OutputModelFactory factory, GrammarAST ast, String rootName) {
public RewriteSelfRuleLabelRef(OutputModelFactory factory, GrammarAST ast) {
super(factory, ast);
this.rootName = rootName;
}
}

View File

@ -34,13 +34,11 @@ import org.antlr.v4.codegen.model.SrcOp;
import org.antlr.v4.tool.GrammarAST;
public class RewriteTokenRef extends SrcOp {
public String rootName;
public String iterName;
public RewriteTokenRef(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName)
String iterName)
{
super(factory, ast);
this.rootName = rootName;
this.iterName = iterName;
}
}

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.tool.GrammarAST;
public class RewriteTokenRefIsRoot extends RewriteTokenRef {
public RewriteTokenRefIsRoot(OutputModelFactory factory, GrammarAST ast,
String rootName, String iterName)
{
super(factory, ast, rootName, iterName);
}
}