template.ast.components.ListComponent.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-2016, 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.
# Additional aspect declarations for the List node type
List.isEmpty [[node instanceof $List && (($List) node).numChildren() <= 0]]
List.additionalDeclarations [[
$if(EmptyContainerSingletons)
protected static final $List $List.EMPTY = new $(List)() {
@Override
public $List add($ASTNode node) {
throw new Error("attempting to add() on empty $(List) singleton!");
}
@Override
public $List addAll(Iterable c) {
throw new Error("attempting to addAll() on empty $(List) singleton!");
}
@Override
public void insertChild($ASTNode node, int i) {
throw new Error("attempting to insertChild() on empty $(List) singleton!");
}
@Override
public void addChild($ASTNode node) {
throw new Error("attempting to addChild() on empty $(List) singleton!");
}
@Override
public void removeChild(int i) {
throw new Error("attempting to removeChild() on empty $(List) singleton!");
}
@Override
public void setChild($ASTNode child, int pos) {
throw new Error("attempting to setChild() in empty $List singleton!");
}
};
$endif
]]
# List component boilerplate code for accessing children etc.
ListComponent [[
$if(!#isNTA)
/**
* Replaces the $Name list.
* @param list The new list node to be used as the $Name list.
* @apilevel high-level
*/
$Modifier void $Host.set$(Name)List($ListType list) {
$SynchBegin
$include(State.incHookConstructionStart)
setChild(list, $Index);
$include(State.incHookConstructionEnd)
$SynchEnd
}
$endif
$if(#ntaShadowingNonNTA)
/**
* This method should not be called. This method throws an exception due to
* the corresponding child being an NTA shadowing a non-NTA child.
* @param node
* @apilevel internal
*/
public void $Host.set$(Name)List($ListType node) {
throw new Error("Can not replace NTA child $(Name)List in $Host!");
}
$endif
/**
* Retrieves the number of children in the $Name list.
* @return Number of children in the $Name list.
* @apilevel high-level
*/
$Modifier int $Host.getNum$Name() {
$SynchBegin
return get$(Name)List().getNumChild();
$SynchEnd
}
/**
* Retrieves the number of children in the $Name list.
* Calling this method will not trigger rewrites.
* @return Number of children in the $Name list.
* @apilevel low-level
*/
$Modifier int $Host.getNum$(Name)NoTransform() {
$SynchBegin
return get$(Name)ListNoTransform().getNumChildNoTransform();
$SynchEnd
}
/**
* Retrieves the element at index {@code i} in the $Name list.
* @param i Index of the element to return.
* @return The element at position {@code i} in the $Name list.
* @apilevel high-level
*/
#annotations
$Modifier $Type $Host.get$Name(int i) {
$SynchBegin
return ($Type) get$(Name)List().getChild(i);
$SynchEnd
}
/**
* Check whether the $Name list has any children.
* @return {@code true} if it has at least one child, {@code false} otherwise.
* @apilevel high-level
*/
$Modifier boolean $Host.has$Name() {
$SynchBegin
return get$(Name)List().getNumChild() != 0;
$SynchEnd
}
/**
* Append an element to the $Name list.
* @param node The element to append to the $Name list.
* @apilevel high-level
*/
$Modifier void $Host.add$Name($Type node) {
$SynchBegin
$ListType list = (parent == null) ? get$(Name)ListNoTransform() : get$(Name)List();
$if(EmptyContainerSingletons)
if (list == $List.EMPTY) {
setChild(new $List(node), $Index);
} else {
list.addChild(node);
}
$else
list.addChild(node);
$endif
$SynchEnd
}
/** @apilevel low-level */
$Modifier void $Host.add$(Name)NoTransform($Type node) {
$SynchBegin
$ListType list = get$(Name)ListNoTransform();
$if(EmptyContainerSingletons)
if (list == $List.EMPTY) {
setChild(new $List(node), $Index);
} else {
list.addChild(node);
}
$else
list.addChild(node);
$endif
$SynchEnd
}
/**
* Replaces the $Name list element at index {@code i} with the new node {@code node}.
* @param node The new node to replace the old list element.
* @param i The list index of the node to be replaced.
* @apilevel high-level
*/
$Modifier void $Host.set$Name($Type node, int i) {
$SynchBegin
$ListType list = get$(Name)List();
$include(State.incHookConstructionStart)
$if(EmptyContainerSingletons)
if (list == $List.EMPTY) {
setChild(new $List(node), $Index);
} else {
list.setChild(node, i);
}
$else
list.setChild(node, i);
$endif
$include(State.incHookConstructionEnd)
$SynchEnd
}
$if(#isNTA)
/**
* Retrieves the child position of the $Name list.
* @return The the child position of the $Name list.
* @apilevel low-level
*/
protected int $Host.get$(Name)ListChildPosition() {
return $Index;
}
/**
* Retrieves the $Name list.
* This method does not invoke AST transformations.
* @return The node representing the $Name list.
* @apilevel low-level
*/
$Modifier $ListType $Host.get$(Name)ListNoTransform() {
$SynchBegin
return ($ListType) getChildNoTransform($Index);
$SynchEnd
}
$else
/**
* Retrieves the $Name list.
* @return The node representing the $Name list.
* @apilevel high-level
*/
#annotations
$include(ListComponent.generatedAnnotations)
$Modifier $ListType $Host.get$(Name)List() {
$SynchBegin
$ListType list = ($ListType) getChild($Index);
return list;
$SynchEnd
}
/**
* Retrieves the $Name list.
* This method does not invoke AST transformations.
* @return The node representing the $Name list.
* @apilevel low-level
*/
#annotations
$Modifier $ListType $Host.get$(Name)ListNoTransform() {
$SynchBegin
return ($ListType) getChildNoTransform($Index);
$SynchEnd
}
$endif
/**
* @return the element at index {@code i} in the $Name list without
* triggering rewrites.
*/
$Modifier $Type $Host.get$(Name)NoTransform(int i) {
$SynchBegin
return ($Type) get$(Name)ListNoTransform().getChildNoTransform(i);
$SynchEnd
}
/**
* Retrieves the $Name list.
* @return The node representing the $Name list.
* @apilevel high-level
*/
$Modifier $ListType $Host.get$(Name)s() {
$SynchBegin
return get$(Name)List();
$SynchEnd
}
/**
* Retrieves the $Name list.
* This method does not invoke AST transformations.
* @return The node representing the $Name list.
* @apilevel low-level
*/
$Modifier $ListType $Host.get$(Name)sNoTransform() {
$SynchBegin
return get$(Name)ListNoTransform();
$SynchEnd
}
]]