All Downloads are FREE. Search and download functionalities are using the official Maven repository.

template.incremental.Debug.tt Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
# 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() {
  $DumpAttributeValues
  $DumpCollAttributeValues
$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 dumping a collection attribute value.
# TODO: Add support for parameterized/NTA collection attributes.
ASTDecl.dumpCollAttributeValue = [[
if ($(AttrSign)_computed) {
  System.out.print("value(" + relativeNodeID() + ":$(AttrSign), [");
  for (Object obj : $(AttrSign)_value) { 
    System.out.print((obj instanceof $ASTNode ? (($ASTNode) obj).relativeNodeID() : obj) + " ");
  }
  System.out.println("])");
}
]]

# Generate code for method dumping dependencies
ASTDecl.dumpDependenciesMethod = [[
private boolean #name.inc_dumpDependencies_visited = false;
public void #name.dumpDependencies() {
  if (inc_dumpDependencies_visited) {
    return;
  }
  inc_dumpDependencies_visited = true;
$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
  $DumpCollAttributeDeps
$if (!#isASTNodeDecl)
$if (IncrementalLevelRegion)
$if (!IsRegionRoot)
  super.dumpDependencies();
$endif
$else
  super.dumpDependencies();
$endif
$endif
  inc_dumpDependencies_visited = false;
}
]]

# 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 collection attribute handler and dumping its dependencies
# TODO: Support parameterized/NTA coll attributes.
ASTDecl.checkAndDumpCollAttributeDeps = [[
$if (IncrementalLevelParam)
if ($(AttrSign)_handler != null) {
  $(AttrSign)_handler.dumpDeps();
}
$endif
]]

# 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 = [[
private boolean #name.inc_dumpDepsInTree_visited = false;
public void #name.dumpDepsInTree() {
  if (inc_dumpDepsInTree_visited) {
    return;
  }
  inc_dumpDepsInTree_visited = true;
  dumpDependencies();
  for (int i = 0; children != null && i < children.length; i++) {
    $ASTNode child = children[i];
    if (child == null) {
      continue;
    }
    child.dumpDepsInTree();
  }
  $DumpDepsInNTAs
  inc_dumpDepsInTree_visited = false;
}
]]

# 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
]]




© 2015 - 2024 Weber Informatics LLC | Privacy Policy