public static RuleList insert_before(final Argument arg) {
if (arg.arguments.size() == 3
&& arg.arguments.get(0) instanceof RuleList
&& arg.arguments.get(1) instanceof TypedLiteral
&& arg.arguments.get(2) instanceof Item) {
final RuleList list = (RuleList) arg.arguments.get(0);
int index = BuiltinHelper.getInteger((TypedLiteral) arg.arguments
.get(1));
final Item item = arg.arguments.get(2);
if (index < 0)
index += list.getItems().size();
if (index < 0 || index >= list.getItems().size())
return null;
List<IExpression> original = new ArrayList<IExpression>(
list.getItems());
list.getItems().clear();
for (int i = 0; i < original.size(); i++) {
final IExpression expr = original.get(i);
if (i == index) {
if (item instanceof Variable)
list.addItem(new RuleVariable(item.getName()));
else if (item instanceof Literal)
list.addItem(new Constant((Literal) item, list));
else if (item instanceof IExpression)
list.addItem((IExpression) item);
}
list.addItem(expr);
}
return list;
}
return null;
}