template.incremental.Debug.tt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jastadd Show documentation
Show all versions of jastadd Show documentation
A metacompilation framework for Java using attribute grammars.
# Copyright (c) 2013, The JastAdd Team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * 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.
# * Neither the name of the Lund University nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
# Generate code for a method creating a relative node id.
ASTDecl.relativeNodeIDMethod = [[
$if (#isASTNodeDecl)
public String $ASTNode.relativeNodeID() {
String path = "";
$ASTNode node = this;
while (node != null) {
$ASTNode parent = node.parent;
int index = -1;
if (parent != null) {
for (int i = 0; parent.children != null && i < parent.children.length; i++) {
if (parent.children[i] != null && parent.children[i] == node && !parent.childIsNTA(i)) {
index = i;
break;
}
}
}
String pathEntry = node.getClass().getSimpleName();
if (index > -1) pathEntry += "[" + index + "]";
path = path.length() == 0 ? pathEntry : pathEntry + "/" + path;
node = parent;
}
return path;
}
$endif
]]
# Generate code for a method creating a relative node id.
# Annotate children with possible rewrites.
ASTDecl.relativeNodeIDMethodWithRewrite = [[
$if(!IncrementalDebug)
$if (#isASTNodeDecl)
public String $ASTNode.relativeNodeID() {
StringBuffer res = new StringBuffer();
$ASTNode parent = this.parent;
int index = -1;
if (parent != null) {
res.append(parent.relativeNodeID() + "/");
for (int i = 0; parent.children != null && i < parent.children.length; i++) {
if (parent.children[i] != null && parent.children[i] == this && !parent.childIsNTA(i)) {
index = i;
break;
}
}
}
res.append(getClass().getSimpleName());
if (index > -1) {
res.append("[" + index + (mayHaveRewrite() ? ",r" : "") + "]");
}
return res.toString();
}
$endif
$endif
]]
# Generate code for a method returning a string with attribute parameter values
ASTDecl.printParamListMethod = [[
$if (#isASTNodeDecl)
protected String $ASTNode.printParamList(java.util.List paramList) {
StringBuffer buf = new StringBuffer();
for (java.util.Iterator itr = paramList.iterator(); itr.hasNext();) {
Object o = itr.next();
if (o instanceof $ASTNode) {
buf.append((($ASTNode) o).relativeNodeID());
} else {
buf.append(o.toString());
}
buf.append(",");
}
return buf.toString();
}
$endif
]]
# Generate code for method printing a value
ASTDecl.printValueMethod = [[
$if (#isASTNodeDecl)
protected String $ASTNode.printValue(Object value) {
if (value == null) {
return "null";
}
if (value instanceof $ASTNode && (($ASTNode) value).inc_state == inc_GARBAGE) {
throw new Error("Cached value is garbage");
}
return value.toString();
}
$endif
]]
# Generate code for method dumping cached values
ASTDecl.dumpCachedValuesMethod = [[
public void #name.dumpCachedValues() {
$if (#isASTNodeDecl)
// Rewritten children
for (int k = 0; children_computed != null && k < children_computed.length; k++) {
if (children_computed[k]) {
System.out.println(children[k].relativeNodeID() + " rewritten, initial=" +
(init_children[k] != null ? init_children[k].relativeNodeID() : null));
}
}
$endif
$DumpAttributeValues
// TODO: dump collection values
$if (!#isASTNodeDecl)
super.dumpCachedValues();
$endif
}
]]
# Generate code for dumping a cached attribute value
ASTDecl.dumpCachedAttributeValue = [[
$if (IsParameterized)
if ($(AttrSign)_values != null) {
for (java.util.Iterator itr = $(AttrSign)_values.keySet().iterator();itr.hasNext();) {
Object key = (Object)itr.next();
Object value = $(AttrSign)_values.get(key);
System.out.println("value(" + relativeNodeID() + ":$(AttrSign)[" +
(key instanceof $ASTNode ? (($ASTNode) key).relativeNodeID() :
(key instanceof java.util.List ? printParamList((java.util.List)key) : key) ) + "], " +
(value instanceof $ASTNode ? (($ASTNode) value).relativeNodeID() : printValue(value)) + ")");
}
}
$else
if ($(AttrSign)_computed) {
$if (PrintAsObject)
System.out.println("value(" + relativeNodeID() + ":$(AttrSign), " + $(AttrSign)_value + ")");
$else
Object obj = $(AttrSign)_value;
System.out.println("value(" + relativeNodeID() + ":$(AttrSign), " +
(obj instanceof $ASTNode ? (($ASTNode) obj).relativeNodeID() : obj) + ")");
$endif
}
$endif
]]
# Generate code for method dumping dependencies
ASTDecl.dumpDependenciesMethod = [[
public void #name.dumpDependencies() {
$if (#isASTNodeDecl)
$if (IncrementalLevelParam)
getParent_handler.dumpDeps();
numChildren_handler.dumpDeps();
for (int k = 0; getChild_handler != null && k < getChild_handler.length; k++) {
if (getChild_handler[k] != null) {
getChild_handler[k].dumpDeps();
}
}
$endif
$if (IncrementalLevelAttr)
getParent_handler.dumpDeps();
numChildren_handler.dumpDeps();
getChild_handler.dumpDeps();
$endif
$if (IncrementalLevelNode)
handler.dumpDeps();
$endif
$endif
$if (IncrementalLevelRegion)
$if (IsRegionRoot)
handler.dumpDeps();
$endif
$endif
$DumpTokenDeps
$DumpAttributeDeps
// TODO: dump collection attribute dependencies
$if (!#isASTNodeDecl)
$if (IncrementalLevelRegion)
$if (!IsRegionRoot)
super.dumpDependencies();
$endif
$else
super.dumpDependencies();
$endif
$endif
}
]]
# Generate code for checking a token handler and dumping its dependencies
ASTDecl.checkAndDumpTokenDeps = [[
if (get$(Id)_handler != null) {
get$(Id)_handler.dumpDeps();
}
]]
# Generate code for checking an attribute handler and dumping its dependencies
ASTDecl.checkAndDumpAttributeDeps = [[
$if (IncrementalLevelParam)
$if (IsParameterized)
for (java.util.Iterator itr = $(AttrSign)_handler.values().iterator(); itr.hasNext();) {
$DDGNodeName handler = ($DDGNodeName)itr.next();
handler.dumpDeps();
}
$else
if ($(AttrSign)_handler != null) {
$(AttrSign)_handler.dumpDeps();
}
$endif
$if (IsNTA)
$if (IsParameterized)
if ($(AttrSign)_proxy != null) {
$(AttrSign)_proxy.dumpDependencies();
}
$else
if ($(AttrSign)_computed && ($(AttrSign)_value instanceof $ASTNode)) {
$(AttrSign)_value.dumpDependencies();
}
$endif
$endif
$endif
$if (IncrementalLevelAttr)
if ($(AttrSign)_handler != null) {
$(AttrSign)_handler.dumpDeps();
}
$if (IsNTA)
$if (IsParameterized)
if ($(AttrSign)_proxy != null) {
$(AttrSign)_proxy.dumpDependencies();
}
$else
if ($(AttrSign)_computed && ($(AttrSign)_value instanceof $ASTNode)) {
$(AttrSign)_value.dumpDependencies();
}
$endif
$endif
$endif
]]
# Generate code for method dumping dependencies in tree
ASTDecl.dumpDepsInTreeMethod = [[
public void #name.dumpDepsInTree() {
dumpDependencies();
for (int i = 0; children != null && i < children.length; i++) {
$ASTNode child = children[i];
if (child == null) {
continue;
}
child.dumpDepsInTree();
}
$DumpDepsInNTAs
}
]]
# Generate string with code for dumping dependencies in NTAs
ASTDecl.checkAndDumpNTADeps = [[
$if (IsParameterized)
if ($(AttrSign)_proxy != null) {
$(AttrSign)_proxy.dumpDepsInTree();
}
$else
if ($(AttrSign)_computed && ($(AttrSign)_value instanceof $ASTNode)) {
$(AttrSign)_value.dumpDepsInTree();
}
$endif
]]