puppetModule = module;
break;
}
}
BlockAcceptingNode newtypeNode = null;
if(puppetModule != null) {
// Find the newtype call
nodes = RubyParserUtils.findNodes(puppetModule.getBody(), new NodeType[] { NodeType.CALLNODE });
for(Node node : nodes) {
CallNode call = (CallNode) node;
if("newtype".equals(call.getName())) {
Node receiver = call.getReceiver();
if(receiver instanceof ConstNode && "Type".equals(((ConstNode) receiver).getName())) {
newtypeNode = call;
break;
}
}
}
if(newtypeNode == null) {
// Try syntax found in iptables.rb. Not sure it's correct
// but it seems to be parsed
// OK by the puppet-tool
nodes = RubyParserUtils.findNodes(puppetModule.getBody(), new NodeType[] { NodeType.FCALLNODE });
for(Node node : nodes) {
FCallNode call = (FCallNode) node;
if("newtype".equals(call.getName())) {
newtypeNode = call;
break;
}
}
}
}
else {
// The call might be a CallNode at the top level
nodes = RubyParserUtils.findNodes((root).getBody(), new NodeType[] { NodeType.CALLNODE });
for(Node node : nodes) {
CallNode call = (CallNode) node;
if("newtype".equals(call.getName())) {
Node receiver = call.getReceiver();
if(receiver instanceof Colon2ConstNode) {
Colon2ConstNode c2cNode = (Colon2ConstNode) receiver;
if("Type".equals(c2cNode.getName()) && c2cNode.getLeftNode() instanceof ConstNode &&
"Puppet".equals(((ConstNode) c2cNode.getLeftNode()).getName())) {
newtypeNode = call;
break;
}
}
}
}
}
if(newtypeNode == null)
throw new IOException("Unable to find newtype call in " + typeFileStr);
// Find the parameter that is passed in the call to newtype. It must
// be one
// single parameter in the form of a Symbol. This Symbol denotes the
// name of
// the new type.
Node argsNode = ((IArgumentNode) newtypeNode).getArgs();
nodes = RubyParserUtils.findNodes(argsNode, new NodeType[] { NodeType.SYMBOLNODE });
if(nodes.size() != 1)
throw new IOException("The newtype call does not take exactly one symbol parameter in " + typeFileStr);
SymbolNode typeName = (SymbolNode) nodes.get(0);
type.setName(typeName.getName());
// Find the assignment of the @doc instance variable
Node iterNode = newtypeNode.getIter();
nodes = RubyParserUtils.findNodes(iterNode, new NodeType[] { NodeType.BLOCKNODE, NodeType.INSTASGNNODE });
if(nodes.isEmpty())
// No block when there's just one assignment
nodes = RubyParserUtils.findNodes(iterNode, new NodeType[] { NodeType.INSTASGNNODE });